2 activities::generate_activity_id,
3 activity_queue::send_to_community,
4 extensions::context::lemmy_context,
11 kind::{DeleteType, DislikeType, LikeType, RemoveType, UndoType},
21 use lemmy_api_common::blocking;
22 use lemmy_db_queries::Crud;
23 use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
24 use lemmy_utils::LemmyError;
25 use lemmy_websocket::LemmyContext;
27 #[async_trait::async_trait(?Send)]
28 impl ApubObjectType for Comment {
32 _context: &LemmyContext,
33 ) -> Result<(), LemmyError> {
40 _context: &LemmyContext,
41 ) -> Result<(), LemmyError> {
45 async fn send_delete(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
46 let post_id = self.post_id;
47 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
49 let community_id = post.community_id;
50 let community = blocking(context.pool(), move |conn| {
51 Community::read(conn, community_id)
55 let mut delete = Delete::new(
56 creator.actor_id.to_owned().into_inner(),
57 self.ap_id.to_owned().into_inner(),
60 .set_many_contexts(lemmy_context())
61 .set_id(generate_activity_id(DeleteType::Delete)?)
63 .set_many_ccs(vec![community.actor_id()]);
65 send_to_community(delete, creator, &community, None, context).await?;
69 async fn send_undo_delete(
72 context: &LemmyContext,
73 ) -> Result<(), LemmyError> {
74 let post_id = self.post_id;
75 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
77 let community_id = post.community_id;
78 let community = blocking(context.pool(), move |conn| {
79 Community::read(conn, community_id)
83 // Generate a fake delete activity, with the correct object
84 let mut delete = Delete::new(
85 creator.actor_id.to_owned().into_inner(),
86 self.ap_id.to_owned().into_inner(),
89 .set_many_contexts(lemmy_context())
90 .set_id(generate_activity_id(DeleteType::Delete)?)
92 .set_many_ccs(vec![community.actor_id()]);
94 // Undo that fake activity
95 let mut undo = Undo::new(
96 creator.actor_id.to_owned().into_inner(),
97 delete.into_any_base()?,
100 .set_many_contexts(lemmy_context())
101 .set_id(generate_activity_id(UndoType::Undo)?)
103 .set_many_ccs(vec![community.actor_id()]);
105 send_to_community(undo, creator, &community, None, context).await?;
109 async fn send_remove(&self, mod_: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
110 let post_id = self.post_id;
111 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
113 let community_id = post.community_id;
114 let community = blocking(context.pool(), move |conn| {
115 Community::read(conn, community_id)
119 let mut remove = Remove::new(
120 mod_.actor_id.to_owned().into_inner(),
121 self.ap_id.to_owned().into_inner(),
124 .set_many_contexts(lemmy_context())
125 .set_id(generate_activity_id(RemoveType::Remove)?)
127 .set_many_ccs(vec![community.actor_id()]);
129 send_to_community(remove, mod_, &community, None, context).await?;
133 async fn send_undo_remove(
136 context: &LemmyContext,
137 ) -> Result<(), LemmyError> {
138 let post_id = self.post_id;
139 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
141 let community_id = post.community_id;
142 let community = blocking(context.pool(), move |conn| {
143 Community::read(conn, community_id)
147 // Generate a fake delete activity, with the correct object
148 let mut remove = Remove::new(
149 mod_.actor_id.to_owned().into_inner(),
150 self.ap_id.to_owned().into_inner(),
153 .set_many_contexts(lemmy_context())
154 .set_id(generate_activity_id(RemoveType::Remove)?)
156 .set_many_ccs(vec![community.actor_id()]);
158 // Undo that fake activity
159 let mut undo = Undo::new(
160 mod_.actor_id.to_owned().into_inner(),
161 remove.into_any_base()?,
164 .set_many_contexts(lemmy_context())
165 .set_id(generate_activity_id(UndoType::Undo)?)
167 .set_many_ccs(vec![community.actor_id()]);
169 send_to_community(undo, mod_, &community, None, context).await?;
174 #[async_trait::async_trait(?Send)]
175 impl ApubLikeableType for Comment {
176 async fn send_like(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
177 let post_id = self.post_id;
178 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
180 let community_id = post.community_id;
181 let community = blocking(context.pool(), move |conn| {
182 Community::read(conn, community_id)
186 let mut like = Like::new(
187 creator.actor_id.to_owned().into_inner(),
188 self.ap_id.to_owned().into_inner(),
191 .set_many_contexts(lemmy_context())
192 .set_id(generate_activity_id(LikeType::Like)?)
194 .set_many_ccs(vec![community.actor_id()]);
196 send_to_community(like, creator, &community, None, context).await?;
200 async fn send_dislike(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
201 let post_id = self.post_id;
202 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
204 let community_id = post.community_id;
205 let community = blocking(context.pool(), move |conn| {
206 Community::read(conn, community_id)
210 let mut dislike = Dislike::new(
211 creator.actor_id.to_owned().into_inner(),
212 self.ap_id.to_owned().into_inner(),
215 .set_many_contexts(lemmy_context())
216 .set_id(generate_activity_id(DislikeType::Dislike)?)
218 .set_many_ccs(vec![community.actor_id()]);
220 send_to_community(dislike, creator, &community, None, context).await?;
224 async fn send_undo_like(
227 context: &LemmyContext,
228 ) -> Result<(), LemmyError> {
229 let post_id = self.post_id;
230 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
232 let community_id = post.community_id;
233 let community = blocking(context.pool(), move |conn| {
234 Community::read(conn, community_id)
238 let mut like = Like::new(
239 creator.actor_id.to_owned().into_inner(),
240 self.ap_id.to_owned().into_inner(),
243 .set_many_contexts(lemmy_context())
244 .set_id(generate_activity_id(DislikeType::Dislike)?)
246 .set_many_ccs(vec![community.actor_id()]);
248 // Undo that fake activity
249 let mut undo = Undo::new(
250 creator.actor_id.to_owned().into_inner(),
251 like.into_any_base()?,
254 .set_many_contexts(lemmy_context())
255 .set_id(generate_activity_id(UndoType::Undo)?)
257 .set_many_ccs(vec![community.actor_id()]);
259 send_to_community(undo, creator, &community, None, context).await?;