2024-05-08 02:46:47 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-07-04 02:14:50 +02:00
|
|
|
use cache::WEBFINGER;
|
2024-04-21 17:43:36 +02:00
|
|
|
use leptos::*;
|
2024-07-04 02:14:50 +02:00
|
|
|
use regex::Regex;
|
|
|
|
use crate::prelude::*;
|
2024-04-21 17:43:36 +02:00
|
|
|
|
2024-06-01 01:49:10 +02:00
|
|
|
use apb::{field::OptionalString, target::Addressed, ActivityMut, Base, Collection, CollectionMut, Document, Object, ObjectMut};
|
2024-04-21 17:43:36 +02:00
|
|
|
|
2024-07-04 02:14:50 +02:00
|
|
|
lazy_static::lazy_static! {
|
|
|
|
static ref REGEX: Regex = regex::Regex::new("<a href=\"(.+)\" class=\"u-url mention\">@(\\w+)(@\\w+|)</a>").expect("failed compiling @ regex");
|
2024-04-29 02:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
2024-07-04 02:14:50 +02:00
|
|
|
pub fn Object(
|
|
|
|
object: crate::Object,
|
|
|
|
#[prop(optional)] reply: bool,
|
|
|
|
) -> impl IntoView {
|
2024-04-30 02:09:23 +02:00
|
|
|
let oid = object.id().unwrap_or_default().to_string();
|
2024-06-01 01:49:10 +02:00
|
|
|
let author_id = object.attributed_to().id().str().unwrap_or_default();
|
2024-07-03 03:21:11 +02:00
|
|
|
let author = cache::OBJECTS.get_or(&author_id, serde_json::Value::String(author_id.clone()).into());
|
2024-04-29 02:33:21 +02:00
|
|
|
let sensitive = object.sensitive().unwrap_or_default();
|
2024-04-30 02:09:23 +02:00
|
|
|
let addressed = object.addressed();
|
|
|
|
let public = addressed.iter().any(|x| x.as_str() == apb::target::PUBLIC);
|
2024-06-01 01:49:10 +02:00
|
|
|
let external_url = object.url().id().str().unwrap_or_else(|| oid.clone());
|
2024-04-29 02:33:21 +02:00
|
|
|
let attachments = object.attachment()
|
|
|
|
.map(|x| view! { <Attachment object=x sensitive=sensitive /> })
|
2024-04-22 01:01:20 +02:00
|
|
|
.collect_view();
|
2024-04-29 21:02:13 +02:00
|
|
|
let comments = object.replies().get()
|
2024-04-30 02:09:23 +02:00
|
|
|
.map_or(0, |x| x.total_items().unwrap_or(0));
|
2024-05-02 00:59:47 +02:00
|
|
|
let shares = object.shares().get()
|
2024-04-30 02:09:23 +02:00
|
|
|
.map_or(0, |x| x.total_items().unwrap_or(0));
|
2024-05-02 00:59:47 +02:00
|
|
|
let likes = object.likes().get()
|
2024-04-30 02:09:23 +02:00
|
|
|
.map_or(0, |x| x.total_items().unwrap_or(0));
|
2024-05-02 00:59:47 +02:00
|
|
|
let already_liked = object.liked_by_me().unwrap_or(false);
|
2024-05-08 02:46:47 +02:00
|
|
|
|
2024-04-23 23:28:19 +02:00
|
|
|
let attachments_padding = if object.attachment().is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(view! { <div class="pb-1"></div> })
|
|
|
|
};
|
2024-06-06 21:47:23 +02:00
|
|
|
|
2024-07-04 02:14:50 +02:00
|
|
|
let mut content = mdhtml::safe_html(object.content().unwrap_or_default());
|
|
|
|
|
|
|
|
let mut results = vec![];
|
|
|
|
for (matched, [id, username, _domain]) in REGEX.captures_iter(&content).map(|c| c.extract()) {
|
|
|
|
// TODO what the fuck mastodon........... why are you putting the fancy url in the A HREF????????
|
|
|
|
let id = id.replace('@', "users/");
|
|
|
|
// TODO ughh ugly on-the-fly html editing, can this be avoided?
|
|
|
|
let to_replace = format!(
|
|
|
|
"<a class=\"clean dim\" href=\"{}\" title=\"{}\"><span class=\"border-button mr-s\"><code class=\"color mr-s\">@</code>{}</span></a>",
|
|
|
|
Uri::web(U::Actor, &id), id, username
|
|
|
|
);
|
|
|
|
results.push((matched.to_string(), to_replace));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (from, to) in results {
|
|
|
|
content = content.replace(&from, &to);
|
|
|
|
}
|
|
|
|
|
2024-06-06 21:47:23 +02:00
|
|
|
let audience_badge = object.audience().id().str()
|
|
|
|
.map(|x| view! {
|
2024-07-04 02:36:31 +02:00
|
|
|
<a class="clean dim" href={Uri::web(U::Actor, &x)}>
|
|
|
|
<span class="border-button ml-1" title={x.clone()}>
|
2024-07-04 02:14:50 +02:00
|
|
|
<code class="color mr-s">&</code>
|
2024-07-04 02:36:31 +02:00
|
|
|
<small class="mr-s">
|
2024-07-03 07:30:29 +02:00
|
|
|
{Uri::pretty(&x, 30)}
|
2024-06-07 05:47:51 +02:00
|
|
|
</small>
|
|
|
|
</span>
|
|
|
|
</a>
|
2024-06-06 21:47:23 +02:00
|
|
|
});
|
|
|
|
|
2024-07-04 02:36:31 +02:00
|
|
|
let hashtag_badges = object.tag().filter_map(|x| {
|
|
|
|
if let Ok(apb::LinkType::Hashtag) = apb::Link::link_type(&x) {
|
|
|
|
let name = apb::Link::name(&x).unwrap_or_default().replace('#', "");
|
|
|
|
let href = Uri::web(U::Hashtag, &name);
|
|
|
|
Some(view! {
|
|
|
|
<a class="clean dim" href={href}>
|
|
|
|
<span class="border-button ml-1">
|
|
|
|
<code class="color mr-s">#</code>
|
|
|
|
<small class="mr-s">
|
|
|
|
{name}
|
|
|
|
</small>
|
|
|
|
</span>
|
|
|
|
</a>
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}).collect_view();
|
|
|
|
|
2024-07-04 02:14:50 +02:00
|
|
|
let post_image = object.image().get().and_then(|x| x.url().id().str()).map(|x| {
|
|
|
|
let (expand, set_expand) = create_signal(false);
|
|
|
|
view! {
|
|
|
|
<img src={x} class="flex-pic box cursor" class:flex-pic-expand=expand on:click=move|_| set_expand.set(!expand.get()) />
|
|
|
|
}
|
2024-07-03 05:58:29 +02:00
|
|
|
});
|
|
|
|
|
2024-05-08 02:46:47 +02:00
|
|
|
let post_inner = view! {
|
2024-06-01 01:13:49 +02:00
|
|
|
<Summary summary=object.summary().ok().map(|x| x.to_string()) >
|
2024-05-08 02:46:47 +02:00
|
|
|
<p inner_html={content}></p>
|
|
|
|
{attachments_padding}
|
|
|
|
{attachments}
|
|
|
|
</Summary>
|
|
|
|
};
|
|
|
|
let post = match object.object_type() {
|
2024-05-13 17:18:16 +02:00
|
|
|
// mastodon, pleroma, misskey
|
2024-06-01 01:13:49 +02:00
|
|
|
Ok(apb::ObjectType::Note) => view! {
|
2024-06-12 06:02:36 +02:00
|
|
|
<article class="tl">{post_inner}</article>
|
2024-05-13 17:18:16 +02:00
|
|
|
}.into_view(),
|
|
|
|
// lemmy with Page, peertube with Video
|
2024-06-01 01:13:49 +02:00
|
|
|
Ok(apb::ObjectType::Document(t)) => view! {
|
2024-07-04 02:14:50 +02:00
|
|
|
<article class="float-container ml-1 mr-1" >
|
2024-07-03 05:58:29 +02:00
|
|
|
{post_image}
|
2024-07-04 02:14:50 +02:00
|
|
|
<div>
|
2024-07-03 05:58:29 +02:00
|
|
|
<h4 class="mt-s mb-1" title={t.as_ref().to_string()}>
|
|
|
|
<b>{object.name().unwrap_or_default().to_string()}</b>
|
|
|
|
</h4>
|
|
|
|
{post_inner}
|
|
|
|
</div>
|
2024-06-24 04:09:46 +02:00
|
|
|
</article>
|
2024-05-08 02:46:47 +02:00
|
|
|
}.into_view(),
|
2024-05-13 17:18:16 +02:00
|
|
|
// wordpress, ... ?
|
2024-06-01 01:13:49 +02:00
|
|
|
Ok(apb::ObjectType::Article) => view! {
|
2024-06-24 04:09:46 +02:00
|
|
|
<article>
|
2024-05-13 17:18:16 +02:00
|
|
|
<h3>{object.name().unwrap_or_default().to_string()}</h3>
|
|
|
|
<hr />
|
|
|
|
{post_inner}
|
2024-06-24 04:09:46 +02:00
|
|
|
</article>
|
2024-05-13 17:18:16 +02:00
|
|
|
}.into_view(),
|
|
|
|
// everything else
|
2024-06-01 01:13:49 +02:00
|
|
|
Ok(t) => view! {
|
2024-05-08 02:46:47 +02:00
|
|
|
<h3>{t.as_ref().to_string()}</h3>
|
|
|
|
{post_inner}
|
|
|
|
}.into_view(),
|
2024-05-13 17:18:16 +02:00
|
|
|
// object without type?
|
2024-06-01 01:13:49 +02:00
|
|
|
Err(_) => view! { <code>missing object type</code> }.into_view(),
|
2024-05-08 02:46:47 +02:00
|
|
|
};
|
2024-04-21 17:43:36 +02:00
|
|
|
view! {
|
2024-05-03 03:19:14 +02:00
|
|
|
<table class="align w-100 ml-s mr-s">
|
2024-04-21 17:43:36 +02:00
|
|
|
<tr>
|
|
|
|
<td><ActorBanner object=author /></td>
|
|
|
|
<td class="rev" >
|
2024-06-01 01:49:10 +02:00
|
|
|
{object.in_reply_to().id().str().map(|reply| view! {
|
2024-05-20 06:25:47 +02:00
|
|
|
<small><i><a class="clean" href={Uri::web(U::Object, &reply)} title={reply}>reply</a></i></small>
|
2024-04-22 01:01:20 +02:00
|
|
|
})}
|
2024-04-30 02:09:23 +02:00
|
|
|
<PrivacyMarker addressed=addressed />
|
2024-05-20 06:25:47 +02:00
|
|
|
<a class="clean hover ml-s" href={Uri::web(U::Object, object.id().unwrap_or_default())}>
|
2024-06-01 01:13:49 +02:00
|
|
|
<DateTime t=object.published().ok() />
|
2024-04-21 17:43:36 +02:00
|
|
|
</a>
|
2024-05-13 01:21:47 +02:00
|
|
|
<sup><small><a class="clean ml-s" href={external_url} target="_blank">"↗"</a></small></sup>
|
2024-04-21 17:43:36 +02:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
2024-05-08 02:46:47 +02:00
|
|
|
{post}
|
2024-04-29 21:02:13 +02:00
|
|
|
<div class="mt-s ml-1 rev">
|
2024-07-04 02:36:31 +02:00
|
|
|
{if !reply { Some(hashtag_badges) } else { None }}
|
2024-07-04 02:14:50 +02:00
|
|
|
{if !reply { audience_badge } else { None }}
|
2024-05-01 16:46:19 +02:00
|
|
|
<ReplyButton n=comments target=oid.clone() />
|
2024-04-30 04:29:14 +02:00
|
|
|
<LikeButton n=likes liked=already_liked target=oid.clone() author=author_id private=!public />
|
|
|
|
<RepostButton n=shares target=oid />
|
2024-04-29 21:02:13 +02:00
|
|
|
</div>
|
2024-04-23 23:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
2024-05-12 00:12:07 +02:00
|
|
|
pub fn Summary(summary: Option<String>, children: Children) -> impl IntoView {
|
|
|
|
let config = use_context::<Signal<crate::Config>>().expect("missing config context");
|
2024-04-23 23:28:19 +02:00
|
|
|
match summary.filter(|x| !x.is_empty()) {
|
|
|
|
None => children().into_view(),
|
|
|
|
Some(summary) => view! {
|
2024-05-12 00:12:07 +02:00
|
|
|
<details class="pa-s" prop:open=move || !config.get().collapse_content_warnings>
|
2024-04-23 23:28:19 +02:00
|
|
|
<summary>
|
2024-04-29 22:01:49 +02:00
|
|
|
<code class="cw center color ml-s w-100">{summary}</code>
|
2024-04-23 23:28:19 +02:00
|
|
|
</summary>
|
|
|
|
{children()}
|
|
|
|
</details>
|
|
|
|
}.into_view(),
|
2024-04-21 17:43:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 02:09:23 +02:00
|
|
|
#[component]
|
|
|
|
pub fn LikeButton(
|
|
|
|
n: u64,
|
|
|
|
target: String,
|
|
|
|
liked: bool,
|
2024-04-30 04:29:14 +02:00
|
|
|
author: String,
|
|
|
|
#[prop(optional)]
|
|
|
|
private: bool,
|
2024-04-30 02:09:23 +02:00
|
|
|
) -> impl IntoView {
|
|
|
|
let (count, set_count) = create_signal(n);
|
|
|
|
let (clicked, set_clicked) = create_signal(!liked);
|
|
|
|
let auth = use_context::<Auth>().expect("missing auth context");
|
|
|
|
view! {
|
|
|
|
<span
|
|
|
|
class:emoji=clicked
|
2024-05-01 18:22:25 +02:00
|
|
|
class:emoji-btn=move || auth.present()
|
|
|
|
class:cursor=move || clicked.get() && auth.present()
|
2024-05-01 16:55:02 +02:00
|
|
|
class="ml-2"
|
2024-04-30 02:09:23 +02:00
|
|
|
on:click=move |_ev| {
|
2024-05-01 18:22:25 +02:00
|
|
|
if !auth.present() { return; }
|
2024-04-30 02:09:23 +02:00
|
|
|
if !clicked.get() { return; }
|
2024-04-30 04:29:14 +02:00
|
|
|
let to = apb::Node::links(vec![author.to_string()]);
|
|
|
|
let cc = if private { apb::Node::Empty } else {
|
|
|
|
apb::Node::links(vec![
|
|
|
|
apb::target::PUBLIC.to_string(),
|
2024-05-29 20:51:30 +02:00
|
|
|
format!("{URL_BASE}/actors/{}/followers", auth.username())
|
2024-04-30 04:29:14 +02:00
|
|
|
])
|
2024-04-30 02:09:23 +02:00
|
|
|
};
|
|
|
|
let payload = serde_json::Value::Object(serde_json::Map::default())
|
|
|
|
.set_activity_type(Some(apb::ActivityType::Like))
|
|
|
|
.set_object(apb::Node::link(target.clone()))
|
|
|
|
.set_to(to)
|
|
|
|
.set_cc(cc);
|
2024-05-08 02:46:47 +02:00
|
|
|
let target = target.clone();
|
2024-04-30 02:09:23 +02:00
|
|
|
spawn_local(async move {
|
2024-05-01 18:22:25 +02:00
|
|
|
match Http::post(&auth.outbox(), &payload, auth).await {
|
2024-04-30 02:09:23 +02:00
|
|
|
Ok(()) => {
|
|
|
|
set_clicked.set(false);
|
|
|
|
set_count.set(count.get() + 1);
|
2024-07-03 03:21:11 +02:00
|
|
|
if let Some(cached) = cache::OBJECTS.get(&target) {
|
2024-05-08 02:46:47 +02:00
|
|
|
let mut new = (*cached).clone().set_liked_by_me(Some(true));
|
|
|
|
if let Some(likes) = new.likes().get() {
|
2024-06-01 01:13:49 +02:00
|
|
|
if let Ok(count) = likes.total_items() {
|
2024-05-08 02:46:47 +02:00
|
|
|
new = new.set_likes(apb::Node::object(likes.clone().set_total_items(Some(count + 1))));
|
|
|
|
}
|
|
|
|
}
|
2024-07-03 04:05:49 +02:00
|
|
|
cache::OBJECTS.store(&target, Arc::new(new));
|
2024-05-08 02:46:47 +02:00
|
|
|
}
|
2024-04-30 02:09:23 +02:00
|
|
|
},
|
|
|
|
Err(e) => tracing::error!("failed sending like: {e}"),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{move || if count.get() > 0 { Some(view! { <small>{count}</small> })} else { None }}
|
|
|
|
" ⭐"
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
2024-05-01 16:46:19 +02:00
|
|
|
pub fn ReplyButton(n: u64, target: String) -> impl IntoView {
|
|
|
|
let reply = use_context::<ReplyControls>().expect("missing reply controls context");
|
2024-05-01 16:55:02 +02:00
|
|
|
let auth = use_context::<Auth>().expect("missing auth context");
|
2024-04-30 02:09:23 +02:00
|
|
|
let comments = if n > 0 {
|
|
|
|
Some(view! { <small>{n}</small> })
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2024-05-01 16:46:19 +02:00
|
|
|
let _target = target.clone(); // TODO ughhhh useless clones
|
2024-04-30 02:09:23 +02:00
|
|
|
view! {
|
2024-05-01 16:46:19 +02:00
|
|
|
<span
|
|
|
|
class:emoji=move || !reply.reply_to.get().map_or(false, |x| x == _target)
|
2024-05-01 16:55:02 +02:00
|
|
|
// TODO can we merge these two classes conditions?
|
2024-05-01 18:22:25 +02:00
|
|
|
class:emoji-btn=move || auth.present()
|
|
|
|
class:cursor=move || auth.present()
|
2024-05-01 16:55:02 +02:00
|
|
|
class="ml-2"
|
2024-05-01 18:22:25 +02:00
|
|
|
on:click=move |_ev| if auth.present() { reply.reply(&target) }
|
2024-05-01 16:46:19 +02:00
|
|
|
>
|
|
|
|
{comments}
|
|
|
|
" 📨"
|
|
|
|
</span>
|
2024-04-30 02:09:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
2024-04-30 04:29:14 +02:00
|
|
|
pub fn RepostButton(n: u64, target: String) -> impl IntoView {
|
|
|
|
let (count, set_count) = create_signal(n);
|
|
|
|
let (clicked, set_clicked) = create_signal(true);
|
|
|
|
let auth = use_context::<Auth>().expect("missing auth context");
|
2024-04-30 02:09:23 +02:00
|
|
|
view! {
|
2024-04-30 04:29:14 +02:00
|
|
|
<span
|
|
|
|
class:emoji=clicked
|
2024-05-01 18:22:25 +02:00
|
|
|
class:emoji-btn=move || auth.present()
|
|
|
|
class:cursor=move || clicked.get() && auth.present()
|
2024-05-01 16:55:02 +02:00
|
|
|
class="ml-2"
|
2024-04-30 04:29:14 +02:00
|
|
|
on:click=move |_ev| {
|
2024-05-01 18:22:25 +02:00
|
|
|
if !auth.present() { return; }
|
2024-04-30 04:29:14 +02:00
|
|
|
if !clicked.get() { return; }
|
|
|
|
set_clicked.set(false);
|
|
|
|
let to = apb::Node::links(vec![apb::target::PUBLIC.to_string()]);
|
2024-05-29 20:51:30 +02:00
|
|
|
let cc = apb::Node::links(vec![format!("{URL_BASE}/actors/{}/followers", auth.username())]);
|
2024-04-30 04:29:14 +02:00
|
|
|
let payload = serde_json::Value::Object(serde_json::Map::default())
|
|
|
|
.set_activity_type(Some(apb::ActivityType::Announce))
|
|
|
|
.set_object(apb::Node::link(target.clone()))
|
|
|
|
.set_to(to)
|
|
|
|
.set_cc(cc);
|
|
|
|
spawn_local(async move {
|
2024-05-01 18:22:25 +02:00
|
|
|
match Http::post(&auth.outbox(), &payload, auth).await {
|
2024-04-30 04:29:14 +02:00
|
|
|
Ok(()) => set_count.set(count.get() + 1),
|
|
|
|
Err(e) => tracing::error!("failed sending like: {e}"),
|
|
|
|
}
|
|
|
|
set_clicked.set(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{move || if count.get() > 0 { Some(view! { <small>{count}</small> })} else { None }}
|
|
|
|
" 🚀"
|
|
|
|
</span>
|
2024-04-30 02:09:23 +02:00
|
|
|
}
|
|
|
|
}
|