Auto resolve reports trigger (#3871)

* Revert "Automatically resolve report when post/comment is removed (#3850)"

This reverts commit f7f6766650.

* Automatically resolve reports using db trigger

* lint

* use mod log tables

* fix migration

* fix ci

* fix clippy
This commit is contained in:
Nutomic 2023-09-05 11:35:10 +02:00 committed by GitHub
parent 366d9d1e2e
commit a0ea8dbc00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 108 additions and 275 deletions

View file

@ -10,11 +10,10 @@ use lemmy_api_common::{
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
comment::{Comment, CommentUpdateForm}, comment::{Comment, CommentUpdateForm},
comment_report::CommentReport,
moderator::{ModRemoveComment, ModRemoveCommentForm}, moderator::{ModRemoveComment, ModRemoveCommentForm},
post::Post, post::Post,
}, },
traits::{Crud, Reportable}, traits::Crud,
}; };
use lemmy_db_views::structs::CommentView; use lemmy_db_views::structs::CommentView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
@ -57,9 +56,6 @@ pub async fn remove_comment(
.await .await
.with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?; .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
CommentReport::resolve_all_for_object(&mut context.pool(), comment_id, local_user_view.person.id)
.await?;
// Mod tables // Mod tables
let form = ModRemoveCommentForm { let form = ModRemoveCommentForm {
mod_person_id: local_user_view.person.id, mod_person_id: local_user_view.person.id,

View file

@ -11,9 +11,8 @@ use lemmy_db_schema::{
source::{ source::{
moderator::{ModRemovePost, ModRemovePostForm}, moderator::{ModRemovePost, ModRemovePostForm},
post::{Post, PostUpdateForm}, post::{Post, PostUpdateForm},
post_report::PostReport,
}, },
traits::{Crud, Reportable}, traits::Crud,
}; };
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
@ -55,9 +54,6 @@ pub async fn remove_post(
) )
.await?; .await?;
PostReport::resolve_all_for_object(&mut context.pool(), post_id, local_user_view.person.id)
.await?;
// Mod tables // Mod tables
let form = ModRemovePostForm { let form = ModRemovePostForm {
mod_person_id: local_user_view.person.id, mod_person_id: local_user_view.person.id,

View file

@ -12,7 +12,6 @@ use lemmy_api_common::{context::LemmyContext, utils::sanitize_html_opt};
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
comment::{Comment, CommentUpdateForm}, comment::{Comment, CommentUpdateForm},
comment_report::CommentReport,
community::{Community, CommunityUpdateForm}, community::{Community, CommunityUpdateForm},
moderator::{ moderator::{
ModRemoveComment, ModRemoveComment,
@ -23,9 +22,8 @@ use lemmy_db_schema::{
ModRemovePostForm, ModRemovePostForm,
}, },
post::{Post, PostUpdateForm}, post::{Post, PostUpdateForm},
post_report::PostReport,
}, },
traits::{Crud, Reportable}, traits::Crud,
}; };
use lemmy_utils::error::{LemmyError, LemmyErrorType}; use lemmy_utils::error::{LemmyError, LemmyErrorType};
use url::Url; use url::Url;
@ -133,7 +131,6 @@ pub(in crate::activities) async fn receive_remove_action(
.await?; .await?;
} }
DeletableObjects::Post(post) => { DeletableObjects::Post(post) => {
PostReport::resolve_all_for_object(&mut context.pool(), post.id, actor.id).await?;
let form = ModRemovePostForm { let form = ModRemovePostForm {
mod_person_id: actor.id, mod_person_id: actor.id,
post_id: post.id, post_id: post.id,
@ -152,7 +149,6 @@ pub(in crate::activities) async fn receive_remove_action(
.await?; .await?;
} }
DeletableObjects::Comment(comment) => { DeletableObjects::Comment(comment) => {
CommentReport::resolve_all_for_object(&mut context.pool(), comment.id, actor.id).await?;
let form = ModRemoveCommentForm { let form = ModRemoveCommentForm {
mod_person_id: actor.id, mod_person_id: actor.id,
comment_id: comment.id, comment_id: comment.id,

View file

@ -1,9 +1,6 @@
use crate::{ use crate::{
newtypes::{CommentId, CommentReportId, PersonId}, newtypes::{CommentReportId, PersonId},
schema::comment_report::{ schema::comment_report::dsl::{comment_report, resolved, resolver_id, updated},
comment_id,
dsl::{comment_report, resolved, resolver_id, updated},
},
source::comment_report::{CommentReport, CommentReportForm}, source::comment_report::{CommentReport, CommentReportForm},
traits::Reportable, traits::Reportable,
utils::{get_conn, naive_now, DbPool}, utils::{get_conn, naive_now, DbPool},
@ -20,7 +17,6 @@ use diesel_async::RunQueryDsl;
impl Reportable for CommentReport { impl Reportable for CommentReport {
type Form = CommentReportForm; type Form = CommentReportForm;
type IdType = CommentReportId; type IdType = CommentReportId;
type ObjectIdType = CommentId;
/// creates a comment report and returns it /// creates a comment report and returns it
/// ///
/// * `conn` - the postgres connection /// * `conn` - the postgres connection
@ -57,22 +53,6 @@ impl Reportable for CommentReport {
.await .await
} }
async fn resolve_all_for_object(
pool: &mut DbPool<'_>,
comment_id_: CommentId,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
update(comment_report.filter(comment_id.eq(comment_id_)))
.set((
resolved.eq(true),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(conn)
.await
}
/// unresolve a comment report /// unresolve a comment report
/// ///
/// * `conn` - the postgres connection /// * `conn` - the postgres connection

View file

@ -1,9 +1,6 @@
use crate::{ use crate::{
newtypes::{PersonId, PostId, PostReportId}, newtypes::{PersonId, PostReportId},
schema::post_report::{ schema::post_report::dsl::{post_report, resolved, resolver_id, updated},
dsl::{post_report, resolved, resolver_id, updated},
post_id,
},
source::post_report::{PostReport, PostReportForm}, source::post_report::{PostReport, PostReportForm},
traits::Reportable, traits::Reportable,
utils::{get_conn, naive_now, DbPool}, utils::{get_conn, naive_now, DbPool},
@ -20,7 +17,6 @@ use diesel_async::RunQueryDsl;
impl Reportable for PostReport { impl Reportable for PostReport {
type Form = PostReportForm; type Form = PostReportForm;
type IdType = PostReportId; type IdType = PostReportId;
type ObjectIdType = PostId;
async fn report(pool: &mut DbPool<'_>, post_report_form: &PostReportForm) -> Result<Self, Error> { async fn report(pool: &mut DbPool<'_>, post_report_form: &PostReportForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
@ -46,22 +42,6 @@ impl Reportable for PostReport {
.await .await
} }
async fn resolve_all_for_object(
pool: &mut DbPool<'_>,
post_id_: PostId,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
update(post_report.filter(post_id.eq(post_id_)))
.set((
resolved.eq(true),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(conn)
.await
}
async fn unresolve( async fn unresolve(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
report_id: Self::IdType, report_id: Self::IdType,
@ -154,21 +134,4 @@ mod tests {
Person::delete(pool, person.id).await.unwrap(); Person::delete(pool, person.id).await.unwrap();
Post::delete(pool, report.post_id).await.unwrap(); Post::delete(pool, report.post_id).await.unwrap();
} }
#[tokio::test]
#[serial]
async fn test_resolve_all_post_reports() {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let (person, report) = init(pool).await;
let resolved_count = PostReport::resolve_all_for_object(pool, report.post_id, person.id)
.await
.unwrap();
assert_eq!(resolved_count, 1);
Person::delete(pool, person.id).await.unwrap();
Post::delete(pool, report.post_id).await.unwrap();
}
} }

View file

@ -1,5 +1,5 @@
use crate::{ use crate::{
newtypes::{PersonId, PrivateMessageId, PrivateMessageReportId}, newtypes::{PersonId, PrivateMessageReportId},
schema::private_message_report::dsl::{private_message_report, resolved, resolver_id, updated}, schema::private_message_report::dsl::{private_message_report, resolved, resolver_id, updated},
source::private_message_report::{PrivateMessageReport, PrivateMessageReportForm}, source::private_message_report::{PrivateMessageReport, PrivateMessageReportForm},
traits::Reportable, traits::Reportable,
@ -17,7 +17,6 @@ use diesel_async::RunQueryDsl;
impl Reportable for PrivateMessageReport { impl Reportable for PrivateMessageReport {
type Form = PrivateMessageReportForm; type Form = PrivateMessageReportForm;
type IdType = PrivateMessageReportId; type IdType = PrivateMessageReportId;
type ObjectIdType = PrivateMessageId;
async fn report( async fn report(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
@ -46,15 +45,6 @@ impl Reportable for PrivateMessageReport {
.await .await
} }
// TODO: this is unused because private message doesnt have remove handler
async fn resolve_all_for_object(
_pool: &mut DbPool<'_>,
_pm_id_: PrivateMessageId,
_by_resolver_id: PersonId,
) -> Result<usize, Error> {
unimplemented!()
}
async fn unresolve( async fn unresolve(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
report_id: Self::IdType, report_id: Self::IdType,

View file

@ -155,7 +155,6 @@ pub trait Readable {
pub trait Reportable { pub trait Reportable {
type Form; type Form;
type IdType; type IdType;
type ObjectIdType;
async fn report(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error> async fn report(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
where where
Self: Sized; Self: Sized;
@ -164,13 +163,6 @@ pub trait Reportable {
report_id: Self::IdType, report_id: Self::IdType,
resolver_id: PersonId, resolver_id: PersonId,
) -> Result<usize, Error> ) -> Result<usize, Error>
where
Self: Sized;
async fn resolve_all_for_object(
pool: &mut DbPool<'_>,
comment_id_: Self::ObjectIdType,
by_resolver_id: PersonId,
) -> Result<usize, Error>
where where
Self: Sized; Self: Sized;
async fn unresolve( async fn unresolve(

View file

@ -194,11 +194,11 @@ mod tests {
structs::LocalUserView, structs::LocalUserView,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::PostAggregates,
source::{ source::{
community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm}, community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm},
instance::Instance, instance::Instance,
local_user::{LocalUser, LocalUserInsertForm}, local_user::{LocalUser, LocalUserInsertForm},
moderator::{ModRemovePost, ModRemovePostForm},
person::{Person, PersonInsertForm}, person::{Person, PersonInsertForm},
post::{Post, PostInsertForm}, post::{Post, PostInsertForm},
post_report::{PostReport, PostReportForm}, post_report::{PostReport, PostReportForm},
@ -291,12 +291,20 @@ mod tests {
reason: "from sara".into(), reason: "from sara".into(),
}; };
let inserted_sara_report = PostReport::report(pool, &sara_report_form).await.unwrap(); PostReport::report(pool, &sara_report_form).await.unwrap();
let new_post_2 = PostInsertForm::builder()
.name("A test post crv 2".into())
.creator_id(inserted_timmy.id)
.community_id(inserted_community.id)
.build();
let inserted_post_2 = Post::create(pool, &new_post_2).await.unwrap();
// jessica reports // jessica reports
let jessica_report_form = PostReportForm { let jessica_report_form = PostReportForm {
creator_id: inserted_jessica.id, creator_id: inserted_jessica.id,
post_id: inserted_post.id, post_id: inserted_post_2.id,
original_post_name: "Orig post".into(), original_post_name: "Orig post".into(),
original_post_url: None, original_post_url: None,
original_post_body: None, original_post_body: None,
@ -307,138 +315,21 @@ mod tests {
.await .await
.unwrap(); .unwrap();
let agg = PostAggregates::read(pool, inserted_post.id).await.unwrap();
let read_jessica_report_view = let read_jessica_report_view =
PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id) PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
.await .await
.unwrap(); .unwrap();
let expected_jessica_report_view = PostReportView {
post_report: inserted_jessica_report.clone(),
post: inserted_post.clone(),
community: Community {
id: inserted_community.id,
name: inserted_community.name,
icon: None,
removed: false,
deleted: false,
nsfw: false,
actor_id: inserted_community.actor_id.clone(),
local: true,
title: inserted_community.title,
description: None,
updated: None,
banner: None,
hidden: false,
posting_restricted_to_mods: false,
published: inserted_community.published,
instance_id: inserted_instance.id,
private_key: inserted_community.private_key.clone(),
public_key: inserted_community.public_key.clone(),
last_refreshed_at: inserted_community.last_refreshed_at,
followers_url: inserted_community.followers_url.clone(),
inbox_url: inserted_community.inbox_url.clone(),
shared_inbox_url: inserted_community.shared_inbox_url.clone(),
moderators_url: inserted_community.moderators_url.clone(),
featured_url: inserted_community.featured_url.clone(),
},
creator: Person {
id: inserted_jessica.id,
name: inserted_jessica.name,
display_name: None,
published: inserted_jessica.published,
avatar: None,
actor_id: inserted_jessica.actor_id.clone(),
local: true,
banned: false,
deleted: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
inbox_url: inserted_jessica.inbox_url.clone(),
shared_inbox_url: None,
matrix_user_id: None,
ban_expires: None,
instance_id: inserted_instance.id,
private_key: inserted_jessica.private_key,
public_key: inserted_jessica.public_key,
last_refreshed_at: inserted_jessica.last_refreshed_at,
},
post_creator: Person {
id: inserted_timmy.id,
name: inserted_timmy.name.clone(),
display_name: None,
published: inserted_timmy.published,
avatar: None,
actor_id: inserted_timmy.actor_id.clone(),
local: true,
banned: false,
deleted: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
inbox_url: inserted_timmy.inbox_url.clone(),
shared_inbox_url: None,
matrix_user_id: None,
ban_expires: None,
instance_id: inserted_instance.id,
private_key: inserted_timmy.private_key.clone(),
public_key: inserted_timmy.public_key.clone(),
last_refreshed_at: inserted_timmy.last_refreshed_at,
},
creator_banned_from_community: false,
my_vote: None,
counts: PostAggregates {
id: agg.id,
post_id: inserted_post.id,
comments: 0,
score: 0,
upvotes: 0,
downvotes: 0,
published: agg.published,
newest_comment_time_necro: inserted_post.published,
newest_comment_time: inserted_post.published,
featured_community: false,
featured_local: false,
hot_rank: 1728,
hot_rank_active: 1728,
controversy_rank: 0.0,
community_id: inserted_post.community_id,
creator_id: inserted_post.creator_id,
},
resolver: None,
};
assert_eq!(read_jessica_report_view, expected_jessica_report_view); assert_eq!(
read_jessica_report_view.post_report,
let mut expected_sara_report_view = expected_jessica_report_view.clone(); inserted_jessica_report
expected_sara_report_view.post_report = inserted_sara_report; );
expected_sara_report_view.my_vote = None; assert_eq!(read_jessica_report_view.post, inserted_post_2);
expected_sara_report_view.creator = Person { assert_eq!(read_jessica_report_view.community.id, inserted_community.id);
id: inserted_sara.id, assert_eq!(read_jessica_report_view.creator.id, inserted_jessica.id);
name: inserted_sara.name, assert_eq!(read_jessica_report_view.post_creator.id, inserted_timmy.id);
display_name: None, assert_eq!(read_jessica_report_view.my_vote, None);
published: inserted_sara.published, assert_eq!(read_jessica_report_view.resolver, None);
avatar: None,
actor_id: inserted_sara.actor_id.clone(),
local: true,
banned: false,
deleted: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
inbox_url: inserted_sara.inbox_url.clone(),
shared_inbox_url: None,
matrix_user_id: None,
ban_expires: None,
instance_id: inserted_instance.id,
private_key: inserted_sara.private_key,
public_key: inserted_sara.public_key,
last_refreshed_at: inserted_sara.last_refreshed_at,
};
// Do a batch read of timmys reports // Do a batch read of timmys reports
let reports = PostReportQuery::default() let reports = PostReportQuery::default()
@ -446,13 +337,8 @@ mod tests {
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(reports[0].creator.id, inserted_jessica.id);
reports, assert_eq!(reports[1].creator.id, inserted_sara.id);
[
expected_jessica_report_view.clone(),
expected_sara_report_view.clone()
]
);
// Make sure the counts are correct // Make sure the counts are correct
let report_count = PostReportView::get_report_count(pool, inserted_timmy.id, false, None) let report_count = PostReportView::get_report_count(pool, inserted_timmy.id, false, None)
@ -460,64 +346,42 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!(2, report_count); assert_eq!(2, report_count);
// Try to resolve the report // Writing post removal to mod log should automatically resolve reports
PostReport::resolve(pool, inserted_jessica_report.id, inserted_timmy.id) let remove_form = ModRemovePostForm {
.await mod_person_id: inserted_timmy.id,
.unwrap(); post_id: inserted_jessica_report.post_id,
reason: None,
removed: Some(true),
};
ModRemovePost::create(pool, &remove_form).await.unwrap();
let read_jessica_report_view_after_resolve = let read_jessica_report_view_after_resolve =
PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id) PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
.await .await
.unwrap(); .unwrap();
assert!(read_jessica_report_view_after_resolve.post_report.resolved);
let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
expected_jessica_report_view_after_resolve
.post_report
.resolved = true;
expected_jessica_report_view_after_resolve
.post_report
.resolver_id = Some(inserted_timmy.id);
expected_jessica_report_view_after_resolve
.post_report
.updated = read_jessica_report_view_after_resolve.post_report.updated;
expected_jessica_report_view_after_resolve.resolver = Some(Person {
id: inserted_timmy.id,
name: inserted_timmy.name.clone(),
display_name: None,
published: inserted_timmy.published,
avatar: None,
actor_id: inserted_timmy.actor_id.clone(),
local: true,
banned: false,
deleted: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
inbox_url: inserted_timmy.inbox_url.clone(),
shared_inbox_url: None,
matrix_user_id: None,
ban_expires: None,
instance_id: inserted_instance.id,
private_key: inserted_timmy.private_key.clone(),
public_key: inserted_timmy.public_key.clone(),
last_refreshed_at: inserted_timmy.last_refreshed_at,
});
assert_eq!( assert_eq!(
read_jessica_report_view_after_resolve, read_jessica_report_view_after_resolve
expected_jessica_report_view_after_resolve .post_report
.resolver_id,
Some(inserted_timmy.id)
);
assert_eq!(
read_jessica_report_view_after_resolve.resolver.unwrap().id,
inserted_timmy.id
); );
// Do a batch read of timmys reports // Do a batch read of timmys reports
// It should only show saras, which is unresolved // It should only show saras, which is unresolved
let reports_after_resolve = PostReportQuery { let reports_after_resolve = PostReportQuery {
unresolved_only: (true), unresolved_only: true,
..Default::default() ..Default::default()
} }
.list(pool, &timmy_view) .list(pool, &timmy_view)
.await .await
.unwrap(); .unwrap();
assert_eq!(reports_after_resolve[0], expected_sara_report_view); assert_eq!(reports_after_resolve.len(), 1);
assert_eq!(reports_after_resolve[0].creator.id, inserted_sara.id);
// Make sure the counts are correct // Make sure the counts are correct
let report_count_after_resolved = let report_count_after_resolved =

View file

@ -0,0 +1,8 @@
DROP TRIGGER IF EXISTS post_removed_resolve_reports ON mod_remove_post;
DROP FUNCTION IF EXISTS post_removed_resolve_reports;
DROP TRIGGER IF EXISTS comment_removed_resolve_reports ON mod_remove_comment;
DROP FUNCTION IF EXISTS comment_removed_resolve_reports;

View file

@ -0,0 +1,48 @@
-- Automatically resolve all reports for a given post once it is marked as removed
CREATE OR REPLACE FUNCTION post_removed_resolve_reports ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE
post_report
SET
resolved = TRUE,
resolver_id = NEW.mod_person_id,
updated = now()
WHERE
post_report.post_id = NEW.post_id;
RETURN NULL;
END
$$;
CREATE OR REPLACE TRIGGER post_removed_resolve_reports
AFTER INSERT ON mod_remove_post
FOR EACH ROW
WHEN (NEW.removed)
EXECUTE PROCEDURE post_removed_resolve_reports ();
-- Same when comment is marked as removed
CREATE OR REPLACE FUNCTION comment_removed_resolve_reports ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE
comment_report
SET
resolved = TRUE,
resolver_id = NEW.mod_person_id,
updated = now()
WHERE
comment_report.comment_id = NEW.comment_id;
RETURN NULL;
END
$$;
CREATE OR REPLACE TRIGGER comment_removed_resolve_reports
AFTER INSERT ON mod_remove_comment
FOR EACH ROW
WHEN (NEW.removed)
EXECUTE PROCEDURE comment_removed_resolve_reports ();