]> Untitled Git - lemmy.git/blob - crates/db_schema/src/source/comment.rs
Add both (De)Serialize to all models (#1851)
[lemmy.git] / crates / db_schema / src / source / comment.rs
1 use crate::{
2   schema::{comment, comment_alias_1, comment_like, comment_saved},
3   source::post::Post,
4   CommentId,
5   DbUrl,
6   PersonId,
7   PostId,
8 };
9 use chrono::NaiveDateTime;
10 use diesel::{ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
11 use lemmy_apub_lib::traits::ApubObject;
12 use lemmy_utils::LemmyError;
13 use serde::{Deserialize, Serialize};
14 use url::Url;
15
16 // WITH RECURSIVE MyTree AS (
17 //     SELECT * FROM comment WHERE parent_id IS NULL
18 //     UNION ALL
19 //     SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id
20 // )
21 // SELECT * FROM MyTree;
22
23 #[derive(
24   Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize,
25 )]
26 #[belongs_to(Post)]
27 #[table_name = "comment"]
28 pub struct Comment {
29   pub id: CommentId,
30   pub creator_id: PersonId,
31   pub post_id: PostId,
32   pub parent_id: Option<CommentId>,
33   pub content: String,
34   pub removed: bool,
35   pub read: bool, // Whether the recipient has read the comment or not
36   pub published: chrono::NaiveDateTime,
37   pub updated: Option<chrono::NaiveDateTime>,
38   pub deleted: bool,
39   pub ap_id: DbUrl,
40   pub local: bool,
41 }
42
43 #[derive(
44   Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize,
45 )]
46 #[belongs_to(Post)]
47 #[table_name = "comment_alias_1"]
48 pub struct CommentAlias1 {
49   pub id: CommentId,
50   pub creator_id: PersonId,
51   pub post_id: PostId,
52   pub parent_id: Option<CommentId>,
53   pub content: String,
54   pub removed: bool,
55   pub read: bool, // Whether the recipient has read the comment or not
56   pub published: chrono::NaiveDateTime,
57   pub updated: Option<chrono::NaiveDateTime>,
58   pub deleted: bool,
59   pub ap_id: DbUrl,
60   pub local: bool,
61 }
62
63 #[derive(Insertable, AsChangeset, Clone, Default)]
64 #[table_name = "comment"]
65 pub struct CommentForm {
66   pub creator_id: PersonId,
67   pub post_id: PostId,
68   pub content: String,
69   pub parent_id: Option<CommentId>,
70   pub removed: Option<bool>,
71   pub read: Option<bool>,
72   pub published: Option<chrono::NaiveDateTime>,
73   pub updated: Option<chrono::NaiveDateTime>,
74   pub deleted: Option<bool>,
75   pub ap_id: Option<DbUrl>,
76   pub local: Option<bool>,
77 }
78
79 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
80 #[belongs_to(Comment)]
81 #[table_name = "comment_like"]
82 pub struct CommentLike {
83   pub id: i32,
84   pub person_id: PersonId,
85   pub comment_id: CommentId,
86   pub post_id: PostId, // TODO this is redundant
87   pub score: i16,
88   pub published: chrono::NaiveDateTime,
89 }
90
91 #[derive(Insertable, AsChangeset, Clone)]
92 #[table_name = "comment_like"]
93 pub struct CommentLikeForm {
94   pub person_id: PersonId,
95   pub comment_id: CommentId,
96   pub post_id: PostId, // TODO this is redundant
97   pub score: i16,
98 }
99
100 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
101 #[belongs_to(Comment)]
102 #[table_name = "comment_saved"]
103 pub struct CommentSaved {
104   pub id: i32,
105   pub comment_id: CommentId,
106   pub person_id: PersonId,
107   pub published: chrono::NaiveDateTime,
108 }
109
110 #[derive(Insertable, AsChangeset)]
111 #[table_name = "comment_saved"]
112 pub struct CommentSavedForm {
113   pub comment_id: CommentId,
114   pub person_id: PersonId,
115 }
116
117 impl ApubObject for Comment {
118   type DataType = PgConnection;
119
120   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
121     None
122   }
123
124   fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, LemmyError> {
125     use crate::schema::comment::dsl::*;
126     let object_id: DbUrl = object_id.into();
127     Ok(comment.filter(ap_id.eq(object_id)).first::<Self>(conn).ok())
128   }
129 }