]> Untitled Git - lemmy.git/blob - crates/db_schema/src/source/post.rs
Remove DeletableApubObject trait
[lemmy.git] / crates / db_schema / src / source / post.rs
1 use crate::{
2   naive_now,
3   schema::{post, post_like, post_read, post_saved},
4   CommunityId,
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 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
17 #[table_name = "post"]
18 pub struct Post {
19   pub id: PostId,
20   pub name: String,
21   pub url: Option<DbUrl>,
22   pub body: Option<String>,
23   pub creator_id: PersonId,
24   pub community_id: CommunityId,
25   pub removed: bool,
26   pub locked: bool,
27   pub published: chrono::NaiveDateTime,
28   pub updated: Option<chrono::NaiveDateTime>,
29   pub deleted: bool,
30   pub nsfw: bool,
31   pub stickied: bool,
32   pub embed_title: Option<String>,
33   pub embed_description: Option<String>,
34   pub embed_html: Option<String>,
35   pub thumbnail_url: Option<DbUrl>,
36   pub ap_id: DbUrl,
37   pub local: bool,
38 }
39
40 #[derive(Insertable, AsChangeset, Default)]
41 #[table_name = "post"]
42 pub struct PostForm {
43   pub name: String,
44   pub creator_id: PersonId,
45   pub community_id: CommunityId,
46   pub nsfw: Option<bool>,
47   pub url: Option<DbUrl>,
48   pub body: Option<String>,
49   pub removed: Option<bool>,
50   pub locked: Option<bool>,
51   pub published: Option<chrono::NaiveDateTime>,
52   pub updated: Option<chrono::NaiveDateTime>,
53   pub deleted: Option<bool>,
54   pub stickied: Option<bool>,
55   pub embed_title: Option<String>,
56   pub embed_description: Option<String>,
57   pub embed_html: Option<String>,
58   pub thumbnail_url: Option<DbUrl>,
59   pub ap_id: Option<DbUrl>,
60   pub local: Option<bool>,
61 }
62
63 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
64 #[belongs_to(Post)]
65 #[table_name = "post_like"]
66 pub struct PostLike {
67   pub id: i32,
68   pub post_id: PostId,
69   pub person_id: PersonId,
70   pub score: i16,
71   pub published: chrono::NaiveDateTime,
72 }
73
74 #[derive(Insertable, AsChangeset, Clone)]
75 #[table_name = "post_like"]
76 pub struct PostLikeForm {
77   pub post_id: PostId,
78   pub person_id: PersonId,
79   pub score: i16,
80 }
81
82 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
83 #[belongs_to(Post)]
84 #[table_name = "post_saved"]
85 pub struct PostSaved {
86   pub id: i32,
87   pub post_id: PostId,
88   pub person_id: PersonId,
89   pub published: chrono::NaiveDateTime,
90 }
91
92 #[derive(Insertable, AsChangeset)]
93 #[table_name = "post_saved"]
94 pub struct PostSavedForm {
95   pub post_id: PostId,
96   pub person_id: PersonId,
97 }
98
99 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
100 #[belongs_to(Post)]
101 #[table_name = "post_read"]
102 pub struct PostRead {
103   pub id: i32,
104   pub post_id: PostId,
105   pub person_id: PersonId,
106   pub published: chrono::NaiveDateTime,
107 }
108
109 #[derive(Insertable, AsChangeset)]
110 #[table_name = "post_read"]
111 pub struct PostReadForm {
112   pub post_id: PostId,
113   pub person_id: PersonId,
114 }
115
116 impl ApubObject for Post {
117   type DataType = PgConnection;
118
119   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
120     None
121   }
122
123   fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, LemmyError> {
124     use crate::schema::post::dsl::*;
125     let object_id: DbUrl = object_id.into();
126     Ok(post.filter(ap_id.eq(object_id)).first::<Self>(conn).ok())
127   }
128
129   fn delete(self, conn: &PgConnection) -> Result<(), LemmyError> {
130     use crate::schema::post::dsl::*;
131     diesel::update(post.find(self.id))
132       .set((deleted.eq(true), updated.eq(naive_now())))
133       .get_result::<Self>(conn)?;
134     Ok(())
135   }
136 }