2020-05-16 14:04:08 +00:00
|
|
|
use crate::db::{limit_and_offset, MaybeOptional};
|
|
|
|
use diesel::{pg::Pg, result::Error, *};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2020-01-22 21:35:29 +00:00
|
|
|
|
|
|
|
// The faked schema since diesel doesn't do views
|
|
|
|
table! {
|
|
|
|
private_message_view (id) {
|
|
|
|
id -> Int4,
|
|
|
|
creator_id -> Int4,
|
|
|
|
recipient_id -> Int4,
|
|
|
|
content -> Text,
|
|
|
|
deleted -> Bool,
|
|
|
|
read -> Bool,
|
|
|
|
published -> Timestamp,
|
|
|
|
updated -> Nullable<Timestamp>,
|
2020-05-06 02:06:24 +00:00
|
|
|
ap_id -> Text,
|
|
|
|
local -> Bool,
|
2020-01-22 21:35:29 +00:00
|
|
|
creator_name -> Varchar,
|
|
|
|
creator_avatar -> Nullable<Text>,
|
2020-05-06 02:06:24 +00:00
|
|
|
creator_actor_id -> Text,
|
|
|
|
creator_local -> Bool,
|
2020-01-22 21:35:29 +00:00
|
|
|
recipient_name -> Varchar,
|
|
|
|
recipient_avatar -> Nullable<Text>,
|
2020-05-06 02:06:24 +00:00
|
|
|
recipient_actor_id -> Text,
|
|
|
|
recipient_local -> Bool,
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(
|
|
|
|
Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
|
|
|
|
)]
|
|
|
|
#[table_name = "private_message_view"]
|
|
|
|
pub struct PrivateMessageView {
|
|
|
|
pub id: i32,
|
|
|
|
pub creator_id: i32,
|
|
|
|
pub recipient_id: i32,
|
|
|
|
pub content: String,
|
|
|
|
pub deleted: bool,
|
|
|
|
pub read: bool,
|
|
|
|
pub published: chrono::NaiveDateTime,
|
|
|
|
pub updated: Option<chrono::NaiveDateTime>,
|
2020-05-06 02:06:24 +00:00
|
|
|
pub ap_id: String,
|
|
|
|
pub local: bool,
|
2020-01-22 21:35:29 +00:00
|
|
|
pub creator_name: String,
|
|
|
|
pub creator_avatar: Option<String>,
|
2020-05-06 02:06:24 +00:00
|
|
|
pub creator_actor_id: String,
|
|
|
|
pub creator_local: bool,
|
2020-01-22 21:35:29 +00:00
|
|
|
pub recipient_name: String,
|
|
|
|
pub recipient_avatar: Option<String>,
|
2020-05-06 02:06:24 +00:00
|
|
|
pub recipient_actor_id: String,
|
|
|
|
pub recipient_local: bool,
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PrivateMessageQueryBuilder<'a> {
|
|
|
|
conn: &'a PgConnection,
|
2020-07-07 14:54:44 +00:00
|
|
|
query: super::private_message_view::private_message_view::BoxedQuery<'a, Pg>,
|
2020-01-22 21:35:29 +00:00
|
|
|
for_recipient_id: i32,
|
|
|
|
unread_only: bool,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PrivateMessageQueryBuilder<'a> {
|
|
|
|
pub fn create(conn: &'a PgConnection, for_recipient_id: i32) -> Self {
|
2020-07-07 14:54:44 +00:00
|
|
|
use super::private_message_view::private_message_view::dsl::*;
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2020-07-07 14:54:44 +00:00
|
|
|
let query = private_message_view.into_boxed();
|
2020-01-22 21:35:29 +00:00
|
|
|
|
|
|
|
PrivateMessageQueryBuilder {
|
|
|
|
conn,
|
|
|
|
query,
|
|
|
|
for_recipient_id,
|
|
|
|
unread_only: false,
|
|
|
|
page: None,
|
|
|
|
limit: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unread_only(mut self, unread_only: bool) -> Self {
|
|
|
|
self.unread_only = unread_only;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
|
|
|
|
self.page = page.get_optional();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
|
|
|
|
self.limit = limit.get_optional();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list(self) -> Result<Vec<PrivateMessageView>, Error> {
|
2020-07-07 14:54:44 +00:00
|
|
|
use super::private_message_view::private_message_view::dsl::*;
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2020-02-02 22:07:16 +00:00
|
|
|
let mut query = self.query.filter(deleted.eq(false));
|
2020-01-22 21:35:29 +00:00
|
|
|
|
|
|
|
// If its unread, I only want the ones to me
|
|
|
|
if self.unread_only {
|
|
|
|
query = query
|
|
|
|
.filter(read.eq(false))
|
|
|
|
.filter(recipient_id.eq(self.for_recipient_id));
|
|
|
|
}
|
|
|
|
// Otherwise, I want the ALL view to show both sent and received
|
|
|
|
else {
|
|
|
|
query = query.filter(
|
|
|
|
recipient_id
|
|
|
|
.eq(self.for_recipient_id)
|
|
|
|
.or(creator_id.eq(self.for_recipient_id)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
let (limit, offset) = limit_and_offset(self.page, self.limit);
|
|
|
|
|
|
|
|
query
|
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
|
|
|
.order_by(published.desc())
|
|
|
|
.load::<PrivateMessageView>(self.conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PrivateMessageView {
|
|
|
|
pub fn read(conn: &PgConnection, from_private_message_id: i32) -> Result<Self, Error> {
|
|
|
|
use super::private_message_view::private_message_view::dsl::*;
|
|
|
|
|
|
|
|
let mut query = private_message_view.into_boxed();
|
|
|
|
|
|
|
|
query = query
|
|
|
|
.filter(id.eq(from_private_message_id))
|
|
|
|
.order_by(published.desc());
|
|
|
|
|
|
|
|
query.first::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|