2022-09-19 22:58:42 +00:00
|
|
|
use crate::structs::PrivateMessageReportView;
|
2023-07-28 08:36:50 +00:00
|
|
|
use diesel::{
|
|
|
|
pg::Pg,
|
|
|
|
result::Error,
|
|
|
|
ExpressionMethods,
|
|
|
|
JoinOnDsl,
|
|
|
|
NullableExpressionMethods,
|
|
|
|
QueryDsl,
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2022-09-19 22:58:42 +00:00
|
|
|
use lemmy_db_schema::{
|
2023-07-28 08:36:50 +00:00
|
|
|
aliases,
|
2022-09-19 22:58:42 +00:00
|
|
|
newtypes::PrivateMessageReportId,
|
2022-09-26 14:09:32 +00:00
|
|
|
schema::{person, private_message, private_message_report},
|
2023-07-28 08:36:50 +00:00
|
|
|
utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
|
2022-09-19 22:58:42 +00:00
|
|
|
};
|
|
|
|
|
2023-07-28 08:36:50 +00:00
|
|
|
fn queries<'a>() -> Queries<
|
|
|
|
impl ReadFn<'a, PrivateMessageReportView, PrivateMessageReportId>,
|
|
|
|
impl ListFn<'a, PrivateMessageReportView, PrivateMessageReportQuery>,
|
|
|
|
> {
|
|
|
|
let all_joins =
|
|
|
|
|query: private_message_report::BoxedQuery<'a, Pg>| {
|
|
|
|
query
|
2022-09-19 22:58:42 +00:00
|
|
|
.inner_join(private_message::table)
|
|
|
|
.inner_join(person::table.on(private_message::creator_id.eq(person::id)))
|
|
|
|
.inner_join(
|
2023-07-28 08:36:50 +00:00
|
|
|
aliases::person1
|
|
|
|
.on(private_message_report::creator_id.eq(aliases::person1.field(person::id))),
|
2022-09-19 22:58:42 +00:00
|
|
|
)
|
2023-07-28 08:36:50 +00:00
|
|
|
.left_join(aliases::person2.on(
|
|
|
|
private_message_report::resolver_id.eq(aliases::person2.field(person::id).nullable()),
|
|
|
|
))
|
2022-09-19 22:58:42 +00:00
|
|
|
.select((
|
|
|
|
private_message_report::all_columns,
|
|
|
|
private_message::all_columns,
|
2023-03-01 17:19:46 +00:00
|
|
|
person::all_columns,
|
2023-07-28 08:36:50 +00:00
|
|
|
aliases::person1.fields(person::all_columns),
|
|
|
|
aliases::person2.fields(person::all_columns).nullable(),
|
2022-09-19 22:58:42 +00:00
|
|
|
))
|
2023-07-28 08:36:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let read = move |mut conn: DbConn<'a>, report_id: PrivateMessageReportId| async move {
|
|
|
|
all_joins(private_message_report::table.find(report_id).into_boxed())
|
2024-04-16 12:48:15 +00:00
|
|
|
.first(&mut conn)
|
2023-07-28 08:36:50 +00:00
|
|
|
.await
|
|
|
|
};
|
|
|
|
|
|
|
|
let list = move |mut conn: DbConn<'a>, options: PrivateMessageReportQuery| async move {
|
|
|
|
let mut query = all_joins(private_message_report::table.into_boxed());
|
|
|
|
|
2024-05-23 12:46:26 +00:00
|
|
|
// If viewing all reports, order by newest, but if viewing unresolved only, show the oldest
|
|
|
|
// first (FIFO)
|
2023-08-11 09:13:14 +00:00
|
|
|
if options.unresolved_only {
|
2024-04-02 17:25:28 +00:00
|
|
|
query = query
|
|
|
|
.filter(private_message_report::resolved.eq(false))
|
|
|
|
.order_by(private_message_report::published.asc());
|
|
|
|
} else {
|
|
|
|
query = query.order_by(private_message_report::published.desc());
|
2023-07-28 08:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let (limit, offset) = limit_and_offset(options.page, options.limit)?;
|
|
|
|
|
|
|
|
query
|
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
2023-08-31 13:26:10 +00:00
|
|
|
.load::<PrivateMessageReportView>(&mut conn)
|
2023-07-28 08:36:50 +00:00
|
|
|
.await
|
|
|
|
};
|
|
|
|
|
|
|
|
Queries::new(read, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PrivateMessageReportView {
|
|
|
|
/// returns the PrivateMessageReportView for the provided report_id
|
|
|
|
///
|
|
|
|
/// * `report_id` - the report id to obtain
|
|
|
|
pub async fn read(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
report_id: PrivateMessageReportId,
|
2024-09-23 15:26:50 +00:00
|
|
|
) -> Result<Self, Error> {
|
2023-07-28 08:36:50 +00:00
|
|
|
queries().read(pool, report_id).await
|
2022-09-19 22:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the current unresolved post report count for the communities you mod
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn get_report_count(pool: &mut DbPool<'_>) -> Result<i64, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use diesel::dsl::count;
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
|
|
|
private_message_report::table
|
|
|
|
.inner_join(private_message::table)
|
|
|
|
.filter(private_message_report::resolved.eq(false))
|
|
|
|
.into_boxed()
|
|
|
|
.select(count(private_message_report::id))
|
|
|
|
.first::<i64>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-09-19 22:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:20:25 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct PrivateMessageReportQuery {
|
|
|
|
pub page: Option<i64>,
|
|
|
|
pub limit: Option<i64>,
|
2023-08-11 09:13:14 +00:00
|
|
|
pub unresolved_only: bool,
|
2022-09-19 22:58:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 10:20:25 +00:00
|
|
|
impl PrivateMessageReportQuery {
|
|
|
|
pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PrivateMessageReportView>, Error> {
|
2023-07-28 08:36:50 +00:00
|
|
|
queries().list(pool, self).await
|
2022-09-19 22:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2024-09-24 17:29:02 +00:00
|
|
|
#[expect(clippy::indexing_slicing)]
|
2022-09-19 22:58:42 +00:00
|
|
|
mod tests {
|
2023-07-17 15:04:14 +00:00
|
|
|
|
2022-09-19 22:58:42 +00:00
|
|
|
use crate::private_message_report_view::PrivateMessageReportQuery;
|
|
|
|
use lemmy_db_schema::{
|
2024-01-04 16:51:55 +00:00
|
|
|
assert_length,
|
2022-09-19 22:58:42 +00:00
|
|
|
source::{
|
2022-10-27 09:24:07 +00:00
|
|
|
instance::Instance,
|
|
|
|
person::{Person, PersonInsertForm},
|
|
|
|
private_message::{PrivateMessage, PrivateMessageInsertForm},
|
2022-09-19 22:58:42 +00:00
|
|
|
private_message_report::{PrivateMessageReport, PrivateMessageReportForm},
|
|
|
|
},
|
|
|
|
traits::{Crud, Reportable},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::build_db_pool_for_tests,
|
2022-09-19 22:58:42 +00:00
|
|
|
};
|
2024-10-02 12:50:21 +00:00
|
|
|
use lemmy_utils::error::LemmyResult;
|
2024-01-04 09:47:18 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2022-09-19 22:58:42 +00:00
|
|
|
use serial_test::serial;
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[tokio::test]
|
2022-09-19 22:58:42 +00:00
|
|
|
#[serial]
|
2024-10-02 12:50:21 +00:00
|
|
|
async fn test_crud() -> LemmyResult<()> {
|
2024-11-07 10:49:05 +00:00
|
|
|
let pool = &build_db_pool_for_tests();
|
2023-07-11 13:09:59 +00:00
|
|
|
let pool = &mut pool.into();
|
2022-09-19 22:58:42 +00:00
|
|
|
|
2024-10-02 12:50:21 +00:00
|
|
|
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2024-06-06 12:29:18 +00:00
|
|
|
let new_person_1 = PersonInsertForm::test_form(inserted_instance.id, "timmy_mrv");
|
2024-10-02 12:50:21 +00:00
|
|
|
let inserted_timmy = Person::create(pool, &new_person_1).await?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
2024-06-06 12:29:18 +00:00
|
|
|
let new_person_2 = PersonInsertForm::test_form(inserted_instance.id, "jessica_mrv");
|
2024-10-02 12:50:21 +00:00
|
|
|
let inserted_jessica = Person::create(pool, &new_person_2).await?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
|
|
|
// timmy sends private message to jessica
|
2024-09-20 12:15:25 +00:00
|
|
|
let pm_form = PrivateMessageInsertForm::new(
|
|
|
|
inserted_timmy.id,
|
|
|
|
inserted_jessica.id,
|
|
|
|
"something offensive".to_string(),
|
|
|
|
);
|
2024-10-02 12:50:21 +00:00
|
|
|
let pm = PrivateMessage::create(pool, &pm_form).await?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
|
|
|
// jessica reports private message
|
|
|
|
let pm_report_form = PrivateMessageReportForm {
|
|
|
|
creator_id: inserted_jessica.id,
|
|
|
|
original_pm_text: pm.content.clone(),
|
|
|
|
private_message_id: pm.id,
|
|
|
|
reason: "its offensive".to_string(),
|
|
|
|
};
|
2024-10-02 12:50:21 +00:00
|
|
|
let pm_report = PrivateMessageReport::report(pool, &pm_report_form).await?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
2024-10-02 12:50:21 +00:00
|
|
|
let reports = PrivateMessageReportQuery::default().list(pool).await?;
|
2024-01-04 16:51:55 +00:00
|
|
|
assert_length!(1, reports);
|
2022-09-19 22:58:42 +00:00
|
|
|
assert!(!reports[0].private_message_report.resolved);
|
|
|
|
assert_eq!(inserted_timmy.name, reports[0].private_message_creator.name);
|
|
|
|
assert_eq!(inserted_jessica.name, reports[0].creator.name);
|
|
|
|
assert_eq!(pm_report.reason, reports[0].private_message_report.reason);
|
|
|
|
assert_eq!(pm.content, reports[0].private_message.content);
|
|
|
|
|
2024-06-06 12:29:18 +00:00
|
|
|
let new_person_3 = PersonInsertForm::test_form(inserted_instance.id, "admin_mrv");
|
2024-10-02 12:50:21 +00:00
|
|
|
let inserted_admin = Person::create(pool, &new_person_3).await?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
|
|
|
// admin resolves the report (after taking appropriate action)
|
2024-10-02 12:50:21 +00:00
|
|
|
PrivateMessageReport::resolve(pool, pm_report.id, inserted_admin.id).await?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
2023-07-17 10:20:25 +00:00
|
|
|
let reports = PrivateMessageReportQuery {
|
2023-08-11 09:13:14 +00:00
|
|
|
unresolved_only: (false),
|
2023-07-17 10:20:25 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.list(pool)
|
2024-10-02 12:50:21 +00:00
|
|
|
.await?;
|
2024-01-04 16:51:55 +00:00
|
|
|
assert_length!(1, reports);
|
2022-09-19 22:58:42 +00:00
|
|
|
assert!(reports[0].private_message_report.resolved);
|
|
|
|
assert!(reports[0].resolver.is_some());
|
|
|
|
assert_eq!(
|
2024-10-02 12:50:21 +00:00
|
|
|
Some(&inserted_admin.name),
|
|
|
|
reports[0].resolver.as_ref().map(|r| &r.name)
|
2022-09-19 22:58:42 +00:00
|
|
|
);
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2024-10-02 12:50:21 +00:00
|
|
|
Instance::delete(pool, inserted_instance.id).await?;
|
|
|
|
|
|
|
|
Ok(())
|
2022-09-19 22:58:42 +00:00
|
|
|
}
|
|
|
|
}
|