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::Like, base::AnyBase, object::Note, prelude::*};
15 use actix_web::HttpResponse;
19 comment::CommentResponse,
21 websocket::{SendComment, SendPost, UserOperation},
24 comment::{CommentForm, CommentLike, CommentLikeForm},
25 comment_view::CommentView,
26 post::{PostForm, PostLike, PostLikeForm},
30 use lemmy_utils::{location_info, LemmyError};
32 pub async fn receive_like(
34 context: &LemmyContext,
35 ) -> Result<HttpResponse, LemmyError> {
36 let like = Like::from_any_base(activity)?.context(location_info!())?;
37 match like.object().as_single_kind_str() {
38 Some("Page") => receive_like_post(like, context).await,
39 Some("Note") => receive_like_comment(like, context).await,
40 _ => receive_unhandled_activity(like),
44 async fn receive_like_post(like: Like, context: &LemmyContext) -> Result<HttpResponse, LemmyError> {
45 let user = get_user_from_activity(&like, context).await?;
46 let page = PageExt::from_any_base(like.object().to_owned().one().context(location_info!())?)?
47 .context(location_info!())?;
49 let post = PostForm::from_apub(&page, context, None).await?;
51 let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
55 let like_form = PostLikeForm {
60 let user_id = user.id;
61 blocking(context.pool(), move |conn| {
62 PostLike::remove(conn, user_id, post_id)?;
63 PostLike::like(conn, &like_form)
68 let post_view = blocking(context.pool(), move |conn| {
69 PostView::read(conn, post_id, None)
73 let res = PostResponse { post: post_view };
75 context.chat_server().do_send(SendPost {
76 op: UserOperation::CreatePostLike,
81 announce_if_community_is_local(like, &user, context).await?;
82 Ok(HttpResponse::Ok().finish())
85 async fn receive_like_comment(
87 context: &LemmyContext,
88 ) -> Result<HttpResponse, LemmyError> {
89 let note = Note::from_any_base(like.object().to_owned().one().context(location_info!())?)?
90 .context(location_info!())?;
91 let user = get_user_from_activity(&like, context).await?;
93 let comment = CommentForm::from_apub(¬e, context, None).await?;
95 let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
99 let like_form = CommentLikeForm {
101 post_id: comment.post_id,
105 let user_id = user.id;
106 blocking(context.pool(), move |conn| {
107 CommentLike::remove(conn, user_id, comment_id)?;
108 CommentLike::like(conn, &like_form)
113 let comment_view = blocking(context.pool(), move |conn| {
114 CommentView::read(conn, comment_id, None)
118 // TODO get those recipient actor ids from somewhere
119 let recipient_ids = vec![];
120 let res = CommentResponse {
121 comment: comment_view,
126 context.chat_server().do_send(SendComment {
127 op: UserOperation::CreateCommentLike,
132 announce_if_community_is_local(like, &user, context).await?;
133 Ok(HttpResponse::Ok().finish())