1
0
Fork 0
forked from alemi/upub

chore: better ap user route

This commit is contained in:
əlemi 2024-03-23 20:24:38 +01:00
parent 34b7db5ce6
commit 2673860106
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -7,27 +7,15 @@ pub use outbox::outbox;
mod following; mod following;
pub use following::follow___; pub use following::follow___;
use std::sync::Arc;
use axum::{extract::{Path, State}, http::StatusCode}; use axum::{extract::{Path, State}, http::StatusCode};
use sea_orm::{DatabaseConnection, EntityTrait}; use sea_orm::EntityTrait;
use crate::{activitystream::{object::{actor::ActorMut, collection::{CollectionMut, CollectionType}, document::{DocumentMut, DocumentType}, ObjectMut}, BaseMut, Node}, model::{self, user}, server::Context, url}; use crate::{activitystream::{object::{actor::ActorMut, collection::{CollectionMut, CollectionType}, document::{DocumentMut, DocumentType}, ObjectMut}, BaseMut, Node}, model::{self, user}, server::Context, url};
use super::{jsonld::LD, JsonLD}; use super::{jsonld::LD, JsonLD};
pub async fn list(State(_db) : State<Arc<DatabaseConnection>>) -> Result<JsonLD<serde_json::Value>, StatusCode> { pub fn ap_user(user: model::user::Model) -> serde_json::Value {
todo!() serde_json::Value::new_object()
}
pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<JsonLD<serde_json::Value>, StatusCode> {
match user::Entity::find_by_id(ctx.uid(id.clone()))
.find_also_related(model::config::Entity)
.one(ctx.db()).await
{
// local user
Ok(Some((user, Some(cfg)))) => {
Ok(JsonLD(serde_json::Value::new_object()
.set_id(Some(&user.id)) .set_id(Some(&user.id))
.set_actor_type(Some(user.actor_type)) .set_actor_type(Some(user.actor_type))
.set_name(user.name.as_deref()) .set_name(user.name.as_deref())
@ -44,32 +32,55 @@ pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result
))) )))
.set_published(Some(user.created)) .set_published(Some(user.created))
.set_preferred_username(Some(&user.preferred_username)) .set_preferred_username(Some(&user.preferred_username))
.set_inbox(Node::maybe_link(user.inbox))
.set_outbox(Node::maybe_link(user.outbox))
.set_following(Node::object(
serde_json::Value::new_object()
.set_id(user.following.as_deref())
.set_collection_type(Some(CollectionType::OrderedCollection))
.set_total_items(Some(user.following_count as u64))
))
.set_followers(Node::object(
serde_json::Value::new_object()
.set_id(user.followers.as_deref())
.set_collection_type(Some(CollectionType::OrderedCollection))
.set_total_items(Some(user.followers_count as u64))
))
// .set_public_key(user.public_key) // TODO
.set_discoverable(Some(true))
.set_endpoints(None)
}
pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result<JsonLD<serde_json::Value>, StatusCode> {
match user::Entity::find_by_id(ctx.uid(id.clone()))
.find_also_related(model::config::Entity)
.one(ctx.db()).await
{
// local user
Ok(Some((user, Some(cfg)))) => {
Ok(JsonLD(ap_user(user.clone()) // ew ugly clone TODO
.set_inbox(Node::link(url!(ctx, "/users/{id}/inbox"))) .set_inbox(Node::link(url!(ctx, "/users/{id}/inbox")))
.set_outbox(Node::link(url!(ctx, "/users/{id}/outbox"))) .set_outbox(Node::link(url!(ctx, "/users/{id}/outbox")))
.set_following(Node::object( .set_following(Node::object(
serde_json::Value::new_object() serde_json::Value::new_object()
.set_id(Some(&url!(ctx, "/users/{id}/following"))) .set_id(Some(&url!(ctx, "/users/{id}/following")))
.set_collection_type(Some(CollectionType::OrderedCollection)) .set_collection_type(Some(CollectionType::OrderedCollection))
.set_total_items(if cfg.show_following_count { Some(0 /* user.following_count TODO */) } else { None }) .set_total_items(if cfg.show_following_count { Some(user.following_count as u64) } else { None })
.set_first(Node::link(url!(ctx, "/users/{id}/following?page=true"))) .set_first(Node::link(url!(ctx, "/users/{id}/following?page=true")))
)) ))
.set_followers(Node::object( .set_followers(Node::object(
serde_json::Value::new_object() serde_json::Value::new_object()
.set_id(Some(&url!(ctx, "/users/{id}/followers"))) .set_id(Some(&url!(ctx, "/users/{id}/followers")))
.set_collection_type(Some(CollectionType::OrderedCollection)) .set_collection_type(Some(CollectionType::OrderedCollection))
.set_total_items(if cfg.show_followers_count { Some(0 /* user.followers_count TODO */) } else { None }) .set_total_items(if cfg.show_followers_count { Some(user.followers_count as u64) } else { None })
.set_first(Node::link(url!(ctx, "/users/{id}/followers?page=true"))) .set_first(Node::link(url!(ctx, "/users/{id}/followers?page=true")))
)) ))
// .set_public_key(user.public_key) // TODO // .set_public_key(user.public_key) // TODO
.set_discoverable(Some(true))
.set_endpoints(None)
.ld_context() .ld_context()
)) ))
}, },
// remote user // remote user
Ok(Some((_user, None))) => { Ok(Some((user, None))) => Ok(JsonLD(ap_user(user).ld_context())),
Err(StatusCode::NOT_IMPLEMENTED)
},
Ok(None) => Err(StatusCode::NOT_FOUND), Ok(None) => Err(StatusCode::NOT_FOUND),
Err(e) => { Err(e) => {
tracing::error!("error querying for user: {e}"); tracing::error!("error querying for user: {e}");
@ -78,4 +89,3 @@ pub async fn view(State(ctx) : State<Context>, Path(id): Path<String>) -> Result
} }
} }