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;
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?;
37 let community_id = self.community_id;
38 let community = blocking(context.pool(), move |conn| {
39 Community::read(conn, community_id)
43 let mut create = Create::new(creator.actor_id.to_owned(), page.into_any_base()?);
45 .set_many_contexts(lemmy_context()?)
46 .set_id(generate_activity_id(CreateType::Create)?)
48 .set_many_ccs(vec![community.actor_id()?]);
50 send_to_community(create, creator, &community, context).await?;
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?;
58 let community_id = self.community_id;
59 let community = blocking(context.pool(), move |conn| {
60 Community::read(conn, community_id)
64 let mut update = Update::new(creator.actor_id.to_owned(), page.into_any_base()?);
66 .set_many_contexts(lemmy_context()?)
67 .set_id(generate_activity_id(UpdateType::Update)?)
69 .set_many_ccs(vec![community.actor_id()?]);
71 send_to_community(update, creator, &community, context).await?;
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)
82 let mut delete = Delete::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
84 .set_many_contexts(lemmy_context()?)
85 .set_id(generate_activity_id(DeleteType::Delete)?)
87 .set_many_ccs(vec![community.actor_id()?]);
89 send_to_community(delete, creator, &community, context).await?;
93 async fn send_undo_delete(
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)
104 let mut delete = Delete::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
106 .set_many_contexts(lemmy_context()?)
107 .set_id(generate_activity_id(DeleteType::Delete)?)
109 .set_many_ccs(vec![community.actor_id()?]);
111 // Undo that fake activity
112 let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
114 .set_many_contexts(lemmy_context()?)
115 .set_id(generate_activity_id(UndoType::Undo)?)
117 .set_many_ccs(vec![community.actor_id()?]);
119 send_to_community(undo, creator, &community, context).await?;
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)
130 let mut remove = Remove::new(mod_.actor_id.to_owned(), Url::parse(&self.ap_id)?);
132 .set_many_contexts(lemmy_context()?)
133 .set_id(generate_activity_id(RemoveType::Remove)?)
135 .set_many_ccs(vec![community.actor_id()?]);
137 send_to_community(remove, mod_, &community, context).await?;
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)
148 let mut remove = Remove::new(mod_.actor_id.to_owned(), Url::parse(&self.ap_id)?);
150 .set_many_contexts(lemmy_context()?)
151 .set_id(generate_activity_id(RemoveType::Remove)?)
153 .set_many_ccs(vec![community.actor_id()?]);
155 // Undo that fake activity
156 let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?);
158 .set_many_contexts(lemmy_context()?)
159 .set_id(generate_activity_id(UndoType::Undo)?)
161 .set_many_ccs(vec![community.actor_id()?]);
163 send_to_community(undo, mod_, &community, context).await?;
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)
177 let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
179 .set_many_contexts(lemmy_context()?)
180 .set_id(generate_activity_id(LikeType::Like)?)
182 .set_many_ccs(vec![community.actor_id()?]);
184 send_to_community(like, &creator, &community, context).await?;
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)
195 let mut dislike = Dislike::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
197 .set_many_contexts(lemmy_context()?)
198 .set_id(generate_activity_id(DislikeType::Dislike)?)
200 .set_many_ccs(vec![community.actor_id()?]);
202 send_to_community(dislike, &creator, &community, context).await?;
206 async fn send_undo_like(
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)
217 let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
219 .set_many_contexts(lemmy_context()?)
220 .set_id(generate_activity_id(LikeType::Like)?)
222 .set_many_ccs(vec![community.actor_id()?]);
224 // Undo that fake activity
225 let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?);
227 .set_many_contexts(lemmy_context()?)
228 .set_id(generate_activity_id(UndoType::Undo)?)
230 .set_many_ccs(vec![community.actor_id()?]);
232 send_to_community(undo, &creator, &community, context).await?;