]> Untitled Git - lemmy.git/blob - crates/db_schema/src/source/comment.rs
88c47d3a4171e5750b74a34152f09ebe19677123
[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::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(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
24 #[belongs_to(Post)]
25 #[table_name = "comment"]
26 pub struct Comment {
27   pub id: CommentId,
28   pub creator_id: PersonId,
29   pub post_id: PostId,
30   pub parent_id: Option<CommentId>,
31   pub content: String,
32   pub removed: bool,
33   pub read: bool, // Whether the recipient has read the comment or not
34   pub published: chrono::NaiveDateTime,
35   pub updated: Option<chrono::NaiveDateTime>,
36   pub deleted: bool,
37   pub ap_id: DbUrl,
38   pub local: bool,
39 }
40
41 #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
42 #[belongs_to(Post)]
43 #[table_name = "comment_alias_1"]
44 pub struct CommentAlias1 {
45   pub id: CommentId,
46   pub creator_id: PersonId,
47   pub post_id: PostId,
48   pub parent_id: Option<CommentId>,
49   pub content: String,
50   pub removed: bool,
51   pub read: bool, // Whether the recipient has read the comment or not
52   pub published: chrono::NaiveDateTime,
53   pub updated: Option<chrono::NaiveDateTime>,
54   pub deleted: bool,
55   pub ap_id: DbUrl,
56   pub local: bool,
57 }
58
59 #[derive(Insertable, AsChangeset, Clone, Default)]
60 #[table_name = "comment"]
61 pub struct CommentForm {
62   pub creator_id: PersonId,
63   pub post_id: PostId,
64   pub content: String,
65   pub parent_id: Option<CommentId>,
66   pub removed: Option<bool>,
67   pub read: Option<bool>,
68   pub published: Option<chrono::NaiveDateTime>,
69   pub updated: Option<chrono::NaiveDateTime>,
70   pub deleted: Option<bool>,
71   pub ap_id: Option<DbUrl>,
72   pub local: Option<bool>,
73 }
74
75 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
76 #[belongs_to(Comment)]
77 #[table_name = "comment_like"]
78 pub struct CommentLike {
79   pub id: i32,
80   pub person_id: PersonId,
81   pub comment_id: CommentId,
82   pub post_id: PostId, // TODO this is redundant
83   pub score: i16,
84   pub published: chrono::NaiveDateTime,
85 }
86
87 #[derive(Insertable, AsChangeset, Clone)]
88 #[table_name = "comment_like"]
89 pub struct CommentLikeForm {
90   pub person_id: PersonId,
91   pub comment_id: CommentId,
92   pub post_id: PostId, // TODO this is redundant
93   pub score: i16,
94 }
95
96 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
97 #[belongs_to(Comment)]
98 #[table_name = "comment_saved"]
99 pub struct CommentSaved {
100   pub id: i32,
101   pub comment_id: CommentId,
102   pub person_id: PersonId,
103   pub published: chrono::NaiveDateTime,
104 }
105
106 #[derive(Insertable, AsChangeset)]
107 #[table_name = "comment_saved"]
108 pub struct CommentSavedForm {
109   pub comment_id: CommentId,
110   pub person_id: PersonId,
111 }
112
113 impl ApubObject for Comment {
114   type DataType = PgConnection;
115
116   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
117     None
118   }
119
120   fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, LemmyError> {
121     use crate::schema::comment::dsl::*;
122     let object_id: DbUrl = object_id.into();
123     Ok(comment.filter(ap_id.eq(object_id)).first::<Self>(conn).ok())
124   }
125 }