2020-07-23 14:36:45 +00:00
|
|
|
use crate::{
|
|
|
|
apub::{
|
2020-07-29 11:46:11 +00:00
|
|
|
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
|
2020-07-28 16:08:28 +00:00
|
|
|
inbox::shared_inbox::{
|
2020-07-29 11:46:11 +00:00
|
|
|
announce_if_community_is_local,
|
|
|
|
get_user_from_activity,
|
|
|
|
receive_unhandled_activity,
|
2020-07-28 16:08:28 +00:00
|
|
|
},
|
2020-07-29 11:46:11 +00:00
|
|
|
FromApub,
|
|
|
|
PageExt,
|
2020-07-23 14:36:45 +00:00
|
|
|
},
|
2020-08-18 13:43:50 +00:00
|
|
|
LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
2020-08-01 13:25:17 +00:00
|
|
|
use activitystreams::{activity::Like, base::AnyBase, object::Note, prelude::*};
|
2020-08-18 13:43:50 +00:00
|
|
|
use actix_web::HttpResponse;
|
2020-08-11 14:31:05 +00:00
|
|
|
use anyhow::Context;
|
2020-09-16 10:25:22 +00:00
|
|
|
use lemmy_structs::{
|
|
|
|
blocking,
|
|
|
|
comment::CommentResponse,
|
|
|
|
post::PostResponse,
|
|
|
|
websocket::{SendComment, SendPost, UserOperation},
|
|
|
|
};
|
2020-07-23 14:36:45 +00:00
|
|
|
use lemmy_db::{
|
|
|
|
comment::{CommentForm, CommentLike, CommentLikeForm},
|
|
|
|
comment_view::CommentView,
|
|
|
|
post::{PostForm, PostLike, PostLikeForm},
|
|
|
|
post_view::PostView,
|
|
|
|
Likeable,
|
|
|
|
};
|
2020-09-01 14:25:34 +00:00
|
|
|
use lemmy_utils::{location_info, LemmyError};
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
pub async fn receive_like(
|
|
|
|
activity: AnyBase,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-11 14:31:05 +00:00
|
|
|
let like = Like::from_any_base(activity)?.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
match like.object().as_single_kind_str() {
|
2020-08-18 13:43:50 +00:00
|
|
|
Some("Page") => receive_like_post(like, context).await,
|
|
|
|
Some("Note") => receive_like_comment(like, context).await,
|
2020-07-23 14:36:45 +00:00
|
|
|
_ => receive_unhandled_activity(like),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn receive_like_post(like: Like, context: &LemmyContext) -> Result<HttpResponse, LemmyError> {
|
|
|
|
let user = get_user_from_activity(&like, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let page = PageExt::from_any_base(like.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = PostForm::from_apub(&page, context, None).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
|
2020-07-23 14:36:45 +00:00
|
|
|
.await?
|
|
|
|
.id;
|
|
|
|
|
|
|
|
let like_form = PostLikeForm {
|
|
|
|
post_id,
|
|
|
|
user_id: user.id,
|
|
|
|
score: 1,
|
|
|
|
};
|
2020-08-12 14:43:45 +00:00
|
|
|
let user_id = user.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2020-08-12 14:43:45 +00:00
|
|
|
PostLike::remove(conn, user_id, post_id)?;
|
2020-07-23 14:36:45 +00:00
|
|
|
PostLike::like(conn, &like_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
|
|
|
PostView::read(conn, post_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
2020-07-23 14:36:45 +00:00
|
|
|
op: UserOperation::CreatePostLike,
|
|
|
|
post: res,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(like, &user, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_like_comment(
|
|
|
|
like: Like,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-11 14:31:05 +00:00
|
|
|
let note = Note::from_any_base(like.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_activity(&like, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment = CommentForm::from_apub(¬e, context, None).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
|
2020-07-23 14:36:45 +00:00
|
|
|
.await?
|
|
|
|
.id;
|
|
|
|
|
|
|
|
let like_form = CommentLikeForm {
|
|
|
|
comment_id,
|
|
|
|
post_id: comment.post_id,
|
|
|
|
user_id: user.id,
|
|
|
|
score: 1,
|
|
|
|
};
|
2020-08-12 14:43:45 +00:00
|
|
|
let user_id = user.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2020-08-12 14:43:45 +00:00
|
|
|
CommentLike::remove(conn, user_id, comment_id)?;
|
2020-07-23 14:36:45 +00:00
|
|
|
CommentLike::like(conn, &like_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment_view = blocking(context.pool(), move |conn| {
|
|
|
|
CommentView::read(conn, comment_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
// TODO get those recipient actor ids from somewhere
|
|
|
|
let recipient_ids = vec![];
|
|
|
|
let res = CommentResponse {
|
|
|
|
comment: comment_view,
|
|
|
|
recipient_ids,
|
2020-07-28 16:08:28 +00:00
|
|
|
form_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendComment {
|
2020-07-23 14:36:45 +00:00
|
|
|
op: UserOperation::CreateCommentLike,
|
|
|
|
comment: res,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(like, &user, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|