Adding distinguish comment. Fixes #2002 (#2391)

* Adding distinguish comment. Fixes #2002

* Adding lemmy:distinguished
This commit is contained in:
Dessalines 2022-08-17 07:38:52 -04:00 committed by GitHub
parent 583ceb2506
commit 870abf8442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 55 additions and 11 deletions

View File

@ -24,8 +24,9 @@ pub struct GetComment {
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct EditComment { pub struct EditComment {
pub content: String,
pub comment_id: CommentId, pub comment_id: CommentId,
pub content: Option<String>,
pub distinguished: Option<bool>,
pub form_id: Option<String>, pub form_id: Option<String>,
pub auth: Sensitive<String>, pub auth: Sensitive<String>,
} }

View File

@ -7,6 +7,7 @@ use lemmy_api_common::{
check_community_deleted_or_removed, check_community_deleted_or_removed,
check_post_deleted_or_removed, check_post_deleted_or_removed,
get_local_user_view_from_jwt, get_local_user_view_from_jwt,
is_mod_or_admin,
}, },
}; };
use lemmy_apub::protocol::activities::{ use lemmy_apub::protocol::activities::{
@ -47,6 +48,7 @@ impl PerformCrud for EditComment {
CommentView::read(conn, comment_id, None) CommentView::read(conn, comment_id, None)
}) })
.await??; .await??;
let mut updated_comment = orig_comment.comment.clone();
// TODO is this necessary? It should really only need to check on create // TODO is this necessary? It should really only need to check on create
check_community_ban( check_community_ban(
@ -63,15 +65,32 @@ impl PerformCrud for EditComment {
return Err(LemmyError::from_message("no_comment_edit_allowed")); return Err(LemmyError::from_message("no_comment_edit_allowed"));
} }
// Do the update if let Some(distinguished) = data.distinguished {
let content_slurs_removed = // Verify that only a mod or admin can distinguish a comment
remove_slurs(&data.content.to_owned(), &context.settings().slur_regex()); is_mod_or_admin(
let comment_id = data.comment_id; context.pool(),
let updated_comment = blocking(context.pool(), move |conn| { local_user_view.person.id,
Comment::update_content(conn, comment_id, &content_slurs_removed) orig_comment.community.id,
}) )
.await? .await?;
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
updated_comment = blocking(context.pool(), move |conn| {
Comment::update_distinguished(conn, comment_id, distinguished)
})
.await?
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
}
// Update the Content
if let Some(content) = &data.content {
let content_slurs_removed = remove_slurs(content, &context.settings().slur_regex());
let comment_id = data.comment_id;
updated_comment = blocking(context.pool(), move |conn| {
Comment::update_content(conn, comment_id, &content_slurs_removed)
})
.await?
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
};
// Do the mentions / recipients // Do the mentions / recipients
let updated_comment_content = updated_comment.content.to_owned(); let updated_comment_content = updated_comment.content.to_owned();

View File

@ -16,6 +16,7 @@
"@type": "@id", "@type": "@id",
"@id": "lemmy:moderators" "@id": "lemmy:moderators"
}, },
"expires": "as:endTime" "expires": "as:endTime",
"distinguished": "lemmy:distinguished"
} }
] ]

View File

@ -21,6 +21,7 @@
"name": "@picard@enterprise.lemmy.ml" "name": "@picard@enterprise.lemmy.ml"
} }
], ],
"distinguished": false,
"published": "2021-03-01T13:42:43.966208+00:00", "published": "2021-03-01T13:42:43.966208+00:00",
"updated": "2021-03-01T13:43:03.955787+00:00" "updated": "2021-03-01T13:43:03.955787+00:00"
} }

View File

@ -121,6 +121,7 @@ impl ApubObject for ApubComment {
published: Some(convert_datetime(self.published)), published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime), updated: self.updated.map(convert_datetime),
tag: maa.tags, tag: maa.tags,
distinguished: Some(self.distinguished),
}; };
Ok(note) Ok(note)
@ -184,6 +185,7 @@ impl ApubObject for ApubComment {
updated: note.updated.map(|u| u.naive_local()), updated: note.updated.map(|u| u.naive_local()),
deleted: None, deleted: None,
ap_id: Some(note.id.into()), ap_id: Some(note.id.into()),
distinguished: note.distinguished,
local: Some(false), local: Some(false),
}; };
let parent_comment_path = parent_comment.map(|t| t.0.path); let parent_comment_path = parent_comment.map(|t| t.0.path);

View File

@ -44,6 +44,8 @@ pub struct Note {
pub(crate) updated: Option<DateTime<FixedOffset>>, pub(crate) updated: Option<DateTime<FixedOffset>>,
#[serde(default)] #[serde(default)]
pub(crate) tag: Vec<MentionOrValue>, pub(crate) tag: Vec<MentionOrValue>,
// lemmy extension
pub(crate) distinguished: Option<bool>,
} }
impl Note { impl Note {

View File

@ -86,6 +86,17 @@ impl Comment {
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn update_distinguished(
conn: &PgConnection,
comment_id: CommentId,
new_distinguished: bool,
) -> Result<Self, Error> {
use crate::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set((distinguished.eq(new_distinguished), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
pub fn create( pub fn create(
conn: &PgConnection, conn: &PgConnection,
comment_form: &CommentForm, comment_form: &CommentForm,
@ -330,6 +341,7 @@ mod tests {
published: inserted_comment.published, published: inserted_comment.published,
updated: None, updated: None,
ap_id: inserted_comment.ap_id.to_owned(), ap_id: inserted_comment.ap_id.to_owned(),
distinguished: false,
local: true, local: true,
}; };

View File

@ -26,6 +26,7 @@ table! {
ap_id -> Varchar, ap_id -> Varchar,
local -> Bool, local -> Bool,
path -> Ltree, path -> Ltree,
distinguished -> Bool,
} }
} }

View File

@ -22,6 +22,7 @@ pub struct Comment {
pub local: bool, pub local: bool,
#[serde(with = "LtreeDef")] #[serde(with = "LtreeDef")]
pub path: Ltree, pub path: Ltree,
pub distinguished: bool,
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
@ -37,6 +38,7 @@ pub struct CommentForm {
pub deleted: Option<bool>, pub deleted: Option<bool>,
pub ap_id: Option<DbUrl>, pub ap_id: Option<DbUrl>,
pub local: Option<bool>, pub local: Option<bool>,
pub distinguished: Option<bool>,
} }
#[derive(PartialEq, Debug, Clone)] #[derive(PartialEq, Debug, Clone)]

View File

@ -531,6 +531,7 @@ mod tests {
ap_id: inserted_comment_0.ap_id, ap_id: inserted_comment_0.ap_id,
updated: None, updated: None,
local: true, local: true,
distinguished: false,
path: top_path, path: top_path,
}, },
creator: PersonSafe { creator: PersonSafe {

View File

@ -0,0 +1 @@
alter table comment drop column distinguished;

View File

@ -0,0 +1 @@
alter table comment add column distinguished boolean not null default false;