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: Url) -> 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: Url) -> 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: &Url) -> 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},
237 let conn = establish_unpooled_connection();
239 let new_user = UserForm {
241 preferred_username: None,
242 password_encrypted: "nope".into(),
244 matrix_user_id: None,
252 theme: "browser".into(),
253 default_sort_type: SortType::Hot as i16,
254 default_listing_type: ListingType::Subscribed as i16,
255 lang: "browser".into(),
257 send_notifications_to_email: false,
263 last_refreshed_at: None,
265 shared_inbox_url: None,
268 let inserted_user = User_::create(&conn, &new_user).unwrap();
270 let new_community = CommunityForm {
271 name: "test community_3".to_string(),
272 title: "nada".to_owned(),
274 creator_id: inserted_user.id,
283 last_refreshed_at: None,
289 shared_inbox_url: None,
292 let inserted_community = Community::create(&conn, &new_community).unwrap();
294 let new_post = PostForm {
295 name: "A test post".into(),
298 creator_id: inserted_user.id,
299 community_id: inserted_community.id,
307 embed_description: None,
315 let inserted_post = Post::create(&conn, &new_post).unwrap();
317 let expected_post = Post {
318 id: inserted_post.id,
319 name: "A test post".into(),
322 creator_id: inserted_user.id,
323 community_id: inserted_community.id,
324 published: inserted_post.published,
332 embed_description: None,
335 ap_id: inserted_post.ap_id.to_owned(),
340 let post_like_form = PostLikeForm {
341 post_id: inserted_post.id,
342 user_id: inserted_user.id,
346 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
348 let expected_post_like = PostLike {
349 id: inserted_post_like.id,
350 post_id: inserted_post.id,
351 user_id: inserted_user.id,
352 published: inserted_post_like.published,
357 let post_saved_form = PostSavedForm {
358 post_id: inserted_post.id,
359 user_id: inserted_user.id,
362 let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
364 let expected_post_saved = PostSaved {
365 id: inserted_post_saved.id,
366 post_id: inserted_post.id,
367 user_id: inserted_user.id,
368 published: inserted_post_saved.published,
372 let post_read_form = PostReadForm {
373 post_id: inserted_post.id,
374 user_id: inserted_user.id,
377 let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
379 let expected_post_read = PostRead {
380 id: inserted_post_read.id,
381 post_id: inserted_post.id,
382 user_id: inserted_user.id,
383 published: inserted_post_read.published,
386 let read_post = Post::read(&conn, inserted_post.id).unwrap();
387 let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
388 let like_removed = PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
389 let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
390 let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
391 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
392 Community::delete(&conn, inserted_community.id).unwrap();
393 User_::delete(&conn, inserted_user.id).unwrap();
395 assert_eq!(expected_post, read_post);
396 assert_eq!(expected_post, inserted_post);
397 assert_eq!(expected_post, updated_post);
398 assert_eq!(expected_post_like, inserted_post_like);
399 assert_eq!(expected_post_saved, inserted_post_saved);
400 assert_eq!(expected_post_read, inserted_post_read);
401 assert_eq!(1, like_removed);
402 assert_eq!(1, saved_removed);
403 assert_eq!(1, read_removed);
404 assert_eq!(1, num_deleted);