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::Like, 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_like(
33 context: &LemmyContext,
34 ) -> Result<HttpResponse, LemmyError> {
35 let like = Like::from_any_base(activity)?.context(location_info!())?;
36 match like.object().as_single_kind_str() {
37 Some("Page") => receive_like_post(like, context).await,
38 Some("Note") => receive_like_comment(like, context).await,
39 _ => receive_unhandled_activity(like),
43 async fn receive_like_post(like: Like, context: &LemmyContext) -> Result<HttpResponse, LemmyError> {
44 let user = get_user_from_activity(&like, context).await?;
45 let page = PageExt::from_any_base(like.object().to_owned().one().context(location_info!())?)?
46 .context(location_info!())?;
48 let post = PostForm::from_apub(&page, context, None).await?;
50 let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
54 let like_form = PostLikeForm {
59 let user_id = user.id;
60 blocking(context.pool(), move |conn| {
61 PostLike::remove(conn, user_id, post_id)?;
62 PostLike::like(conn, &like_form)
67 let post_view = blocking(context.pool(), move |conn| {
68 PostView::read(conn, post_id, None)
72 let res = PostResponse { post: post_view };
74 context.chat_server().do_send(SendPost {
75 op: UserOperation::CreatePostLike,
80 announce_if_community_is_local(like, &user, context).await?;
81 Ok(HttpResponse::Ok().finish())
84 async fn receive_like_comment(
86 context: &LemmyContext,
87 ) -> Result<HttpResponse, LemmyError> {
88 let note = Note::from_any_base(like.object().to_owned().one().context(location_info!())?)?
89 .context(location_info!())?;
90 let user = get_user_from_activity(&like, context).await?;
92 let comment = CommentForm::from_apub(¬e, context, None).await?;
94 let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
98 let like_form = CommentLikeForm {
100 post_id: comment.post_id,
104 let user_id = user.id;
105 blocking(context.pool(), move |conn| {
106 CommentLike::remove(conn, user_id, comment_id)?;
107 CommentLike::like(conn, &like_form)
112 let comment_view = blocking(context.pool(), move |conn| {
113 CommentView::read(conn, comment_id, None)
117 // TODO get those recipient actor ids from somewhere
118 let recipient_ids = vec![];
119 let res = CommentResponse {
120 comment: comment_view,
125 context.chat_server().do_send(SendComment {
126 op: UserOperation::CreateCommentLike,
131 announce_if_community_is_local(like, &user, context).await?;
132 Ok(HttpResponse::Ok().finish())