2 activities::receive::get_actor_as_person,
3 inbox::receive_for_community::verify_mod_activity,
9 activity::{Announce, Create, Dislike, Like, Update},
13 use lemmy_api_common::{blocking, post::PostResponse};
14 use lemmy_db_queries::{source::post::Post_, ApubObject, Crud, Likeable};
15 use lemmy_db_schema::{
18 post::{Post, PostLike, PostLikeForm},
22 use lemmy_db_views::post_view::PostView;
23 use lemmy_utils::{location_info, LemmyError};
24 use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation};
26 pub(crate) async fn receive_create_post(
28 context: &LemmyContext,
29 request_counter: &mut i32,
30 ) -> Result<(), LemmyError> {
31 let person = get_actor_as_person(&create, context, request_counter).await?;
32 let page = PageExt::from_any_base(create.object().to_owned().one().context(location_info!())?)?
33 .context(location_info!())?;
35 let post = Post::from_apub(&page, context, person.actor_id(), request_counter, false).await?;
38 let post_id = post.id;
39 let post_view = blocking(context.pool(), move |conn| {
40 PostView::read(conn, post_id, None)
44 let res = PostResponse { post_view };
46 context.chat_server().do_send(SendPost {
47 op: UserOperation::CreatePost,
55 pub(crate) async fn receive_update_post(
57 announce: Option<Announce>,
58 context: &LemmyContext,
59 request_counter: &mut i32,
60 ) -> Result<(), LemmyError> {
61 let person = get_actor_as_person(&update, context, request_counter).await?;
62 let page = PageExt::from_any_base(update.object().to_owned().one().context(location_info!())?)?
63 .context(location_info!())?;
65 let post_id: DbUrl = page
67 .context(location_info!())?
70 let old_post = blocking(context.pool(), move |conn| {
71 Post::read_from_apub_id(conn, &post_id)
75 // If sticked or locked state was changed, make sure the actor is a mod
76 let stickied = page.ext_one.stickied.context(location_info!())?;
77 let locked = !page.ext_one.comments_enabled.context(location_info!())?;
78 let mut mod_action_allowed = false;
79 if (stickied != old_post.stickied) || (locked != old_post.locked) {
80 let community = blocking(context.pool(), move |conn| {
81 Community::read(conn, old_post.community_id)
84 // Only check mod status if the community is local, otherwise we trust that it was sent correctly.
86 verify_mod_activity(&update, announce, &community, context).await?;
88 mod_action_allowed = true;
91 let post = Post::from_apub(
100 let post_id = post.id;
102 let post_view = blocking(context.pool(), move |conn| {
103 PostView::read(conn, post_id, None)
107 let res = PostResponse { post_view };
109 context.chat_server().do_send(SendPost {
110 op: UserOperation::EditPost,
118 pub(crate) async fn receive_like_post(
121 context: &LemmyContext,
122 request_counter: &mut i32,
123 ) -> Result<(), LemmyError> {
124 let person = get_actor_as_person(&like, context, request_counter).await?;
126 let post_id = post.id;
127 let like_form = PostLikeForm {
129 person_id: person.id,
132 let person_id = person.id;
133 blocking(context.pool(), move |conn| {
134 PostLike::remove(conn, person_id, post_id)?;
135 PostLike::like(conn, &like_form)
140 let post_view = blocking(context.pool(), move |conn| {
141 PostView::read(conn, post_id, None)
145 let res = PostResponse { post_view };
147 context.chat_server().do_send(SendPost {
148 op: UserOperation::CreatePostLike,
156 pub(crate) async fn receive_dislike_post(
159 context: &LemmyContext,
160 request_counter: &mut i32,
161 ) -> Result<(), LemmyError> {
162 let person = get_actor_as_person(&dislike, context, request_counter).await?;
164 let post_id = post.id;
165 let like_form = PostLikeForm {
167 person_id: person.id,
170 let person_id = person.id;
171 blocking(context.pool(), move |conn| {
172 PostLike::remove(conn, person_id, post_id)?;
173 PostLike::like(conn, &like_form)
178 let post_view = blocking(context.pool(), move |conn| {
179 PostView::read(conn, post_id, None)
183 let res = PostResponse { post_view };
185 context.chat_server().do_send(SendPost {
186 op: UserOperation::CreatePostLike,
194 pub(crate) async fn receive_delete_post(
195 context: &LemmyContext,
197 ) -> Result<(), LemmyError> {
198 let deleted_post = blocking(context.pool(), move |conn| {
199 Post::update_deleted(conn, post.id, true)
204 let post_id = deleted_post.id;
205 let post_view = blocking(context.pool(), move |conn| {
206 PostView::read(conn, post_id, None)
210 let res = PostResponse { post_view };
211 context.chat_server().do_send(SendPost {
212 op: UserOperation::EditPost,
220 pub(crate) async fn receive_remove_post(
221 context: &LemmyContext,
223 ) -> Result<(), LemmyError> {
224 let removed_post = blocking(context.pool(), move |conn| {
225 Post::update_removed(conn, post.id, true)
230 let post_id = removed_post.id;
231 let post_view = blocking(context.pool(), move |conn| {
232 PostView::read(conn, post_id, None)
236 let res = PostResponse { post_view };
237 context.chat_server().do_send(SendPost {
238 op: UserOperation::EditPost,