2020-07-23 14:36:45 +00:00
|
|
|
use crate::{
|
|
|
|
api::{comment::CommentResponse, post::PostResponse},
|
|
|
|
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
|
|
|
},
|
|
|
|
blocking,
|
|
|
|
routes::ChatServerParam,
|
|
|
|
websocket::{
|
|
|
|
server::{SendComment, SendPost},
|
|
|
|
UserOperation,
|
|
|
|
},
|
2020-07-29 11:46:11 +00:00
|
|
|
DbPool,
|
|
|
|
LemmyError,
|
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-07-23 14:36:45 +00:00
|
|
|
use actix_web::{client::Client, HttpResponse};
|
|
|
|
use lemmy_db::{
|
|
|
|
comment::{CommentForm, CommentLike, CommentLikeForm},
|
|
|
|
comment_view::CommentView,
|
|
|
|
post::{PostForm, PostLike, PostLikeForm},
|
|
|
|
post_view::PostView,
|
|
|
|
Likeable,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub async fn receive_like(
|
|
|
|
activity: AnyBase,
|
|
|
|
client: &Client,
|
|
|
|
pool: &DbPool,
|
|
|
|
chat_server: ChatServerParam,
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
|
|
|
let like = Like::from_any_base(activity)?.unwrap();
|
|
|
|
match like.object().as_single_kind_str() {
|
|
|
|
Some("Page") => receive_like_post(like, client, pool, chat_server).await,
|
|
|
|
Some("Note") => receive_like_comment(like, client, pool, chat_server).await,
|
|
|
|
_ => receive_unhandled_activity(like),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_like_post(
|
|
|
|
like: Like,
|
|
|
|
client: &Client,
|
|
|
|
pool: &DbPool,
|
|
|
|
chat_server: ChatServerParam,
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
|
|
|
let user = get_user_from_activity(&like, client, pool).await?;
|
|
|
|
let page = PageExt::from_any_base(like.object().to_owned().one().unwrap())?.unwrap();
|
|
|
|
|
2020-07-29 11:58:39 +00:00
|
|
|
let post = PostForm::from_apub(&page, client, pool).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-07-29 11:46:11 +00:00
|
|
|
let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, client, pool)
|
2020-07-23 14:36:45 +00:00
|
|
|
.await?
|
|
|
|
.id;
|
|
|
|
|
|
|
|
let like_form = PostLikeForm {
|
|
|
|
post_id,
|
|
|
|
user_id: user.id,
|
|
|
|
score: 1,
|
|
|
|
};
|
|
|
|
blocking(pool, move |conn| {
|
|
|
|
PostLike::remove(conn, &like_form)?;
|
|
|
|
PostLike::like(conn, &like_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
|
|
|
|
|
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
|
|
|
chat_server.do_send(SendPost {
|
|
|
|
op: UserOperation::CreatePostLike,
|
|
|
|
post: res,
|
|
|
|
my_id: None,
|
|
|
|
});
|
|
|
|
|
|
|
|
announce_if_community_is_local(like, &user, client, pool).await?;
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_like_comment(
|
|
|
|
like: Like,
|
|
|
|
client: &Client,
|
|
|
|
pool: &DbPool,
|
|
|
|
chat_server: ChatServerParam,
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
|
|
|
let note = Note::from_any_base(like.object().to_owned().one().unwrap())?.unwrap();
|
|
|
|
let user = get_user_from_activity(&like, client, pool).await?;
|
|
|
|
|
2020-07-29 11:58:39 +00:00
|
|
|
let comment = CommentForm::from_apub(¬e, client, pool).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-07-29 11:46:11 +00:00
|
|
|
let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, client, pool)
|
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,
|
|
|
|
};
|
|
|
|
blocking(pool, move |conn| {
|
|
|
|
CommentLike::remove(conn, &like_form)?;
|
|
|
|
CommentLike::like(conn, &like_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let comment_view =
|
|
|
|
blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
|
|
|
|
|
|
|
|
// 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
|
|
|
};
|
|
|
|
|
|
|
|
chat_server.do_send(SendComment {
|
|
|
|
op: UserOperation::CreateCommentLike,
|
|
|
|
comment: res,
|
|
|
|
my_id: None,
|
|
|
|
});
|
|
|
|
|
|
|
|
announce_if_community_is_local(like, &user, client, pool).await?;
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|