2021-03-10 22:33:55 +00:00
|
|
|
use crate::{activities::receive::get_actor_as_person, objects::FromApub, ActorType, PageExt};
|
2020-10-21 17:37:50 +00:00
|
|
|
use activitystreams::{
|
2020-10-28 16:14:18 +00:00
|
|
|
activity::{Create, Dislike, Like, Remove, Update},
|
2020-10-21 17:37:50 +00:00
|
|
|
prelude::*,
|
|
|
|
};
|
|
|
|
use anyhow::Context;
|
2021-03-01 13:08:41 +00:00
|
|
|
use lemmy_api_structs::{blocking, post::PostResponse};
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_db_queries::{source::post::Post_, Likeable};
|
2020-12-18 17:27:25 +00:00
|
|
|
use lemmy_db_schema::source::post::{Post, PostLike, PostLikeForm};
|
2020-12-21 16:30:34 +00:00
|
|
|
use lemmy_db_views::post_view::PostView;
|
2020-10-21 17:37:50 +00:00
|
|
|
use lemmy_utils::{location_info, LemmyError};
|
|
|
|
use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation};
|
|
|
|
|
|
|
|
pub(crate) async fn receive_create_post(
|
|
|
|
create: Create,
|
|
|
|
context: &LemmyContext,
|
2020-10-22 18:27:32 +00:00
|
|
|
request_counter: &mut i32,
|
2020-10-28 16:14:18 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2021-03-10 22:33:55 +00:00
|
|
|
let person = get_actor_as_person(&create, context, request_counter).await?;
|
2020-10-21 17:37:50 +00:00
|
|
|
let page = PageExt::from_any_base(create.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
let post = Post::from_apub(&page, context, person.actor_id(), request_counter).await?;
|
2020-10-21 17:37:50 +00:00
|
|
|
|
|
|
|
// Refetch the view
|
2020-12-08 17:38:48 +00:00
|
|
|
let post_id = post.id;
|
2020-10-21 17:37:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
2020-12-08 17:38:48 +00:00
|
|
|
PostView::read(conn, post_id, None)
|
2020-10-21 17:37:50 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-10-21 17:37:50 +00:00
|
|
|
|
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::CreatePost,
|
|
|
|
post: res,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-21 17:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn receive_update_post(
|
|
|
|
update: Update,
|
|
|
|
context: &LemmyContext,
|
2020-10-22 18:27:32 +00:00
|
|
|
request_counter: &mut i32,
|
2020-10-28 16:14:18 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2021-03-10 22:33:55 +00:00
|
|
|
let person = get_actor_as_person(&update, context, request_counter).await?;
|
2020-10-21 17:37:50 +00:00
|
|
|
let page = PageExt::from_any_base(update.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
let post = Post::from_apub(&page, context, person.actor_id(), request_counter).await?;
|
2020-10-21 17:37:50 +00:00
|
|
|
|
2020-12-08 17:38:48 +00:00
|
|
|
let post_id = post.id;
|
2020-10-21 17:37:50 +00:00
|
|
|
// Refetch the view
|
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
2020-12-08 17:38:48 +00:00
|
|
|
PostView::read(conn, post_id, None)
|
2020-10-21 17:37:50 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-10-21 17:37:50 +00:00
|
|
|
|
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::EditPost,
|
|
|
|
post: res,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-21 17:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn receive_like_post(
|
|
|
|
like: Like,
|
2020-12-08 17:38:48 +00:00
|
|
|
post: Post,
|
2020-10-21 17:37:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-10-22 18:27:32 +00:00
|
|
|
request_counter: &mut i32,
|
2020-10-28 16:14:18 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2021-03-10 22:33:55 +00:00
|
|
|
let person = get_actor_as_person(&like, context, request_counter).await?;
|
2020-10-21 17:37:50 +00:00
|
|
|
|
2020-12-08 17:38:48 +00:00
|
|
|
let post_id = post.id;
|
2020-10-21 17:37:50 +00:00
|
|
|
let like_form = PostLikeForm {
|
|
|
|
post_id,
|
2021-03-10 22:33:55 +00:00
|
|
|
person_id: person.id,
|
2020-10-21 17:37:50 +00:00
|
|
|
score: 1,
|
|
|
|
};
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_id = person.id;
|
2020-10-21 17:37:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostLike::remove(conn, person_id, post_id)?;
|
2020-10-21 17:37:50 +00:00
|
|
|
PostLike::like(conn, &like_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
|
|
|
PostView::read(conn, post_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-10-21 17:37:50 +00:00
|
|
|
|
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::CreatePostLike,
|
|
|
|
post: res,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-21 17:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn receive_dislike_post(
|
|
|
|
dislike: Dislike,
|
2020-12-08 17:38:48 +00:00
|
|
|
post: Post,
|
2020-10-21 17:37:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-10-22 18:27:32 +00:00
|
|
|
request_counter: &mut i32,
|
2020-10-28 16:14:18 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2021-03-10 22:33:55 +00:00
|
|
|
let person = get_actor_as_person(&dislike, context, request_counter).await?;
|
2020-10-21 17:37:50 +00:00
|
|
|
|
2020-12-08 17:38:48 +00:00
|
|
|
let post_id = post.id;
|
2020-10-21 17:37:50 +00:00
|
|
|
let like_form = PostLikeForm {
|
|
|
|
post_id,
|
2021-03-10 22:33:55 +00:00
|
|
|
person_id: person.id,
|
2020-10-21 17:37:50 +00:00
|
|
|
score: -1,
|
|
|
|
};
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_id = person.id;
|
2020-10-21 17:37:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostLike::remove(conn, person_id, post_id)?;
|
2020-10-21 17:37:50 +00:00
|
|
|
PostLike::like(conn, &like_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
|
|
|
PostView::read(conn, post_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-10-21 17:37:50 +00:00
|
|
|
|
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::CreatePostLike,
|
|
|
|
post: res,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-21 17:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn receive_delete_post(
|
|
|
|
context: &LemmyContext,
|
|
|
|
post: Post,
|
2020-10-28 16:14:18 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-10-21 17:37:50 +00:00
|
|
|
let deleted_post = blocking(context.pool(), move |conn| {
|
|
|
|
Post::update_deleted(conn, post.id, true)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let post_id = deleted_post.id;
|
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
|
|
|
PostView::read(conn, post_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-10-21 17:37:50 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::EditPost,
|
|
|
|
post: res,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-21 17:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn receive_remove_post(
|
|
|
|
context: &LemmyContext,
|
|
|
|
_remove: Remove,
|
|
|
|
post: Post,
|
2020-10-28 16:14:18 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-10-21 17:37:50 +00:00
|
|
|
let removed_post = blocking(context.pool(), move |conn| {
|
|
|
|
Post::update_removed(conn, post.id, true)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let post_id = removed_post.id;
|
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
|
|
|
PostView::read(conn, post_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-10-21 17:37:50 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::EditPost,
|
|
|
|
post: res,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-21 17:37:50 +00:00
|
|
|
}
|