3 schema::{comment, comment_alias_1, comment_like, comment_saved},
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};
17 // WITH RECURSIVE MyTree AS (
18 // SELECT * FROM comment WHERE parent_id IS NULL
20 // SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id
22 // SELECT * FROM MyTree;
25 Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize,
28 #[table_name = "comment"]
31 pub creator_id: PersonId,
33 pub parent_id: Option<CommentId>,
36 pub read: bool, // Whether the recipient has read the comment or not
37 pub published: chrono::NaiveDateTime,
38 pub updated: Option<chrono::NaiveDateTime>,
45 Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize,
48 #[table_name = "comment_alias_1"]
49 pub struct CommentAlias1 {
51 pub creator_id: PersonId,
53 pub parent_id: Option<CommentId>,
56 pub read: bool, // Whether the recipient has read the comment or not
57 pub published: chrono::NaiveDateTime,
58 pub updated: Option<chrono::NaiveDateTime>,
64 #[derive(Insertable, AsChangeset, Clone, Default)]
65 #[table_name = "comment"]
66 pub struct CommentForm {
67 pub creator_id: PersonId,
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>,
80 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
81 #[belongs_to(Comment)]
82 #[table_name = "comment_like"]
83 pub struct CommentLike {
85 pub person_id: PersonId,
86 pub comment_id: CommentId,
87 pub post_id: PostId, // TODO this is redundant
89 pub published: chrono::NaiveDateTime,
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
101 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
102 #[belongs_to(Comment)]
103 #[table_name = "comment_saved"]
104 pub struct CommentSaved {
106 pub comment_id: CommentId,
107 pub person_id: PersonId,
108 pub published: chrono::NaiveDateTime,
111 #[derive(Insertable, AsChangeset)]
112 #[table_name = "comment_saved"]
113 pub struct CommentSavedForm {
114 pub comment_id: CommentId,
115 pub person_id: PersonId,
118 impl ApubObject for Comment {
119 type DataType = PgConnection;
121 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
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())
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)?;