]> Untitled Git - lemmy.git/blob - server/src/db/post.rs
Merge branch 'admin_settings' into dev
[lemmy.git] / server / src / db / post.rs
1 use super::*;
2 use crate::schema::{post, post_like, post_read, post_saved};
3
4 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
5 #[table_name = "post"]
6 pub struct Post {
7   pub id: i32,
8   pub name: String,
9   pub url: Option<String>,
10   pub body: Option<String>,
11   pub creator_id: i32,
12   pub community_id: i32,
13   pub removed: bool,
14   pub locked: bool,
15   pub published: chrono::NaiveDateTime,
16   pub updated: Option<chrono::NaiveDateTime>,
17   pub deleted: bool,
18   pub nsfw: bool,
19   pub stickied: bool,
20   pub embed_title: Option<String>,
21   pub embed_description: Option<String>,
22   pub embed_html: Option<String>,
23   pub thumbnail_url: Option<String>,
24 }
25
26 #[derive(Insertable, AsChangeset, Clone)]
27 #[table_name = "post"]
28 pub struct PostForm {
29   pub name: String,
30   pub url: Option<String>,
31   pub body: Option<String>,
32   pub creator_id: i32,
33   pub community_id: i32,
34   pub removed: Option<bool>,
35   pub locked: Option<bool>,
36   pub updated: Option<chrono::NaiveDateTime>,
37   pub deleted: Option<bool>,
38   pub nsfw: bool,
39   pub stickied: Option<bool>,
40   pub embed_title: Option<String>,
41   pub embed_description: Option<String>,
42   pub embed_html: Option<String>,
43   pub thumbnail_url: Option<String>,
44 }
45
46 impl Crud<PostForm> for Post {
47   fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
48     use crate::schema::post::dsl::*;
49     post.find(post_id).first::<Self>(conn)
50   }
51
52   fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
53     use crate::schema::post::dsl::*;
54     diesel::delete(post.find(post_id)).execute(conn)
55   }
56
57   fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
58     use crate::schema::post::dsl::*;
59     insert_into(post).values(new_post).get_result::<Self>(conn)
60   }
61
62   fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
63     use crate::schema::post::dsl::*;
64     diesel::update(post.find(post_id))
65       .set(new_post)
66       .get_result::<Self>(conn)
67   }
68 }
69
70 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
71 #[belongs_to(Post)]
72 #[table_name = "post_like"]
73 pub struct PostLike {
74   pub id: i32,
75   pub post_id: i32,
76   pub user_id: i32,
77   pub score: i16,
78   pub published: chrono::NaiveDateTime,
79 }
80
81 #[derive(Insertable, AsChangeset, Clone)]
82 #[table_name = "post_like"]
83 pub struct PostLikeForm {
84   pub post_id: i32,
85   pub user_id: i32,
86   pub score: i16,
87 }
88
89 impl Likeable<PostLikeForm> for PostLike {
90   fn read(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> {
91     use crate::schema::post_like::dsl::*;
92     post_like
93       .filter(post_id.eq(post_id_from))
94       .load::<Self>(conn)
95   }
96   fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
97     use crate::schema::post_like::dsl::*;
98     insert_into(post_like)
99       .values(post_like_form)
100       .get_result::<Self>(conn)
101   }
102   fn remove(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<usize, Error> {
103     use crate::schema::post_like::dsl::*;
104     diesel::delete(
105       post_like
106         .filter(post_id.eq(post_like_form.post_id))
107         .filter(user_id.eq(post_like_form.user_id)),
108     )
109     .execute(conn)
110   }
111 }
112
113 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
114 #[belongs_to(Post)]
115 #[table_name = "post_saved"]
116 pub struct PostSaved {
117   pub id: i32,
118   pub post_id: i32,
119   pub user_id: i32,
120   pub published: chrono::NaiveDateTime,
121 }
122
123 #[derive(Insertable, AsChangeset, Clone)]
124 #[table_name = "post_saved"]
125 pub struct PostSavedForm {
126   pub post_id: i32,
127   pub user_id: i32,
128 }
129
130 impl Saveable<PostSavedForm> for PostSaved {
131   fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
132     use crate::schema::post_saved::dsl::*;
133     insert_into(post_saved)
134       .values(post_saved_form)
135       .get_result::<Self>(conn)
136   }
137   fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
138     use crate::schema::post_saved::dsl::*;
139     diesel::delete(
140       post_saved
141         .filter(post_id.eq(post_saved_form.post_id))
142         .filter(user_id.eq(post_saved_form.user_id)),
143     )
144     .execute(conn)
145   }
146 }
147
148 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
149 #[belongs_to(Post)]
150 #[table_name = "post_read"]
151 pub struct PostRead {
152   pub id: i32,
153   pub post_id: i32,
154   pub user_id: i32,
155   pub published: chrono::NaiveDateTime,
156 }
157
158 #[derive(Insertable, AsChangeset, Clone)]
159 #[table_name = "post_read"]
160 pub struct PostReadForm {
161   pub post_id: i32,
162   pub user_id: i32,
163 }
164
165 impl Readable<PostReadForm> for PostRead {
166   fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
167     use crate::schema::post_read::dsl::*;
168     insert_into(post_read)
169       .values(post_read_form)
170       .get_result::<Self>(conn)
171   }
172   fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
173     use crate::schema::post_read::dsl::*;
174     diesel::delete(
175       post_read
176         .filter(post_id.eq(post_read_form.post_id))
177         .filter(user_id.eq(post_read_form.user_id)),
178     )
179     .execute(conn)
180   }
181 }
182
183 #[cfg(test)]
184 mod tests {
185   use super::super::community::*;
186   use super::super::user::*;
187   use super::*;
188   #[test]
189   fn test_crud() {
190     let conn = establish_unpooled_connection();
191
192     let new_user = UserForm {
193       name: "jim".into(),
194       fedi_name: "rrf".into(),
195       preferred_username: None,
196       password_encrypted: "nope".into(),
197       email: None,
198       matrix_user_id: None,
199       avatar: None,
200       admin: false,
201       banned: false,
202       updated: None,
203       show_nsfw: false,
204       theme: "darkly".into(),
205       default_sort_type: SortType::Hot as i16,
206       default_listing_type: ListingType::Subscribed as i16,
207       lang: "browser".into(),
208       show_avatars: true,
209       send_notifications_to_email: false,
210     };
211
212     let inserted_user = User_::create(&conn, &new_user).unwrap();
213
214     let new_community = CommunityForm {
215       name: "test community_3".to_string(),
216       title: "nada".to_owned(),
217       description: None,
218       category_id: 1,
219       creator_id: inserted_user.id,
220       removed: None,
221       deleted: None,
222       updated: None,
223       nsfw: false,
224     };
225
226     let inserted_community = Community::create(&conn, &new_community).unwrap();
227
228     let new_post = PostForm {
229       name: "A test post".into(),
230       url: None,
231       body: None,
232       creator_id: inserted_user.id,
233       community_id: inserted_community.id,
234       removed: None,
235       deleted: None,
236       locked: None,
237       stickied: None,
238       nsfw: false,
239       updated: None,
240       embed_title: None,
241       embed_description: None,
242       embed_html: None,
243       thumbnail_url: None,
244     };
245
246     let inserted_post = Post::create(&conn, &new_post).unwrap();
247
248     let expected_post = Post {
249       id: inserted_post.id,
250       name: "A test post".into(),
251       url: None,
252       body: None,
253       creator_id: inserted_user.id,
254       community_id: inserted_community.id,
255       published: inserted_post.published,
256       removed: false,
257       locked: false,
258       stickied: false,
259       nsfw: false,
260       deleted: false,
261       updated: None,
262       embed_title: None,
263       embed_description: None,
264       embed_html: None,
265       thumbnail_url: None,
266     };
267
268     // Post Like
269     let post_like_form = PostLikeForm {
270       post_id: inserted_post.id,
271       user_id: inserted_user.id,
272       score: 1,
273     };
274
275     let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
276
277     let expected_post_like = PostLike {
278       id: inserted_post_like.id,
279       post_id: inserted_post.id,
280       user_id: inserted_user.id,
281       published: inserted_post_like.published,
282       score: 1,
283     };
284
285     // Post Save
286     let post_saved_form = PostSavedForm {
287       post_id: inserted_post.id,
288       user_id: inserted_user.id,
289     };
290
291     let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
292
293     let expected_post_saved = PostSaved {
294       id: inserted_post_saved.id,
295       post_id: inserted_post.id,
296       user_id: inserted_user.id,
297       published: inserted_post_saved.published,
298     };
299
300     // Post Read
301     let post_read_form = PostReadForm {
302       post_id: inserted_post.id,
303       user_id: inserted_user.id,
304     };
305
306     let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
307
308     let expected_post_read = PostRead {
309       id: inserted_post_read.id,
310       post_id: inserted_post.id,
311       user_id: inserted_user.id,
312       published: inserted_post_read.published,
313     };
314
315     let read_post = Post::read(&conn, inserted_post.id).unwrap();
316     let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
317     let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
318     let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
319     let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
320     let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
321     Community::delete(&conn, inserted_community.id).unwrap();
322     User_::delete(&conn, inserted_user.id).unwrap();
323
324     assert_eq!(expected_post, read_post);
325     assert_eq!(expected_post, inserted_post);
326     assert_eq!(expected_post, updated_post);
327     assert_eq!(expected_post_like, inserted_post_like);
328     assert_eq!(expected_post_saved, inserted_post_saved);
329     assert_eq!(expected_post_read, inserted_post_read);
330     assert_eq!(1, like_removed);
331     assert_eq!(1, saved_removed);
332     assert_eq!(1, read_removed);
333     assert_eq!(1, num_deleted);
334   }
335 }