]> Untitled Git - lemmy.git/commitdiff
Separate comment distinguish (#2740)
authorDessalines <dessalines@users.noreply.github.com>
Sat, 18 Feb 2023 14:46:34 +0000 (09:46 -0500)
committerGitHub <noreply@github.com>
Sat, 18 Feb 2023 14:46:34 +0000 (09:46 -0500)
* 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 <me@nutomic.com>
crates/api/src/comment/distinguish.rs [new file with mode: 0644]
crates/api/src/comment/mod.rs
crates/api_common/src/comment.rs
crates/api_common/src/websocket/mod.rs
crates/api_crud/src/comment/update.rs
crates/apub/src/activities/unfederated.rs
src/api_routes_http.rs
src/api_routes_websocket.rs

diff --git a/crates/api/src/comment/distinguish.rs b/crates/api/src/comment/distinguish.rs
new file mode 100644 (file)
index 0000000..cf51107
--- /dev/null
@@ -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<LemmyContext>,
+    _websocket_id: Option<ConnectionId>,
+  ) -> Result<CommentResponse, LemmyError> {
+    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,
+    })
+  }
+}
index 73d7c0b53b08667bf5c037a22498b3dec3eadc65..27584c3604e8ff17eebc3cfbe43a094dc1e65b30 100644 (file)
@@ -1,2 +1,3 @@
+mod distinguish;
 mod like;
 mod save;
index ea83f8aa1fa4df4553f4c523dacabed3af302111..4f0023de08854bb25e6c69d436366041b4ff74f5 100644 (file)
@@ -27,12 +27,18 @@ pub struct GetComment {
 pub struct EditComment {
   pub comment_id: CommentId,
   pub content: Option<String>,
-  pub distinguished: Option<bool>,
   pub language_id: Option<LanguageId>,
   pub form_id: Option<String>,
   pub auth: Sensitive<String>,
 }
 
+#[derive(Debug, Serialize, Deserialize, Clone, Default)]
+pub struct DistinguishComment {
+  pub comment_id: CommentId,
+  pub distinguished: bool,
+  pub auth: Sensitive<String>,
+}
+
 #[derive(Debug, Serialize, Deserialize, Clone, Default)]
 pub struct DeleteComment {
   pub comment_id: CommentId,
index 2af6cff06e9a94bbaab72442e727ebeafaeb1afe..76f17ae9ff7af8c4b2bdab1b099bc7fbe524da30 100644 (file)
@@ -33,6 +33,7 @@ pub enum UserOperation {
   GetCaptcha,
   SaveComment,
   CreateCommentLike,
+  DistinguishComment,
   CreateCommentReport,
   ResolveCommentReport,
   ListCommentReports,
index f75de78bac95bc5f9f9e6e2eb0c476b4bcf157f2..06536330f30146898f6212cc67f59631e8e52ff1 100644 (file)
@@ -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();
index cca3340ed1013398c711359787d0b0d039e3c189..6cb4d44d90399c09917669333026c9fae1fd96bc 100644 (file)
@@ -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;
 }
index ce7ce68bb93b8111c2f8eb2d0b46b9ba5ddd3d65..882efc8ce477e0d0952d75c5bef45f0e27c85393 100644 (file)
@@ -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::<MarkCommentReplyAsRead>),
           )
+          .route(
+            "/distinguish",
+            web::post().to(route_post::<DistinguishComment>),
+          )
           .route("/like", web::post().to(route_post::<CreateCommentLike>))
           .route("/save", web::put().to(route_post::<SaveComment>))
           .route("/list", web::get().to(route_get_apub::<GetComments>))
index b258183827d341ea4593c50224fbfe57cda275aa..407bbde3c9a49f79d4be7fb197edcb125bdd5454 100644 (file)
@@ -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::<CreateCommentLike>(context, id, op, data).await
     }
+    UserOperation::DistinguishComment => {
+      do_websocket_operation::<DistinguishComment>(context, id, op, data).await
+    }
     UserOperation::CreateCommentReport => {
       do_websocket_operation::<CreateCommentReport>(context, id, op, data).await
     }