2 newtypes::{CommunityId, DbUrl, PersonId, PostId},
13 traits::{Crud, DeleteableOrRemoveable, Likeable, Readable, Saveable},
14 utils::{naive_now, FETCH_LIMIT_MAX},
16 use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, *};
22 fn read(conn: &PgConnection, post_id: PostId) -> Result<Self, Error> {
23 use crate::schema::post::dsl::*;
24 post.find(post_id).first::<Self>(conn)
27 fn delete(conn: &PgConnection, post_id: PostId) -> Result<usize, Error> {
28 use crate::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 crate::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 crate::schema::post::dsl::*;
39 diesel::update(post.find(post_id))
41 .get_result::<Self>(conn)
46 pub fn list_for_community(
48 the_community_id: CommunityId,
49 ) -> Result<Vec<Self>, Error> {
50 use crate::schema::post::dsl::*;
52 .filter(community_id.eq(the_community_id))
53 .filter(deleted.eq(false))
54 .filter(removed.eq(false))
55 .then_order_by(published.desc())
56 .then_order_by(stickied.desc())
57 .limit(FETCH_LIMIT_MAX)
61 pub fn update_ap_id(conn: &PgConnection, post_id: PostId, apub_id: DbUrl) -> Result<Self, Error> {
62 use crate::schema::post::dsl::*;
64 diesel::update(post.find(post_id))
65 .set(ap_id.eq(apub_id))
66 .get_result::<Self>(conn)
69 pub fn permadelete_for_creator(
71 for_creator_id: PersonId,
72 ) -> Result<Vec<Self>, Error> {
73 use crate::schema::post::dsl::*;
75 let perma_deleted = "*Permananently Deleted*";
76 let perma_deleted_url = "https://deleted.com";
78 diesel::update(post.filter(creator_id.eq(for_creator_id)))
80 name.eq(perma_deleted),
81 url.eq(perma_deleted_url),
82 body.eq(perma_deleted),
84 updated.eq(naive_now()),
86 .get_results::<Self>(conn)
89 pub fn update_deleted(
93 ) -> Result<Self, Error> {
94 use crate::schema::post::dsl::*;
95 diesel::update(post.find(post_id))
96 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
97 .get_result::<Self>(conn)
100 pub fn update_removed(
104 ) -> Result<Self, Error> {
105 use crate::schema::post::dsl::*;
106 diesel::update(post.find(post_id))
107 .set((removed.eq(new_removed), updated.eq(naive_now())))
108 .get_result::<Self>(conn)
111 pub fn update_removed_for_creator(
113 for_creator_id: PersonId,
114 for_community_id: Option<CommunityId>,
116 ) -> Result<Vec<Self>, Error> {
117 use crate::schema::post::dsl::*;
119 let mut update = diesel::update(post).into_boxed();
120 update = update.filter(creator_id.eq(for_creator_id));
122 if let Some(for_community_id) = for_community_id {
123 update = update.filter(community_id.eq(for_community_id));
127 .set((removed.eq(new_removed), updated.eq(naive_now())))
128 .get_results::<Self>(conn)
131 pub fn update_locked(
135 ) -> Result<Self, Error> {
136 use crate::schema::post::dsl::*;
137 diesel::update(post.find(post_id))
138 .set(locked.eq(new_locked))
139 .get_result::<Self>(conn)
142 pub fn update_stickied(
146 ) -> Result<Self, Error> {
147 use crate::schema::post::dsl::*;
148 diesel::update(post.find(post_id))
149 .set(stickied.eq(new_stickied))
150 .get_result::<Self>(conn)
153 pub fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool {
154 person_id == post_creator_id
157 pub fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
158 use crate::schema::post::dsl::*;
164 .get_result::<Self>(conn)
166 pub fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
167 use crate::schema::post::dsl::*;
168 let object_id: DbUrl = object_id.into();
171 .filter(ap_id.eq(object_id))
178 pub fn fetch_pictrs_posts_for_creator(
180 for_creator_id: PersonId,
181 ) -> Result<Vec<Self>, Error> {
182 use crate::schema::post::dsl::*;
183 let pictrs_search = "%pictrs/image%";
186 .filter(creator_id.eq(for_creator_id))
187 .filter(url.like(pictrs_search))
191 /// Sets the url and thumbnails fields to None
192 pub fn remove_pictrs_post_images_and_thumbnails_for_creator(
194 for_creator_id: PersonId,
195 ) -> Result<Vec<Self>, Error> {
196 use crate::schema::post::dsl::*;
197 let pictrs_search = "%pictrs/image%";
201 .filter(creator_id.eq(for_creator_id))
202 .filter(url.like(pictrs_search)),
205 url.eq::<Option<String>>(None),
206 thumbnail_url.eq::<Option<String>>(None),
208 .get_results::<Self>(conn)
211 pub fn fetch_pictrs_posts_for_community(
213 for_community_id: CommunityId,
214 ) -> Result<Vec<Self>, Error> {
215 use crate::schema::post::dsl::*;
216 let pictrs_search = "%pictrs/image%";
218 .filter(community_id.eq(for_community_id))
219 .filter(url.like(pictrs_search))
223 /// Sets the url and thumbnails fields to None
224 pub fn remove_pictrs_post_images_and_thumbnails_for_community(
226 for_community_id: CommunityId,
227 ) -> Result<Vec<Self>, Error> {
228 use crate::schema::post::dsl::*;
229 let pictrs_search = "%pictrs/image%";
233 .filter(community_id.eq(for_community_id))
234 .filter(url.like(pictrs_search)),
237 url.eq::<Option<String>>(None),
238 thumbnail_url.eq::<Option<String>>(None),
240 .get_results::<Self>(conn)
244 impl Likeable for PostLike {
245 type Form = PostLikeForm;
246 type IdType = PostId;
247 fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
248 use crate::schema::post_like::dsl::*;
249 insert_into(post_like)
250 .values(post_like_form)
251 .on_conflict((post_id, person_id))
254 .get_result::<Self>(conn)
256 fn remove(conn: &PgConnection, person_id: PersonId, post_id: PostId) -> Result<usize, Error> {
257 use crate::schema::post_like::dsl;
260 .filter(dsl::post_id.eq(post_id))
261 .filter(dsl::person_id.eq(person_id)),
267 impl Saveable for PostSaved {
268 type Form = PostSavedForm;
269 fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
270 use crate::schema::post_saved::dsl::*;
271 insert_into(post_saved)
272 .values(post_saved_form)
273 .on_conflict((post_id, person_id))
275 .set(post_saved_form)
276 .get_result::<Self>(conn)
278 fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
279 use crate::schema::post_saved::dsl::*;
282 .filter(post_id.eq(post_saved_form.post_id))
283 .filter(person_id.eq(post_saved_form.person_id)),
289 impl Readable for PostRead {
290 type Form = PostReadForm;
291 fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
292 use crate::schema::post_read::dsl::*;
293 insert_into(post_read)
294 .values(post_read_form)
295 .on_conflict((post_id, person_id))
298 .get_result::<Self>(conn)
301 fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
302 use crate::schema::post_read::dsl::*;
305 .filter(post_id.eq(post_read_form.post_id))
306 .filter(person_id.eq(post_read_form.person_id)),
312 impl DeleteableOrRemoveable for Post {
313 fn blank_out_deleted_or_removed_info(mut self) -> Self {
314 self.name = "".into();
317 self.embed_title = None;
318 self.embed_description = None;
319 self.embed_video_url = None;
320 self.thumbnail_url = None;
330 community::{Community, CommunityForm},
334 traits::{Crud, Likeable, Readable, Saveable},
335 utils::establish_unpooled_connection,
337 use serial_test::serial;
342 let conn = establish_unpooled_connection();
344 let new_person = PersonForm {
346 public_key: Some("pubkey".to_string()),
347 ..PersonForm::default()
350 let inserted_person = Person::create(&conn, &new_person).unwrap();
352 let new_community = CommunityForm {
353 name: "test community_3".to_string(),
354 title: "nada".to_owned(),
355 public_key: Some("pubkey".to_string()),
356 ..CommunityForm::default()
359 let inserted_community = Community::create(&conn, &new_community).unwrap();
361 let new_post = PostForm {
362 name: "A test post".into(),
363 creator_id: inserted_person.id,
364 community_id: inserted_community.id,
365 ..PostForm::default()
368 let inserted_post = Post::create(&conn, &new_post).unwrap();
370 let expected_post = Post {
371 id: inserted_post.id,
372 name: "A test post".into(),
375 creator_id: inserted_person.id,
376 community_id: inserted_community.id,
377 published: inserted_post.published,
385 embed_description: None,
386 embed_video_url: None,
388 ap_id: inserted_post.ap_id.to_owned(),
393 let post_like_form = PostLikeForm {
394 post_id: inserted_post.id,
395 person_id: inserted_person.id,
399 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
401 let expected_post_like = PostLike {
402 id: inserted_post_like.id,
403 post_id: inserted_post.id,
404 person_id: inserted_person.id,
405 published: inserted_post_like.published,
410 let post_saved_form = PostSavedForm {
411 post_id: inserted_post.id,
412 person_id: inserted_person.id,
415 let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
417 let expected_post_saved = PostSaved {
418 id: inserted_post_saved.id,
419 post_id: inserted_post.id,
420 person_id: inserted_person.id,
421 published: inserted_post_saved.published,
425 let post_read_form = PostReadForm {
426 post_id: inserted_post.id,
427 person_id: inserted_person.id,
430 let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
432 let expected_post_read = PostRead {
433 id: inserted_post_read.id,
434 post_id: inserted_post.id,
435 person_id: inserted_person.id,
436 published: inserted_post_read.published,
439 let read_post = Post::read(&conn, inserted_post.id).unwrap();
440 let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
441 let like_removed = PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
442 let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
443 let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
444 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
445 Community::delete(&conn, inserted_community.id).unwrap();
446 Person::delete(&conn, inserted_person.id).unwrap();
448 assert_eq!(expected_post, read_post);
449 assert_eq!(expected_post, inserted_post);
450 assert_eq!(expected_post, updated_post);
451 assert_eq!(expected_post_like, inserted_post_like);
452 assert_eq!(expected_post_saved, inserted_post_saved);
453 assert_eq!(expected_post_read, inserted_post_read);
454 assert_eq!(1, like_removed);
455 assert_eq!(1, saved_removed);
456 assert_eq!(1, read_removed);
457 assert_eq!(1, num_deleted);