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
|
|
|
|
2024-04-18 04:48:49 +02:00
|
|
|
use axum::extract::{Path, Query, State};
|
2024-04-14 02:36:59 +02:00
|
|
|
use sea_orm::EntityTrait;
|
2024-03-23 04:31:48 +01:00
|
|
|
|
2024-04-19 03:28:39 +02:00
|
|
|
use apb::{ActorMut, BaseMut, CollectionMut, Node};
|
2024-04-18 05:25:56 +02:00
|
|
|
use crate::{errors::UpubError, model::{self, user}, server::{auth::AuthIdentity, fetcher::Fetcher, Context}, url};
|
2024-03-23 04:31:48 +01:00
|
|
|
|
2024-04-18 04:48:49 +02:00
|
|
|
use super::{jsonld::LD, JsonLD, TryFetch};
|
2024-03-23 04:31:48 +01:00
|
|
|
|
|
|
|
|
2024-04-14 02:36:59 +02:00
|
|
|
pub async fn view(
|
|
|
|
State(ctx) : State<Context>,
|
2024-04-18 03:06:56 +02:00
|
|
|
AuthIdentity(auth): AuthIdentity,
|
2024-04-14 02:36:59 +02:00
|
|
|
Path(id): Path<String>,
|
2024-04-18 04:48:49 +02:00
|
|
|
Query(query): Query<TryFetch>,
|
2024-04-14 02:36:59 +02:00
|
|
|
) -> crate::Result<JsonLD<serde_json::Value>> {
|
2024-04-15 21:36:31 +02:00
|
|
|
let uid = if id.starts_with('+') {
|
|
|
|
format!("https://{}", id.replacen('+', "", 1).replace('@', "/"))
|
2024-04-14 02:36:59 +02:00
|
|
|
} else {
|
|
|
|
ctx.uid(id.clone())
|
|
|
|
};
|
2024-04-18 04:48:49 +02:00
|
|
|
match user::Entity::find_by_id(&uid)
|
2024-03-23 06:10:53 +01:00
|
|
|
.find_also_related(model::config::Entity)
|
2024-04-13 06:55:16 +02:00
|
|
|
.one(ctx.db()).await?
|
2024-03-23 06:10:53 +01:00
|
|
|
{
|
|
|
|
// local user
|
2024-04-18 03:06:56 +02:00
|
|
|
Some((user, Some(cfg))) => {
|
2024-04-19 03:28:39 +02:00
|
|
|
Ok(JsonLD(user.clone().ap() // ew ugly clone TODO
|
2024-04-18 04:14:16 +02:00
|
|
|
.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))
|
|
|
|
))
|
2024-04-18 03:06:56 +02:00
|
|
|
.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(
|
2024-04-18 04:48:49 +02:00
|
|
|
if auth.is_local_user(&user.id) || cfg.show_following {
|
2024-04-18 03:06:56 +02:00
|
|
|
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(
|
2024-04-18 04:48:49 +02:00
|
|
|
if auth.is_local_user(&user.id) || cfg.show_followers {
|
2024-04-18 03:06:56 +02:00
|
|
|
Some(user.followers_count as u64)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
)
|
|
|
|
))
|
2024-03-23 06:10:53 +01:00
|
|
|
// .set_public_key(user.public_key) // TODO
|
2024-03-23 06:32:15 +01:00
|
|
|
.ld_context()
|
2024-03-23 06:10:53 +01:00
|
|
|
))
|
|
|
|
},
|
2024-04-13 06:50:04 +02:00
|
|
|
// remote user TODDO doesn't work?
|
2024-04-19 03:28:39 +02:00
|
|
|
Some((user, None)) => Ok(JsonLD(user.ap().ld_context())),
|
2024-04-18 04:50:06 +02:00
|
|
|
None => if auth.is_local() && query.fetch && !ctx.is_local(&uid) {
|
2024-04-19 03:28:39 +02:00
|
|
|
Ok(JsonLD(ctx.fetch_user(&uid).await?.ap().ld_context()))
|
2024-04-18 04:48:49 +02:00
|
|
|
} else {
|
|
|
|
Err(UpubError::not_found())
|
|
|
|
},
|
2024-03-23 04:31:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|