lemmy/crates/db_schema/src/impls/private_message_report.rs

56 lines
1.5 KiB
Rust
Raw Normal View History

use crate::{
newtypes::{PersonId, PrivateMessageReportId},
2022-11-09 10:05:00 +00:00
schema::private_message_report::dsl::*,
source::private_message_report::{PrivateMessageReport, PrivateMessageReportForm},
traits::Reportable,
2022-11-09 10:05:00 +00:00
utils::{get_conn, naive_now, DbPool},
};
use diesel::{dsl::*, result::Error, *};
2022-11-09 10:05:00 +00:00
use diesel_async::RunQueryDsl;
2022-11-09 10:05:00 +00:00
#[async_trait]
impl Reportable for PrivateMessageReport {
type Form = PrivateMessageReportForm;
type IdType = PrivateMessageReportId;
2022-11-09 10:05:00 +00:00
async fn report(pool: &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)
2022-11-09 10:05:00 +00:00
.await
}
2022-11-09 10:05:00 +00:00
async fn resolve(
pool: &DbPool,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
2022-11-09 10:05:00 +00:00
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)
2022-11-09 10:05:00 +00:00
.await
}
2022-11-09 10:05:00 +00:00
async fn unresolve(
pool: &DbPool,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
2022-11-09 10:05:00 +00:00
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)
2022-11-09 10:05:00 +00:00
.await
}
}