1 use crate::{ApubObject, Crud, DeleteableOrRemoveable, Likeable, Readable, Saveable};
2 use diesel::{dsl::*, result::Error, *};
21 impl Crud<PostForm, PostId> for Post {
22 fn read(conn: &PgConnection, post_id: PostId) -> Result<Self, Error> {
23 use lemmy_db_schema::schema::post::dsl::*;
24 post.find(post_id).first::<Self>(conn)
27 fn delete(conn: &PgConnection, post_id: PostId) -> Result<usize, Error> {
28 use lemmy_db_schema::schema::post::dsl::*;
29 diesel::delete(post.find(post_id)).execute(conn)
32 fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
33 use lemmy_db_schema::schema::post::dsl::*;
34 insert_into(post).values(new_post).get_result::<Self>(conn)
37 fn update(conn: &PgConnection, post_id: PostId, new_post: &PostForm) -> Result<Self, Error> {
38 use lemmy_db_schema::schema::post::dsl::*;
39 diesel::update(post.find(post_id))
41 .get_result::<Self>(conn)
46 //fn read(conn: &PgConnection, post_id: i32) -> Result<Post, Error>;
47 fn list_for_community(
49 the_community_id: CommunityId,
50 ) -> Result<Vec<Post>, Error>;
51 fn update_ap_id(conn: &PgConnection, post_id: PostId, apub_id: DbUrl) -> Result<Post, Error>;
52 fn permadelete_for_creator(
54 for_creator_id: PersonId,
55 ) -> Result<Vec<Post>, Error>;
56 fn update_deleted(conn: &PgConnection, post_id: PostId, new_deleted: bool)
57 -> Result<Post, Error>;
58 fn update_removed(conn: &PgConnection, post_id: PostId, new_removed: bool)
59 -> Result<Post, Error>;
60 fn update_removed_for_creator(
62 for_creator_id: PersonId,
63 for_community_id: Option<CommunityId>,
65 ) -> Result<Vec<Post>, Error>;
66 fn update_locked(conn: &PgConnection, post_id: PostId, new_locked: bool) -> Result<Post, Error>;
71 ) -> Result<Post, Error>;
72 fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool;
76 fn list_for_community(
78 the_community_id: CommunityId,
79 ) -> Result<Vec<Self>, Error> {
80 use lemmy_db_schema::schema::post::dsl::*;
82 .filter(community_id.eq(the_community_id))
83 .then_order_by(published.desc())
84 .then_order_by(stickied.desc())
89 fn update_ap_id(conn: &PgConnection, post_id: PostId, apub_id: DbUrl) -> Result<Self, Error> {
90 use lemmy_db_schema::schema::post::dsl::*;
92 diesel::update(post.find(post_id))
93 .set(ap_id.eq(apub_id))
94 .get_result::<Self>(conn)
97 fn permadelete_for_creator(
99 for_creator_id: PersonId,
100 ) -> Result<Vec<Self>, Error> {
101 use lemmy_db_schema::schema::post::dsl::*;
103 let perma_deleted = "*Permananently Deleted*";
104 let perma_deleted_url = "https://deleted.com";
106 diesel::update(post.filter(creator_id.eq(for_creator_id)))
108 name.eq(perma_deleted),
109 url.eq(perma_deleted_url),
110 body.eq(perma_deleted),
112 updated.eq(naive_now()),
114 .get_results::<Self>(conn)
121 ) -> Result<Self, Error> {
122 use lemmy_db_schema::schema::post::dsl::*;
123 diesel::update(post.find(post_id))
124 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
125 .get_result::<Self>(conn)
132 ) -> Result<Self, Error> {
133 use lemmy_db_schema::schema::post::dsl::*;
134 diesel::update(post.find(post_id))
135 .set((removed.eq(new_removed), updated.eq(naive_now())))
136 .get_result::<Self>(conn)
139 fn update_removed_for_creator(
141 for_creator_id: PersonId,
142 for_community_id: Option<CommunityId>,
144 ) -> Result<Vec<Self>, Error> {
145 use lemmy_db_schema::schema::post::dsl::*;
147 let mut update = diesel::update(post).into_boxed();
148 update = update.filter(creator_id.eq(for_creator_id));
150 if let Some(for_community_id) = for_community_id {
151 update = update.filter(community_id.eq(for_community_id));
155 .set((removed.eq(new_removed), updated.eq(naive_now())))
156 .get_results::<Self>(conn)
159 fn update_locked(conn: &PgConnection, post_id: PostId, new_locked: bool) -> Result<Self, Error> {
160 use lemmy_db_schema::schema::post::dsl::*;
161 diesel::update(post.find(post_id))
162 .set(locked.eq(new_locked))
163 .get_result::<Self>(conn)
170 ) -> Result<Self, Error> {
171 use lemmy_db_schema::schema::post::dsl::*;
172 diesel::update(post.find(post_id))
173 .set(stickied.eq(new_stickied))
174 .get_result::<Self>(conn)
177 fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool {
178 person_id == post_creator_id
182 impl ApubObject<PostForm> for Post {
183 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
184 use lemmy_db_schema::schema::post::dsl::*;
185 post.filter(ap_id.eq(object_id)).first::<Self>(conn)
188 fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
189 use lemmy_db_schema::schema::post::dsl::*;
195 .get_result::<Self>(conn)
199 impl Likeable<PostLikeForm, PostId> for PostLike {
200 fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
201 use lemmy_db_schema::schema::post_like::dsl::*;
202 insert_into(post_like)
203 .values(post_like_form)
204 .on_conflict((post_id, person_id))
207 .get_result::<Self>(conn)
209 fn remove(conn: &PgConnection, person_id: PersonId, post_id: PostId) -> Result<usize, Error> {
210 use lemmy_db_schema::schema::post_like::dsl;
213 .filter(dsl::post_id.eq(post_id))
214 .filter(dsl::person_id.eq(person_id)),
220 impl Saveable<PostSavedForm> for PostSaved {
221 fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
222 use lemmy_db_schema::schema::post_saved::dsl::*;
223 insert_into(post_saved)
224 .values(post_saved_form)
225 .on_conflict((post_id, person_id))
227 .set(post_saved_form)
228 .get_result::<Self>(conn)
230 fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
231 use lemmy_db_schema::schema::post_saved::dsl::*;
234 .filter(post_id.eq(post_saved_form.post_id))
235 .filter(person_id.eq(post_saved_form.person_id)),
241 impl Readable<PostReadForm> for PostRead {
242 fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
243 use lemmy_db_schema::schema::post_read::dsl::*;
244 insert_into(post_read)
245 .values(post_read_form)
246 .on_conflict((post_id, person_id))
249 .get_result::<Self>(conn)
252 fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
253 use lemmy_db_schema::schema::post_read::dsl::*;
256 .filter(post_id.eq(post_read_form.post_id))
257 .filter(person_id.eq(post_read_form.person_id)),
263 impl DeleteableOrRemoveable for Post {
264 fn blank_out_deleted_or_removed_info(mut self) -> Self {
265 self.name = "".into();
268 self.embed_title = None;
269 self.embed_description = None;
270 self.embed_html = None;
271 self.thumbnail_url = None;
279 use crate::{establish_unpooled_connection, source::post::*};
280 use lemmy_db_schema::source::{
281 community::{Community, CommunityForm},
284 use serial_test::serial;
289 let conn = establish_unpooled_connection();
291 let new_person = PersonForm {
293 ..PersonForm::default()
296 let inserted_person = Person::create(&conn, &new_person).unwrap();
298 let new_community = CommunityForm {
299 name: "test community_3".to_string(),
300 title: "nada".to_owned(),
301 ..CommunityForm::default()
304 let inserted_community = Community::create(&conn, &new_community).unwrap();
306 let new_post = PostForm {
307 name: "A test post".into(),
308 creator_id: inserted_person.id,
309 community_id: inserted_community.id,
310 ..PostForm::default()
313 let inserted_post = Post::create(&conn, &new_post).unwrap();
315 let expected_post = Post {
316 id: inserted_post.id,
317 name: "A test post".into(),
320 creator_id: inserted_person.id,
321 community_id: inserted_community.id,
322 published: inserted_post.published,
330 embed_description: None,
333 ap_id: inserted_post.ap_id.to_owned(),
338 let post_like_form = PostLikeForm {
339 post_id: inserted_post.id,
340 person_id: inserted_person.id,
344 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
346 let expected_post_like = PostLike {
347 id: inserted_post_like.id,
348 post_id: inserted_post.id,
349 person_id: inserted_person.id,
350 published: inserted_post_like.published,
355 let post_saved_form = PostSavedForm {
356 post_id: inserted_post.id,
357 person_id: inserted_person.id,
360 let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
362 let expected_post_saved = PostSaved {
363 id: inserted_post_saved.id,
364 post_id: inserted_post.id,
365 person_id: inserted_person.id,
366 published: inserted_post_saved.published,
370 let post_read_form = PostReadForm {
371 post_id: inserted_post.id,
372 person_id: inserted_person.id,
375 let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
377 let expected_post_read = PostRead {
378 id: inserted_post_read.id,
379 post_id: inserted_post.id,
380 person_id: inserted_person.id,
381 published: inserted_post_read.published,
384 let read_post = Post::read(&conn, inserted_post.id).unwrap();
385 let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
386 let like_removed = PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
387 let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
388 let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
389 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
390 Community::delete(&conn, inserted_community.id).unwrap();
391 Person::delete(&conn, inserted_person.id).unwrap();
393 assert_eq!(expected_post, read_post);
394 assert_eq!(expected_post, inserted_post);
395 assert_eq!(expected_post, updated_post);
396 assert_eq!(expected_post_like, inserted_post_like);
397 assert_eq!(expected_post_saved, inserted_post_saved);
398 assert_eq!(expected_post_read, inserted_post_read);
399 assert_eq!(1, like_removed);
400 assert_eq!(1, saved_removed);
401 assert_eq!(1, read_removed);
402 assert_eq!(1, num_deleted);