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