upub/src/routes/activitypub/user/mod.rs

82 lines
2.5 KiB
Rust
Raw Normal View History

2024-03-25 02:00:57 +01:00
pub mod inbox;
2024-03-23 04:31:48 +01:00
2024-03-25 02:00:57 +01:00
pub mod outbox;
2024-03-23 04:31:48 +01:00
2024-03-27 04:23:42 +01:00
pub mod following;
2024-03-23 04:31:48 +01:00
use axum::extract::{Path, Query, State};
use sea_orm::EntityTrait;
2024-03-23 04:31:48 +01:00
use apb::{ActorMut, BaseMut, CollectionMut, Node};
use crate::{errors::UpubError, model::{self, user}, server::{auth::AuthIdentity, fetcher::Fetcher, Context}, url};
2024-03-23 04:31:48 +01:00
use super::{jsonld::LD, JsonLD, TryFetch};
2024-03-23 04:31:48 +01:00
pub async fn view(
State(ctx) : State<Context>,
AuthIdentity(auth): AuthIdentity,
Path(id): Path<String>,
Query(query): Query<TryFetch>,
) -> crate::Result<JsonLD<serde_json::Value>> {
let uid = if id.starts_with('+') {
format!("https://{}", id.replacen('+', "", 1).replace('@', "/"))
} else {
ctx.uid(id.clone())
};
match user::Entity::find_by_id(&uid)
.find_also_related(model::config::Entity)
2024-04-13 06:55:16 +02:00
.one(ctx.db()).await?
{
// local user
Some((user, Some(cfg))) => {
Ok(JsonLD(user.clone().ap() // ew ugly clone TODO
.set_inbox(Node::link(url!(ctx, "/users/{id}/inbox"))) // TODO unread activities as count
.set_outbox(Node::object(
serde_json::Value::new_object()
.set_id(Some(&url!(ctx, "/users/{id}/outbox")))
.set_collection_type(Some(apb::CollectionType::OrderedCollection))
.set_first(Node::link(url!(ctx, "/users/{id}/outbox/page")))
.set_total_items(Some(user.statuses_count as u64))
))
.set_following(Node::object(
serde_json::Value::new_object()
.set_id(Some(&url!(ctx, "/users/{id}/following")))
.set_collection_type(Some(apb::CollectionType::OrderedCollection))
.set_first(Node::link(url!(ctx, "/users/{id}/following/page")))
.set_total_items(
if auth.is_local_user(&user.id) || cfg.show_following {
Some(user.following_count as u64)
} else {
None
}
)
))
.set_followers(Node::object(
serde_json::Value::new_object()
.set_id(Some(&url!(ctx, "/users/{id}/followers")))
.set_collection_type(Some(apb::CollectionType::OrderedCollection))
.set_first(Node::link(url!(ctx, "/users/{id}/followers/page")))
.set_total_items(
if auth.is_local_user(&user.id) || cfg.show_followers {
Some(user.followers_count as u64)
} else {
None
}
)
))
// .set_public_key(user.public_key) // TODO
2024-03-23 06:32:15 +01:00
.ld_context()
))
},
// remote user TODDO doesn't work?
Some((user, None)) => Ok(JsonLD(user.ap().ld_context())),
None => if auth.is_local() && query.fetch && !ctx.is_local(&uid) {
Ok(JsonLD(ctx.fetch_user(&uid).await?.ap().ld_context()))
} else {
Err(UpubError::not_found())
},
2024-03-23 04:31:48 +01:00
}
}