2020-10-12 14:10:09 +00:00
|
|
|
use crate::{
|
2020-10-28 16:14:18 +00:00
|
|
|
activities::receive::get_actor_as_user,
|
2020-10-12 14:10:09 +00:00
|
|
|
fetcher::get_or_fetch_and_insert_comment,
|
|
|
|
FromApub,
|
2020-11-24 17:53:43 +00:00
|
|
|
NoteExt,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2020-11-24 17:53:43 +00:00
|
|
|
use activitystreams::{activity::*, prelude::*};
|
2020-10-12 14:10:09 +00:00
|
|
|
use anyhow::Context;
|
|
|
|
use lemmy_db::{
|
|
|
|
comment::{Comment, CommentForm, CommentLike},
|
|
|
|
comment_view::CommentView,
|
|
|
|
Likeable,
|
|
|
|
};
|
|
|
|
use lemmy_structs::{blocking, comment::CommentResponse};
|
|
|
|
use lemmy_utils::{location_info, LemmyError};
|
|
|
|
use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation};
|
|
|
|
|
|
|
|
pub(crate) async fn receive_undo_like_comment(
|
|
|
|
like: &Like,
|
|
|
|
context: &LemmyContext,
|
2020-10-22 18:27:32 +00:00
|
|
|
request_counter: &mut i32,
|
2020-10-28 16:14:18 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-10-22 18:27:32 +00:00
|
|
|
let user = get_actor_as_user(like, context, request_counter).await?;
|
2020-11-24 17:53:43 +00:00
|
|
|
let note = NoteExt::from_any_base(like.object().to_owned().one().context(location_info!())?)?
|
2020-10-12 14:10:09 +00:00
|
|
|
.context(location_info!())?;
|
|
|
|
|
2020-10-22 18:27:32 +00:00
|
|
|
let comment = CommentForm::from_apub(¬e, context, None, request_counter).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-10-22 18:27:32 +00:00
|
|
|
let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context, request_counter)
|
2020-10-12 14:10:09 +00:00
|
|
|
.await?
|
|
|
|
.id;
|
|
|
|
|
|
|
|
let user_id = user.id;
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
CommentLike::remove(conn, user_id, comment_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let comment_view = blocking(context.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,
|
|
|
|
form_id: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
context.chat_server().do_send(SendComment {
|
|
|
|
op: UserOperation::CreateCommentLike,
|
|
|
|
comment: res,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn receive_undo_dislike_comment(
|
|
|
|
dislike: &Dislike,
|
|
|
|
context: &LemmyContext,
|
2020-10-22 18:27:32 +00:00
|
|
|
request_counter: &mut i32,
|
2020-10-28 16:14:18 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-10-22 18:27:32 +00:00
|
|
|
let user = get_actor_as_user(dislike, context, request_counter).await?;
|
2020-11-24 17:53:43 +00:00
|
|
|
let note = NoteExt::from_any_base(
|
2020-10-12 14:10:09 +00:00
|
|
|
dislike
|
|
|
|
.object()
|
|
|
|
.to_owned()
|
|
|
|
.one()
|
|
|
|
.context(location_info!())?,
|
|
|
|
)?
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
2020-10-22 18:27:32 +00:00
|
|
|
let comment = CommentForm::from_apub(¬e, context, None, request_counter).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-10-22 18:27:32 +00:00
|
|
|
let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context, request_counter)
|
2020-10-12 14:10:09 +00:00
|
|
|
.await?
|
|
|
|
.id;
|
|
|
|
|
|
|
|
let user_id = user.id;
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
CommentLike::remove(conn, user_id, comment_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let comment_view = blocking(context.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,
|
|
|
|
form_id: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
context.chat_server().do_send(SendComment {
|
|
|
|
op: UserOperation::CreateCommentLike,
|
|
|
|
comment: res,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn receive_undo_delete_comment(
|
|
|
|
context: &LemmyContext,
|
2020-10-13 12:09:49 +00:00
|
|
|
comment: Comment,
|
2020-10-28 16:14:18 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-10-13 12:09:49 +00:00
|
|
|
let deleted_comment = blocking(context.pool(), move |conn| {
|
|
|
|
Comment::update_deleted(conn, comment.id, false)
|
2020-10-12 14:10:09 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
2020-10-13 12:09:49 +00:00
|
|
|
let comment_id = deleted_comment.id;
|
2020-10-12 14:10:09 +00:00
|
|
|
let comment_view = blocking(context.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,
|
|
|
|
form_id: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
context.chat_server().do_send(SendComment {
|
|
|
|
op: UserOperation::EditComment,
|
|
|
|
comment: res,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn receive_undo_remove_comment(
|
|
|
|
context: &LemmyContext,
|
2020-10-13 12:09:49 +00:00
|
|
|
comment: Comment,
|
2020-10-28 16:14:18 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-10-13 12:09:49 +00:00
|
|
|
let removed_comment = blocking(context.pool(), move |conn| {
|
|
|
|
Comment::update_removed(conn, comment.id, false)
|
2020-10-12 14:10:09 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
2020-10-13 12:09:49 +00:00
|
|
|
let comment_id = removed_comment.id;
|
2020-10-12 14:10:09 +00:00
|
|
|
let comment_view = blocking(context.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,
|
|
|
|
form_id: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
context.chat_server().do_send(SendComment {
|
|
|
|
op: UserOperation::EditComment,
|
|
|
|
comment: res,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|