2021-11-06 12:37:55 +00:00
|
|
|
use crate::PerformCrud;
|
2021-03-25 19:19:40 +00:00
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
|
|
|
blocking,
|
|
|
|
check_community_ban,
|
2021-10-14 16:33:19 +00:00
|
|
|
check_community_deleted_or_removed,
|
2021-08-19 20:54:15 +00:00
|
|
|
check_person_block,
|
2021-10-14 16:33:19 +00:00
|
|
|
check_post_deleted_or_removed,
|
2021-03-25 19:19:40 +00:00
|
|
|
comment::*,
|
|
|
|
get_local_user_view_from_jwt,
|
|
|
|
get_post,
|
|
|
|
};
|
2021-07-31 14:57:37 +00:00
|
|
|
use lemmy_apub::{
|
2021-10-29 10:32:42 +00:00
|
|
|
fetcher::post_or_comment::PostOrComment,
|
|
|
|
generate_local_apub_endpoint,
|
2021-11-06 12:37:55 +00:00
|
|
|
objects::comment::ApubComment,
|
2021-10-29 10:32:42 +00:00
|
|
|
protocol::activities::{
|
|
|
|
create_or_update::comment::CreateOrUpdateComment,
|
2021-08-02 20:33:40 +00:00
|
|
|
voting::vote::{Vote, VoteType},
|
|
|
|
CreateOrUpdateType,
|
|
|
|
},
|
2021-07-31 14:57:37 +00:00
|
|
|
EndpointType,
|
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
|
|
|
|
person_mention::PersonMention,
|
|
|
|
},
|
|
|
|
traits::{Crud, Likeable},
|
2021-10-08 14:28:32 +00:00
|
|
|
};
|
2021-09-04 12:47:41 +00:00
|
|
|
use lemmy_db_views::comment_view::CommentView;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_utils::{
|
|
|
|
utils::{remove_slurs, scrape_text_for_mentions},
|
|
|
|
ApiError,
|
|
|
|
ConnectionId,
|
|
|
|
LemmyError,
|
|
|
|
};
|
2021-10-18 21:36:44 +00:00
|
|
|
use lemmy_websocket::{
|
|
|
|
send::{send_comment_ws_message, send_local_notifs},
|
|
|
|
LemmyContext,
|
|
|
|
UserOperationCrud,
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for CreateComment {
|
|
|
|
type Response = CommentResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<CommentResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &CreateComment = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
let content_slurs_removed =
|
|
|
|
remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Check for a community ban
|
|
|
|
let post_id = data.post_id;
|
|
|
|
let post = get_post(post_id, context.pool()).await?;
|
2021-08-02 20:33:40 +00:00
|
|
|
let community_id = post.community_id;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-08-02 20:33:40 +00:00
|
|
|
check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
|
2021-10-14 16:33:19 +00:00
|
|
|
check_community_deleted_or_removed(community_id, context.pool()).await?;
|
|
|
|
check_post_deleted_or_removed(&post)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-08-19 20:54:15 +00:00
|
|
|
check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
// Check if post is locked, no new comments
|
|
|
|
if post.locked {
|
2021-10-13 19:50:21 +00:00
|
|
|
return Err(ApiError::err_plain("locked").into());
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there's a parent_id, check to make sure that comment is in that post
|
|
|
|
if let Some(parent_id) = data.parent_id {
|
|
|
|
// Make sure the parent comment exists
|
2021-07-05 16:07:26 +00:00
|
|
|
let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
|
2021-04-16 13:10:43 +00:00
|
|
|
.await?
|
2021-10-13 19:50:21 +00:00
|
|
|
.map_err(|e| ApiError::err("couldnt_create_comment", e))?;
|
2021-08-19 20:54:15 +00:00
|
|
|
|
|
|
|
check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
|
|
|
|
|
|
|
|
// Strange issue where sometimes the post ID is incorrect
|
2021-03-25 19:19:40 +00:00
|
|
|
if parent.post_id != post_id {
|
2021-10-13 19:50:21 +00:00
|
|
|
return Err(ApiError::err_plain("couldnt_create_comment").into());
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let comment_form = CommentForm {
|
|
|
|
content: content_slurs_removed,
|
|
|
|
parent_id: data.parent_id.to_owned(),
|
|
|
|
post_id: data.post_id,
|
|
|
|
creator_id: local_user_view.person.id,
|
2021-03-29 20:24:50 +00:00
|
|
|
..CommentForm::default()
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Create the comment
|
|
|
|
let comment_form2 = comment_form.clone();
|
2021-04-16 13:10:43 +00:00
|
|
|
let inserted_comment = blocking(context.pool(), move |conn| {
|
2021-07-05 16:07:26 +00:00
|
|
|
Comment::create(conn, &comment_form2)
|
2021-03-25 19:19:40 +00:00
|
|
|
})
|
|
|
|
.await?
|
2021-10-13 19:50:21 +00:00
|
|
|
.map_err(|e| ApiError::err("couldnt_create_comment", e))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Necessary to update the ap_id
|
|
|
|
let inserted_comment_id = inserted_comment.id;
|
2021-09-22 15:57:09 +00:00
|
|
|
let protocol_and_hostname = context.settings().get_protocol_and_hostname();
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
let updated_comment: Comment =
|
2021-04-16 13:10:43 +00:00
|
|
|
blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
|
2021-10-25 16:09:21 +00:00
|
|
|
let apub_id = generate_local_apub_endpoint(
|
2021-09-22 15:57:09 +00:00
|
|
|
EndpointType::Comment,
|
|
|
|
&inserted_comment_id.to_string(),
|
|
|
|
&protocol_and_hostname,
|
|
|
|
)?;
|
2021-07-05 16:07:26 +00:00
|
|
|
Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
|
2021-03-25 19:19:40 +00:00
|
|
|
})
|
|
|
|
.await?
|
2021-10-13 19:50:21 +00:00
|
|
|
.map_err(|e| ApiError::err("couldnt_create_comment", e))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Scan the comment for user mentions, add those rows
|
|
|
|
let post_id = post.id;
|
|
|
|
let mentions = scrape_text_for_mentions(&comment_form.content);
|
|
|
|
let recipient_ids = send_local_notifs(
|
|
|
|
mentions,
|
2021-10-18 21:36:44 +00:00
|
|
|
&updated_comment,
|
|
|
|
&local_user_view.person,
|
|
|
|
&post,
|
2021-03-25 19:19:40 +00:00
|
|
|
true,
|
2021-10-18 21:36:44 +00:00
|
|
|
context,
|
2021-03-25 19:19:40 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// You like your own comment by default
|
|
|
|
let like_form = CommentLikeForm {
|
|
|
|
comment_id: inserted_comment.id,
|
|
|
|
post_id,
|
|
|
|
person_id: local_user_view.person.id,
|
|
|
|
score: 1,
|
|
|
|
};
|
|
|
|
|
2021-07-05 16:07:26 +00:00
|
|
|
let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
|
2021-10-13 19:50:21 +00:00
|
|
|
blocking(context.pool(), like)
|
|
|
|
.await?
|
|
|
|
.map_err(|e| ApiError::err("couldnt_like_comment", e))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-11-06 12:37:55 +00:00
|
|
|
let apub_comment: ApubComment = updated_comment.into();
|
|
|
|
CreateOrUpdateComment::send(
|
|
|
|
apub_comment.clone(),
|
|
|
|
&local_user_view.person.clone().into(),
|
|
|
|
CreateOrUpdateType::Create,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
let object = PostOrComment::Comment(apub_comment);
|
2021-08-02 20:33:40 +00:00
|
|
|
Vote::send(
|
|
|
|
&object,
|
2021-10-18 21:36:44 +00:00
|
|
|
&local_user_view.person.clone().into(),
|
2021-08-02 20:33:40 +00:00
|
|
|
community_id,
|
|
|
|
VoteType::Like,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-09-04 12:47:41 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
let comment_id = inserted_comment.id;
|
|
|
|
let comment_view = blocking(context.pool(), move |conn| {
|
|
|
|
CommentView::read(conn, comment_id, Some(person_id))
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
// If its a comment to yourself, mark it as read
|
2021-09-04 12:47:41 +00:00
|
|
|
if local_user_view.person.id == comment_view.get_recipient_id() {
|
2021-08-17 18:04:58 +00:00
|
|
|
let comment_id = inserted_comment.id;
|
2021-04-16 13:10:43 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
Comment::update_read(conn, comment_id, true)
|
|
|
|
})
|
|
|
|
.await?
|
2021-10-13 19:50:21 +00:00
|
|
|
.map_err(|e| ApiError::err("couldnt_update_comment", e))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
2021-10-08 14:28:32 +00:00
|
|
|
// If its a reply, mark the parent as read
|
|
|
|
if let Some(parent_id) = data.parent_id {
|
|
|
|
let parent_comment = blocking(context.pool(), move |conn| {
|
|
|
|
CommentView::read(conn, parent_id, Some(person_id))
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
if local_user_view.person.id == parent_comment.get_recipient_id() {
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
Comment::update_read(conn, parent_id, true)
|
|
|
|
})
|
|
|
|
.await?
|
2021-10-13 19:50:21 +00:00
|
|
|
.map_err(|e| ApiError::err("couldnt_update_parent_comment", e))?;
|
2021-10-08 14:28:32 +00:00
|
|
|
}
|
|
|
|
// If the parent has PersonMentions mark them as read too
|
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
let person_mention = blocking(context.pool(), move |conn| {
|
|
|
|
PersonMention::read_by_comment_and_person(conn, parent_id, person_id)
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
if let Ok(mention) = person_mention {
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
PersonMention::update_read(conn, mention.id, true)
|
|
|
|
})
|
|
|
|
.await?
|
2021-10-13 19:50:21 +00:00
|
|
|
.map_err(|e| ApiError::err("couldnt_update_person_mentions", e))?;
|
2021-10-08 14:28:32 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
send_comment_ws_message(
|
|
|
|
inserted_comment.id,
|
|
|
|
UserOperationCrud::CreateComment,
|
2021-03-25 19:19:40 +00:00
|
|
|
websocket_id,
|
2021-08-17 18:04:58 +00:00
|
|
|
data.form_id.to_owned(),
|
|
|
|
Some(local_user_view.person.id),
|
|
|
|
recipient_ids,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
}
|