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