]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/send/post.rs
4af40de28b2d58206a92c1c1eaa2c683a47dbda1
[lemmy.git] / crates / apub / src / activities / send / post.rs
1 use crate::{
2   activities::send::generate_activity_id,
3   activity_queue::send_to_community,
4   extensions::context::lemmy_context,
5   objects::ToApub,
6   ActorType,
7   ApubLikeableType,
8   ApubObjectType,
9 };
10 use activitystreams::{
11   activity::{
12     kind::{CreateType, DeleteType, DislikeType, LikeType, RemoveType, UndoType, UpdateType},
13     Create,
14     Delete,
15     Dislike,
16     Like,
17     Remove,
18     Undo,
19     Update,
20   },
21   prelude::*,
22   public,
23 };
24 use lemmy_db_queries::Crud;
25 use lemmy_db_schema::source::{community::Community, post::Post, user::User_};
26 use lemmy_structs::blocking;
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::LemmyContext;
29 use url::Url;
30
31 #[async_trait::async_trait(?Send)]
32 impl ApubObjectType for Post {
33   /// Send out information about a newly created post, to the followers of the community.
34   async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
35     let page = self.to_apub(context.pool()).await?;
36
37     let community_id = self.community_id;
38     let community = blocking(context.pool(), move |conn| {
39       Community::read(conn, community_id)
40     })
41     .await??;
42
43     let mut create = Create::new(creator.actor_id.to_owned(), page.into_any_base()?);
44     create
45       .set_many_contexts(lemmy_context()?)
46       .set_id(generate_activity_id(CreateType::Create)?)
47       .set_to(public())
48       .set_many_ccs(vec![community.actor_id()?]);
49
50     send_to_community(create, creator, &community, context).await?;
51     Ok(())
52   }
53
54   /// Send out information about an edited post, to the followers of the community.
55   async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
56     let page = self.to_apub(context.pool()).await?;
57
58     let community_id = self.community_id;
59     let community = blocking(context.pool(), move |conn| {
60       Community::read(conn, community_id)
61     })
62     .await??;
63
64     let mut update = Update::new(creator.actor_id.to_owned(), page.into_any_base()?);
65     update
66       .set_many_contexts(lemmy_context()?)
67       .set_id(generate_activity_id(UpdateType::Update)?)
68       .set_to(public())
69       .set_many_ccs(vec![community.actor_id()?]);
70
71     send_to_community(update, creator, &community, context).await?;
72     Ok(())
73   }
74
75   async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
76     let community_id = self.community_id;
77     let community = blocking(context.pool(), move |conn| {
78       Community::read(conn, community_id)
79     })
80     .await??;
81
82     let mut delete = Delete::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
83     delete
84       .set_many_contexts(lemmy_context()?)
85       .set_id(generate_activity_id(DeleteType::Delete)?)
86       .set_to(public())
87       .set_many_ccs(vec![community.actor_id()?]);
88
89     send_to_community(delete, creator, &community, context).await?;
90     Ok(())
91   }
92
93   async fn send_undo_delete(
94     &self,
95     creator: &User_,
96     context: &LemmyContext,
97   ) -> Result<(), LemmyError> {
98     let community_id = self.community_id;
99     let community = blocking(context.pool(), move |conn| {
100       Community::read(conn, community_id)
101     })
102     .await??;
103
104     let mut delete = Delete::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
105     delete
106       .set_many_contexts(lemmy_context()?)
107       .set_id(generate_activity_id(DeleteType::Delete)?)
108       .set_to(public())
109       .set_many_ccs(vec![community.actor_id()?]);
110
111     // Undo that fake activity
112     let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
113     undo
114       .set_many_contexts(lemmy_context()?)
115       .set_id(generate_activity_id(UndoType::Undo)?)
116       .set_to(public())
117       .set_many_ccs(vec![community.actor_id()?]);
118
119     send_to_community(undo, creator, &community, context).await?;
120     Ok(())
121   }
122
123   async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
124     let community_id = self.community_id;
125     let community = blocking(context.pool(), move |conn| {
126       Community::read(conn, community_id)
127     })
128     .await??;
129
130     let mut remove = Remove::new(mod_.actor_id.to_owned(), Url::parse(&self.ap_id)?);
131     remove
132       .set_many_contexts(lemmy_context()?)
133       .set_id(generate_activity_id(RemoveType::Remove)?)
134       .set_to(public())
135       .set_many_ccs(vec![community.actor_id()?]);
136
137     send_to_community(remove, mod_, &community, context).await?;
138     Ok(())
139   }
140
141   async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
142     let community_id = self.community_id;
143     let community = blocking(context.pool(), move |conn| {
144       Community::read(conn, community_id)
145     })
146     .await??;
147
148     let mut remove = Remove::new(mod_.actor_id.to_owned(), Url::parse(&self.ap_id)?);
149     remove
150       .set_many_contexts(lemmy_context()?)
151       .set_id(generate_activity_id(RemoveType::Remove)?)
152       .set_to(public())
153       .set_many_ccs(vec![community.actor_id()?]);
154
155     // Undo that fake activity
156     let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?);
157     undo
158       .set_many_contexts(lemmy_context()?)
159       .set_id(generate_activity_id(UndoType::Undo)?)
160       .set_to(public())
161       .set_many_ccs(vec![community.actor_id()?]);
162
163     send_to_community(undo, mod_, &community, context).await?;
164     Ok(())
165   }
166 }
167
168 #[async_trait::async_trait(?Send)]
169 impl ApubLikeableType for Post {
170   async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
171     let community_id = self.community_id;
172     let community = blocking(context.pool(), move |conn| {
173       Community::read(conn, community_id)
174     })
175     .await??;
176
177     let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
178     like
179       .set_many_contexts(lemmy_context()?)
180       .set_id(generate_activity_id(LikeType::Like)?)
181       .set_to(public())
182       .set_many_ccs(vec![community.actor_id()?]);
183
184     send_to_community(like, &creator, &community, context).await?;
185     Ok(())
186   }
187
188   async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
189     let community_id = self.community_id;
190     let community = blocking(context.pool(), move |conn| {
191       Community::read(conn, community_id)
192     })
193     .await??;
194
195     let mut dislike = Dislike::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
196     dislike
197       .set_many_contexts(lemmy_context()?)
198       .set_id(generate_activity_id(DislikeType::Dislike)?)
199       .set_to(public())
200       .set_many_ccs(vec![community.actor_id()?]);
201
202     send_to_community(dislike, &creator, &community, context).await?;
203     Ok(())
204   }
205
206   async fn send_undo_like(
207     &self,
208     creator: &User_,
209     context: &LemmyContext,
210   ) -> Result<(), LemmyError> {
211     let community_id = self.community_id;
212     let community = blocking(context.pool(), move |conn| {
213       Community::read(conn, community_id)
214     })
215     .await??;
216
217     let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
218     like
219       .set_many_contexts(lemmy_context()?)
220       .set_id(generate_activity_id(LikeType::Like)?)
221       .set_to(public())
222       .set_many_ccs(vec![community.actor_id()?]);
223
224     // Undo that fake activity
225     let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?);
226     undo
227       .set_many_contexts(lemmy_context()?)
228       .set_id(generate_activity_id(UndoType::Undo)?)
229       .set_to(public())
230       .set_many_ccs(vec![community.actor_id()?]);
231
232     send_to_community(undo, &creator, &community, context).await?;
233     Ok(())
234   }
235 }