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_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;
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: &User_, 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, context).await?;
56 /// Send out information about an edited post, to the followers of the community.
57 async fn send_update(&self, creator: &User_, 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, context).await?;
80 async fn send_delete(&self, creator: &User_, 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, 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, context).await?;
137 async fn send_remove(&self, mod_: &User_, 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, context).await?;
158 async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
159 let community_id = self.community_id;
160 let community = blocking(context.pool(), move |conn| {
161 Community::read(conn, community_id)
165 let mut remove = Remove::new(
166 mod_.actor_id.to_owned().into_inner(),
167 self.ap_id.to_owned().into_inner(),
170 .set_many_contexts(lemmy_context()?)
171 .set_id(generate_activity_id(RemoveType::Remove)?)
173 .set_many_ccs(vec![community.actor_id()]);
175 // Undo that fake activity
176 let mut undo = Undo::new(
177 mod_.actor_id.to_owned().into_inner(),
178 remove.into_any_base()?,
181 .set_many_contexts(lemmy_context()?)
182 .set_id(generate_activity_id(UndoType::Undo)?)
184 .set_many_ccs(vec![community.actor_id()]);
186 send_to_community(undo, mod_, &community, context).await?;
191 #[async_trait::async_trait(?Send)]
192 impl ApubLikeableType for Post {
193 async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
194 let community_id = self.community_id;
195 let community = blocking(context.pool(), move |conn| {
196 Community::read(conn, community_id)
200 let mut like = Like::new(
201 creator.actor_id.to_owned().into_inner(),
202 self.ap_id.to_owned().into_inner(),
205 .set_many_contexts(lemmy_context()?)
206 .set_id(generate_activity_id(LikeType::Like)?)
208 .set_many_ccs(vec![community.actor_id()]);
210 send_to_community(like, &creator, &community, context).await?;
214 async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
215 let community_id = self.community_id;
216 let community = blocking(context.pool(), move |conn| {
217 Community::read(conn, community_id)
221 let mut dislike = Dislike::new(
222 creator.actor_id.to_owned().into_inner(),
223 self.ap_id.to_owned().into_inner(),
226 .set_many_contexts(lemmy_context()?)
227 .set_id(generate_activity_id(DislikeType::Dislike)?)
229 .set_many_ccs(vec![community.actor_id()]);
231 send_to_community(dislike, &creator, &community, context).await?;
235 async fn send_undo_like(
238 context: &LemmyContext,
239 ) -> Result<(), LemmyError> {
240 let community_id = self.community_id;
241 let community = blocking(context.pool(), move |conn| {
242 Community::read(conn, community_id)
246 let mut like = Like::new(
247 creator.actor_id.to_owned().into_inner(),
248 self.ap_id.to_owned().into_inner(),
251 .set_many_contexts(lemmy_context()?)
252 .set_id(generate_activity_id(LikeType::Like)?)
254 .set_many_ccs(vec![community.actor_id()]);
256 // Undo that fake activity
257 let mut undo = Undo::new(
258 creator.actor_id.to_owned().into_inner(),
259 like.into_any_base()?,
262 .set_many_contexts(lemmy_context()?)
263 .set_id(generate_activity_id(UndoType::Undo)?)
265 .set_many_ccs(vec![community.actor_id()]);
267 send_to_community(undo, &creator, &community, context).await?;