1 use crate::{activities::receive::get_actor_as_user, objects::FromApub, ActorType, PageExt};
3 activity::{Create, Dislike, Like, Remove, Update},
7 use lemmy_api_structs::{blocking, post::PostResponse};
8 use lemmy_db_queries::{source::post::Post_, Likeable};
9 use lemmy_db_schema::source::post::{Post, PostLike, PostLikeForm};
10 use lemmy_db_views::post_view::PostView;
11 use lemmy_utils::{location_info, LemmyError};
12 use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation};
14 pub(crate) async fn receive_create_post(
16 context: &LemmyContext,
17 request_counter: &mut i32,
18 ) -> Result<(), LemmyError> {
19 let user = get_actor_as_user(&create, context, request_counter).await?;
20 let page = PageExt::from_any_base(create.object().to_owned().one().context(location_info!())?)?
21 .context(location_info!())?;
23 let post = Post::from_apub(&page, context, user.actor_id(), request_counter).await?;
26 let post_id = post.id;
27 let post_view = blocking(context.pool(), move |conn| {
28 PostView::read(conn, post_id, None)
32 let res = PostResponse { post_view };
34 context.chat_server().do_send(SendPost {
35 op: UserOperation::CreatePost,
43 pub(crate) async fn receive_update_post(
45 context: &LemmyContext,
46 request_counter: &mut i32,
47 ) -> Result<(), LemmyError> {
48 let user = get_actor_as_user(&update, context, request_counter).await?;
49 let page = PageExt::from_any_base(update.object().to_owned().one().context(location_info!())?)?
50 .context(location_info!())?;
52 let post = Post::from_apub(&page, context, user.actor_id(), request_counter).await?;
54 let post_id = post.id;
56 let post_view = blocking(context.pool(), move |conn| {
57 PostView::read(conn, post_id, None)
61 let res = PostResponse { post_view };
63 context.chat_server().do_send(SendPost {
64 op: UserOperation::EditPost,
72 pub(crate) async fn receive_like_post(
75 context: &LemmyContext,
76 request_counter: &mut i32,
77 ) -> Result<(), LemmyError> {
78 let user = get_actor_as_user(&like, context, request_counter).await?;
80 let post_id = post.id;
81 let like_form = PostLikeForm {
86 let user_id = user.id;
87 blocking(context.pool(), move |conn| {
88 PostLike::remove(conn, user_id, post_id)?;
89 PostLike::like(conn, &like_form)
94 let post_view = blocking(context.pool(), move |conn| {
95 PostView::read(conn, post_id, None)
99 let res = PostResponse { post_view };
101 context.chat_server().do_send(SendPost {
102 op: UserOperation::CreatePostLike,
110 pub(crate) async fn receive_dislike_post(
113 context: &LemmyContext,
114 request_counter: &mut i32,
115 ) -> Result<(), LemmyError> {
116 let user = get_actor_as_user(&dislike, context, request_counter).await?;
118 let post_id = post.id;
119 let like_form = PostLikeForm {
124 let user_id = user.id;
125 blocking(context.pool(), move |conn| {
126 PostLike::remove(conn, user_id, post_id)?;
127 PostLike::like(conn, &like_form)
132 let post_view = blocking(context.pool(), move |conn| {
133 PostView::read(conn, post_id, None)
137 let res = PostResponse { post_view };
139 context.chat_server().do_send(SendPost {
140 op: UserOperation::CreatePostLike,
148 pub(crate) async fn receive_delete_post(
149 context: &LemmyContext,
151 ) -> Result<(), LemmyError> {
152 let deleted_post = blocking(context.pool(), move |conn| {
153 Post::update_deleted(conn, post.id, true)
158 let post_id = deleted_post.id;
159 let post_view = blocking(context.pool(), move |conn| {
160 PostView::read(conn, post_id, None)
164 let res = PostResponse { post_view };
165 context.chat_server().do_send(SendPost {
166 op: UserOperation::EditPost,
174 pub(crate) async fn receive_remove_post(
175 context: &LemmyContext,
178 ) -> Result<(), LemmyError> {
179 let removed_post = blocking(context.pool(), move |conn| {
180 Post::update_removed(conn, post.id, true)
185 let post_id = removed_post.id;
186 let post_view = blocking(context.pool(), move |conn| {
187 PostView::read(conn, post_id, None)
191 let res = PostResponse { post_view };
192 context.chat_server().do_send(SendPost {
193 op: UserOperation::EditPost,