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,
14 use activitystreams::{activity::Dislike, base::AnyBase, object::Note, prelude::*};
15 use actix_web::HttpResponse;
18 comment::{CommentForm, CommentLike, CommentLikeForm},
19 comment_view::CommentView,
20 post::{PostForm, PostLike, PostLikeForm},
26 comment::CommentResponse,
28 websocket::{SendComment, SendPost, UserOperation},
30 use lemmy_utils::{location_info, LemmyError};
32 pub async fn receive_dislike(
34 context: &LemmyContext,
35 ) -> Result<HttpResponse, LemmyError> {
36 let dislike = Dislike::from_any_base(activity)?.context(location_info!())?;
37 match dislike.object().as_single_kind_str() {
38 Some("Page") => receive_dislike_post(dislike, context).await,
39 Some("Note") => receive_dislike_comment(dislike, context).await,
40 _ => receive_unhandled_activity(dislike),
44 async fn receive_dislike_post(
46 context: &LemmyContext,
47 ) -> Result<HttpResponse, LemmyError> {
48 let user = get_user_from_activity(&dislike, context).await?;
49 let page = PageExt::from_any_base(
54 .context(location_info!())?,
56 .context(location_info!())?;
58 let post = PostForm::from_apub(&page, context, None).await?;
60 let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
64 let like_form = PostLikeForm {
69 let user_id = user.id;
70 blocking(context.pool(), move |conn| {
71 PostLike::remove(conn, user_id, post_id)?;
72 PostLike::like(conn, &like_form)
77 let post_view = blocking(context.pool(), move |conn| {
78 PostView::read(conn, post_id, None)
82 let res = PostResponse { post: post_view };
84 context.chat_server().do_send(SendPost {
85 op: UserOperation::CreatePostLike,
90 announce_if_community_is_local(dislike, &user, context).await?;
91 Ok(HttpResponse::Ok().finish())
94 async fn receive_dislike_comment(
96 context: &LemmyContext,
97 ) -> Result<HttpResponse, LemmyError> {
98 let note = Note::from_any_base(
103 .context(location_info!())?,
105 .context(location_info!())?;
106 let user = get_user_from_activity(&dislike, context).await?;
108 let comment = CommentForm::from_apub(¬e, context, None).await?;
110 let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
114 let like_form = CommentLikeForm {
116 post_id: comment.post_id,
120 let user_id = user.id;
121 blocking(context.pool(), move |conn| {
122 CommentLike::remove(conn, user_id, comment_id)?;
123 CommentLike::like(conn, &like_form)
128 let comment_view = blocking(context.pool(), move |conn| {
129 CommentView::read(conn, comment_id, None)
133 // TODO get those recipient actor ids from somewhere
134 let recipient_ids = vec![];
135 let res = CommentResponse {
136 comment: comment_view,
141 context.chat_server().do_send(SendComment {
142 op: UserOperation::CreateCommentLike,
147 announce_if_community_is_local(dislike, &user, context).await?;
148 Ok(HttpResponse::Ok().finish())