lemmy/crates/db_schema/src/impls/private_message_report.rs
Dessalines a3bf2f1cf1
Auto resolve reports on removing a comment or post. Fixes #4390 (#4402)
* Automatically resolve report when post/comment is removed (#3850)

* Automatically resolve report when post/comment is removed

* also handle apub removes

* Removing auto-resolve report triggers.

* Dont allow creating reports for deleted / removed items.

* Running pgformat.

* Fixing test.

* Addressing PR comments.

* Forgot comment report.

---------

Co-authored-by: Nutomic <me@nutomic.com>
2024-02-15 08:52:04 -05:00

73 lines
1.9 KiB
Rust

use crate::{
newtypes::{PersonId, PrivateMessageId, PrivateMessageReportId},
schema::private_message_report::dsl::{private_message_report, resolved, resolver_id, updated},
source::private_message_report::{PrivateMessageReport, PrivateMessageReportForm},
traits::Reportable,
utils::{get_conn, naive_now, DbPool},
};
use diesel::{
dsl::{insert_into, update},
result::Error,
ExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
#[async_trait]
impl Reportable for PrivateMessageReport {
type Form = PrivateMessageReportForm;
type IdType = PrivateMessageReportId;
type ObjectIdType = PrivateMessageId;
async fn report(
pool: &mut DbPool<'_>,
pm_report_form: &PrivateMessageReportForm,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(private_message_report)
.values(pm_report_form)
.get_result::<Self>(conn)
.await
}
async fn resolve(
pool: &mut DbPool<'_>,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
update(private_message_report.find(report_id))
.set((
resolved.eq(true),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(conn)
.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(
pool: &mut DbPool<'_>,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
update(private_message_report.find(report_id))
.set((
resolved.eq(false),
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(conn)
.await
}
}