1 use actix_web::web::Data;
3 use lemmy_api_common::{
6 check_community_deleted_or_removed,
8 check_post_deleted_or_removed,
10 get_local_user_view_from_jwt,
14 fetcher::post_or_comment::PostOrComment,
15 generate_local_apub_endpoint,
16 protocol::activities::{
17 create_or_update::comment::CreateOrUpdateComment,
18 voting::vote::{Vote, VoteType},
23 use lemmy_db_schema::{
25 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
26 person_mention::PersonMention,
28 traits::{Crud, Likeable},
30 use lemmy_db_views::comment_view::CommentView;
32 utils::{remove_slurs, scrape_text_for_mentions},
37 use lemmy_websocket::{
38 send::{send_comment_ws_message, send_local_notifs},
43 use crate::PerformCrud;
45 #[async_trait::async_trait(?Send)]
46 impl PerformCrud for CreateComment {
47 type Response = CommentResponse;
51 context: &Data<LemmyContext>,
52 websocket_id: Option<ConnectionId>,
53 ) -> Result<CommentResponse, LemmyError> {
54 let data: &CreateComment = self;
56 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
58 let content_slurs_removed =
59 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
61 // Check for a community ban
62 let post_id = data.post_id;
63 let post = get_post(post_id, context.pool()).await?;
64 let community_id = post.community_id;
66 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
67 check_community_deleted_or_removed(community_id, context.pool()).await?;
68 check_post_deleted_or_removed(&post)?;
70 check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
72 // Check if post is locked, no new comments
74 return Err(ApiError::err_plain("locked").into());
77 // If there's a parent_id, check to make sure that comment is in that post
78 if let Some(parent_id) = data.parent_id {
79 // Make sure the parent comment exists
80 let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
82 .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
84 check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
86 // Strange issue where sometimes the post ID is incorrect
87 if parent.post_id != post_id {
88 return Err(ApiError::err_plain("couldnt_create_comment").into());
92 let comment_form = CommentForm {
93 content: content_slurs_removed,
94 parent_id: data.parent_id.to_owned(),
95 post_id: data.post_id,
96 creator_id: local_user_view.person.id,
97 ..CommentForm::default()
100 // Create the comment
101 let comment_form2 = comment_form.clone();
102 let inserted_comment = blocking(context.pool(), move |conn| {
103 Comment::create(conn, &comment_form2)
106 .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
108 // Necessary to update the ap_id
109 let inserted_comment_id = inserted_comment.id;
110 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
112 let updated_comment: Comment =
113 blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
114 let apub_id = generate_local_apub_endpoint(
115 EndpointType::Comment,
116 &inserted_comment_id.to_string(),
117 &protocol_and_hostname,
119 Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
122 .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
124 CreateOrUpdateComment::send(
125 &updated_comment.clone().into(),
126 &local_user_view.person.clone().into(),
127 CreateOrUpdateType::Create,
132 // Scan the comment for user mentions, add those rows
133 let post_id = post.id;
134 let mentions = scrape_text_for_mentions(&comment_form.content);
135 let recipient_ids = send_local_notifs(
138 &local_user_view.person,
145 // You like your own comment by default
146 let like_form = CommentLikeForm {
147 comment_id: inserted_comment.id,
149 person_id: local_user_view.person.id,
153 let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
154 blocking(context.pool(), like)
156 .map_err(|e| ApiError::err("couldnt_like_comment", e))?;
158 let object = PostOrComment::Comment(updated_comment.into());
161 &local_user_view.person.clone().into(),
168 let person_id = local_user_view.person.id;
169 let comment_id = inserted_comment.id;
170 let comment_view = blocking(context.pool(), move |conn| {
171 CommentView::read(conn, comment_id, Some(person_id))
175 // If its a comment to yourself, mark it as read
176 if local_user_view.person.id == comment_view.get_recipient_id() {
177 let comment_id = inserted_comment.id;
178 blocking(context.pool(), move |conn| {
179 Comment::update_read(conn, comment_id, true)
182 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
184 // If its a reply, mark the parent as read
185 if let Some(parent_id) = data.parent_id {
186 let parent_comment = blocking(context.pool(), move |conn| {
187 CommentView::read(conn, parent_id, Some(person_id))
190 if local_user_view.person.id == parent_comment.get_recipient_id() {
191 blocking(context.pool(), move |conn| {
192 Comment::update_read(conn, parent_id, true)
195 .map_err(|e| ApiError::err("couldnt_update_parent_comment", e))?;
197 // If the parent has PersonMentions mark them as read too
198 let person_id = local_user_view.person.id;
199 let person_mention = blocking(context.pool(), move |conn| {
200 PersonMention::read_by_comment_and_person(conn, parent_id, person_id)
203 if let Ok(mention) = person_mention {
204 blocking(context.pool(), move |conn| {
205 PersonMention::update_read(conn, mention.id, true)
208 .map_err(|e| ApiError::err("couldnt_update_person_mentions", e))?;
212 send_comment_ws_message(
214 UserOperationCrud::CreateComment,
216 data.form_id.to_owned(),
217 Some(local_user_view.person.id),