]> Untitled Git - lemmy.git/blob - crates/db_schema/src/source/private_message.rs
a0d1b9d1062a9793709fc0195a96ca3749be421f
[lemmy.git] / crates / db_schema / src / source / private_message.rs
1 use crate::{schema::private_message, DbUrl, PersonId, PrivateMessageId};
2 use chrono::NaiveDateTime;
3 use diesel::{ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
4 use lemmy_apub_lib::traits::ApubObject;
5 use lemmy_utils::LemmyError;
6 use serde::Serialize;
7 use url::Url;
8
9 #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
10 #[table_name = "private_message"]
11 pub struct PrivateMessage {
12   pub id: PrivateMessageId,
13   pub creator_id: PersonId,
14   pub recipient_id: PersonId,
15   pub content: String,
16   pub deleted: bool,
17   pub read: bool,
18   pub published: chrono::NaiveDateTime,
19   pub updated: Option<chrono::NaiveDateTime>,
20   pub ap_id: DbUrl,
21   pub local: bool,
22 }
23
24 #[derive(Insertable, AsChangeset, Default)]
25 #[table_name = "private_message"]
26 pub struct PrivateMessageForm {
27   pub creator_id: PersonId,
28   pub recipient_id: PersonId,
29   pub content: String,
30   pub deleted: Option<bool>,
31   pub read: Option<bool>,
32   pub published: Option<chrono::NaiveDateTime>,
33   pub updated: Option<chrono::NaiveDateTime>,
34   pub ap_id: Option<DbUrl>,
35   pub local: Option<bool>,
36 }
37
38 impl ApubObject for PrivateMessage {
39   type DataType = PgConnection;
40
41   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
42     None
43   }
44
45   fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, LemmyError> {
46     use crate::schema::private_message::dsl::*;
47     let object_id: DbUrl = object_id.into();
48     Ok(
49       private_message
50         .filter(ap_id.eq(object_id))
51         .first::<Self>(conn)
52         .ok(),
53     )
54   }
55 }