2 activities::send::generate_activity_id,
3 activity_queue::send_to_community,
4 extensions::context::lemmy_context,
10 use activitystreams::{
12 kind::{CreateType, DeleteType, DislikeType, LikeType, RemoveType, UndoType, UpdateType},
24 use lemmy_api_common::blocking;
25 use lemmy_db_queries::Crud;
26 use lemmy_db_schema::source::{community::Community, person::Person, post::Post};
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::LemmyContext;
30 #[async_trait::async_trait(?Send)]
31 impl ApubObjectType for Post {
32 /// Send out information about a newly created post, to the followers of the community.
33 async fn send_create(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
34 let page = self.to_apub(context.pool()).await?;
36 let community_id = self.community_id;
37 let community = blocking(context.pool(), move |conn| {
38 Community::read(conn, community_id)
42 let mut create = Create::new(
43 creator.actor_id.to_owned().into_inner(),
44 page.into_any_base()?,
47 .set_many_contexts(lemmy_context()?)
48 .set_id(generate_activity_id(CreateType::Create)?)
50 .set_many_ccs(vec![community.actor_id()]);
52 send_to_community(create, creator, &community, None, context).await?;
56 /// Send out information about an edited post, to the followers of the community.
57 async fn send_update(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
58 let page = self.to_apub(context.pool()).await?;
60 let community_id = self.community_id;
61 let community = blocking(context.pool(), move |conn| {
62 Community::read(conn, community_id)
66 let mut update = Update::new(
67 creator.actor_id.to_owned().into_inner(),
68 page.into_any_base()?,
71 .set_many_contexts(lemmy_context()?)
72 .set_id(generate_activity_id(UpdateType::Update)?)
74 .set_many_ccs(vec![community.actor_id()]);
76 send_to_community(update, creator, &community, None, context).await?;
80 async fn send_delete(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
81 let community_id = self.community_id;
82 let community = blocking(context.pool(), move |conn| {
83 Community::read(conn, community_id)
87 let mut delete = Delete::new(
88 creator.actor_id.to_owned().into_inner(),
89 self.ap_id.to_owned().into_inner(),
92 .set_many_contexts(lemmy_context()?)
93 .set_id(generate_activity_id(DeleteType::Delete)?)
95 .set_many_ccs(vec![community.actor_id()]);
97 send_to_community(delete, creator, &community, None, context).await?;
101 async fn send_undo_delete(
104 context: &LemmyContext,
105 ) -> Result<(), LemmyError> {
106 let community_id = self.community_id;
107 let community = blocking(context.pool(), move |conn| {
108 Community::read(conn, community_id)
112 let mut delete = Delete::new(
113 creator.actor_id.to_owned().into_inner(),
114 self.ap_id.to_owned().into_inner(),
117 .set_many_contexts(lemmy_context()?)
118 .set_id(generate_activity_id(DeleteType::Delete)?)
120 .set_many_ccs(vec![community.actor_id()]);
122 // Undo that fake activity
123 let mut undo = Undo::new(
124 creator.actor_id.to_owned().into_inner(),
125 delete.into_any_base()?,
128 .set_many_contexts(lemmy_context()?)
129 .set_id(generate_activity_id(UndoType::Undo)?)
131 .set_many_ccs(vec![community.actor_id()]);
133 send_to_community(undo, creator, &community, None, context).await?;
137 async fn send_remove(&self, mod_: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
138 let community_id = self.community_id;
139 let community = blocking(context.pool(), move |conn| {
140 Community::read(conn, community_id)
144 let mut remove = Remove::new(
145 mod_.actor_id.to_owned().into_inner(),
146 self.ap_id.to_owned().into_inner(),
149 .set_many_contexts(lemmy_context()?)
150 .set_id(generate_activity_id(RemoveType::Remove)?)
152 .set_many_ccs(vec![community.actor_id()]);
154 send_to_community(remove, mod_, &community, None, context).await?;
158 async fn send_undo_remove(
161 context: &LemmyContext,
162 ) -> Result<(), LemmyError> {
163 let community_id = self.community_id;
164 let community = blocking(context.pool(), move |conn| {
165 Community::read(conn, community_id)
169 let mut remove = Remove::new(
170 mod_.actor_id.to_owned().into_inner(),
171 self.ap_id.to_owned().into_inner(),
174 .set_many_contexts(lemmy_context()?)
175 .set_id(generate_activity_id(RemoveType::Remove)?)
177 .set_many_ccs(vec![community.actor_id()]);
179 // Undo that fake activity
180 let mut undo = Undo::new(
181 mod_.actor_id.to_owned().into_inner(),
182 remove.into_any_base()?,
185 .set_many_contexts(lemmy_context()?)
186 .set_id(generate_activity_id(UndoType::Undo)?)
188 .set_many_ccs(vec![community.actor_id()]);
190 send_to_community(undo, mod_, &community, None, context).await?;
195 #[async_trait::async_trait(?Send)]
196 impl ApubLikeableType for Post {
197 async fn send_like(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
198 let community_id = self.community_id;
199 let community = blocking(context.pool(), move |conn| {
200 Community::read(conn, community_id)
204 let mut like = Like::new(
205 creator.actor_id.to_owned().into_inner(),
206 self.ap_id.to_owned().into_inner(),
209 .set_many_contexts(lemmy_context()?)
210 .set_id(generate_activity_id(LikeType::Like)?)
212 .set_many_ccs(vec![community.actor_id()]);
214 send_to_community(like, &creator, &community, None, context).await?;
218 async fn send_dislike(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
219 let community_id = self.community_id;
220 let community = blocking(context.pool(), move |conn| {
221 Community::read(conn, community_id)
225 let mut dislike = Dislike::new(
226 creator.actor_id.to_owned().into_inner(),
227 self.ap_id.to_owned().into_inner(),
230 .set_many_contexts(lemmy_context()?)
231 .set_id(generate_activity_id(DislikeType::Dislike)?)
233 .set_many_ccs(vec![community.actor_id()]);
235 send_to_community(dislike, &creator, &community, None, context).await?;
239 async fn send_undo_like(
242 context: &LemmyContext,
243 ) -> Result<(), LemmyError> {
244 let community_id = self.community_id;
245 let community = blocking(context.pool(), move |conn| {
246 Community::read(conn, community_id)
250 let mut like = Like::new(
251 creator.actor_id.to_owned().into_inner(),
252 self.ap_id.to_owned().into_inner(),
255 .set_many_contexts(lemmy_context()?)
256 .set_id(generate_activity_id(LikeType::Like)?)
258 .set_many_ccs(vec![community.actor_id()]);
260 // Undo that fake activity
261 let mut undo = Undo::new(
262 creator.actor_id.to_owned().into_inner(),
263 like.into_any_base()?,
266 .set_many_contexts(lemmy_context()?)
267 .set_id(generate_activity_id(UndoType::Undo)?)
269 .set_many_ccs(vec![community.actor_id()]);
271 send_to_community(undo, &creator, &community, None, context).await?;