From: Dessalines Date: Sat, 18 Feb 2023 14:46:34 +0000 (-0500) Subject: Separate comment distinguish (#2740) X-Git-Url: http://these/git/readmes/README.ja.md?a=commitdiff_plain;h=1917e3d4955680b9a4b062edbc16efa4d8355d20;p=lemmy.git Separate comment distinguish (#2740) * Combine prod and dev docker setups using build-arg - Fixes #2603 * Dont use cache for release build. * Separate comment distinguish into its own action. - Fixes #2708 --------- Co-authored-by: Nutomic --- diff --git a/crates/api/src/comment/distinguish.rs b/crates/api/src/comment/distinguish.rs new file mode 100644 index 00000000..cf51107f --- /dev/null +++ b/crates/api/src/comment/distinguish.rs @@ -0,0 +1,66 @@ +use crate::Perform; +use actix_web::web::Data; +use lemmy_api_common::{ + comment::{CommentResponse, DistinguishComment}, + context::LemmyContext, + utils::{check_community_ban, get_local_user_view_from_jwt, is_mod_or_admin}, +}; +use lemmy_db_schema::{ + source::comment::{Comment, CommentUpdateForm}, + traits::Crud, +}; +use lemmy_db_views::structs::CommentView; +use lemmy_utils::{error::LemmyError, ConnectionId}; + +#[async_trait::async_trait(?Send)] +impl Perform for DistinguishComment { + type Response = CommentResponse; + + #[tracing::instrument(skip(context, _websocket_id))] + async fn perform( + &self, + context: &Data, + _websocket_id: Option, + ) -> Result { + let data: &DistinguishComment = self; + let local_user_view = + get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + + let comment_id = data.comment_id; + let orig_comment = CommentView::read(context.pool(), comment_id, None).await?; + + check_community_ban( + local_user_view.person.id, + orig_comment.community.id, + context.pool(), + ) + .await?; + + // Verify that only a mod or admin can distinguish a comment + is_mod_or_admin( + context.pool(), + local_user_view.person.id, + orig_comment.community.id, + ) + .await?; + + // Update the Comment + let comment_id = data.comment_id; + let form = CommentUpdateForm::builder() + .distinguished(Some(data.distinguished)) + .build(); + Comment::update(context.pool(), comment_id, &form) + .await + .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?; + + let comment_id = data.comment_id; + let person_id = local_user_view.person.id; + let comment_view = CommentView::read(context.pool(), comment_id, Some(person_id)).await?; + + Ok(CommentResponse { + comment_view, + recipient_ids: Vec::new(), + form_id: None, + }) + } +} diff --git a/crates/api/src/comment/mod.rs b/crates/api/src/comment/mod.rs index 73d7c0b5..27584c36 100644 --- a/crates/api/src/comment/mod.rs +++ b/crates/api/src/comment/mod.rs @@ -1,2 +1,3 @@ +mod distinguish; mod like; mod save; diff --git a/crates/api_common/src/comment.rs b/crates/api_common/src/comment.rs index ea83f8aa..4f0023de 100644 --- a/crates/api_common/src/comment.rs +++ b/crates/api_common/src/comment.rs @@ -27,12 +27,18 @@ pub struct GetComment { pub struct EditComment { pub comment_id: CommentId, pub content: Option, - pub distinguished: Option, pub language_id: Option, pub form_id: Option, pub auth: Sensitive, } +#[derive(Debug, Serialize, Deserialize, Clone, Default)] +pub struct DistinguishComment { + pub comment_id: CommentId, + pub distinguished: bool, + pub auth: Sensitive, +} + #[derive(Debug, Serialize, Deserialize, Clone, Default)] pub struct DeleteComment { pub comment_id: CommentId, diff --git a/crates/api_common/src/websocket/mod.rs b/crates/api_common/src/websocket/mod.rs index 2af6cff0..76f17ae9 100644 --- a/crates/api_common/src/websocket/mod.rs +++ b/crates/api_common/src/websocket/mod.rs @@ -33,6 +33,7 @@ pub enum UserOperation { GetCaptcha, SaveComment, CreateCommentLike, + DistinguishComment, CreateCommentReport, ResolveCommentReport, ListCommentReports, diff --git a/crates/api_crud/src/comment/update.rs b/crates/api_crud/src/comment/update.rs index f75de78b..06536330 100644 --- a/crates/api_crud/src/comment/update.rs +++ b/crates/api_crud/src/comment/update.rs @@ -3,12 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ comment::{CommentResponse, EditComment}, context::LemmyContext, - utils::{ - check_community_ban, - get_local_user_view_from_jwt, - is_mod_or_admin, - local_site_to_slur_regex, - }, + utils::{check_community_ban, get_local_user_view_from_jwt, local_site_to_slur_regex}, websocket::{ send::{send_comment_ws_message, send_local_notifs}, UserOperationCrud, @@ -60,16 +55,6 @@ impl PerformCrud for EditComment { return Err(LemmyError::from_message("no_comment_edit_allowed")); } - if data.distinguished.is_some() { - // Verify that only a mod or admin can distinguish a comment - is_mod_or_admin( - context.pool(), - local_user_view.person.id, - orig_comment.community.id, - ) - .await?; - } - let language_id = self.language_id; CommunityLanguage::is_allowed_community_language( context.pool(), @@ -86,7 +71,6 @@ impl PerformCrud for EditComment { let comment_id = data.comment_id; let form = CommentUpdateForm::builder() .content(content_slurs_removed) - .distinguished(data.distinguished) .language_id(data.language_id) .updated(Some(Some(naive_now()))) .build(); diff --git a/crates/apub/src/activities/unfederated.rs b/crates/apub/src/activities/unfederated.rs index cca3340e..6cb4d44d 100644 --- a/crates/apub/src/activities/unfederated.rs +++ b/crates/apub/src/activities/unfederated.rs @@ -3,6 +3,7 @@ use lemmy_api_common::{ comment::{ CommentReportResponse, CommentResponse, + DistinguishComment, GetComment, GetComments, GetCommentsResponse, @@ -342,6 +343,10 @@ impl SendActivity for SaveComment { type Response = CommentResponse; } +impl SendActivity for DistinguishComment { + type Response = CommentResponse; +} + impl SendActivity for ListCommentReports { type Response = ListCommentReportsResponse; } diff --git a/src/api_routes_http.rs b/src/api_routes_http.rs index ce7ce68b..882efc8c 100644 --- a/src/api_routes_http.rs +++ b/src/api_routes_http.rs @@ -7,6 +7,7 @@ use lemmy_api_common::{ CreateCommentLike, CreateCommentReport, DeleteComment, + DistinguishComment, EditComment, GetComment, GetComments, @@ -218,6 +219,10 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) { "/mark_as_read", web::post().to(route_post::), ) + .route( + "/distinguish", + web::post().to(route_post::), + ) .route("/like", web::post().to(route_post::)) .route("/save", web::put().to(route_post::)) .route("/list", web::get().to(route_get_apub::)) diff --git a/src/api_routes_websocket.rs b/src/api_routes_websocket.rs index b2581838..407bbde3 100644 --- a/src/api_routes_websocket.rs +++ b/src/api_routes_websocket.rs @@ -9,6 +9,7 @@ use lemmy_api_common::{ CreateCommentLike, CreateCommentReport, DeleteComment, + DistinguishComment, EditComment, GetComment, GetComments, @@ -590,6 +591,9 @@ pub async fn match_websocket_operation( UserOperation::CreateCommentLike => { do_websocket_operation::(context, id, op, data).await } + UserOperation::DistinguishComment => { + do_websocket_operation::(context, id, op, data).await + } UserOperation::CreateCommentReport => { do_websocket_operation::(context, id, op, data).await }