Rewrite community followers and user outbox to use our own structs

This commit is contained in:
Felix Ableitner 2021-10-28 17:52:11 +02:00
parent d89156810d
commit 358ef99ea2
7 changed files with 74 additions and 36 deletions

View file

@ -0,0 +1,38 @@
use crate::generate_followers_url;
use activitystreams::collection::kind::CollectionType;
use lemmy_api_common::blocking;
use lemmy_db_schema::source::community::Community;
use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CommunityFollowers {
id: Url,
r#type: CollectionType,
total_items: i32,
items: Vec<()>,
}
impl CommunityFollowers {
pub(crate) async fn new(
community: Community,
context: &LemmyContext,
) -> Result<CommunityFollowers, LemmyError> {
let community_id = community.id;
let community_followers = blocking(context.pool(), move |conn| {
CommunityFollowerView::for_community(conn, community_id)
})
.await??;
Ok(CommunityFollowers {
id: generate_followers_url(&community.actor_id)?.into_inner(),
r#type: CollectionType::Collection,
total_items: community_followers.len() as i32,
items: vec![],
})
}
}

View file

@ -14,10 +14,8 @@ use lemmy_db_schema::{
use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView; use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use url::Url; use url::Url;
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct GroupModerators { pub struct GroupModerators {

View file

@ -18,15 +18,14 @@ use lemmy_db_schema::{
}; };
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use url::Url; use url::Url;
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct GroupOutbox { pub struct GroupOutbox {
r#type: OrderedCollectionType, r#type: OrderedCollectionType,
id: Url, id: Url,
total_items: i32,
ordered_items: Vec<CreateOrUpdatePost>, ordered_items: Vec<CreateOrUpdatePost>,
} }
@ -83,6 +82,7 @@ impl ApubObject for ApubCommunityOutbox {
Ok(GroupOutbox { Ok(GroupOutbox {
r#type: OrderedCollectionType::OrderedCollection, r#type: OrderedCollectionType::OrderedCollection,
id: generate_outbox_url(&data.0.actor_id)?.into(), id: generate_outbox_url(&data.0.actor_id)?.into(),
total_items: ordered_items.len() as i32,
ordered_items, ordered_items,
}) })
} }

View file

@ -1,8 +1,10 @@
use crate::objects::community::ApubCommunity; use crate::objects::community::ApubCommunity;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
pub(crate) mod community_followers;
pub(crate) mod community_moderators; pub(crate) mod community_moderators;
pub(crate) mod community_outbox; pub(crate) mod community_outbox;
pub(crate) mod user_outbox;
/// Put community in the data, so we dont have to read it again from the database. /// Put community in the data, so we dont have to read it again from the database.
pub(crate) struct CommunityContext(pub ApubCommunity, pub LemmyContext); pub(crate) struct CommunityContext(pub ApubCommunity, pub LemmyContext);

View file

@ -0,0 +1,26 @@
use crate::generate_outbox_url;
use activitystreams::collection::kind::OrderedCollectionType;
use lemmy_db_schema::source::person::Person;
use lemmy_utils::LemmyError;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct UserOutbox {
r#type: OrderedCollectionType,
id: Url,
ordered_items: Vec<()>,
total_items: i32,
}
impl UserOutbox {
pub(crate) async fn new(user: Person) -> Result<UserOutbox, LemmyError> {
Ok(UserOutbox {
r#type: OrderedCollectionType::OrderedCollection,
id: generate_outbox_url(&user.actor_id)?.into_inner(),
ordered_items: vec![],
total_items: 0,
})
}
}

View file

@ -6,6 +6,7 @@ use crate::{
verify_person_in_community, verify_person_in_community,
}, },
collections::{ collections::{
community_followers::CommunityFollowers,
community_moderators::ApubCommunityModerators, community_moderators::ApubCommunityModerators,
community_outbox::ApubCommunityOutbox, community_outbox::ApubCommunityOutbox,
CommunityContext, CommunityContext,
@ -21,10 +22,6 @@ use crate::{
}, },
objects::community::ApubCommunity, objects::community::ApubCommunity,
}; };
use activitystreams::{
base::BaseExt,
collection::{CollectionExt, UnorderedCollection},
};
use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse}; use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub_lib::{ use lemmy_apub_lib::{
@ -32,7 +29,6 @@ use lemmy_apub_lib::{
verify::verify_domains_match, verify::verify_domains_match,
}; };
use lemmy_db_schema::source::community::Community; use lemmy_db_schema::source::community::Community;
use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use log::info; use log::info;
@ -118,19 +114,8 @@ pub(crate) async fn get_apub_community_followers(
Community::read_from_name(conn, &info.community_name) Community::read_from_name(conn, &info.community_name)
}) })
.await??; .await??;
let followers = CommunityFollowers::new(community, &context).await?;
let community_id = community.id; Ok(create_apub_response(&followers))
let community_followers = blocking(context.pool(), move |conn| {
CommunityFollowerView::for_community(conn, community_id)
})
.await??;
let mut collection = UnorderedCollection::new();
collection
.set_id(community.followers_url.into())
.set_total_items(community_followers.len() as u64);
let collection = WithContext::new(collection);
Ok(create_apub_response(&collection))
} }
/// Returns the community outbox, which is populated by a maximum of 20 posts (but no other /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other

View file

@ -8,8 +8,8 @@ use crate::{
undo_delete::UndoDeletePrivateMessage, undo_delete::UndoDeletePrivateMessage,
}, },
}, },
collections::user_outbox::UserOutbox,
context::WithContext, context::WithContext,
generate_outbox_url,
http::{ http::{
create_apub_response, create_apub_response,
create_apub_tombstone_response, create_apub_tombstone_response,
@ -18,10 +18,6 @@ use crate::{
}, },
objects::person::ApubPerson, objects::person::ApubPerson,
}; };
use activitystreams::{
base::BaseExt,
collection::{CollectionExt, OrderedCollection},
};
use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse}; use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler, ApubObject}; use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler, ApubObject};
@ -30,7 +26,6 @@ use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use log::info; use log::info;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct PersonQuery { pub struct PersonQuery {
@ -100,12 +95,6 @@ pub(crate) async fn get_apub_person_outbox(
Person::find_by_name(conn, &info.user_name) Person::find_by_name(conn, &info.user_name)
}) })
.await??; .await??;
// TODO: populate the person outbox let outbox = UserOutbox::new(person).await?;
let mut collection = OrderedCollection::new(); Ok(create_apub_response(&outbox))
collection
.set_many_items(Vec::<Url>::new())
.set_id(generate_outbox_url(&person.actor_id)?.into())
.set_total_items(0_u64);
let collection = WithContext::new(collection);
Ok(create_apub_response(&collection))
} }