feat(web): jank hashtags
better than nothing!
This commit is contained in:
parent
9a4d23de08
commit
40b9f68aef
1 changed files with 48 additions and 22 deletions
|
@ -29,10 +29,15 @@ fn post_author(post_id: &str) -> Option<crate::Object> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct MentionMatch {
|
enum TextMatch {
|
||||||
href: String,
|
Mention {
|
||||||
name: String,
|
href: String,
|
||||||
domain: String,
|
name: String,
|
||||||
|
domain: String,
|
||||||
|
},
|
||||||
|
Hashtag {
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type PrivacyControl = ReadSignal<Privacy>;
|
pub type PrivacyControl = ReadSignal<Privacy>;
|
||||||
|
@ -150,21 +155,25 @@ pub fn PostBox(advanced: WriteSignal<bool>) -> impl IntoView {
|
||||||
let summary_ref: NodeRef<html::Input> = create_node_ref();
|
let summary_ref: NodeRef<html::Input> = create_node_ref();
|
||||||
|
|
||||||
// TODO is this too abusive with resources? im even checking if TLD exists...
|
// TODO is this too abusive with resources? im even checking if TLD exists...
|
||||||
|
// TODO debounce this!
|
||||||
let mentions = create_local_resource(
|
let mentions = create_local_resource(
|
||||||
move || content.get(),
|
move || content.get(),
|
||||||
move |c| async move {
|
move |c| async move {
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
for word in c.split(' ') {
|
for word in c.split(' ') {
|
||||||
if !word.starts_with('@') { break };
|
if word.starts_with('@') {
|
||||||
let stripped = word.replacen('@', "", 1);
|
let stripped = word.replacen('@', "", 1);
|
||||||
if let Some((name, domain)) = stripped.split_once('@') {
|
if let Some((name, domain)) = stripped.split_once('@') {
|
||||||
if let Some(tld) = domain.split('.').last() {
|
if let Some(tld) = domain.split('.').last() {
|
||||||
if tld::exist(tld) {
|
if tld::exist(tld) {
|
||||||
if let Some(uid) = cache::WEBFINGER.blocking_resolve(name, domain).await {
|
if let Some(uid) = cache::WEBFINGER.blocking_resolve(name, domain).await {
|
||||||
out.push(MentionMatch { name: name.to_string(), domain: domain.to_string(), href: uid });
|
out.push(TextMatch::Mention { name: name.to_string(), domain: domain.to_string(), href: uid });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if word.starts_with('#') {
|
||||||
|
out.push(TextMatch::Hashtag { name: word.replacen('#', "", 1) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
|
@ -193,11 +202,17 @@ pub fn PostBox(advanced: WriteSignal<bool>) -> impl IntoView {
|
||||||
}
|
}
|
||||||
{move ||
|
{move ||
|
||||||
mentions.get()
|
mentions.get()
|
||||||
.map(|x| x.into_iter().map(|u| match cache::OBJECTS.get(&u.href) {
|
.map(|x| x
|
||||||
Some(u) => view! { <span class="nowrap"><span class="emoji mr-s ml-s">"📨"</span><ActorStrip object=u /></span> }.into_view(),
|
.into_iter()
|
||||||
None => view! { <span class="nowrap"><span class="emoji mr-s ml-s">"📨"</span><a href={Uri::web(U::Actor, &u.href)}>{u.href}</a></span> }.into_view(),
|
.map(|u| match u {
|
||||||
})
|
TextMatch::Mention { href, .. } => match cache::OBJECTS.get(&href) {
|
||||||
.collect_view())
|
Some(u) => view! { <span class="nowrap"><span class="emoji mr-s ml-s">"📨"</span><ActorStrip object=u /></span> }.into_view(),
|
||||||
|
None => view! { <span class="nowrap"><span class="emoji mr-s ml-s">"📨"</span><a href={Uri::web(U::Actor, &href)}>{href}</a></span> }.into_view(),
|
||||||
|
},
|
||||||
|
TextMatch::Hashtag { name } => view! { <code class="color">#{name}</code> }.into_view(),
|
||||||
|
})
|
||||||
|
.collect_view()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
<table class="align w-100">
|
<table class="align w-100">
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -224,11 +239,20 @@ pub fn PostBox(advanced: WriteSignal<bool>) -> impl IntoView {
|
||||||
let mut mention_tags : Vec<serde_json::Value> = mentions.get()
|
let mut mention_tags : Vec<serde_json::Value> = mentions.get()
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|x| {
|
.map(|x| match x {
|
||||||
use apb::LinkMut;
|
TextMatch::Mention { name, domain, href } => {
|
||||||
LinkMut::set_name(apb::new(), Some(format!("@{}@{}", x.name, x.domain))) // TODO ewww but name clashes
|
use apb::LinkMut;
|
||||||
.set_link_type(Some(apb::LinkType::Mention))
|
LinkMut::set_name(apb::new(), Some(format!("@{}@{}", name, domain))) // TODO ewww but name clashes
|
||||||
.set_href(Some(x.href))
|
.set_link_type(Some(apb::LinkType::Mention))
|
||||||
|
.set_href(Some(href))
|
||||||
|
},
|
||||||
|
TextMatch::Hashtag { name } => {
|
||||||
|
use apb::LinkMut;
|
||||||
|
let href = format!("{URL_BASE}/tags/{name}");
|
||||||
|
LinkMut::set_name(apb::new(), Some(name)) // TODO ewww but name clashes
|
||||||
|
.set_link_type(Some(apb::LinkType::Hashtag))
|
||||||
|
.set_href(Some(href))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
@ -249,7 +273,9 @@ pub fn PostBox(advanced: WriteSignal<bool>) -> impl IntoView {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for mention in mentions.get().as_deref().unwrap_or(&[]) {
|
for mention in mentions.get().as_deref().unwrap_or(&[]) {
|
||||||
to_vec.push(mention.href.clone());
|
if let TextMatch::Mention { href, .. } = mention {
|
||||||
|
to_vec.push(href.clone());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let payload = apb::new()
|
let payload = apb::new()
|
||||||
.set_object_type(Some(apb::ObjectType::Note))
|
.set_object_type(Some(apb::ObjectType::Note))
|
||||||
|
|
Loading…
Reference in a new issue