Skip to main content

upub_web/components/
user.rs

1use leptos::prelude::*;
2use crate::{prelude::*, IconGradient};
3
4use apb::{Activity, ActivityMut, Actor, Base, Object, ObjectMut};
5
6lazy_static::lazy_static! {
7	static ref REGEX: regex::Regex = regex::Regex::new(r":\w+:").expect("failed compiling custom emoji regex");
8}
9
10#[component]
11pub fn ActorStrip(object: crate::Doc) -> impl IntoView {
12	let actor_id = object.id().unwrap_or_default().to_string();
13	let username = object.preferred_username().unwrap_or_default().to_string();
14	let domain = object.id().unwrap_or_default().replace("https://", "").split('/').next().unwrap_or_default().to_string();
15	let (avatar_url, avatar_style) = object.icon_url_and_style();
16	view! {
17		<a href={Uri::web(U::Actor, &actor_id)} class="clean hover force-break">
18			<img src={avatar_url} style={avatar_style} class="avatar avatar-inline inline mr-s" /><b>{username}</b><small>@{domain}</small>
19		</a>
20	}
21}
22
23#[component]
24pub fn ActorBanner(object: crate::Doc) -> impl IntoView {
25	match object.as_ref() {
26		serde_json::Value::String(id) => view! {
27			<div><b>?</b>" "<a class="clean hover" href={Uri::web(U::Actor, id)}>{Uri::pretty(id, 50)}</a></div>
28		}.into_any(),
29		serde_json::Value::Object(_) => {
30			let uid = object.id().unwrap_or_default().to_string();
31			let uri = Uri::web(U::Actor, &uid);
32			let username = object.preferred_username().unwrap_or_default().to_string();
33			let domain = object.id().unwrap_or_default().replace("https://", "").replace("http://", "").split('/').next().unwrap_or_default().to_string();
34			let display_name = object.name().unwrap_or(username.clone());
35			let (avatar_url, avatar_style) = object.icon_url_and_style();
36
37			view! {
38				<div>
39					<table class="align" >
40					<tr>
41						<td rowspan="2" >
42							<a href={uri.clone()} >
43								<img class="avatar avatar-actor" src={avatar_url} style={avatar_style} />
44							</a>
45						</td>
46						<td>
47							<b class="displayname"><DisplayName name=display_name /></b>
48						</td>
49					</tr>
50					<tr>
51						<td class="top" >
52							<a class="hover" href={uri} >
53								<small class="force-break">{username}@{domain}</small>
54							</a>
55						</td>
56					</tr>
57					</table>
58				</div>
59			}.into_any()
60		},
61		_ => view! {
62			<div><b>invalid actor</b></div>
63		}.into_any()
64	}
65}
66
67#[component]
68fn DisplayName(mut name: String) -> impl IntoView {
69	for m in REGEX.find_iter(&name.clone()) {
70		// TODO this is a clear unmitigated unsanitized html injection ahahahahaha but accounts 
71		//      with many custom emojis in their names mess with my frontend and i dont want to 
72		//      deal with it rn
73		name = name.replace(m.as_str(), &format!("<u class=\"moreinfo\" title=\"{}\">[::]</u>", m.as_str()));
74	}
75	view! { <span class="force-break" inner_html=name></span> }
76}
77
78#[component]
79pub fn FollowRequestButtons(activity_id: String, actor_id: String) -> impl IntoView {
80	let auth = use_context::<Auth>().expect("missing auth context");
81	// TODO lmao what is going on with this double move / triple clone ???????????
82	let _activity_id = activity_id.clone();
83	let _actor_id = actor_id.clone();
84	let from_actor = cache::OBJECTS.get(&activity_id).and_then(|x| x.actor().id().ok()).unwrap_or_default();
85	let _from_actor = from_actor.clone();
86	if actor_id == auth.user_id() {
87		Some(view! {
88			<input type="submit" value="accept"
89				on:click=move |_| {
90					let activity_id = _activity_id.clone();
91					let actor_id = _from_actor.clone();
92					leptos::task::spawn_local(async move {
93						send_follow_response(
94							apb::ActivityType::Accept(apb::AcceptType::Accept),
95							activity_id,
96							actor_id,
97							auth
98						).await
99					})
100				}
101			/>
102			<span class="ma-1"></span>
103			<input type="submit" value="reject"
104				on:click=move |_| {
105					let activity_id = activity_id.clone();
106					let actor_id = from_actor.clone();
107					leptos::task::spawn_local(async move {
108						send_follow_response(
109							apb::ActivityType::Reject(apb::RejectType::Reject),
110							activity_id,
111							actor_id,
112							auth
113						).await
114					})
115				}
116			/>
117		})
118	} else {
119		None
120	}
121}
122
123async fn send_follow_response(kind: apb::ActivityType, target: String, to: String, auth: Auth) {
124	let payload = serde_json::Value::Object(serde_json::Map::default())
125		.set_activity_type(Some(kind))
126		.set_object(apb::Node::link(target))
127		.set_to(apb::Node::links(vec![to]));
128	if let Err(e) = Http::post(&auth.outbox(), &payload, auth).await {
129		tracing::error!("failed posting follow response: {e}");
130	}
131}