1 use crate::structs::PrivateMessageView;
2 use diesel::{pg::Pg, result::Error, *};
4 newtypes::{PersonId, PrivateMessageId},
5 schema::{person, private_message},
7 person::{Person, PersonSafe},
8 private_message::PrivateMessage,
10 traits::{ToSafe, ViewToVec},
11 utils::limit_and_offset,
14 use typed_builder::TypedBuilder;
16 type PrivateMessageViewTuple = (PrivateMessage, PersonSafe, PersonSafe);
18 impl PrivateMessageView {
20 conn: &mut PgConnection,
21 private_message_id: PrivateMessageId,
22 ) -> Result<Self, Error> {
23 let person_alias_1 = diesel::alias!(person as person1);
25 let (private_message, creator, recipient) = private_message::table
26 .find(private_message_id)
27 .inner_join(person::table.on(private_message::creator_id.eq(person::id)))
29 person_alias_1.on(private_message::recipient_id.eq(person_alias_1.field(person::id))),
31 .order_by(private_message::published.desc())
33 private_message::all_columns,
34 Person::safe_columns_tuple(),
35 person_alias_1.fields(Person::safe_columns_tuple()),
37 .first::<PrivateMessageViewTuple>(conn)?;
39 Ok(PrivateMessageView {
46 /// Gets the number of unread messages
47 pub fn get_unread_messages(
48 conn: &mut PgConnection,
49 my_person_id: PersonId,
50 ) -> Result<i64, Error> {
52 private_message::table
53 .filter(private_message::read.eq(false))
54 .filter(private_message::recipient_id.eq(my_person_id))
55 .filter(private_message::deleted.eq(false))
56 .select(count(private_message::id))
61 #[derive(TypedBuilder)]
62 #[builder(field_defaults(default))]
63 pub struct PrivateMessageQuery<'a> {
65 conn: &'a mut PgConnection,
67 recipient_id: PersonId,
68 unread_only: Option<bool>,
73 impl<'a> PrivateMessageQuery<'a> {
74 pub fn list(self) -> Result<Vec<PrivateMessageView>, Error> {
75 let person_alias_1 = diesel::alias!(person as person1);
77 let mut query = private_message::table
78 .inner_join(person::table.on(private_message::creator_id.eq(person::id)))
80 person_alias_1.on(private_message::recipient_id.eq(person_alias_1.field(person::id))),
83 private_message::all_columns,
84 Person::safe_columns_tuple(),
85 person_alias_1.fields(Person::safe_columns_tuple()),
89 // If its unread, I only want the ones to me
90 if self.unread_only.unwrap_or(false) {
92 .filter(private_message::read.eq(false))
93 .filter(private_message::recipient_id.eq(self.recipient_id));
95 // Otherwise, I want the ALL view to show both sent and received
98 private_message::recipient_id
99 .eq(self.recipient_id)
100 .or(private_message::creator_id.eq(self.recipient_id)),
104 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
107 .filter(private_message::deleted.eq(false))
110 .order_by(private_message::published.desc());
113 "Private Message View Query: {:?}",
114 debug_query::<Pg, _>(&query)
117 let res = query.load::<PrivateMessageViewTuple>(self.conn)?;
119 Ok(PrivateMessageView::from_tuple_to_vec(res))
123 impl ViewToVec for PrivateMessageView {
124 type DbTuple = PrivateMessageViewTuple;
125 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
129 private_message: a.0,
133 .collect::<Vec<Self>>()