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