2024-04-21 18:56:25 +02:00
|
|
|
use std::{collections::BTreeSet, pin::Pin};
|
2024-04-17 22:07:47 +02:00
|
|
|
|
2024-04-21 18:56:25 +02:00
|
|
|
use apb::{Activity, Base, Object};
|
2024-04-17 22:07:47 +02:00
|
|
|
use leptos::*;
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct Timeline {
|
|
|
|
pub feed: RwSignal<Vec<String>>,
|
|
|
|
pub next: RwSignal<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Timeline {
|
|
|
|
pub fn new(url: String) -> Self {
|
|
|
|
let feed = create_rw_signal(vec![]);
|
|
|
|
let next = create_rw_signal(url);
|
|
|
|
Timeline { feed, next }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(&self, url: String) {
|
|
|
|
self.feed.set(vec![]);
|
|
|
|
self.next.set(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn more(&self, auth: Signal<Option<String>>) -> reqwest::Result<()> {
|
|
|
|
use apb::{Collection, CollectionPage};
|
|
|
|
|
|
|
|
let feed_url = self.next.get();
|
|
|
|
let collection : serde_json::Value = Http::fetch(&feed_url, auth).await?;
|
|
|
|
let activities : Vec<serde_json::Value> = collection
|
|
|
|
.ordered_items()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let mut feed = self.feed.get();
|
|
|
|
let mut older = process_activities(activities, auth).await;
|
|
|
|
feed.append(&mut older);
|
|
|
|
self.feed.set(feed);
|
|
|
|
|
|
|
|
if let Some(next) = collection.next().id() {
|
|
|
|
self.next.set(next);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-22 01:01:20 +02:00
|
|
|
#[component]
|
|
|
|
pub fn TimelineRepliesRecursive(tl: Timeline, root: String) -> impl IntoView {
|
|
|
|
let root_values = move || tl.feed
|
|
|
|
.get()
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|x| CACHE.get(&x))
|
2024-04-22 01:44:00 +02:00
|
|
|
.filter(|x| match x.object_type() {
|
2024-04-22 02:10:27 +02:00
|
|
|
Some(apb::ObjectType::Activity(apb::ActivityType::Create)) => x.object().get().map(|o| o.in_reply_to().id().map(|r| r == root).unwrap_or(false)).unwrap_or(false),
|
|
|
|
Some(apb::ObjectType::Activity(_)) => x.object().id().map(|o| o == root).unwrap_or(false),
|
2024-04-22 01:44:00 +02:00
|
|
|
_ => x.in_reply_to().id().map(|r| r == root).unwrap_or(false),
|
|
|
|
})
|
2024-04-22 01:01:20 +02:00
|
|
|
.collect::<Vec<serde_json::Value>>();
|
|
|
|
|
|
|
|
view! {
|
|
|
|
<For
|
2024-04-22 01:44:00 +02:00
|
|
|
each=root_values
|
2024-04-22 01:01:20 +02:00
|
|
|
key=|k| k.id().unwrap_or_default().to_string()
|
|
|
|
children=move |object: serde_json::Value| {
|
2024-04-22 02:10:27 +02:00
|
|
|
match object.object_type() {
|
|
|
|
Some(apb::ObjectType::Activity(_)) => view! {
|
|
|
|
<ActivityLine activity=object />
|
|
|
|
}.into_view(),
|
|
|
|
_ => {
|
|
|
|
let oid = object.id().unwrap_or_default().to_string();
|
|
|
|
view! {
|
|
|
|
<Object object=object />
|
|
|
|
<div class="ml-1">
|
|
|
|
<TimelineRepliesRecursive tl=tl root=oid />
|
|
|
|
</div>
|
|
|
|
}.into_view()
|
|
|
|
},
|
2024-04-22 01:01:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/ >
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
|
|
|
pub fn TimelineReplies(tl: Timeline, root: String) -> impl IntoView {
|
|
|
|
let auth = use_context::<Auth>().expect("missing auth context");
|
|
|
|
|
|
|
|
view! {
|
|
|
|
<div>
|
|
|
|
<TimelineRepliesRecursive tl=tl root=root />
|
|
|
|
</div>
|
|
|
|
<div class="center mt-1 mb-1" >
|
|
|
|
<button type="button"
|
|
|
|
on:click=move |_| {
|
|
|
|
spawn_local(async move {
|
|
|
|
if let Err(e) = tl.more(auth).await {
|
|
|
|
tracing::error!("error fetching more items for timeline: {e}");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>more</button>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 22:07:47 +02:00
|
|
|
#[component]
|
|
|
|
pub fn TimelineFeed(tl: Timeline) -> impl IntoView {
|
|
|
|
let auth = use_context::<Auth>().expect("missing auth context");
|
|
|
|
view! {
|
|
|
|
<For
|
|
|
|
each=move || tl.feed.get()
|
|
|
|
key=|k| k.to_string()
|
|
|
|
children=move |id: String| {
|
|
|
|
match CACHE.get(&id) {
|
2024-04-21 17:43:36 +02:00
|
|
|
Some(item) => match item.base_type() {
|
|
|
|
Some(apb::BaseType::Object(apb::ObjectType::Activity(_))) => {
|
|
|
|
let object_id = item.object().id().unwrap_or_default();
|
2024-04-21 18:56:25 +02:00
|
|
|
let object = CACHE.get(&object_id).map(|obj| {
|
2024-04-22 01:01:20 +02:00
|
|
|
view! { <Object object=obj /> }
|
2024-04-21 18:56:25 +02:00
|
|
|
});
|
2024-04-21 17:43:36 +02:00
|
|
|
view! {
|
|
|
|
<ActivityLine activity=item />
|
|
|
|
{object}
|
2024-04-19 06:59:34 +02:00
|
|
|
<hr/ >
|
2024-04-21 17:43:36 +02:00
|
|
|
}.into_view()
|
|
|
|
},
|
2024-04-19 06:59:34 +02:00
|
|
|
Some(apb::BaseType::Object(apb::ObjectType::Note)) => view! {
|
2024-04-21 18:56:25 +02:00
|
|
|
<Object object=item.clone() />
|
2024-04-19 06:59:34 +02:00
|
|
|
<hr/ >
|
|
|
|
}.into_view(),
|
|
|
|
_ => view! { <p><code>type not implemented</code></p><hr /> }.into_view(),
|
2024-04-17 22:07:47 +02:00
|
|
|
},
|
|
|
|
None => view! {
|
|
|
|
<p><code>{id}</code>" "[<a href={uri}>go</a>]</p>
|
|
|
|
}.into_view(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/ >
|
|
|
|
<div class="center mt-1 mb-1" >
|
|
|
|
<button type="button"
|
|
|
|
on:click=move |_| {
|
|
|
|
spawn_local(async move {
|
|
|
|
if let Err(e) = tl.more(auth).await {
|
|
|
|
tracing::error!("error fetching more items for timeline: {e}");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>more</button>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn process_activities(
|
|
|
|
activities: Vec<serde_json::Value>,
|
|
|
|
auth: Signal<Option<String>>,
|
|
|
|
) -> Vec<String> {
|
2024-04-21 17:43:36 +02:00
|
|
|
use apb::ActivityMut;
|
2024-04-21 18:56:25 +02:00
|
|
|
let mut sub_tasks : Vec<Pin<Box<dyn futures::Future<Output = ()>>>> = Vec::new();
|
2024-04-17 22:07:47 +02:00
|
|
|
let mut gonna_fetch = BTreeSet::new();
|
2024-04-21 18:56:25 +02:00
|
|
|
let mut actors_seen = BTreeSet::new();
|
2024-04-17 22:07:47 +02:00
|
|
|
let mut out = Vec::new();
|
|
|
|
|
|
|
|
for activity in activities {
|
|
|
|
// save embedded object if present
|
|
|
|
if let Some(object) = activity.object().get() {
|
2024-04-21 18:56:25 +02:00
|
|
|
// also fetch actor attributed to
|
|
|
|
if let Some(attributed_to) = object.attributed_to().id() {
|
|
|
|
actors_seen.insert(attributed_to);
|
|
|
|
}
|
2024-04-17 22:07:47 +02:00
|
|
|
if let Some(object_uri) = object.id() {
|
|
|
|
CACHE.put(object_uri.to_string(), object.clone());
|
2024-04-21 18:56:25 +02:00
|
|
|
} else {
|
|
|
|
tracing::warn!("embedded object without id: {object:?}");
|
2024-04-17 22:07:47 +02:00
|
|
|
}
|
|
|
|
} else { // try fetching it
|
|
|
|
if let Some(object_id) = activity.object().id() {
|
|
|
|
if !gonna_fetch.contains(&object_id) {
|
|
|
|
gonna_fetch.insert(object_id.clone());
|
2024-04-21 18:56:25 +02:00
|
|
|
sub_tasks.push(Box::pin(fetch_and_update_with_user(FetchKind::Object, object_id, auth)));
|
2024-04-17 22:07:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// save activity, removing embedded object
|
|
|
|
let object_id = activity.object().id();
|
|
|
|
if let Some(activity_id) = activity.id() {
|
|
|
|
out.push(activity_id.to_string());
|
|
|
|
CACHE.put(
|
|
|
|
activity_id.to_string(),
|
|
|
|
activity.clone().set_object(apb::Node::maybe_link(object_id))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(uid) = activity.actor().id() {
|
|
|
|
if CACHE.get(&uid).is_none() && !gonna_fetch.contains(&uid) {
|
|
|
|
gonna_fetch.insert(uid.clone());
|
2024-04-21 18:56:25 +02:00
|
|
|
sub_tasks.push(Box::pin(fetch_and_update(FetchKind::User, uid, auth)));
|
2024-04-17 22:07:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-21 18:56:25 +02:00
|
|
|
for user in actors_seen {
|
|
|
|
sub_tasks.push(Box::pin(fetch_and_update(FetchKind::User, user, auth)));
|
|
|
|
}
|
|
|
|
|
2024-04-17 22:07:47 +02:00
|
|
|
futures::future::join_all(sub_tasks).await;
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2024-04-21 18:56:25 +02:00
|
|
|
async fn fetch_and_update(kind: FetchKind, id: String, auth: Signal<Option<String>>) {
|
2024-04-18 05:00:44 +02:00
|
|
|
match Http::fetch(&Uri::api(kind, &id, false), auth).await {
|
2024-04-17 22:07:47 +02:00
|
|
|
Ok(data) => CACHE.put(id, data),
|
|
|
|
Err(e) => console_warn(&format!("could not fetch '{id}': {e}")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-21 18:56:25 +02:00
|
|
|
async fn fetch_and_update_with_user(kind: FetchKind, id: String, auth: Signal<Option<String>>) {
|
|
|
|
fetch_and_update(kind.clone(), id.clone(), auth).await;
|
|
|
|
if let Some(obj) = CACHE.get(&id) {
|
|
|
|
if let Some(actor_id) = match kind {
|
|
|
|
FetchKind::Object => obj.attributed_to().id(),
|
|
|
|
FetchKind::Activity => obj.actor().id(),
|
|
|
|
FetchKind::User | FetchKind::Context => None,
|
|
|
|
} {
|
|
|
|
fetch_and_update(FetchKind::User, actor_id, auth).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|