]> Untitled Git - lemmy.git/blob - crates/db_schema/src/source/comment.rs
8c553a51a4812b35034d8e2dd68325212a4f5bca
[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 };
5 use serde::Serialize;
6 use url::{ParseError, Url};
7
8 // WITH RECURSIVE MyTree AS (
9 //     SELECT * FROM comment WHERE parent_id IS NULL
10 //     UNION ALL
11 //     SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id
12 // )
13 // SELECT * FROM MyTree;
14
15 #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
16 #[belongs_to(Post)]
17 #[table_name = "comment"]
18 pub struct Comment {
19   pub id: i32,
20   pub creator_id: i32,
21   pub post_id: i32,
22   pub parent_id: Option<i32>,
23   pub content: String,
24   pub removed: bool,
25   pub read: bool, // Whether the recipient has read the comment or not
26   pub published: chrono::NaiveDateTime,
27   pub updated: Option<chrono::NaiveDateTime>,
28   pub deleted: bool,
29   pub ap_id: String,
30   pub local: bool,
31 }
32
33 #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
34 #[belongs_to(Post)]
35 #[table_name = "comment_alias_1"]
36 pub struct CommentAlias1 {
37   pub id: i32,
38   pub creator_id: i32,
39   pub post_id: i32,
40   pub parent_id: Option<i32>,
41   pub content: String,
42   pub removed: bool,
43   pub read: bool, // Whether the recipient has read the comment or not
44   pub published: chrono::NaiveDateTime,
45   pub updated: Option<chrono::NaiveDateTime>,
46   pub deleted: bool,
47   pub ap_id: String,
48   pub local: bool,
49 }
50
51 #[derive(Insertable, AsChangeset, Clone)]
52 #[table_name = "comment"]
53 pub struct CommentForm {
54   pub creator_id: i32,
55   pub post_id: i32,
56   pub parent_id: Option<i32>,
57   pub content: String,
58   pub removed: Option<bool>,
59   pub read: Option<bool>,
60   pub published: Option<chrono::NaiveDateTime>,
61   pub updated: Option<chrono::NaiveDateTime>,
62   pub deleted: Option<bool>,
63   pub ap_id: Option<String>,
64   pub local: bool,
65 }
66
67 impl CommentForm {
68   pub fn get_ap_id(&self) -> Result<Url, ParseError> {
69     Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string()))
70   }
71 }
72
73 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
74 #[belongs_to(Comment)]
75 #[table_name = "comment_like"]
76 pub struct CommentLike {
77   pub id: i32,
78   pub user_id: i32,
79   pub comment_id: i32,
80   pub post_id: i32, // TODO this is redundant
81   pub score: i16,
82   pub published: chrono::NaiveDateTime,
83 }
84
85 #[derive(Insertable, AsChangeset, Clone)]
86 #[table_name = "comment_like"]
87 pub struct CommentLikeForm {
88   pub user_id: i32,
89   pub comment_id: i32,
90   pub post_id: i32, // TODO this is redundant
91   pub score: i16,
92 }
93
94 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
95 #[belongs_to(Comment)]
96 #[table_name = "comment_saved"]
97 pub struct CommentSaved {
98   pub id: i32,
99   pub comment_id: i32,
100   pub user_id: i32,
101   pub published: chrono::NaiveDateTime,
102 }
103
104 #[derive(Insertable, AsChangeset)]
105 #[table_name = "comment_saved"]
106 pub struct CommentSavedForm {
107   pub comment_id: i32,
108   pub user_id: i32,
109 }