Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding distinguish comment. Fixes #2002 #2391

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/api_common/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ pub struct GetComment {

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct EditComment {
pub content: String,
pub comment_id: CommentId,
pub content: Option<String>,
pub distinguished: Option<bool>,
pub form_id: Option<String>,
pub auth: Sensitive<String>,
}
Expand Down
37 changes: 28 additions & 9 deletions crates/api_crud/src/comment/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use lemmy_api_common::{
check_community_deleted_or_removed,
check_post_deleted_or_removed,
get_local_user_view_from_jwt,
is_mod_or_admin,
},
};
use lemmy_apub::protocol::activities::{
Expand Down Expand Up @@ -47,6 +48,7 @@ impl PerformCrud for EditComment {
CommentView::read(conn, comment_id, None)
})
.await??;
let mut updated_comment = orig_comment.comment.clone();

// TODO is this necessary? It should really only need to check on create
check_community_ban(
Expand All @@ -63,15 +65,32 @@ impl PerformCrud for EditComment {
return Err(LemmyError::from_message("no_comment_edit_allowed"));
}

// Do the update
let content_slurs_removed =
remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
let comment_id = data.comment_id;
let 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"))?;
if let Some(distinguished) = data.distinguished {
// 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?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this means that admins/mods can distuingish any comment by another user? Then there should be some indication who did it.

But actually, i think that would make it less of a mod tool, and more like giving gold on reddit. Imo it would be better if admins/mods could only distinguish their own comments (when they are giving a warning or similar).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, there's a check right above this one that makes sure they can only do these actions on their own comments.


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
let updated_comment_content = updated_comment.content.to_owned();
Expand Down
3 changes: 2 additions & 1 deletion crates/apub/assets/lemmy/context.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@type": "@id",
"@id": "lemmy:moderators"
},
"expires": "as:endTime"
"expires": "as:endTime",
"distinguished": "lemmy:distinguished"
}
]
1 change: 1 addition & 0 deletions crates/apub/assets/lemmy/objects/note.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"name": "@picard@enterprise.lemmy.ml"
}
],
"distinguished": false,
"published": "2021-03-01T13:42:43.966208+00:00",
"updated": "2021-03-01T13:43:03.955787+00:00"
}
2 changes: 2 additions & 0 deletions crates/apub/src/objects/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl ApubObject for ApubComment {
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
tag: maa.tags,
distinguished: Some(self.distinguished),
};

Ok(note)
Expand Down Expand Up @@ -184,6 +185,7 @@ impl ApubObject for ApubComment {
updated: note.updated.map(|u| u.naive_local()),
deleted: None,
ap_id: Some(note.id.into()),
distinguished: note.distinguished,
local: Some(false),
};
let parent_comment_path = parent_comment.map(|t| t.0.path);
Expand Down
2 changes: 2 additions & 0 deletions crates/apub/src/protocol/objects/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct Note {
pub(crate) updated: Option<DateTime<FixedOffset>>,
#[serde(default)]
pub(crate) tag: Vec<MentionOrValue>,
// lemmy extension
pub(crate) distinguished: Option<bool>,
}

impl Note {
Expand Down
12 changes: 12 additions & 0 deletions crates/db_schema/src/impls/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ impl Comment {
.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(
conn: &PgConnection,
comment_form: &CommentForm,
Expand Down Expand Up @@ -330,6 +341,7 @@ mod tests {
published: inserted_comment.published,
updated: None,
ap_id: inserted_comment.ap_id.to_owned(),
distinguished: false,
local: true,
};

Expand Down
1 change: 1 addition & 0 deletions crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ table! {
ap_id -> Varchar,
local -> Bool,
path -> Ltree,
distinguished -> Bool,
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/db_schema/src/source/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Comment {
pub local: bool,
#[serde(with = "LtreeDef")]
pub path: Ltree,
pub distinguished: bool,
}

#[derive(Clone, Default)]
Expand All @@ -37,6 +38,7 @@ pub struct CommentForm {
pub deleted: Option<bool>,
pub ap_id: Option<DbUrl>,
pub local: Option<bool>,
pub distinguished: Option<bool>,
}

#[derive(PartialEq, Debug, Clone)]
Expand Down
1 change: 1 addition & 0 deletions crates/db_views/src/comment_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ mod tests {
ap_id: inserted_comment_0.ap_id,
updated: None,
local: true,
distinguished: false,
path: top_path,
},
creator: PersonSafe {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table comment drop column distinguished;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table comment add column distinguished boolean not null default false;