2022-09-19 22:58:42 +00:00
|
|
|
use crate::structs::PrivateMessageReportView;
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel::{result::Error, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl};
|
|
|
|
use diesel_async::RunQueryDsl;
|
2022-09-19 22:58:42 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
newtypes::PrivateMessageReportId,
|
2022-09-26 14:09:32 +00:00
|
|
|
schema::{person, private_message, private_message_report},
|
2022-09-19 22:58:42 +00:00
|
|
|
source::{
|
2023-03-01 17:19:46 +00:00
|
|
|
person::Person,
|
2022-09-19 22:58:42 +00:00
|
|
|
private_message::PrivateMessage,
|
|
|
|
private_message_report::PrivateMessageReport,
|
|
|
|
},
|
2023-03-01 17:19:46 +00:00
|
|
|
traits::JoinView,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, limit_and_offset, DbPool},
|
2022-09-19 22:58:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type PrivateMessageReportViewTuple = (
|
|
|
|
PrivateMessageReport,
|
|
|
|
PrivateMessage,
|
2023-03-01 17:19:46 +00:00
|
|
|
Person,
|
|
|
|
Person,
|
|
|
|
Option<Person>,
|
2022-09-19 22:58:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
impl PrivateMessageReportView {
|
|
|
|
/// returns the PrivateMessageReportView for the provided report_id
|
|
|
|
///
|
|
|
|
/// * `report_id` - the report id to obtain
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn read(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
report_id: PrivateMessageReportId,
|
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-09-26 14:09:32 +00:00
|
|
|
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
|
|
|
|
|
2022-09-19 22:58:42 +00:00
|
|
|
let (private_message_report, private_message, private_message_creator, creator, resolver) =
|
|
|
|
private_message_report::table
|
|
|
|
.find(report_id)
|
|
|
|
.inner_join(private_message::table)
|
|
|
|
.inner_join(person::table.on(private_message::creator_id.eq(person::id)))
|
|
|
|
.inner_join(
|
2022-09-26 14:09:32 +00:00
|
|
|
person_alias_1
|
|
|
|
.on(private_message_report::creator_id.eq(person_alias_1.field(person::id))),
|
2022-09-19 22:58:42 +00:00
|
|
|
)
|
|
|
|
.left_join(
|
2022-09-26 14:09:32 +00:00
|
|
|
person_alias_2.on(
|
|
|
|
private_message_report::resolver_id.eq(person_alias_2.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,
|
|
|
|
person_alias_1.fields(person::all_columns),
|
|
|
|
person_alias_2.fields(person::all_columns).nullable(),
|
2022-09-19 22:58:42 +00:00
|
|
|
))
|
2022-11-09 10:05:00 +00:00
|
|
|
.first::<PrivateMessageReportViewTuple>(conn)
|
|
|
|
.await?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
private_message_report,
|
|
|
|
private_message,
|
|
|
|
private_message_creator,
|
|
|
|
creator,
|
|
|
|
resolver,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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>,
|
|
|
|
pub unresolved_only: Option<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> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-09-26 14:09:32 +00:00
|
|
|
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
|
|
|
|
|
2022-09-19 22:58:42 +00:00
|
|
|
let mut query = private_message_report::table
|
|
|
|
.inner_join(private_message::table)
|
|
|
|
.inner_join(person::table.on(private_message::creator_id.eq(person::id)))
|
|
|
|
.inner_join(
|
2022-09-26 14:09:32 +00:00
|
|
|
person_alias_1.on(private_message_report::creator_id.eq(person_alias_1.field(person::id))),
|
2022-09-19 22:58:42 +00:00
|
|
|
)
|
|
|
|
.left_join(
|
2022-09-26 14:09:32 +00:00
|
|
|
person_alias_2
|
|
|
|
.on(private_message_report::resolver_id.eq(person_alias_2.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,
|
|
|
|
person_alias_1.fields(person::all_columns),
|
|
|
|
person_alias_2.fields(person::all_columns).nullable(),
|
2022-09-19 22:58:42 +00:00
|
|
|
))
|
|
|
|
.into_boxed();
|
|
|
|
|
2023-06-06 09:01:48 +00:00
|
|
|
if self.unresolved_only.unwrap_or(false) {
|
2022-09-19 22:58:42 +00:00
|
|
|
query = query.filter(private_message_report::resolved.eq(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
let (limit, offset) = limit_and_offset(self.page, self.limit)?;
|
|
|
|
|
|
|
|
query = query
|
|
|
|
.order_by(private_message::published.desc())
|
|
|
|
.limit(limit)
|
|
|
|
.offset(offset);
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let res = query.load::<PrivateMessageReportViewTuple>(conn).await?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
Ok(
|
|
|
|
res
|
|
|
|
.into_iter()
|
|
|
|
.map(PrivateMessageReportView::from_tuple)
|
|
|
|
.collect(),
|
|
|
|
)
|
2022-09-19 22:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
impl JoinView for PrivateMessageReportView {
|
|
|
|
type JoinTuple = PrivateMessageReportViewTuple;
|
|
|
|
fn from_tuple(a: Self::JoinTuple) -> Self {
|
|
|
|
Self {
|
|
|
|
private_message_report: a.0,
|
|
|
|
private_message: a.1,
|
|
|
|
private_message_creator: a.2,
|
|
|
|
creator: a.3,
|
|
|
|
resolver: a.4,
|
|
|
|
}
|
2022-09-19 22:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::private_message_report_view::PrivateMessageReportQuery;
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
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
|
|
|
};
|
|
|
|
use serial_test::serial;
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[tokio::test]
|
2022-09-19 22:58:42 +00:00
|
|
|
#[serial]
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn test_crud() {
|
|
|
|
let pool = &build_db_pool_for_tests().await;
|
2023-07-11 13:09:59 +00:00
|
|
|
let pool = &mut pool.into();
|
2022-09-19 22:58:42 +00:00
|
|
|
|
2023-03-01 02:36:57 +00:00
|
|
|
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
let new_person_1 = PersonInsertForm::builder()
|
|
|
|
.name("timmy_mrv".into())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_timmy = Person::create(pool, &new_person_1).await.unwrap();
|
2022-09-19 22:58:42 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let new_person_2 = PersonInsertForm::builder()
|
|
|
|
.name("jessica_mrv".into())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_jessica = Person::create(pool, &new_person_2).await.unwrap();
|
2022-09-19 22:58:42 +00:00
|
|
|
|
|
|
|
// timmy sends private message to jessica
|
2022-10-27 09:24:07 +00:00
|
|
|
let pm_form = PrivateMessageInsertForm::builder()
|
|
|
|
.creator_id(inserted_timmy.id)
|
|
|
|
.recipient_id(inserted_jessica.id)
|
|
|
|
.content("something offensive".to_string())
|
|
|
|
.build();
|
2022-11-09 10:05:00 +00:00
|
|
|
let pm = PrivateMessage::create(pool, &pm_form).await.unwrap();
|
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(),
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
let pm_report = PrivateMessageReport::report(pool, &pm_report_form)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-09-19 22:58:42 +00:00
|
|
|
|
2023-07-17 10:20:25 +00:00
|
|
|
let reports = PrivateMessageReportQuery::default()
|
|
|
|
.list(pool)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-09-19 22:58:42 +00:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(1, reports.len());
|
|
|
|
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);
|
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let new_person_3 = PersonInsertForm::builder()
|
|
|
|
.name("admin_mrv".into())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_admin = Person::create(pool, &new_person_3).await.unwrap();
|
2022-09-19 22:58:42 +00:00
|
|
|
|
|
|
|
// admin resolves the report (after taking appropriate action)
|
2022-11-09 10:05:00 +00:00
|
|
|
PrivateMessageReport::resolve(pool, pm_report.id, inserted_admin.id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-09-19 22:58:42 +00:00
|
|
|
|
2023-07-17 10:20:25 +00:00
|
|
|
let reports = PrivateMessageReportQuery {
|
|
|
|
unresolved_only: (Some(false)),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.list(pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-09-19 22:58:42 +00:00
|
|
|
assert_eq!(1, reports.len());
|
|
|
|
assert!(reports[0].private_message_report.resolved);
|
|
|
|
assert!(reports[0].resolver.is_some());
|
|
|
|
assert_eq!(
|
|
|
|
inserted_admin.name,
|
|
|
|
reports[0].resolver.as_ref().unwrap().name
|
|
|
|
);
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
Instance::delete(pool, inserted_instance.id).await.unwrap();
|
2022-09-19 22:58:42 +00:00
|
|
|
}
|
|
|
|
}
|