1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
7 get_local_user_view_from_jwt,
12 activities::comment::create_or_update::{CreateOrUpdateComment, CreateOrUpdateType},
13 generate_apub_endpoint,
17 use lemmy_db_queries::{source::comment::Comment_, Crud, Likeable};
18 use lemmy_db_schema::source::comment::*;
19 use lemmy_db_views::comment_view::CommentView;
21 utils::{remove_slurs, scrape_text_for_mentions},
26 use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperationCrud};
28 #[async_trait::async_trait(?Send)]
29 impl PerformCrud for CreateComment {
30 type Response = CommentResponse;
34 context: &Data<LemmyContext>,
35 websocket_id: Option<ConnectionId>,
36 ) -> Result<CommentResponse, LemmyError> {
37 let data: &CreateComment = self;
38 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
40 let content_slurs_removed = remove_slurs(&data.content.to_owned());
42 // Check for a community ban
43 let post_id = data.post_id;
44 let post = get_post(post_id, context.pool()).await?;
46 check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?;
48 // Check if post is locked, no new comments
50 return Err(ApiError::err("locked").into());
53 // If there's a parent_id, check to make sure that comment is in that post
54 if let Some(parent_id) = data.parent_id {
55 // Make sure the parent comment exists
56 let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
58 .map_err(|_| ApiError::err("couldnt_create_comment"))?;
59 if parent.post_id != post_id {
60 return Err(ApiError::err("couldnt_create_comment").into());
64 let comment_form = CommentForm {
65 content: content_slurs_removed,
66 parent_id: data.parent_id.to_owned(),
67 post_id: data.post_id,
68 creator_id: local_user_view.person.id,
69 ..CommentForm::default()
73 let comment_form2 = comment_form.clone();
74 let inserted_comment = blocking(context.pool(), move |conn| {
75 Comment::create(conn, &comment_form2)
78 .map_err(|_| ApiError::err("couldnt_create_comment"))?;
80 // Necessary to update the ap_id
81 let inserted_comment_id = inserted_comment.id;
82 let updated_comment: Comment =
83 blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
85 generate_apub_endpoint(EndpointType::Comment, &inserted_comment_id.to_string())?;
86 Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
89 .map_err(|_| ApiError::err("couldnt_create_comment"))?;
91 CreateOrUpdateComment::send(
93 &local_user_view.person,
94 CreateOrUpdateType::Create,
99 // Scan the comment for user mentions, add those rows
100 let post_id = post.id;
101 let mentions = scrape_text_for_mentions(&comment_form.content);
102 let recipient_ids = send_local_notifs(
104 updated_comment.clone(),
105 local_user_view.person.clone(),
112 // You like your own comment by default
113 let like_form = CommentLikeForm {
114 comment_id: inserted_comment.id,
116 person_id: local_user_view.person.id,
120 let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
121 if blocking(context.pool(), like).await?.is_err() {
122 return Err(ApiError::err("couldnt_like_comment").into());
126 .send_like(&local_user_view.person, context)
129 let person_id = local_user_view.person.id;
130 let mut comment_view = blocking(context.pool(), move |conn| {
131 CommentView::read(conn, inserted_comment.id, Some(person_id))
135 // If its a comment to yourself, mark it as read
136 let comment_id = comment_view.comment.id;
137 if local_user_view.person.id == comment_view.get_recipient_id() {
138 blocking(context.pool(), move |conn| {
139 Comment::update_read(conn, comment_id, true)
142 .map_err(|_| ApiError::err("couldnt_update_comment"))?;
143 comment_view.comment.read = true;
146 let mut res = CommentResponse {
149 form_id: data.form_id.to_owned(),
152 context.chat_server().do_send(SendComment {
153 op: UserOperationCrud::CreateComment,
154 comment: res.clone(),
158 res.recipient_ids = Vec::new(); // Necessary to avoid doubles