3 fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
5 announce_if_community_is_local,
6 get_user_from_activity,
7 receive_unhandled_activity,
13 messages::{SendComment, SendPost},
18 use activitystreams::{activity::Dislike, base::AnyBase, object::Note, prelude::*};
19 use actix_web::HttpResponse;
21 use lemmy_api_structs::{blocking, comment::CommentResponse, post::PostResponse};
23 comment::{CommentForm, CommentLike, CommentLikeForm},
24 comment_view::CommentView,
25 post::{PostForm, PostLike, PostLikeForm},
29 use lemmy_utils::{location_info, LemmyError};
31 pub async fn receive_dislike(
33 context: &LemmyContext,
34 ) -> Result<HttpResponse, LemmyError> {
35 let dislike = Dislike::from_any_base(activity)?.context(location_info!())?;
36 match dislike.object().as_single_kind_str() {
37 Some("Page") => receive_dislike_post(dislike, context).await,
38 Some("Note") => receive_dislike_comment(dislike, context).await,
39 _ => receive_unhandled_activity(dislike),
43 async fn receive_dislike_post(
45 context: &LemmyContext,
46 ) -> Result<HttpResponse, LemmyError> {
47 let user = get_user_from_activity(&dislike, context).await?;
48 let page = PageExt::from_any_base(
53 .context(location_info!())?,
55 .context(location_info!())?;
57 let post = PostForm::from_apub(&page, context, None).await?;
59 let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
63 let like_form = PostLikeForm {
68 let user_id = user.id;
69 blocking(context.pool(), move |conn| {
70 PostLike::remove(conn, user_id, post_id)?;
71 PostLike::like(conn, &like_form)
76 let post_view = blocking(context.pool(), move |conn| {
77 PostView::read(conn, post_id, None)
81 let res = PostResponse { post: post_view };
83 context.chat_server().do_send(SendPost {
84 op: UserOperation::CreatePostLike,
89 announce_if_community_is_local(dislike, &user, context).await?;
90 Ok(HttpResponse::Ok().finish())
93 async fn receive_dislike_comment(
95 context: &LemmyContext,
96 ) -> Result<HttpResponse, LemmyError> {
97 let note = Note::from_any_base(
102 .context(location_info!())?,
104 .context(location_info!())?;
105 let user = get_user_from_activity(&dislike, context).await?;
107 let comment = CommentForm::from_apub(¬e, context, None).await?;
109 let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
113 let like_form = CommentLikeForm {
115 post_id: comment.post_id,
119 let user_id = user.id;
120 blocking(context.pool(), move |conn| {
121 CommentLike::remove(conn, user_id, comment_id)?;
122 CommentLike::like(conn, &like_form)
127 let comment_view = blocking(context.pool(), move |conn| {
128 CommentView::read(conn, comment_id, None)
132 // TODO get those recipient actor ids from somewhere
133 let recipient_ids = vec![];
134 let res = CommentResponse {
135 comment: comment_view,
140 context.chat_server().do_send(SendComment {
141 op: UserOperation::CreateCommentLike,
146 announce_if_community_is_local(dislike, &user, context).await?;
147 Ok(HttpResponse::Ok().finish())