4 // The faked schema since diesel doesn't do views
6 private_message_view (id) {
13 published -> Timestamp,
14 updated -> Nullable<Timestamp>,
15 creator_name -> Varchar,
16 creator_avatar -> Nullable<Text>,
17 recipient_name -> Varchar,
18 recipient_avatar -> Nullable<Text>,
23 private_message_mview (id) {
30 published -> Timestamp,
31 updated -> Nullable<Timestamp>,
32 creator_name -> Varchar,
33 creator_avatar -> Nullable<Text>,
34 recipient_name -> Varchar,
35 recipient_avatar -> Nullable<Text>,
40 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
42 #[table_name = "private_message_view"]
43 pub struct PrivateMessageView {
46 pub recipient_id: i32,
50 pub published: chrono::NaiveDateTime,
51 pub updated: Option<chrono::NaiveDateTime>,
52 pub creator_name: String,
53 pub creator_avatar: Option<String>,
54 pub recipient_name: String,
55 pub recipient_avatar: Option<String>,
58 pub struct PrivateMessageQueryBuilder<'a> {
59 conn: &'a PgConnection,
60 query: super::private_message_view::private_message_mview::BoxedQuery<'a, Pg>,
61 for_recipient_id: i32,
67 impl<'a> PrivateMessageQueryBuilder<'a> {
68 pub fn create(conn: &'a PgConnection, for_recipient_id: i32) -> Self {
69 use super::private_message_view::private_message_mview::dsl::*;
71 let query = private_message_mview.into_boxed();
73 PrivateMessageQueryBuilder {
83 pub fn unread_only(mut self, unread_only: bool) -> Self {
84 self.unread_only = unread_only;
88 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
89 self.page = page.get_optional();
93 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
94 self.limit = limit.get_optional();
98 pub fn list(self) -> Result<Vec<PrivateMessageView>, Error> {
99 use super::private_message_view::private_message_mview::dsl::*;
101 let mut query = self.query.filter(deleted.eq(false));
103 // If its unread, I only want the ones to me
104 if self.unread_only {
106 .filter(read.eq(false))
107 .filter(recipient_id.eq(self.for_recipient_id));
109 // Otherwise, I want the ALL view to show both sent and received
111 query = query.filter(
113 .eq(self.for_recipient_id)
114 .or(creator_id.eq(self.for_recipient_id)),
118 let (limit, offset) = limit_and_offset(self.page, self.limit);
123 .order_by(published.desc())
124 .load::<PrivateMessageView>(self.conn)
128 impl PrivateMessageView {
129 pub fn read(conn: &PgConnection, from_private_message_id: i32) -> Result<Self, Error> {
130 use super::private_message_view::private_message_view::dsl::*;
132 let mut query = private_message_view.into_boxed();
135 .filter(id.eq(from_private_message_id))
136 .order_by(published.desc());
138 query.first::<Self>(conn)