1 use crate::{ApubObject, Crud, Likeable, Readable, Saveable};
2 use diesel::{dsl::*, result::Error, *};
17 impl Crud<PostForm> for Post {
18 fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
19 use lemmy_db_schema::schema::post::dsl::*;
20 post.find(post_id).first::<Self>(conn)
23 fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
24 use lemmy_db_schema::schema::post::dsl::*;
25 diesel::delete(post.find(post_id)).execute(conn)
28 fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
29 use lemmy_db_schema::schema::post::dsl::*;
30 insert_into(post).values(new_post).get_result::<Self>(conn)
33 fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
34 use lemmy_db_schema::schema::post::dsl::*;
35 diesel::update(post.find(post_id))
37 .get_result::<Self>(conn)
42 //fn read(conn: &PgConnection, post_id: i32) -> Result<Post, Error>;
43 fn list_for_community(conn: &PgConnection, the_community_id: i32) -> Result<Vec<Post>, Error>;
44 fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Post, Error>;
45 fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Post>, Error>;
46 fn update_deleted(conn: &PgConnection, post_id: i32, new_deleted: bool) -> Result<Post, Error>;
47 fn update_removed(conn: &PgConnection, post_id: i32, new_removed: bool) -> Result<Post, Error>;
48 fn update_removed_for_creator(
51 for_community_id: Option<i32>,
53 ) -> Result<Vec<Post>, Error>;
54 fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Post, Error>;
55 fn update_stickied(conn: &PgConnection, post_id: i32, new_stickied: bool) -> Result<Post, Error>;
56 fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool;
60 // TODO: this is a duplicate?
61 //fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
62 // use lemmy_db_schema::schema::post::dsl::*;
63 // post.filter(id.eq(post_id)).first::<Self>(conn)
66 fn list_for_community(conn: &PgConnection, the_community_id: i32) -> Result<Vec<Self>, Error> {
67 use lemmy_db_schema::schema::post::dsl::*;
69 .filter(community_id.eq(the_community_id))
70 .then_order_by(published.desc())
71 .then_order_by(stickied.desc())
76 fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Self, Error> {
77 use lemmy_db_schema::schema::post::dsl::*;
79 diesel::update(post.find(post_id))
80 .set(ap_id.eq(apub_id))
81 .get_result::<Self>(conn)
84 fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Self>, Error> {
85 use lemmy_db_schema::schema::post::dsl::*;
87 let perma_deleted = "*Permananently Deleted*";
88 let perma_deleted_url = "https://deleted.com";
90 diesel::update(post.filter(creator_id.eq(for_creator_id)))
92 name.eq(perma_deleted),
93 url.eq(perma_deleted_url),
94 body.eq(perma_deleted),
96 updated.eq(naive_now()),
98 .get_results::<Self>(conn)
101 fn update_deleted(conn: &PgConnection, post_id: i32, new_deleted: bool) -> Result<Self, Error> {
102 use lemmy_db_schema::schema::post::dsl::*;
103 diesel::update(post.find(post_id))
104 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
105 .get_result::<Self>(conn)
108 fn update_removed(conn: &PgConnection, post_id: i32, new_removed: bool) -> Result<Self, Error> {
109 use lemmy_db_schema::schema::post::dsl::*;
110 diesel::update(post.find(post_id))
111 .set((removed.eq(new_removed), updated.eq(naive_now())))
112 .get_result::<Self>(conn)
115 fn update_removed_for_creator(
118 for_community_id: Option<i32>,
120 ) -> Result<Vec<Self>, Error> {
121 use lemmy_db_schema::schema::post::dsl::*;
123 let mut update = diesel::update(post).into_boxed();
124 update = update.filter(creator_id.eq(for_creator_id));
126 if let Some(for_community_id) = for_community_id {
127 update = update.filter(community_id.eq(for_community_id));
131 .set((removed.eq(new_removed), updated.eq(naive_now())))
132 .get_results::<Self>(conn)
135 fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Self, Error> {
136 use lemmy_db_schema::schema::post::dsl::*;
137 diesel::update(post.find(post_id))
138 .set(locked.eq(new_locked))
139 .get_result::<Self>(conn)
142 fn update_stickied(conn: &PgConnection, post_id: i32, new_stickied: bool) -> Result<Self, Error> {
143 use lemmy_db_schema::schema::post::dsl::*;
144 diesel::update(post.find(post_id))
145 .set(stickied.eq(new_stickied))
146 .get_result::<Self>(conn)
149 fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool {
150 user_id == post_creator_id
154 impl ApubObject<PostForm> for Post {
155 fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
156 use lemmy_db_schema::schema::post::dsl::*;
157 post.filter(ap_id.eq(object_id)).first::<Self>(conn)
160 fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
161 use lemmy_db_schema::schema::post::dsl::*;
167 .get_result::<Self>(conn)
171 impl Likeable<PostLikeForm> for PostLike {
172 fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
173 use lemmy_db_schema::schema::post_like::dsl::*;
174 insert_into(post_like)
175 .values(post_like_form)
176 .on_conflict((post_id, user_id))
179 .get_result::<Self>(conn)
181 fn remove(conn: &PgConnection, user_id: i32, post_id: i32) -> Result<usize, Error> {
182 use lemmy_db_schema::schema::post_like::dsl;
185 .filter(dsl::post_id.eq(post_id))
186 .filter(dsl::user_id.eq(user_id)),
192 impl Saveable<PostSavedForm> for PostSaved {
193 fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
194 use lemmy_db_schema::schema::post_saved::dsl::*;
195 insert_into(post_saved)
196 .values(post_saved_form)
197 .on_conflict((post_id, user_id))
199 .set(post_saved_form)
200 .get_result::<Self>(conn)
202 fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
203 use lemmy_db_schema::schema::post_saved::dsl::*;
206 .filter(post_id.eq(post_saved_form.post_id))
207 .filter(user_id.eq(post_saved_form.user_id)),
213 impl Readable<PostReadForm> for PostRead {
214 fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
215 use lemmy_db_schema::schema::post_read::dsl::*;
216 insert_into(post_read)
217 .values(post_read_form)
218 .get_result::<Self>(conn)
221 fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
222 use lemmy_db_schema::schema::post_read::dsl::*;
225 .filter(post_id.eq(post_read_form.post_id))
226 .filter(user_id.eq(post_read_form.user_id)),
234 use crate::{source::post::*, tests::establish_unpooled_connection, ListingType, SortType};
235 use lemmy_db_schema::source::{
236 community::{Community, CommunityForm},
242 let conn = establish_unpooled_connection();
244 let new_user = UserForm {
246 preferred_username: None,
247 password_encrypted: "nope".into(),
249 matrix_user_id: None,
257 theme: "browser".into(),
258 default_sort_type: SortType::Hot as i16,
259 default_listing_type: ListingType::Subscribed as i16,
260 lang: "browser".into(),
262 send_notifications_to_email: false,
268 last_refreshed_at: None,
271 let inserted_user = User_::create(&conn, &new_user).unwrap();
273 let new_community = CommunityForm {
274 name: "test community_3".to_string(),
275 title: "nada".to_owned(),
278 creator_id: inserted_user.id,
287 last_refreshed_at: None,
293 let inserted_community = Community::create(&conn, &new_community).unwrap();
295 let new_post = PostForm {
296 name: "A test post".into(),
299 creator_id: inserted_user.id,
300 community_id: inserted_community.id,
308 embed_description: None,
316 let inserted_post = Post::create(&conn, &new_post).unwrap();
318 let expected_post = Post {
319 id: inserted_post.id,
320 name: "A test post".into(),
323 creator_id: inserted_user.id,
324 community_id: inserted_community.id,
325 published: inserted_post.published,
333 embed_description: None,
336 ap_id: inserted_post.ap_id.to_owned(),
341 let post_like_form = PostLikeForm {
342 post_id: inserted_post.id,
343 user_id: inserted_user.id,
347 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
349 let expected_post_like = PostLike {
350 id: inserted_post_like.id,
351 post_id: inserted_post.id,
352 user_id: inserted_user.id,
353 published: inserted_post_like.published,
358 let post_saved_form = PostSavedForm {
359 post_id: inserted_post.id,
360 user_id: inserted_user.id,
363 let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
365 let expected_post_saved = PostSaved {
366 id: inserted_post_saved.id,
367 post_id: inserted_post.id,
368 user_id: inserted_user.id,
369 published: inserted_post_saved.published,
373 let post_read_form = PostReadForm {
374 post_id: inserted_post.id,
375 user_id: inserted_user.id,
378 let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
380 let expected_post_read = PostRead {
381 id: inserted_post_read.id,
382 post_id: inserted_post.id,
383 user_id: inserted_user.id,
384 published: inserted_post_read.published,
387 let read_post = Post::read(&conn, inserted_post.id).unwrap();
388 let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
389 let like_removed = PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
390 let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
391 let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
392 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
393 Community::delete(&conn, inserted_community.id).unwrap();
394 User_::delete(&conn, inserted_user.id).unwrap();
396 assert_eq!(expected_post, read_post);
397 assert_eq!(expected_post, inserted_post);
398 assert_eq!(expected_post, updated_post);
399 assert_eq!(expected_post_like, inserted_post_like);
400 assert_eq!(expected_post_saved, inserted_post_saved);
401 assert_eq!(expected_post_read, inserted_post_read);
402 assert_eq!(1, like_removed);
403 assert_eq!(1, saved_removed);
404 assert_eq!(1, read_removed);
405 assert_eq!(1, num_deleted);