2 newtypes::{CommunityId, DbUrl, PersonId, PostId},
14 traits::{Crud, DeleteableOrRemoveable, Likeable, Readable, Saveable},
15 utils::{naive_now, FETCH_LIMIT_MAX},
17 use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, *};
21 type InsertForm = PostInsertForm;
22 type UpdateForm = PostUpdateForm;
24 fn read(conn: &mut PgConnection, post_id: PostId) -> Result<Self, Error> {
25 use crate::schema::post::dsl::*;
26 post.find(post_id).first::<Self>(conn)
29 fn delete(conn: &mut PgConnection, post_id: PostId) -> Result<usize, Error> {
30 use crate::schema::post::dsl::*;
31 diesel::delete(post.find(post_id)).execute(conn)
34 fn create(conn: &mut PgConnection, form: &Self::InsertForm) -> Result<Self, Error> {
35 use crate::schema::post::dsl::*;
41 .get_result::<Self>(conn)
45 conn: &mut PgConnection,
47 new_post: &Self::UpdateForm,
48 ) -> Result<Self, Error> {
49 use crate::schema::post::dsl::*;
50 diesel::update(post.find(post_id))
52 .get_result::<Self>(conn)
57 pub fn list_for_community(
58 conn: &mut PgConnection,
59 the_community_id: CommunityId,
60 ) -> Result<Vec<Self>, Error> {
61 use crate::schema::post::dsl::*;
63 .filter(community_id.eq(the_community_id))
64 .filter(deleted.eq(false))
65 .filter(removed.eq(false))
66 .then_order_by(published.desc())
67 .then_order_by(stickied.desc())
68 .limit(FETCH_LIMIT_MAX)
72 pub fn permadelete_for_creator(
73 conn: &mut PgConnection,
74 for_creator_id: PersonId,
75 ) -> Result<Vec<Self>, Error> {
76 use crate::schema::post::dsl::*;
78 let perma_deleted = "*Permananently Deleted*";
79 let perma_deleted_url = "https://deleted.com";
81 diesel::update(post.filter(creator_id.eq(for_creator_id)))
83 name.eq(perma_deleted),
84 url.eq(perma_deleted_url),
85 body.eq(perma_deleted),
87 updated.eq(naive_now()),
89 .get_results::<Self>(conn)
92 pub fn update_removed_for_creator(
93 conn: &mut PgConnection,
94 for_creator_id: PersonId,
95 for_community_id: Option<CommunityId>,
97 ) -> Result<Vec<Self>, Error> {
98 use crate::schema::post::dsl::*;
100 let mut update = diesel::update(post).into_boxed();
101 update = update.filter(creator_id.eq(for_creator_id));
103 if let Some(for_community_id) = for_community_id {
104 update = update.filter(community_id.eq(for_community_id));
108 .set((removed.eq(new_removed), updated.eq(naive_now())))
109 .get_results::<Self>(conn)
112 pub fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool {
113 person_id == post_creator_id
116 pub fn read_from_apub_id(conn: &mut PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
117 use crate::schema::post::dsl::*;
118 let object_id: DbUrl = object_id.into();
121 .filter(ap_id.eq(object_id))
128 pub fn fetch_pictrs_posts_for_creator(
129 conn: &mut PgConnection,
130 for_creator_id: PersonId,
131 ) -> Result<Vec<Self>, Error> {
132 use crate::schema::post::dsl::*;
133 let pictrs_search = "%pictrs/image%";
136 .filter(creator_id.eq(for_creator_id))
137 .filter(url.like(pictrs_search))
141 /// Sets the url and thumbnails fields to None
142 pub fn remove_pictrs_post_images_and_thumbnails_for_creator(
143 conn: &mut PgConnection,
144 for_creator_id: PersonId,
145 ) -> Result<Vec<Self>, Error> {
146 use crate::schema::post::dsl::*;
147 let pictrs_search = "%pictrs/image%";
151 .filter(creator_id.eq(for_creator_id))
152 .filter(url.like(pictrs_search)),
155 url.eq::<Option<String>>(None),
156 thumbnail_url.eq::<Option<String>>(None),
158 .get_results::<Self>(conn)
161 pub fn fetch_pictrs_posts_for_community(
162 conn: &mut PgConnection,
163 for_community_id: CommunityId,
164 ) -> Result<Vec<Self>, Error> {
165 use crate::schema::post::dsl::*;
166 let pictrs_search = "%pictrs/image%";
168 .filter(community_id.eq(for_community_id))
169 .filter(url.like(pictrs_search))
173 /// Sets the url and thumbnails fields to None
174 pub fn remove_pictrs_post_images_and_thumbnails_for_community(
175 conn: &mut PgConnection,
176 for_community_id: CommunityId,
177 ) -> Result<Vec<Self>, Error> {
178 use crate::schema::post::dsl::*;
179 let pictrs_search = "%pictrs/image%";
183 .filter(community_id.eq(for_community_id))
184 .filter(url.like(pictrs_search)),
187 url.eq::<Option<String>>(None),
188 thumbnail_url.eq::<Option<String>>(None),
190 .get_results::<Self>(conn)
194 impl Likeable for PostLike {
195 type Form = PostLikeForm;
196 type IdType = PostId;
197 fn like(conn: &mut PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
198 use crate::schema::post_like::dsl::*;
199 insert_into(post_like)
200 .values(post_like_form)
201 .on_conflict((post_id, person_id))
204 .get_result::<Self>(conn)
206 fn remove(conn: &mut PgConnection, person_id: PersonId, post_id: PostId) -> Result<usize, Error> {
207 use crate::schema::post_like::dsl;
210 .filter(dsl::post_id.eq(post_id))
211 .filter(dsl::person_id.eq(person_id)),
217 impl Saveable for PostSaved {
218 type Form = PostSavedForm;
219 fn save(conn: &mut PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
220 use crate::schema::post_saved::dsl::*;
221 insert_into(post_saved)
222 .values(post_saved_form)
223 .on_conflict((post_id, person_id))
225 .set(post_saved_form)
226 .get_result::<Self>(conn)
228 fn unsave(conn: &mut PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
229 use crate::schema::post_saved::dsl::*;
232 .filter(post_id.eq(post_saved_form.post_id))
233 .filter(person_id.eq(post_saved_form.person_id)),
239 impl Readable for PostRead {
240 type Form = PostReadForm;
241 fn mark_as_read(conn: &mut PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
242 use crate::schema::post_read::dsl::*;
243 insert_into(post_read)
244 .values(post_read_form)
245 .on_conflict((post_id, person_id))
248 .get_result::<Self>(conn)
252 conn: &mut PgConnection,
253 post_read_form: &PostReadForm,
254 ) -> Result<usize, Error> {
255 use crate::schema::post_read::dsl::*;
258 .filter(post_id.eq(post_read_form.post_id))
259 .filter(person_id.eq(post_read_form.person_id)),
265 impl DeleteableOrRemoveable for Post {
266 fn blank_out_deleted_or_removed_info(mut self) -> Self {
267 self.name = "".into();
270 self.embed_title = None;
271 self.embed_description = None;
272 self.embed_video_url = None;
273 self.thumbnail_url = None;
283 community::{Community, CommunityInsertForm},
288 traits::{Crud, Likeable, Readable, Saveable},
289 utils::establish_unpooled_connection,
291 use serial_test::serial;
296 let conn = &mut establish_unpooled_connection();
298 let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
300 let new_person = PersonInsertForm::builder()
302 .public_key("pubkey".to_string())
303 .instance_id(inserted_instance.id)
306 let inserted_person = Person::create(conn, &new_person).unwrap();
308 let new_community = CommunityInsertForm::builder()
309 .name("test community_3".to_string())
310 .title("nada".to_owned())
311 .public_key("pubkey".to_string())
312 .instance_id(inserted_instance.id)
315 let inserted_community = Community::create(conn, &new_community).unwrap();
317 let new_post = PostInsertForm::builder()
318 .name("A test post".into())
319 .creator_id(inserted_person.id)
320 .community_id(inserted_community.id)
323 let inserted_post = Post::create(conn, &new_post).unwrap();
325 let expected_post = Post {
326 id: inserted_post.id,
327 name: "A test post".into(),
330 creator_id: inserted_person.id,
331 community_id: inserted_community.id,
332 published: inserted_post.published,
340 embed_description: None,
341 embed_video_url: None,
343 ap_id: inserted_post.ap_id.to_owned(),
345 language_id: Default::default(),
349 let post_like_form = PostLikeForm {
350 post_id: inserted_post.id,
351 person_id: inserted_person.id,
355 let inserted_post_like = PostLike::like(conn, &post_like_form).unwrap();
357 let expected_post_like = PostLike {
358 id: inserted_post_like.id,
359 post_id: inserted_post.id,
360 person_id: inserted_person.id,
361 published: inserted_post_like.published,
366 let post_saved_form = PostSavedForm {
367 post_id: inserted_post.id,
368 person_id: inserted_person.id,
371 let inserted_post_saved = PostSaved::save(conn, &post_saved_form).unwrap();
373 let expected_post_saved = PostSaved {
374 id: inserted_post_saved.id,
375 post_id: inserted_post.id,
376 person_id: inserted_person.id,
377 published: inserted_post_saved.published,
381 let post_read_form = PostReadForm {
382 post_id: inserted_post.id,
383 person_id: inserted_person.id,
386 let inserted_post_read = PostRead::mark_as_read(conn, &post_read_form).unwrap();
388 let expected_post_read = PostRead {
389 id: inserted_post_read.id,
390 post_id: inserted_post.id,
391 person_id: inserted_person.id,
392 published: inserted_post_read.published,
395 let read_post = Post::read(conn, inserted_post.id).unwrap();
397 let new_post_update = PostUpdateForm::builder()
398 .name(Some("A test post".into()))
400 let updated_post = Post::update(conn, inserted_post.id, &new_post_update).unwrap();
402 let like_removed = PostLike::remove(conn, inserted_person.id, inserted_post.id).unwrap();
403 let saved_removed = PostSaved::unsave(conn, &post_saved_form).unwrap();
404 let read_removed = PostRead::mark_as_unread(conn, &post_read_form).unwrap();
405 let num_deleted = Post::delete(conn, inserted_post.id).unwrap();
406 Community::delete(conn, inserted_community.id).unwrap();
407 Person::delete(conn, inserted_person.id).unwrap();
408 Instance::delete(conn, inserted_instance.id).unwrap();
410 assert_eq!(expected_post, read_post);
411 assert_eq!(expected_post, inserted_post);
412 assert_eq!(expected_post, updated_post);
413 assert_eq!(expected_post_like, inserted_post_like);
414 assert_eq!(expected_post_saved, inserted_post_saved);
415 assert_eq!(expected_post_read, inserted_post_read);
416 assert_eq!(1, like_removed);
417 assert_eq!(1, saved_removed);
418 assert_eq!(1, read_removed);
419 assert_eq!(1, num_deleted);