From 40392aef56c9e1825ce14f0f983307fd07c6b1b2 Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 11 Jun 2024 05:51:12 +0200 Subject: [PATCH] feat: add streams and move feed to its own field --- apb/src/types/object/actor.rs | 8 +++++ upub/routes/src/activitypub/actor/mod.rs | 12 +++++--- upub/routes/src/activitypub/actor/streams.rs | 32 ++++++++++++++++++++ upub/routes/src/activitypub/mod.rs | 2 ++ 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 upub/routes/src/activitypub/actor/streams.rs diff --git a/apb/src/types/object/actor.rs b/apb/src/types/object/actor.rs index 397ae3f..8f9017f 100644 --- a/apb/src/types/object/actor.rs +++ b/apb/src/types/object/actor.rs @@ -43,6 +43,8 @@ pub trait Actor : Object { fn following_me(&self) -> Field { Err(FieldErr("followingMe")) } #[cfg(feature = "activitypub-fe")] fn followed_by_me(&self) -> Field { Err(FieldErr("followedByMe")) } + #[cfg(feature = "activitypub-fe")] + fn feed(&self) -> Node { Node::Empty } #[cfg(feature = "activitypub-counters")] fn followers_count(&self) -> Field { Err(FieldErr("followersCount")) } @@ -96,6 +98,8 @@ pub trait ActorMut : ObjectMut { fn set_following_me(self, val: Option) -> Self; #[cfg(feature = "activitypub-fe")] fn set_followed_by_me(self, val: Option) -> Self; + #[cfg(feature = "activitypub-fe")] + fn set_feed(self, val: Node) -> Self; #[cfg(feature = "activitypub-counters")] fn set_followers_count(self, val: Option) -> Self; @@ -150,6 +154,8 @@ impl Actor for serde_json::Value { crate::getter! { followingMe -> bool } #[cfg(feature = "activitypub-fe")] crate::getter! { followedByMe -> bool } + #[cfg(feature = "activitypub-fe")] + crate::getter! { feed -> node Self::Collection } #[cfg(feature = "activitypub-counters")] crate::getter! { followingCount -> u64 } @@ -199,6 +205,8 @@ impl ActorMut for serde_json::Value { crate::setter! { followingMe -> bool } #[cfg(feature = "activitypub-fe")] crate::setter! { followedByMe -> bool } + #[cfg(feature = "activitypub-fe")] + crate::setter! { feed -> node Self::Collection } #[cfg(feature = "activitypub-counters")] crate::setter! { followingCount -> u64 } diff --git a/upub/routes/src/activitypub/actor/mod.rs b/upub/routes/src/activitypub/actor/mod.rs index cfd505b..5d4e5d7 100644 --- a/upub/routes/src/activitypub/actor/mod.rs +++ b/upub/routes/src/activitypub/actor/mod.rs @@ -1,10 +1,8 @@ pub mod inbox; - pub mod outbox; - pub mod following; - pub mod feed; +pub mod streams; use axum::extract::{Path, Query, State}; @@ -60,7 +58,7 @@ pub async fn view( let mut user = user_model.ap() .set_inbox(Node::link(upub::url!(ctx, "/actors/{id}/inbox"))) .set_outbox(Node::link(upub::url!(ctx, "/actors/{id}/outbox"))) - .set_streams(Node::link(upub::url!(ctx, "/actors/{id}/feed"))) + .set_streams(Node::link(upub::url!(ctx, "/actors/{id}/streams"))) .set_following(Node::link(upub::url!(ctx, "/actors/{id}/following"))) .set_followers(Node::link(upub::url!(ctx, "/actors/{id}/followers"))) .set_following_me(following_me) @@ -72,6 +70,10 @@ pub async fn view( .set_proxy_url(Some(&upub::url!(ctx, "/proxy"))) )); + if auth.is(&uid) { + user = user.set_feed(Node::link(upub::url!(ctx, "/actors/{id}/feed"))); + } + if !auth.is(&uid) && !cfg.show_followers_count { user = user.set_followers_count(None); } @@ -91,6 +93,8 @@ pub async fn view( user_model.ap() .set_following_me(following_me) .set_followed_by_me(followed_by_me) + // TODO should we set streams?? we offer this collection but actor is remote + .set_streams(Node::link(upub::url!(ctx, "/actors/{id}/streams"))) .ld_context() )), None => Err(crate::ApiError::not_found()), diff --git a/upub/routes/src/activitypub/actor/streams.rs b/upub/routes/src/activitypub/actor/streams.rs new file mode 100644 index 0000000..68d6c6b --- /dev/null +++ b/upub/routes/src/activitypub/actor/streams.rs @@ -0,0 +1,32 @@ +use axum::extract::{Path, Query, State}; +use sea_orm::{ColumnTrait, Condition}; + +use upub::Context; + +use crate::{activitypub::Pagination, builders::JsonLD, AuthIdentity}; + +pub async fn get( + State(ctx): State, + Path(id): Path, +) -> crate::ApiResult> { + crate::builders::collection(&upub::url!(ctx, "/actors/{id}/streams"), None) +} + +pub async fn page( + State(ctx): State, + Path(id): Path, + AuthIdentity(auth): AuthIdentity, + Query(page): Query, +) -> crate::ApiResult> { + crate::builders::paginate_objects( + upub::url!(ctx, "/actors/{id}/streams/page"), + Condition::all() + .add(auth.filter_objects()) + .add(upub::model::object::Column::AttributedTo.eq(ctx.uid(&id))), + ctx.db(), + page, + auth.my_id(), + false, + ) + .await +} diff --git a/upub/routes/src/activitypub/mod.rs b/upub/routes/src/activitypub/mod.rs index 0bd37f1..aa1ae4e 100644 --- a/upub/routes/src/activitypub/mod.rs +++ b/upub/routes/src/activitypub/mod.rs @@ -51,6 +51,8 @@ impl ActivityPubRouter for Router { .route("/actors/:id/outbox", post(ap::actor::outbox::post)) .route("/actors/:id/outbox", get(ap::actor::outbox::get)) .route("/actors/:id/outbox/page", get(ap::actor::outbox::page)) + .route("/actors/:id/streams", get(ap::actor::streams::get)) + .route("/actors/:id/streams/page", get(ap::actor::streams::page)) .route("/actors/:id/feed", get(ap::actor::feed::get)) .route("/actors/:id/feed/page", get(ap::actor::feed::page)) .route("/actors/:id/followers", get(ap::actor::following::get::))