]> Untitled Git - lemmy.git/blob - crates/apub/src/collections/community_outbox.rs
Rewrite community followers and user outbox to use our own structs
[lemmy.git] / crates / apub / src / collections / community_outbox.rs
1 use crate::{
2   activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType},
3   collections::CommunityContext,
4   generate_outbox_url,
5   objects::{person::ApubPerson, post::ApubPost},
6 };
7 use activitystreams::collection::kind::OrderedCollectionType;
8 use chrono::NaiveDateTime;
9 use lemmy_api_common::blocking;
10 use lemmy_apub_lib::{
11   data::Data,
12   traits::{ActivityHandler, ApubObject},
13   verify::verify_domains_match,
14 };
15 use lemmy_db_schema::{
16   source::{person::Person, post::Post},
17   traits::Crud,
18 };
19 use lemmy_utils::LemmyError;
20 use serde::{Deserialize, Serialize};
21 use url::Url;
22
23 #[derive(Clone, Debug, Deserialize, Serialize)]
24 #[serde(rename_all = "camelCase")]
25 pub struct GroupOutbox {
26   r#type: OrderedCollectionType,
27   id: Url,
28   total_items: i32,
29   ordered_items: Vec<CreateOrUpdatePost>,
30 }
31
32 #[derive(Clone, Debug)]
33 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
34
35 #[async_trait::async_trait(?Send)]
36 impl ApubObject for ApubCommunityOutbox {
37   type DataType = CommunityContext;
38   type TombstoneType = ();
39   type ApubType = GroupOutbox;
40
41   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
42     None
43   }
44
45   async fn read_from_apub_id(
46     _object_id: Url,
47     data: &Self::DataType,
48   ) -> Result<Option<Self>, LemmyError> {
49     // Only read from database if its a local community, otherwise fetch over http
50     if data.0.local {
51       let community_id = data.0.id;
52       let post_list: Vec<ApubPost> = blocking(data.1.pool(), move |conn| {
53         Post::list_for_community(conn, community_id)
54       })
55       .await??
56       .into_iter()
57       .map(Into::into)
58       .collect();
59       Ok(Some(ApubCommunityOutbox(post_list)))
60     } else {
61       Ok(None)
62     }
63   }
64
65   async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
66     // do nothing (it gets deleted automatically with the community)
67     Ok(())
68   }
69
70   async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
71     let mut ordered_items = vec![];
72     for post in &self.0 {
73       let actor = post.creator_id;
74       let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor))
75         .await??
76         .into();
77       let a =
78         CreateOrUpdatePost::new(post, &actor, &data.0, CreateOrUpdateType::Create, &data.1).await?;
79       ordered_items.push(a);
80     }
81
82     Ok(GroupOutbox {
83       r#type: OrderedCollectionType::OrderedCollection,
84       id: generate_outbox_url(&data.0.actor_id)?.into(),
85       total_items: ordered_items.len() as i32,
86       ordered_items,
87     })
88   }
89
90   fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
91     // no tombstone for this, there is only a tombstone for the community
92     unimplemented!()
93   }
94
95   async fn from_apub(
96     apub: &Self::ApubType,
97     data: &Self::DataType,
98     expected_domain: &Url,
99     request_counter: &mut i32,
100   ) -> Result<Self, LemmyError> {
101     verify_domains_match(expected_domain, &apub.id)?;
102     let mut outbox_activities = apub.ordered_items.clone();
103     if outbox_activities.len() > 20 {
104       outbox_activities = outbox_activities[0..20].to_vec();
105     }
106
107     // We intentionally ignore errors here. This is because the outbox might contain posts from old
108     // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
109     // item and only parse the ones that work.
110     for activity in outbox_activities {
111       activity
112         .receive(&Data::new(data.1.clone()), request_counter)
113         .await
114         .ok();
115     }
116
117     // This return value is unused, so just set an empty vec
118     Ok(ApubCommunityOutbox { 0: vec![] })
119   }
120 }