1 use crate::{ApubObject, Crud, Likeable, Readable, Saveable};
2 use diesel::{dsl::*, result::Error, *};
18 impl Crud<PostForm> for Post {
19 fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
20 use lemmy_db_schema::schema::post::dsl::*;
21 post.find(post_id).first::<Self>(conn)
24 fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
25 use lemmy_db_schema::schema::post::dsl::*;
26 diesel::delete(post.find(post_id)).execute(conn)
29 fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
30 use lemmy_db_schema::schema::post::dsl::*;
31 insert_into(post).values(new_post).get_result::<Self>(conn)
34 fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
35 use lemmy_db_schema::schema::post::dsl::*;
36 diesel::update(post.find(post_id))
38 .get_result::<Self>(conn)
43 //fn read(conn: &PgConnection, post_id: i32) -> Result<Post, Error>;
44 fn list_for_community(conn: &PgConnection, the_community_id: i32) -> Result<Vec<Post>, Error>;
45 fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: DbUrl) -> Result<Post, Error>;
46 fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Post>, Error>;
47 fn update_deleted(conn: &PgConnection, post_id: i32, new_deleted: bool) -> Result<Post, Error>;
48 fn update_removed(conn: &PgConnection, post_id: i32, new_removed: bool) -> Result<Post, Error>;
49 fn update_removed_for_creator(
52 for_community_id: Option<i32>,
54 ) -> Result<Vec<Post>, Error>;
55 fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Post, Error>;
56 fn update_stickied(conn: &PgConnection, post_id: i32, new_stickied: bool) -> Result<Post, Error>;
57 fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool;
61 fn list_for_community(conn: &PgConnection, the_community_id: i32) -> Result<Vec<Self>, Error> {
62 use lemmy_db_schema::schema::post::dsl::*;
64 .filter(community_id.eq(the_community_id))
65 .then_order_by(published.desc())
66 .then_order_by(stickied.desc())
71 fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: DbUrl) -> Result<Self, Error> {
72 use lemmy_db_schema::schema::post::dsl::*;
74 diesel::update(post.find(post_id))
75 .set(ap_id.eq(apub_id))
76 .get_result::<Self>(conn)
79 fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Self>, Error> {
80 use lemmy_db_schema::schema::post::dsl::*;
82 let perma_deleted = "*Permananently Deleted*";
83 let perma_deleted_url = "https://deleted.com";
85 diesel::update(post.filter(creator_id.eq(for_creator_id)))
87 name.eq(perma_deleted),
88 url.eq(perma_deleted_url),
89 body.eq(perma_deleted),
91 updated.eq(naive_now()),
93 .get_results::<Self>(conn)
96 fn update_deleted(conn: &PgConnection, post_id: i32, new_deleted: bool) -> Result<Self, Error> {
97 use lemmy_db_schema::schema::post::dsl::*;
98 diesel::update(post.find(post_id))
99 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
100 .get_result::<Self>(conn)
103 fn update_removed(conn: &PgConnection, post_id: i32, new_removed: bool) -> Result<Self, Error> {
104 use lemmy_db_schema::schema::post::dsl::*;
105 diesel::update(post.find(post_id))
106 .set((removed.eq(new_removed), updated.eq(naive_now())))
107 .get_result::<Self>(conn)
110 fn update_removed_for_creator(
113 for_community_id: Option<i32>,
115 ) -> Result<Vec<Self>, Error> {
116 use lemmy_db_schema::schema::post::dsl::*;
118 let mut update = diesel::update(post).into_boxed();
119 update = update.filter(creator_id.eq(for_creator_id));
121 if let Some(for_community_id) = for_community_id {
122 update = update.filter(community_id.eq(for_community_id));
126 .set((removed.eq(new_removed), updated.eq(naive_now())))
127 .get_results::<Self>(conn)
130 fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Self, Error> {
131 use lemmy_db_schema::schema::post::dsl::*;
132 diesel::update(post.find(post_id))
133 .set(locked.eq(new_locked))
134 .get_result::<Self>(conn)
137 fn update_stickied(conn: &PgConnection, post_id: i32, new_stickied: bool) -> Result<Self, Error> {
138 use lemmy_db_schema::schema::post::dsl::*;
139 diesel::update(post.find(post_id))
140 .set(stickied.eq(new_stickied))
141 .get_result::<Self>(conn)
144 fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool {
145 user_id == post_creator_id
149 impl ApubObject<PostForm> for Post {
150 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
151 use lemmy_db_schema::schema::post::dsl::*;
152 post.filter(ap_id.eq(object_id)).first::<Self>(conn)
155 fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
156 use lemmy_db_schema::schema::post::dsl::*;
162 .get_result::<Self>(conn)
166 impl Likeable<PostLikeForm> for PostLike {
167 fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
168 use lemmy_db_schema::schema::post_like::dsl::*;
169 insert_into(post_like)
170 .values(post_like_form)
171 .on_conflict((post_id, user_id))
174 .get_result::<Self>(conn)
176 fn remove(conn: &PgConnection, user_id: i32, post_id: i32) -> Result<usize, Error> {
177 use lemmy_db_schema::schema::post_like::dsl;
180 .filter(dsl::post_id.eq(post_id))
181 .filter(dsl::user_id.eq(user_id)),
187 impl Saveable<PostSavedForm> for PostSaved {
188 fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
189 use lemmy_db_schema::schema::post_saved::dsl::*;
190 insert_into(post_saved)
191 .values(post_saved_form)
192 .on_conflict((post_id, user_id))
194 .set(post_saved_form)
195 .get_result::<Self>(conn)
197 fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
198 use lemmy_db_schema::schema::post_saved::dsl::*;
201 .filter(post_id.eq(post_saved_form.post_id))
202 .filter(user_id.eq(post_saved_form.user_id)),
208 impl Readable<PostReadForm> for PostRead {
209 fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
210 use lemmy_db_schema::schema::post_read::dsl::*;
211 insert_into(post_read)
212 .values(post_read_form)
213 .get_result::<Self>(conn)
216 fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
217 use lemmy_db_schema::schema::post_read::dsl::*;
220 .filter(post_id.eq(post_read_form.post_id))
221 .filter(user_id.eq(post_read_form.user_id)),
229 use crate::{establish_unpooled_connection, source::post::*, ListingType, SortType};
230 use lemmy_db_schema::source::{
231 community::{Community, CommunityForm},
234 use serial_test::serial;
239 let conn = establish_unpooled_connection();
241 let new_user = UserForm {
243 preferred_username: None,
244 password_encrypted: "nope".into(),
246 matrix_user_id: None,
254 theme: "browser".into(),
255 default_sort_type: SortType::Hot as i16,
256 default_listing_type: ListingType::Subscribed as i16,
257 lang: "browser".into(),
259 send_notifications_to_email: false,
265 last_refreshed_at: None,
267 shared_inbox_url: None,
270 let inserted_user = User_::create(&conn, &new_user).unwrap();
272 let new_community = CommunityForm {
273 name: "test community_3".to_string(),
274 title: "nada".to_owned(),
276 creator_id: inserted_user.id,
285 last_refreshed_at: None,
291 shared_inbox_url: None,
294 let inserted_community = Community::create(&conn, &new_community).unwrap();
296 let new_post = PostForm {
297 name: "A test post".into(),
300 creator_id: inserted_user.id,
301 community_id: inserted_community.id,
309 embed_description: None,
317 let inserted_post = Post::create(&conn, &new_post).unwrap();
319 let expected_post = Post {
320 id: inserted_post.id,
321 name: "A test post".into(),
324 creator_id: inserted_user.id,
325 community_id: inserted_community.id,
326 published: inserted_post.published,
334 embed_description: None,
337 ap_id: inserted_post.ap_id.to_owned(),
342 let post_like_form = PostLikeForm {
343 post_id: inserted_post.id,
344 user_id: inserted_user.id,
348 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
350 let expected_post_like = PostLike {
351 id: inserted_post_like.id,
352 post_id: inserted_post.id,
353 user_id: inserted_user.id,
354 published: inserted_post_like.published,
359 let post_saved_form = PostSavedForm {
360 post_id: inserted_post.id,
361 user_id: inserted_user.id,
364 let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
366 let expected_post_saved = PostSaved {
367 id: inserted_post_saved.id,
368 post_id: inserted_post.id,
369 user_id: inserted_user.id,
370 published: inserted_post_saved.published,
374 let post_read_form = PostReadForm {
375 post_id: inserted_post.id,
376 user_id: inserted_user.id,
379 let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
381 let expected_post_read = PostRead {
382 id: inserted_post_read.id,
383 post_id: inserted_post.id,
384 user_id: inserted_user.id,
385 published: inserted_post_read.published,
388 let read_post = Post::read(&conn, inserted_post.id).unwrap();
389 let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
390 let like_removed = PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
391 let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
392 let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
393 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
394 Community::delete(&conn, inserted_community.id).unwrap();
395 User_::delete(&conn, inserted_user.id).unwrap();
397 assert_eq!(expected_post, read_post);
398 assert_eq!(expected_post, inserted_post);
399 assert_eq!(expected_post, updated_post);
400 assert_eq!(expected_post_like, inserted_post_like);
401 assert_eq!(expected_post_saved, inserted_post_saved);
402 assert_eq!(expected_post_read, inserted_post_read);
403 assert_eq!(1, like_removed);
404 assert_eq!(1, saved_removed);
405 assert_eq!(1, read_removed);
406 assert_eq!(1, num_deleted);