3 schema::{post, post_like, post_read, post_saved},
9 use diesel::{dsl::*, result::Error, *};
10 use url::{ParseError, Url};
12 #[derive(Queryable, Identifiable, PartialEq, Debug)]
13 #[table_name = "post"]
17 pub url: Option<String>,
18 pub body: Option<String>,
20 pub community_id: i32,
23 pub published: chrono::NaiveDateTime,
24 pub updated: Option<chrono::NaiveDateTime>,
28 pub embed_title: Option<String>,
29 pub embed_description: Option<String>,
30 pub embed_html: Option<String>,
31 pub thumbnail_url: Option<String>,
36 #[derive(Insertable, AsChangeset)]
37 #[table_name = "post"]
40 pub url: Option<String>,
41 pub body: Option<String>,
43 pub community_id: i32,
44 pub removed: Option<bool>,
45 pub locked: Option<bool>,
46 pub published: Option<chrono::NaiveDateTime>,
47 pub updated: Option<chrono::NaiveDateTime>,
48 pub deleted: Option<bool>,
50 pub stickied: Option<bool>,
51 pub embed_title: Option<String>,
52 pub embed_description: Option<String>,
53 pub embed_html: Option<String>,
54 pub thumbnail_url: Option<String>,
55 pub ap_id: Option<String>,
60 pub fn get_ap_id(&self) -> Result<Url, ParseError> {
61 Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string()))
66 pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
67 use crate::schema::post::dsl::*;
68 post.filter(id.eq(post_id)).first::<Self>(conn)
71 pub fn list_for_community(
73 the_community_id: i32,
74 ) -> Result<Vec<Self>, Error> {
75 use crate::schema::post::dsl::*;
77 .filter(community_id.eq(the_community_id))
78 .then_order_by(published.desc())
79 .then_order_by(stickied.desc())
84 pub fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
85 use crate::schema::post::dsl::*;
86 post.filter(ap_id.eq(object_id)).first::<Self>(conn)
89 pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Self, Error> {
90 use crate::schema::post::dsl::*;
92 diesel::update(post.find(post_id))
93 .set(ap_id.eq(apub_id))
94 .get_result::<Self>(conn)
97 pub fn permadelete_for_creator(
100 ) -> Result<Vec<Self>, Error> {
101 use crate::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)
117 pub fn update_deleted(
121 ) -> Result<Self, Error> {
122 use crate::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)
128 pub fn update_removed(
132 ) -> Result<Self, Error> {
133 use crate::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 pub fn update_removed_for_creator(
142 for_community_id: Option<i32>,
144 ) -> Result<Vec<Self>, Error> {
145 use crate::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 pub fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Self, Error> {
160 use crate::schema::post::dsl::*;
161 diesel::update(post.find(post_id))
162 .set(locked.eq(new_locked))
163 .get_result::<Self>(conn)
166 pub fn update_stickied(
170 ) -> Result<Self, Error> {
171 use crate::schema::post::dsl::*;
172 diesel::update(post.find(post_id))
173 .set(stickied.eq(new_stickied))
174 .get_result::<Self>(conn)
177 pub fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool {
178 user_id == post_creator_id
181 pub fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
182 use crate::schema::post::dsl::*;
188 .get_result::<Self>(conn)
192 impl Crud<PostForm> for Post {
193 fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
194 use crate::schema::post::dsl::*;
195 post.find(post_id).first::<Self>(conn)
198 fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
199 use crate::schema::post::dsl::*;
200 diesel::delete(post.find(post_id)).execute(conn)
203 fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
204 use crate::schema::post::dsl::*;
205 insert_into(post).values(new_post).get_result::<Self>(conn)
208 fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
209 use crate::schema::post::dsl::*;
210 diesel::update(post.find(post_id))
212 .get_result::<Self>(conn)
216 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
218 #[table_name = "post_like"]
219 pub struct PostLike {
224 pub published: chrono::NaiveDateTime,
227 #[derive(Insertable, AsChangeset, Clone)]
228 #[table_name = "post_like"]
229 pub struct PostLikeForm {
235 impl Likeable<PostLikeForm> for PostLike {
236 fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
237 use crate::schema::post_like::dsl::*;
238 insert_into(post_like)
239 .values(post_like_form)
240 .get_result::<Self>(conn)
242 fn remove(conn: &PgConnection, user_id: i32, post_id: i32) -> Result<usize, Error> {
243 use crate::schema::post_like::dsl;
246 .filter(dsl::post_id.eq(post_id))
247 .filter(dsl::user_id.eq(user_id)),
253 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
255 #[table_name = "post_saved"]
256 pub struct PostSaved {
260 pub published: chrono::NaiveDateTime,
263 #[derive(Insertable, AsChangeset)]
264 #[table_name = "post_saved"]
265 pub struct PostSavedForm {
270 impl Saveable<PostSavedForm> for PostSaved {
271 fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
272 use crate::schema::post_saved::dsl::*;
273 insert_into(post_saved)
274 .values(post_saved_form)
275 .get_result::<Self>(conn)
277 fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
278 use crate::schema::post_saved::dsl::*;
281 .filter(post_id.eq(post_saved_form.post_id))
282 .filter(user_id.eq(post_saved_form.user_id)),
288 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
290 #[table_name = "post_read"]
291 pub struct PostRead {
298 pub published: chrono::NaiveDateTime,
301 #[derive(Insertable, AsChangeset)]
302 #[table_name = "post_read"]
303 pub struct PostReadForm {
309 impl Readable<PostReadForm> for PostRead {
310 fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
311 use crate::schema::post_read::dsl::*;
312 insert_into(post_read)
313 .values(post_read_form)
314 .get_result::<Self>(conn)
317 fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
318 use crate::schema::post_read::dsl::*;
321 .filter(post_id.eq(post_read_form.post_id))
322 .filter(user_id.eq(post_read_form.user_id)),
333 tests::establish_unpooled_connection,
341 let conn = establish_unpooled_connection();
343 let new_user = UserForm {
345 preferred_username: None,
346 password_encrypted: "nope".into(),
348 matrix_user_id: None,
355 theme: "darkly".into(),
356 default_sort_type: SortType::Hot as i16,
357 default_listing_type: ListingType::Subscribed as i16,
358 lang: "browser".into(),
360 send_notifications_to_email: false,
366 last_refreshed_at: None,
369 let inserted_user = User_::create(&conn, &new_user).unwrap();
371 let new_community = CommunityForm {
372 name: "test community_3".to_string(),
373 title: "nada".to_owned(),
376 creator_id: inserted_user.id,
385 last_refreshed_at: None,
391 let inserted_community = Community::create(&conn, &new_community).unwrap();
393 let new_post = PostForm {
394 name: "A test post".into(),
397 creator_id: inserted_user.id,
398 community_id: inserted_community.id,
406 embed_description: None,
414 let inserted_post = Post::create(&conn, &new_post).unwrap();
416 let expected_post = Post {
417 id: inserted_post.id,
418 name: "A test post".into(),
421 creator_id: inserted_user.id,
422 community_id: inserted_community.id,
423 published: inserted_post.published,
431 embed_description: None,
434 ap_id: inserted_post.ap_id.to_owned(),
439 let post_like_form = PostLikeForm {
440 post_id: inserted_post.id,
441 user_id: inserted_user.id,
445 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
447 let expected_post_like = PostLike {
448 id: inserted_post_like.id,
449 post_id: inserted_post.id,
450 user_id: inserted_user.id,
451 published: inserted_post_like.published,
456 let post_saved_form = PostSavedForm {
457 post_id: inserted_post.id,
458 user_id: inserted_user.id,
461 let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
463 let expected_post_saved = PostSaved {
464 id: inserted_post_saved.id,
465 post_id: inserted_post.id,
466 user_id: inserted_user.id,
467 published: inserted_post_saved.published,
471 let post_read_form = PostReadForm {
472 post_id: inserted_post.id,
473 user_id: inserted_user.id,
476 let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
478 let expected_post_read = PostRead {
479 id: inserted_post_read.id,
480 post_id: inserted_post.id,
481 user_id: inserted_user.id,
482 published: inserted_post_read.published,
485 let read_post = Post::read(&conn, inserted_post.id).unwrap();
486 let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
487 let like_removed = PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
488 let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
489 let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
490 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
491 Community::delete(&conn, inserted_community.id).unwrap();
492 User_::delete(&conn, inserted_user.id).unwrap();
494 assert_eq!(expected_post, read_post);
495 assert_eq!(expected_post, inserted_post);
496 assert_eq!(expected_post, updated_post);
497 assert_eq!(expected_post_like, inserted_post_like);
498 assert_eq!(expected_post_saved, inserted_post_saved);
499 assert_eq!(expected_post_read, inserted_post_read);
500 assert_eq!(1, like_removed);
501 assert_eq!(1, saved_removed);
502 assert_eq!(1, read_removed);
503 assert_eq!(1, num_deleted);