From ce28c2e9fbfffac0ff07460f214fec50f03ee70e Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 25 Sep 2022 20:59:09 +0000 Subject: [PATCH] Add totalItems property to followers, following and subscribers collections --- src/activitypub/collections.rs | 5 +++++ src/activitypub/views.rs | 37 +++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/activitypub/collections.rs b/src/activitypub/collections.rs index 7f8b073..c95aae5 100644 --- a/src/activitypub/collections.rs +++ b/src/activitypub/collections.rs @@ -17,18 +17,23 @@ pub struct OrderedCollection { #[serde(skip_serializing_if = "Option::is_none")] first: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + total_items: Option, } impl OrderedCollection { pub fn new( collection_id: String, first_page_id: Option, + total_items: Option, ) -> Self { Self { context: json!(AP_CONTEXT), id: collection_id, object_type: ORDERED_COLLECTION.to_string(), first: first_page_id, + total_items, } } } diff --git a/src/activitypub/views.rs b/src/activitypub/views.rs index 8dc6bb9..d921e6e 100644 --- a/src/activitypub/views.rs +++ b/src/activitypub/views.rs @@ -119,6 +119,7 @@ async fn outbox( let collection = OrderedCollection::new( collection_id, Some(first_page_id), + None, ); let response = HttpResponse::Ok() .content_type(ACTIVITY_CONTENT_TYPE) @@ -134,7 +135,8 @@ async fn outbox( None, // include only public posts false, // exclude replies false, // exclude reposts - None, COLLECTION_PAGE_SIZE, + None, + COLLECTION_PAGE_SIZE, ).await?; let activities: Vec<_> = posts.iter().filter_map(|post| { if post.in_reply_to_id.is_some() || post.repost_of_id.is_some() { @@ -162,18 +164,25 @@ async fn outbox( #[get("/followers")] async fn followers_collection( config: web::Data, + db_pool: web::Data, username: web::Path, query_params: web::Query, ) -> Result { if query_params.page.is_some() { // Social graph is not available return Err(HttpError::PermissionError); - } + }; + let db_client = &**get_database_client(&db_pool).await?; + let user = get_user_by_name(db_client, &username).await?; let collection_id = local_actor_followers( &config.instance_url(), &username, ); - let collection = OrderedCollection::new(collection_id, None); + let collection = OrderedCollection::new( + collection_id, + None, + Some(user.profile.follower_count), + ); let response = HttpResponse::Ok() .content_type(ACTIVITY_CONTENT_TYPE) .json(collection); @@ -183,18 +192,25 @@ async fn followers_collection( #[get("/following")] async fn following_collection( config: web::Data, + db_pool: web::Data, username: web::Path, query_params: web::Query, ) -> Result { if query_params.page.is_some() { // Social graph is not available return Err(HttpError::PermissionError); - } + }; + let db_client = &**get_database_client(&db_pool).await?; + let user = get_user_by_name(db_client, &username).await?; let collection_id = local_actor_following( &config.instance_url(), &username, ); - let collection = OrderedCollection::new(collection_id, None); + let collection = OrderedCollection::new( + collection_id, + None, + Some(user.profile.following_count), + ); let response = HttpResponse::Ok() .content_type(ACTIVITY_CONTENT_TYPE) .json(collection); @@ -204,18 +220,25 @@ async fn following_collection( #[get("/subscribers")] async fn subscribers_collection( config: web::Data, + db_pool: web::Data, username: web::Path, query_params: web::Query, ) -> Result { if query_params.page.is_some() { // Subscriber list is hidden return Err(HttpError::PermissionError); - } + }; + let db_client = &**get_database_client(&db_pool).await?; + let user = get_user_by_name(db_client, &username).await?; let collection_id = local_actor_subscribers( &config.instance_url(), &username, ); - let collection = OrderedCollection::new(collection_id, None); + let collection = OrderedCollection::new( + collection_id, + None, + Some(user.profile.subscriber_count), + ); let response = HttpResponse::Ok() .content_type(ACTIVITY_CONTENT_TYPE) .json(collection);