2 fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
4 announce_if_community_is_local,
5 get_user_from_activity,
6 receive_unhandled_activity,
11 use activitystreams::{activity::Dislike, base::AnyBase, object::Note, prelude::*};
12 use actix_web::HttpResponse;
15 comment::{CommentForm, CommentLike, CommentLikeForm},
16 comment_view::CommentView,
17 post::{PostForm, PostLike, PostLikeForm},
21 use lemmy_structs::{blocking, comment::CommentResponse, post::PostResponse};
22 use lemmy_utils::{location_info, LemmyError};
23 use lemmy_websocket::{
24 messages::{SendComment, SendPost},
29 pub async fn receive_dislike(
31 context: &LemmyContext,
32 ) -> Result<HttpResponse, LemmyError> {
33 let dislike = Dislike::from_any_base(activity)?.context(location_info!())?;
34 match dislike.object().as_single_kind_str() {
35 Some("Page") => receive_dislike_post(dislike, context).await,
36 Some("Note") => receive_dislike_comment(dislike, context).await,
37 _ => receive_unhandled_activity(dislike),
41 async fn receive_dislike_post(
43 context: &LemmyContext,
44 ) -> Result<HttpResponse, LemmyError> {
45 let user = get_user_from_activity(&dislike, context).await?;
46 let page = PageExt::from_any_base(
51 .context(location_info!())?,
53 .context(location_info!())?;
55 let post = PostForm::from_apub(&page, context, None).await?;
57 let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
61 let like_form = PostLikeForm {
66 let user_id = user.id;
67 blocking(context.pool(), move |conn| {
68 PostLike::remove(conn, user_id, post_id)?;
69 PostLike::like(conn, &like_form)
74 let post_view = blocking(context.pool(), move |conn| {
75 PostView::read(conn, post_id, None)
79 let res = PostResponse { post: post_view };
81 context.chat_server().do_send(SendPost {
82 op: UserOperation::CreatePostLike,
87 announce_if_community_is_local(dislike, &user, context).await?;
88 Ok(HttpResponse::Ok().finish())
91 async fn receive_dislike_comment(
93 context: &LemmyContext,
94 ) -> Result<HttpResponse, LemmyError> {
95 let note = Note::from_any_base(
100 .context(location_info!())?,
102 .context(location_info!())?;
103 let user = get_user_from_activity(&dislike, context).await?;
105 let comment = CommentForm::from_apub(¬e, context, None).await?;
107 let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
111 let like_form = CommentLikeForm {
113 post_id: comment.post_id,
117 let user_id = user.id;
118 blocking(context.pool(), move |conn| {
119 CommentLike::remove(conn, user_id, comment_id)?;
120 CommentLike::like(conn, &like_form)
125 let comment_view = blocking(context.pool(), move |conn| {
126 CommentView::read(conn, comment_id, None)
130 // TODO get those recipient actor ids from somewhere
131 let recipient_ids = vec![];
132 let res = CommentResponse {
133 comment: comment_view,
138 context.chat_server().do_send(SendComment {
139 op: UserOperation::CreateCommentLike,
144 announce_if_community_is_local(dislike, &user, context).await?;
145 Ok(HttpResponse::Ok().finish())