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