]> Untitled Git - lemmy.git/blob - crates/apub/src/collections/community_outbox.rs
Move @context out of object/activity definitions
[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 serde_with::skip_serializing_none;
22 use url::Url;
23
24 #[skip_serializing_none]
25 #[derive(Clone, Debug, Deserialize, Serialize)]
26 #[serde(rename_all = "camelCase")]
27 pub struct GroupOutbox {
28   r#type: OrderedCollectionType,
29   id: Url,
30   ordered_items: Vec<CreateOrUpdatePost>,
31 }
32
33 #[derive(Clone, Debug)]
34 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
35
36 #[async_trait::async_trait(?Send)]
37 impl ApubObject for ApubCommunityOutbox {
38   type DataType = CommunityContext;
39   type TombstoneType = ();
40   type ApubType = GroupOutbox;
41
42   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
43     None
44   }
45
46   async fn read_from_apub_id(
47     _object_id: Url,
48     data: &Self::DataType,
49   ) -> Result<Option<Self>, LemmyError> {
50     // Only read from database if its a local community, otherwise fetch over http
51     if data.0.local {
52       let community_id = data.0.id;
53       let post_list: Vec<ApubPost> = blocking(data.1.pool(), move |conn| {
54         Post::list_for_community(conn, community_id)
55       })
56       .await??
57       .into_iter()
58       .map(Into::into)
59       .collect();
60       Ok(Some(ApubCommunityOutbox(post_list)))
61     } else {
62       Ok(None)
63     }
64   }
65
66   async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
67     // do nothing (it gets deleted automatically with the community)
68     Ok(())
69   }
70
71   async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
72     let mut ordered_items = vec![];
73     for post in &self.0 {
74       let actor = post.creator_id;
75       let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor))
76         .await??
77         .into();
78       let a =
79         CreateOrUpdatePost::new(post, &actor, &data.0, CreateOrUpdateType::Create, &data.1).await?;
80       ordered_items.push(a);
81     }
82
83     Ok(GroupOutbox {
84       r#type: OrderedCollectionType::OrderedCollection,
85       id: generate_outbox_url(&data.0.actor_id)?.into(),
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 }