diff options
author | Dessalines <tyhou13@gmx.com> | 2020-04-24 15:55:54 -0400 |
---|---|---|
committer | Dessalines <tyhou13@gmx.com> | 2020-04-24 15:55:54 -0400 |
commit | d8467408396191d6a2b8e043910e3d8267ff4fcc (patch) | |
tree | e280bc579fcf6b54606e353a88c9bd2bb4b45d11 /server/src/apub/community.rs | |
parent | 66a2c4a2c38fdc90295c0ee2d599e0acecf8ad3f (diff) |
Some more cleanup.
Diffstat (limited to 'server/src/apub/community.rs')
-rw-r--r-- | server/src/apub/community.rs | 99 |
1 files changed, 47 insertions, 52 deletions
diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index fe31527f..920c41ad 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -5,10 +5,9 @@ pub struct CommunityQuery { community_name: String, } -// TODO turn these as_group, as_page, into apub trait... something like to_apub -impl Community { +impl ToApub<GroupExt> for Community { // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network. - fn as_group(&self, conn: &PgConnection) -> Result<GroupExt, Error> { + fn to_apub(&self, conn: &PgConnection) -> Result<GroupExt, Error> { let mut group = Group::default(); let oprops: &mut ObjectProperties = group.as_mut(); @@ -45,29 +44,26 @@ impl Community { Ok(group.extend(actor_props).extend(public_key.to_ext())) } +} - pub fn get_followers_url(&self) -> String { - format!("{}/followers", &self.actor_id) - } - pub fn get_inbox_url(&self) -> String { - format!("{}/inbox", &self.actor_id) - } - pub fn get_outbox_url(&self) -> String { - format!("{}/outbox", &self.actor_id) +impl ActorType for Community { + fn actor_id(&self) -> String { + self.actor_id.to_owned() } } -impl CommunityForm { +impl FromApub<GroupExt> for CommunityForm { /// Parse an ActivityPub group received from another instance into a Lemmy community. - pub fn from_group(group: &GroupExt, conn: &PgConnection) -> Result<Self, Error> { + fn from_apub(group: &GroupExt, conn: &PgConnection) -> Result<Self, Error> { let oprops = &group.base.base.object_props; let aprops = &group.base.extension; let public_key: &PublicKey = &group.extension.public_key; - let followers_uri = Url::parse(&aprops.get_followers().unwrap().to_string())?; - let outbox_uri = Url::parse(&aprops.get_outbox().to_string())?; - let _outbox = fetch_remote_object::<OrderedCollection>(&outbox_uri)?; - let _followers = fetch_remote_object::<UnorderedCollection>(&followers_uri)?; + let _followers_uri = Url::parse(&aprops.get_followers().unwrap().to_string())?; + let _outbox_uri = Url::parse(&aprops.get_outbox().to_string())?; + // TODO don't do extra fetching here + // let _outbox = fetch_remote_object::<OrderedCollection>(&outbox_uri)?; + // let _followers = fetch_remote_object::<UnorderedCollection>(&followers_uri)?; let apub_id = &oprops.get_attributed_to_xsd_any_uri().unwrap().to_string(); let creator = get_or_fetch_and_upsert_remote_user(&apub_id, conn)?; @@ -101,10 +97,9 @@ impl CommunityForm { pub async fn get_apub_community_http( info: Path<CommunityQuery>, db: DbPoolParam, - chat_server: ChatServerParam, ) -> Result<HttpResponse<Body>, Error> { let community = Community::read_from_name(&&db.get()?, &info.community_name)?; - let c = community.as_group(&db.get().unwrap())?; + let c = community.to_apub(&db.get().unwrap())?; Ok(create_apub_response(&c)) } @@ -113,14 +108,13 @@ pub async fn get_apub_community_http( pub async fn get_apub_community_followers( info: Path<CommunityQuery>, db: DbPoolParam, - chat_server: ChatServerParam, ) -> Result<HttpResponse<Body>, Error> { let community = Community::read_from_name(&&db.get()?, &info.community_name)?; - let connection = establish_unpooled_connection(); + let conn = db.get()?; + //As we are an object, we validated that the community id was valid - let community_followers = - CommunityFollowerView::for_community(&connection, community.id).unwrap(); + let community_followers = CommunityFollowerView::for_community(&conn, community.id).unwrap(); let mut collection = UnorderedCollection::default(); let oprops: &mut ObjectProperties = collection.as_mut(); @@ -133,32 +127,33 @@ pub async fn get_apub_community_followers( Ok(create_apub_response(&collection)) } -/// Returns an UnorderedCollection with the latest posts from the community. -pub async fn get_apub_community_outbox( - info: Path<CommunityQuery>, - db: DbPoolParam, - chat_server: ChatServerParam, -) -> Result<HttpResponse<Body>, Error> { - let community = Community::read_from_name(&&db.get()?, &info.community_name)?; - - let conn = establish_unpooled_connection(); - //As we are an object, we validated that the community id was valid - let community_posts: Vec<Post> = Post::list_for_community(&conn, community.id)?; - - let mut collection = OrderedCollection::default(); - let oprops: &mut ObjectProperties = collection.as_mut(); - oprops - .set_context_xsd_any_uri(context())? - .set_id(community.actor_id)?; - collection - .collection_props - .set_many_items_base_boxes( - community_posts - .iter() - .map(|c| c.as_page(&conn).unwrap()) - .collect(), - )? - .set_total_items(community_posts.len() as u64)?; - - Ok(create_apub_response(&collection)) -} +// TODO should not be doing this +// Returns an UnorderedCollection with the latest posts from the community. +//pub async fn get_apub_community_outbox( +// info: Path<CommunityQuery>, +// db: DbPoolParam, +// chat_server: ChatServerParam, +//) -> Result<HttpResponse<Body>, Error> { +// let community = Community::read_from_name(&&db.get()?, &info.community_name)?; + +// let conn = establish_unpooled_connection(); +// //As we are an object, we validated that the community id was valid +// let community_posts: Vec<Post> = Post::list_for_community(&conn, community.id)?; + +// let mut collection = OrderedCollection::default(); +// let oprops: &mut ObjectProperties = collection.as_mut(); +// oprops +// .set_context_xsd_any_uri(context())? +// .set_id(community.actor_id)?; +// collection +// .collection_props +// .set_many_items_base_boxes( +// community_posts +// .iter() +// .map(|c| c.as_page(&conn).unwrap()) +// .collect(), +// )? +// .set_total_items(community_posts.len() as u64)?; + +// Ok(create_apub_response(&collection)) +//} |