]> Untitled Git - lemmy.git/blob - crates/db_schema/src/source/post.rs
b0cc78e0e2d84381f9c74ef7af66377664a95823
[lemmy.git] / crates / db_schema / src / source / post.rs
1 use crate::schema::{post, post_like, post_read, post_saved};
2 use serde::Serialize;
3 use url::{ParseError, Url};
4
5 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
6 #[table_name = "post"]
7 pub struct Post {
8   pub id: i32,
9   pub name: String,
10   pub url: Option<String>,
11   pub body: Option<String>,
12   pub creator_id: i32,
13   pub community_id: i32,
14   pub removed: bool,
15   pub locked: bool,
16   pub published: chrono::NaiveDateTime,
17   pub updated: Option<chrono::NaiveDateTime>,
18   pub deleted: bool,
19   pub nsfw: bool,
20   pub stickied: bool,
21   pub embed_title: Option<String>,
22   pub embed_description: Option<String>,
23   pub embed_html: Option<String>,
24   pub thumbnail_url: Option<String>,
25   pub ap_id: String,
26   pub local: bool,
27 }
28
29 #[derive(Insertable, AsChangeset)]
30 #[table_name = "post"]
31 pub struct PostForm {
32   pub name: String,
33   pub url: Option<String>,
34   pub body: Option<String>,
35   pub creator_id: i32,
36   pub community_id: i32,
37   pub removed: Option<bool>,
38   pub locked: Option<bool>,
39   pub published: Option<chrono::NaiveDateTime>,
40   pub updated: Option<chrono::NaiveDateTime>,
41   pub deleted: Option<bool>,
42   pub nsfw: bool,
43   pub stickied: Option<bool>,
44   pub embed_title: Option<String>,
45   pub embed_description: Option<String>,
46   pub embed_html: Option<String>,
47   pub thumbnail_url: Option<String>,
48   pub ap_id: Option<String>,
49   pub local: bool,
50 }
51
52 impl PostForm {
53   pub fn get_ap_id(&self) -> Result<Url, ParseError> {
54     Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string()))
55   }
56 }
57
58 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
59 #[belongs_to(Post)]
60 #[table_name = "post_like"]
61 pub struct PostLike {
62   pub id: i32,
63   pub post_id: i32,
64   pub user_id: i32,
65   pub score: i16,
66   pub published: chrono::NaiveDateTime,
67 }
68
69 #[derive(Insertable, AsChangeset, Clone)]
70 #[table_name = "post_like"]
71 pub struct PostLikeForm {
72   pub post_id: i32,
73   pub user_id: i32,
74   pub score: i16,
75 }
76
77 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
78 #[belongs_to(Post)]
79 #[table_name = "post_saved"]
80 pub struct PostSaved {
81   pub id: i32,
82   pub post_id: i32,
83   pub user_id: i32,
84   pub published: chrono::NaiveDateTime,
85 }
86
87 #[derive(Insertable, AsChangeset)]
88 #[table_name = "post_saved"]
89 pub struct PostSavedForm {
90   pub post_id: i32,
91   pub user_id: i32,
92 }
93
94 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
95 #[belongs_to(Post)]
96 #[table_name = "post_read"]
97 pub struct PostRead {
98   pub id: i32,
99
100   pub post_id: i32,
101
102   pub user_id: i32,
103
104   pub published: chrono::NaiveDateTime,
105 }
106
107 #[derive(Insertable, AsChangeset)]
108 #[table_name = "post_read"]
109 pub struct PostReadForm {
110   pub post_id: i32,
111
112   pub user_id: i32,
113 }