1 use crate::structs::PrivateMessageReportView;
2 use diesel::{result::Error, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl};
3 use diesel_async::RunQueryDsl;
5 newtypes::PrivateMessageReportId,
6 schema::{person, private_message, private_message_report},
8 person::{Person, PersonSafe},
9 private_message::PrivateMessage,
10 private_message_report::PrivateMessageReport,
12 traits::{ToSafe, ViewToVec},
13 utils::{get_conn, limit_and_offset, DbPool},
15 use typed_builder::TypedBuilder;
17 type PrivateMessageReportViewTuple = (
25 impl PrivateMessageReportView {
26 /// returns the PrivateMessageReportView for the provided report_id
28 /// * `report_id` - the report id to obtain
29 pub async fn read(pool: &DbPool, report_id: PrivateMessageReportId) -> Result<Self, Error> {
30 let conn = &mut get_conn(pool).await?;
31 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
33 let (private_message_report, private_message, private_message_creator, creator, resolver) =
34 private_message_report::table
36 .inner_join(private_message::table)
37 .inner_join(person::table.on(private_message::creator_id.eq(person::id)))
40 .on(private_message_report::creator_id.eq(person_alias_1.field(person::id))),
44 private_message_report::resolver_id.eq(person_alias_2.field(person::id).nullable()),
48 private_message_report::all_columns,
49 private_message::all_columns,
50 Person::safe_columns_tuple(),
51 person_alias_1.fields(Person::safe_columns_tuple()),
53 .fields(Person::safe_columns_tuple())
56 .first::<PrivateMessageReportViewTuple>(conn)
60 private_message_report,
62 private_message_creator,
68 /// Returns the current unresolved post report count for the communities you mod
69 pub async fn get_report_count(pool: &DbPool) -> Result<i64, Error> {
70 use diesel::dsl::count;
71 let conn = &mut get_conn(pool).await?;
73 private_message_report::table
74 .inner_join(private_message::table)
75 .filter(private_message_report::resolved.eq(false))
77 .select(count(private_message_report::id))
83 #[derive(TypedBuilder)]
84 #[builder(field_defaults(default))]
85 pub struct PrivateMessageReportQuery<'a> {
90 unresolved_only: Option<bool>,
93 impl<'a> PrivateMessageReportQuery<'a> {
94 pub async fn list(self) -> Result<Vec<PrivateMessageReportView>, Error> {
95 let conn = &mut get_conn(self.pool).await?;
96 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
98 let mut query = private_message_report::table
99 .inner_join(private_message::table)
100 .inner_join(person::table.on(private_message::creator_id.eq(person::id)))
102 person_alias_1.on(private_message_report::creator_id.eq(person_alias_1.field(person::id))),
106 .on(private_message_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
109 private_message_report::all_columns,
110 private_message::all_columns,
111 Person::safe_columns_tuple(),
112 person_alias_1.fields(Person::safe_columns_tuple()),
114 .fields(Person::safe_columns_tuple())
119 if self.unresolved_only.unwrap_or(true) {
120 query = query.filter(private_message_report::resolved.eq(false));
123 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
126 .order_by(private_message::published.desc())
130 let res = query.load::<PrivateMessageReportViewTuple>(conn).await?;
132 Ok(PrivateMessageReportView::from_tuple_to_vec(res))
136 impl ViewToVec for PrivateMessageReportView {
137 type DbTuple = PrivateMessageReportViewTuple;
138 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
142 private_message_report: a.0,
143 private_message: a.1,
144 private_message_creator: a.2,
148 .collect::<Vec<Self>>()
154 use crate::private_message_report_view::PrivateMessageReportQuery;
155 use lemmy_db_schema::{
158 person::{Person, PersonInsertForm},
159 private_message::{PrivateMessage, PrivateMessageInsertForm},
160 private_message_report::{PrivateMessageReport, PrivateMessageReportForm},
162 traits::{Crud, Reportable},
163 utils::build_db_pool_for_tests,
165 use serial_test::serial;
169 async fn test_crud() {
170 let pool = &build_db_pool_for_tests().await;
172 let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
174 let new_person_1 = PersonInsertForm::builder()
175 .name("timmy_mrv".into())
176 .public_key("pubkey".to_string())
177 .instance_id(inserted_instance.id)
179 let inserted_timmy = Person::create(pool, &new_person_1).await.unwrap();
181 let new_person_2 = PersonInsertForm::builder()
182 .name("jessica_mrv".into())
183 .public_key("pubkey".to_string())
184 .instance_id(inserted_instance.id)
186 let inserted_jessica = Person::create(pool, &new_person_2).await.unwrap();
188 // timmy sends private message to jessica
189 let pm_form = PrivateMessageInsertForm::builder()
190 .creator_id(inserted_timmy.id)
191 .recipient_id(inserted_jessica.id)
192 .content("something offensive".to_string())
194 let pm = PrivateMessage::create(pool, &pm_form).await.unwrap();
196 // jessica reports private message
197 let pm_report_form = PrivateMessageReportForm {
198 creator_id: inserted_jessica.id,
199 original_pm_text: pm.content.clone(),
200 private_message_id: pm.id,
201 reason: "its offensive".to_string(),
203 let pm_report = PrivateMessageReport::report(pool, &pm_report_form)
207 let reports = PrivateMessageReportQuery::builder()
213 assert_eq!(1, reports.len());
214 assert!(!reports[0].private_message_report.resolved);
215 assert_eq!(inserted_timmy.name, reports[0].private_message_creator.name);
216 assert_eq!(inserted_jessica.name, reports[0].creator.name);
217 assert_eq!(pm_report.reason, reports[0].private_message_report.reason);
218 assert_eq!(pm.content, reports[0].private_message.content);
220 let new_person_3 = PersonInsertForm::builder()
221 .name("admin_mrv".into())
222 .public_key("pubkey".to_string())
223 .instance_id(inserted_instance.id)
225 let inserted_admin = Person::create(pool, &new_person_3).await.unwrap();
227 // admin resolves the report (after taking appropriate action)
228 PrivateMessageReport::resolve(pool, pm_report.id, inserted_admin.id)
232 let reports = PrivateMessageReportQuery::builder()
234 .unresolved_only(Some(false))
239 assert_eq!(1, reports.len());
240 assert!(reports[0].private_message_report.resolved);
241 assert!(reports[0].resolver.is_some());
244 reports[0].resolver.as_ref().unwrap().name
247 Instance::delete(pool, inserted_instance.id).await.unwrap();