2024-04-15 05:27:53 +02:00
|
|
|
pub mod context;
|
2024-04-14 06:45:51 +02:00
|
|
|
|
|
|
|
use apb::{target::Addressed, Activity, ActivityMut, Actor, Base, Collection, Object, ObjectMut};
|
2024-04-15 05:27:53 +02:00
|
|
|
use context::CTX;
|
2024-04-14 06:45:51 +02:00
|
|
|
use leptos::{leptos_dom::logging::console_log, *};
|
2024-04-15 05:00:23 +02:00
|
|
|
use leptos_router::*;
|
2024-04-14 06:45:51 +02:00
|
|
|
|
2024-04-15 05:27:53 +02:00
|
|
|
|
2024-04-15 05:00:23 +02:00
|
|
|
pub const URL_BASE: &str = "https://feditest.alemi.dev";
|
|
|
|
pub const URL_PREFIX: &str = "/web";
|
2024-04-14 06:45:51 +02:00
|
|
|
|
|
|
|
#[derive(Debug, serde::Serialize)]
|
|
|
|
struct LoginForm {
|
|
|
|
email: String,
|
|
|
|
password: String,
|
|
|
|
}
|
|
|
|
|
2024-04-15 22:03:32 +02:00
|
|
|
fn web_uri(kind: &str, url: &str) -> String {
|
|
|
|
if url.starts_with(URL_BASE) {
|
|
|
|
format!("/web/{kind}/{}", url.split('/').last().unwrap_or_default().to_string())
|
|
|
|
} else {
|
|
|
|
format!("/web/{kind}/+{}", url.replace("https://", "").replace('/', "@"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-14 06:45:51 +02:00
|
|
|
#[component]
|
|
|
|
pub fn LoginBox(
|
|
|
|
rx: Signal<Option<String>>,
|
|
|
|
tx: WriteSignal<Option<String>>,
|
|
|
|
) -> impl IntoView {
|
|
|
|
let username_ref: NodeRef<html::Input> = create_node_ref();
|
|
|
|
let password_ref: NodeRef<html::Input> = create_node_ref();
|
|
|
|
view! {
|
|
|
|
<div>
|
2024-04-15 03:38:16 +02:00
|
|
|
<div class="w-100" class:hidden=move || { rx.get().unwrap_or_default().is_empty() }>
|
2024-04-15 05:00:23 +02:00
|
|
|
"Hello "<a href="/web/users/test" >test</a>
|
2024-04-15 03:38:16 +02:00
|
|
|
<input style="float:right" type="submit" value="logout" on:click=move |_| {
|
2024-04-14 06:45:51 +02:00
|
|
|
tx.set(None);
|
|
|
|
} />
|
|
|
|
</div>
|
|
|
|
<div class:hidden=move || { !rx.get().unwrap_or_default().is_empty() }>
|
2024-04-15 17:54:28 +02:00
|
|
|
<input class="w-100" type="text" node_ref=username_ref placeholder="username" />
|
|
|
|
<input class="w-100" type="text" node_ref=password_ref placeholder="password" />
|
|
|
|
<input class="w-100" type="submit" value="login" on:click=move |_| {
|
|
|
|
console_log("logging in...");
|
|
|
|
let email = username_ref.get().map(|x| x.value()).unwrap_or("".into());
|
|
|
|
let password = password_ref.get().map(|x| x.value()).unwrap_or("".into());
|
|
|
|
spawn_local(async move {
|
|
|
|
let auth = reqwest::Client::new()
|
|
|
|
.post(format!("{URL_BASE}/auth"))
|
|
|
|
.json(&LoginForm { email, password })
|
|
|
|
.send()
|
|
|
|
.await.unwrap()
|
|
|
|
.json::<String>()
|
|
|
|
.await.unwrap();
|
|
|
|
tx.set(Some(auth));
|
|
|
|
});
|
|
|
|
} />
|
2024-04-14 06:45:51 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
|
|
|
pub fn PostBox(token: Signal<Option<String>>) -> impl IntoView {
|
|
|
|
let summary_ref: NodeRef<html::Input> = create_node_ref();
|
|
|
|
let content_ref: NodeRef<html::Textarea> = create_node_ref();
|
|
|
|
view! {
|
2024-04-15 17:54:28 +02:00
|
|
|
<div class:hidden=move || { token.get().unwrap_or_default().is_empty() }>
|
2024-04-14 06:45:51 +02:00
|
|
|
<input class="w-100" type="text" node_ref=summary_ref placeholder="CW" />
|
|
|
|
<textarea class="w-100" node_ref=content_ref placeholder="hello world!" ></textarea>
|
|
|
|
<button class="w-100" type="button" on:click=move |_| {
|
|
|
|
spawn_local(async move {
|
|
|
|
let summary = summary_ref.get().map(|x| x.value());
|
|
|
|
let content = content_ref.get().map(|x| x.value()).unwrap_or("".into());
|
|
|
|
reqwest::Client::new()
|
2024-04-15 05:00:23 +02:00
|
|
|
.post(format!("{URL_BASE}/users/test/outbox"))
|
2024-04-14 06:45:51 +02:00
|
|
|
.header("Authorization", format!("Bearer {}", token.get().unwrap_or_default()))
|
|
|
|
.json(
|
|
|
|
&serde_json::Value::Object(serde_json::Map::default())
|
|
|
|
.set_object_type(Some(apb::ObjectType::Note))
|
|
|
|
.set_summary(summary.as_deref())
|
|
|
|
.set_content(Some(&content))
|
|
|
|
.set_to(apb::Node::links(vec![apb::target::PUBLIC.to_string()]))
|
2024-04-15 05:00:23 +02:00
|
|
|
.set_cc(apb::Node::links(vec![format!("{URL_BASE}/users/test/followers")]))
|
2024-04-14 06:45:51 +02:00
|
|
|
)
|
|
|
|
.send()
|
|
|
|
.await.unwrap()
|
|
|
|
.error_for_status()
|
|
|
|
.unwrap();
|
|
|
|
})
|
|
|
|
} >post</button>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
|
|
|
pub fn TimelinePicker(
|
|
|
|
tx: WriteSignal<String>,
|
|
|
|
rx: ReadSignal<String>,
|
|
|
|
) -> impl IntoView {
|
|
|
|
let targets = (
|
|
|
|
"https://feditest.alemi.dev/users/test/inbox/page".to_string(),
|
|
|
|
"https://feditest.alemi.dev/users/test/outbox/page".to_string(),
|
|
|
|
"https://feditest.alemi.dev/inbox/page".to_string(),
|
|
|
|
"https://feditest.alemi.dev/outbox/page".to_string(),
|
|
|
|
);
|
|
|
|
let (my_in, my_out, our_in, our_out) = targets.clone();
|
|
|
|
let (my_in_, my_out_, our_in_, our_out_) = targets;
|
|
|
|
view! {
|
2024-04-15 03:38:16 +02:00
|
|
|
<input type="submit" class:active=move || rx.get() == my_in_ on:click=move |_| tx.set(my_in.clone()) value="my inbox" />
|
|
|
|
<input type="submit" class:active=move || rx.get() == my_out_ on:click=move |_| tx.set(my_out.clone()) value="my outbox" />
|
|
|
|
<input type="submit" class:active=move || rx.get() == our_in_ on:click=move |_| tx.set(our_in.clone()) value="global inbox" />
|
|
|
|
<input type="submit" class:active=move || rx.get() == our_out_ on:click=move |_| tx.set(our_out.clone()) value="global outbox" />
|
2024-04-14 06:45:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
2024-04-15 03:38:16 +02:00
|
|
|
pub fn ActorBanner(object: serde_json::Value) -> impl IntoView {
|
2024-04-14 06:45:51 +02:00
|
|
|
match object {
|
|
|
|
serde_json::Value::String(id) => view! {
|
|
|
|
<div><b>{id}</b></div>
|
|
|
|
},
|
|
|
|
serde_json::Value::Object(_) => {
|
2024-04-15 22:03:32 +02:00
|
|
|
let uid = object.id().unwrap_or_default().to_string();
|
|
|
|
let uri = web_uri("users", &uid);
|
2024-04-14 06:45:51 +02:00
|
|
|
let avatar_url = object.icon().get().map(|x| x.url().id().unwrap_or_default()).unwrap_or_default();
|
|
|
|
let display_name = object.name().unwrap_or_default().to_string();
|
|
|
|
let username = object.preferred_username().unwrap_or_default().to_string();
|
|
|
|
let domain = object.id().unwrap_or_default().replace("https://", "").split('/').next().unwrap_or_default().to_string();
|
|
|
|
view! {
|
|
|
|
<div>
|
|
|
|
<table class="align" >
|
|
|
|
<tr>
|
|
|
|
<td rowspan="2" ><img class="avatar-circle" src={avatar_url} /></td>
|
|
|
|
<td><b>{display_name}</b></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
2024-04-15 22:03:32 +02:00
|
|
|
<td class="top" ><a class="clean" href={uri} ><small>{username}@{domain}</small></a></td>
|
2024-04-14 06:45:51 +02:00
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => view! {
|
|
|
|
<div><b>invalid actor</b></div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 03:38:16 +02:00
|
|
|
#[component]
|
|
|
|
pub fn Actor() -> impl IntoView {
|
|
|
|
let params = use_params_map();
|
|
|
|
let actor = create_local_resource(move || params.get().get("id").cloned().unwrap_or_default(), |uid| {
|
|
|
|
async move {
|
2024-04-15 05:27:53 +02:00
|
|
|
let uid = format!("{URL_BASE}/users/{uid}");
|
2024-04-15 22:03:32 +02:00
|
|
|
match CTX.cache.actors.get(&uid) {
|
2024-04-15 05:27:53 +02:00
|
|
|
Some(x) => x.clone(),
|
|
|
|
None => reqwest::get(uid)
|
2024-04-15 03:38:16 +02:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.json::<serde_json::Value>()
|
|
|
|
.await
|
2024-04-15 05:27:53 +02:00
|
|
|
.unwrap(),
|
|
|
|
}
|
2024-04-15 03:38:16 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
view! {
|
|
|
|
{move || match actor.get() {
|
|
|
|
None => view! { <p>loading...</p> }.into_view(),
|
|
|
|
Some(x) => view! {
|
|
|
|
<div class="ml-3 mr-3 mt-3">
|
|
|
|
<ActorBanner object=x.clone() />
|
|
|
|
<p
|
2024-04-15 22:03:32 +02:00
|
|
|
class="pb-2 pt-2 pr-2 pl-2"
|
2024-04-15 03:38:16 +02:00
|
|
|
style={format!(
|
|
|
|
"background-image: url({}); background-size: cover;",
|
|
|
|
x.image().get().map(|x| x.url().id().unwrap_or_default()).unwrap_or_default()
|
|
|
|
)}
|
|
|
|
>
|
2024-04-15 22:03:32 +02:00
|
|
|
{x.summary().unwrap_or("").to_string()}
|
2024-04-15 03:38:16 +02:00
|
|
|
</p>
|
|
|
|
<ul>
|
2024-04-15 22:03:32 +02:00
|
|
|
<li><code>type</code>" "<b>{x.actor_type().unwrap_or(apb::ActorType::Person).as_ref().to_string()}</b></li>
|
2024-04-15 03:38:16 +02:00
|
|
|
<li><code>following</code>" "<b>{x.following().get().map(|x| x.total_items().unwrap_or(0))}</b></li>
|
|
|
|
<li><code>followers</code>" "<b>{x.followers().get().map(|x| x.total_items().unwrap_or(0))}</b></li>
|
|
|
|
<li><code>created</code>" "{x.published().map(|x| x.to_rfc3339())}</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
}.into_view(),
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-14 06:45:51 +02:00
|
|
|
#[component]
|
2024-04-15 22:03:32 +02:00
|
|
|
pub fn ObjectPage() -> impl IntoView {
|
|
|
|
let params = use_params_map();
|
|
|
|
let object = create_local_resource(move || params.get().get("id").cloned().unwrap_or_default(), |oid| {
|
|
|
|
async move {
|
|
|
|
let uid = format!("{URL_BASE}/objects/{oid}");
|
|
|
|
match CTX.cache.actors.get(&uid) {
|
|
|
|
Some(x) => x.clone(),
|
|
|
|
None => reqwest::get(uid)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.json::<serde_json::Value>()
|
|
|
|
.await
|
|
|
|
.unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
view! {
|
|
|
|
{move || match object.get() {
|
|
|
|
Some(o) => view!{ <Object object=o /> }.into_view(),
|
|
|
|
None => view! { <p> loading ... </p> }.into_view(),
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
|
|
|
pub fn Object(object: serde_json::Value) -> impl IntoView {
|
|
|
|
let summary = object.summary().unwrap_or_default().to_string();
|
|
|
|
let content = object.content().unwrap_or_default().to_string();
|
|
|
|
let date = object.published().map(|x| x.to_rfc3339()).unwrap_or_default();
|
|
|
|
let author_id = object.attributed_to().id().unwrap_or_default();
|
|
|
|
let author = CTX.cache.actors.get(&author_id).map(|x| view! { <ActorBanner object=x.clone() /> });
|
|
|
|
view! {
|
|
|
|
{author}
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<td>{summary}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>{content}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>{date}</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
|
|
|
pub fn InlineActivity(activity: serde_json::Value) -> impl IntoView {
|
2024-04-14 06:45:51 +02:00
|
|
|
let object = activity.clone().object().extract().unwrap_or_else(||
|
|
|
|
serde_json::Value::String(activity.object().id().unwrap_or_default())
|
|
|
|
);
|
|
|
|
let object_id = object.id().unwrap_or_default().to_string();
|
2024-04-15 22:03:32 +02:00
|
|
|
let object_uri = web_uri("objects", &object_id);
|
2024-04-14 06:45:51 +02:00
|
|
|
let content = dissolve::strip_html_tags(object.content().unwrap_or_default());
|
|
|
|
let addressed = activity.addressed();
|
|
|
|
let audience = format!("[ {} ]", addressed.join(", "));
|
|
|
|
let privacy = if addressed.iter().any(|x| x == apb::target::PUBLIC) {
|
|
|
|
"[public]"
|
|
|
|
} else if addressed.iter().any(|x| x.ends_with("/followers")) {
|
|
|
|
"[followers]"
|
|
|
|
} else {
|
|
|
|
"[private]"
|
|
|
|
};
|
|
|
|
let title = object.summary().unwrap_or_default().to_string();
|
2024-04-15 22:03:32 +02:00
|
|
|
let date = object.published().map(|x| x.to_rfc3339()).unwrap_or_else(||
|
|
|
|
activity.published().map(|x| x.to_rfc3339()).unwrap_or_default()
|
|
|
|
);
|
2024-04-14 06:45:51 +02:00
|
|
|
let kind = activity.activity_type().unwrap_or(apb::ActivityType::Activity);
|
|
|
|
view! {
|
|
|
|
{match kind {
|
|
|
|
// post
|
|
|
|
apb::ActivityType::Create => view! {
|
|
|
|
<div>
|
|
|
|
<p><i>{title}</i></p>
|
2024-04-15 22:03:32 +02:00
|
|
|
{
|
|
|
|
content
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| view! { <p>{x}</p> }.into_view())
|
|
|
|
.collect::<Vec<View>>()
|
|
|
|
}
|
2024-04-14 06:45:51 +02:00
|
|
|
</div>
|
|
|
|
},
|
|
|
|
kind => view! {
|
|
|
|
<div>
|
2024-04-15 22:03:32 +02:00
|
|
|
<b>{kind.as_ref().to_string()}</b>" >> "<a href={object_uri}>{object_id}</a>
|
2024-04-14 06:45:51 +02:00
|
|
|
</div>
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
<small><u title={audience} >{privacy}</u>" "{date}</small>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 03:03:01 +02:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
#[error("{0}")]
|
|
|
|
struct OmgReqwestErrorIsNotClonable(String);
|
|
|
|
|
2024-04-14 06:45:51 +02:00
|
|
|
#[component]
|
|
|
|
pub fn Timeline(
|
|
|
|
token: Signal<Option<String>>,
|
|
|
|
) -> impl IntoView {
|
2024-04-15 05:00:23 +02:00
|
|
|
let (timeline, set_timeline) = create_signal(format!("{URL_BASE}/inbox/page"));
|
2024-04-15 05:27:53 +02:00
|
|
|
let items = create_local_resource(move || timeline.get(), move |feed_url| async move {
|
|
|
|
fetch_activities_with_users(&feed_url, token).await
|
2024-04-15 03:03:01 +02:00
|
|
|
});
|
|
|
|
view! {
|
|
|
|
<div class="ml-1">
|
|
|
|
<TimelinePicker tx=set_timeline rx=timeline />
|
2024-04-15 22:03:32 +02:00
|
|
|
<div class="boxscroll" >
|
2024-04-15 05:00:23 +02:00
|
|
|
<ErrorBoundary fallback=move |err| view! { <p>{format!("{:?}", err.get())}</p> } >
|
|
|
|
{move || items.with(|x| match x {
|
|
|
|
None => Ok(view! { <p>loading...</p> }.into_view()),
|
|
|
|
Some(data) => match data {
|
|
|
|
Err(e) => Err(OmgReqwestErrorIsNotClonable(e.to_string())),
|
|
|
|
Ok(values) => Ok(
|
|
|
|
values
|
|
|
|
.iter()
|
|
|
|
.map(|object| {
|
|
|
|
let actor = object.actor().extract().unwrap_or_else(||
|
|
|
|
serde_json::Value::String(object.actor().id().unwrap_or_default())
|
|
|
|
);
|
|
|
|
view! {
|
|
|
|
<div class="ml-1 mr-1 mt-1">
|
|
|
|
<ActorBanner object=actor />
|
2024-04-15 22:03:32 +02:00
|
|
|
<InlineActivity activity=object.clone() />
|
2024-04-15 05:00:23 +02:00
|
|
|
</div>
|
|
|
|
<hr/ >
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<Fragment>>()
|
|
|
|
.into_view()
|
|
|
|
),
|
|
|
|
}
|
|
|
|
})}
|
|
|
|
</ErrorBoundary>
|
|
|
|
</div>
|
2024-04-15 03:03:01 +02:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
2024-04-14 06:45:51 +02:00
|
|
|
|
2024-04-15 03:03:01 +02:00
|
|
|
async fn fetch_activities_with_users(
|
|
|
|
feed_url: &str,
|
|
|
|
token: Signal<Option<String>>,
|
|
|
|
) -> reqwest::Result<Vec<serde_json::Value>> {
|
|
|
|
let mut req = reqwest::Client::new().get(feed_url);
|
2024-04-14 06:45:51 +02:00
|
|
|
|
2024-04-15 03:03:01 +02:00
|
|
|
if let Some(token) = token.get() {
|
|
|
|
req = req.header("Authorization", format!("Bearer {token}"));
|
|
|
|
}
|
2024-04-14 06:45:51 +02:00
|
|
|
|
2024-04-15 03:03:01 +02:00
|
|
|
let activities : Vec<serde_json::Value> = req
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.json::<serde_json::Value>()
|
|
|
|
.await?
|
|
|
|
.ordered_items()
|
|
|
|
.collect();
|
2024-04-14 06:45:51 +02:00
|
|
|
|
2024-04-15 03:03:01 +02:00
|
|
|
// i could make this fancier with iterators and futures::join_all but they would run
|
|
|
|
// concurrently and make a ton of parallel request, we actually want these sequential because
|
|
|
|
// first one may fetch same user as second one
|
|
|
|
// some fancier logic may make a set of all actors and fetch uniques concurrently...
|
|
|
|
let mut out = Vec::new();
|
|
|
|
for x in activities {
|
|
|
|
if let Some(uid) = x.actor().id() {
|
2024-04-15 22:03:32 +02:00
|
|
|
if let Some(actor) = CTX.cache.actors.get(&uid) {
|
2024-04-15 03:03:01 +02:00
|
|
|
out.push(x.set_actor(apb::Node::object(actor.clone())))
|
|
|
|
} else {
|
|
|
|
let mut req = reqwest::Client::new()
|
|
|
|
.get(format!("https://feditest.alemi.dev/users/+?id={uid}"));
|
2024-04-14 06:45:51 +02:00
|
|
|
|
2024-04-15 03:03:01 +02:00
|
|
|
if let Some(token) = token.get() {
|
|
|
|
req = req.header("Authorization", format!("Bearer {token}"));
|
2024-04-14 06:45:51 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 03:03:01 +02:00
|
|
|
// TODO don't fail whole timeline fetch when one user fails fetching...
|
|
|
|
let actor = req.send().await?.json::<serde_json::Value>().await?;
|
2024-04-15 22:03:32 +02:00
|
|
|
CTX.cache.actors.insert(uid, actor.clone());
|
2024-04-15 03:03:01 +02:00
|
|
|
|
|
|
|
out.push(x.set_actor(apb::Node::object(actor)))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out.push(x)
|
2024-04-14 06:45:51 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-15 03:03:01 +02:00
|
|
|
|
|
|
|
Ok(out)
|
2024-04-14 06:45:51 +02:00
|
|
|
}
|