3 schema::{post, post_like, post_read, post_saved},
9 use diesel::{dsl::*, result::Error, *};
10 use serde::{Deserialize, Serialize};
11 use url::{ParseError, Url};
13 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
14 #[table_name = "post"]
18 pub url: Option<String>,
19 pub body: Option<String>,
21 pub community_id: i32,
24 pub published: chrono::NaiveDateTime,
25 pub updated: Option<chrono::NaiveDateTime>,
29 pub embed_title: Option<String>,
30 pub embed_description: Option<String>,
31 pub embed_html: Option<String>,
32 pub thumbnail_url: Option<String>,
37 #[derive(Insertable, AsChangeset, Clone, Debug)]
38 #[table_name = "post"]
41 pub url: Option<String>,
42 pub body: Option<String>,
44 pub community_id: i32,
45 pub removed: Option<bool>,
46 pub locked: Option<bool>,
47 pub published: Option<chrono::NaiveDateTime>,
48 pub updated: Option<chrono::NaiveDateTime>,
49 pub deleted: Option<bool>,
51 pub stickied: Option<bool>,
52 pub embed_title: Option<String>,
53 pub embed_description: Option<String>,
54 pub embed_html: Option<String>,
55 pub thumbnail_url: Option<String>,
61 pub fn get_ap_id(&self) -> Result<Url, ParseError> {
62 Url::parse(&self.ap_id)
67 pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
68 use crate::schema::post::dsl::*;
69 post.filter(id.eq(post_id)).first::<Self>(conn)
72 pub fn list_for_community(
74 the_community_id: i32,
75 ) -> Result<Vec<Self>, Error> {
76 use crate::schema::post::dsl::*;
78 .filter(community_id.eq(the_community_id))
79 .then_order_by(published.desc())
80 .then_order_by(stickied.desc())
85 pub fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
86 use crate::schema::post::dsl::*;
87 post.filter(ap_id.eq(object_id)).first::<Self>(conn)
90 pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Self, Error> {
91 use crate::schema::post::dsl::*;
93 diesel::update(post.find(post_id))
94 .set(ap_id.eq(apub_id))
95 .get_result::<Self>(conn)
98 pub fn permadelete(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
99 use crate::schema::post::dsl::*;
101 let perma_deleted = "*Permananently Deleted*";
102 let perma_deleted_url = "https://deleted.com";
104 diesel::update(post.find(post_id))
106 name.eq(perma_deleted),
107 url.eq(perma_deleted_url),
108 body.eq(perma_deleted),
110 updated.eq(naive_now()),
112 .get_result::<Self>(conn)
115 pub fn update_deleted(
119 ) -> Result<Self, Error> {
120 use crate::schema::post::dsl::*;
121 diesel::update(post.find(post_id))
122 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
123 .get_result::<Self>(conn)
126 pub fn update_removed(
130 ) -> Result<Self, Error> {
131 use crate::schema::post::dsl::*;
132 diesel::update(post.find(post_id))
133 .set((removed.eq(new_removed), updated.eq(naive_now())))
134 .get_result::<Self>(conn)
137 pub fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Self, Error> {
138 use crate::schema::post::dsl::*;
139 diesel::update(post.find(post_id))
140 .set(locked.eq(new_locked))
141 .get_result::<Self>(conn)
144 pub fn update_stickied(
148 ) -> Result<Self, Error> {
149 use crate::schema::post::dsl::*;
150 diesel::update(post.find(post_id))
151 .set(stickied.eq(new_stickied))
152 .get_result::<Self>(conn)
155 pub fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool {
156 user_id == post_creator_id
160 impl Crud<PostForm> for Post {
161 fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
162 use crate::schema::post::dsl::*;
163 post.find(post_id).first::<Self>(conn)
166 fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
167 use crate::schema::post::dsl::*;
168 diesel::delete(post.find(post_id)).execute(conn)
171 fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
172 use crate::schema::post::dsl::*;
173 insert_into(post).values(new_post).get_result::<Self>(conn)
176 fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
177 use crate::schema::post::dsl::*;
178 diesel::update(post.find(post_id))
180 .get_result::<Self>(conn)
184 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
186 #[table_name = "post_like"]
187 pub struct PostLike {
192 pub published: chrono::NaiveDateTime,
195 #[derive(Insertable, AsChangeset, Clone)]
196 #[table_name = "post_like"]
197 pub struct PostLikeForm {
203 impl Likeable<PostLikeForm> for PostLike {
204 fn read(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> {
205 use crate::schema::post_like::dsl::*;
207 .filter(post_id.eq(post_id_from))
210 fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
211 use crate::schema::post_like::dsl::*;
212 insert_into(post_like)
213 .values(post_like_form)
214 .get_result::<Self>(conn)
216 fn remove(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<usize, Error> {
217 use crate::schema::post_like::dsl::*;
220 .filter(post_id.eq(post_like_form.post_id))
221 .filter(user_id.eq(post_like_form.user_id)),
227 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
229 #[table_name = "post_saved"]
230 pub struct PostSaved {
234 pub published: chrono::NaiveDateTime,
237 #[derive(Insertable, AsChangeset, Clone)]
238 #[table_name = "post_saved"]
239 pub struct PostSavedForm {
244 impl Saveable<PostSavedForm> for PostSaved {
245 fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
246 use crate::schema::post_saved::dsl::*;
247 insert_into(post_saved)
248 .values(post_saved_form)
249 .get_result::<Self>(conn)
251 fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
252 use crate::schema::post_saved::dsl::*;
255 .filter(post_id.eq(post_saved_form.post_id))
256 .filter(user_id.eq(post_saved_form.user_id)),
262 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
264 #[table_name = "post_read"]
265 pub struct PostRead {
269 pub published: chrono::NaiveDateTime,
272 #[derive(Insertable, AsChangeset, Clone)]
273 #[table_name = "post_read"]
274 pub struct PostReadForm {
279 impl Readable<PostReadForm> for PostRead {
280 fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
281 use crate::schema::post_read::dsl::*;
282 insert_into(post_read)
283 .values(post_read_form)
284 .get_result::<Self>(conn)
286 fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
287 use crate::schema::post_read::dsl::*;
290 .filter(post_id.eq(post_read_form.post_id))
291 .filter(user_id.eq(post_read_form.user_id)),
302 tests::establish_unpooled_connection,
310 let conn = establish_unpooled_connection();
312 let new_user = UserForm {
314 preferred_username: None,
315 password_encrypted: "nope".into(),
317 matrix_user_id: None,
324 theme: "darkly".into(),
325 default_sort_type: SortType::Hot as i16,
326 default_listing_type: ListingType::Subscribed as i16,
327 lang: "browser".into(),
329 send_notifications_to_email: false,
330 actor_id: "changeme_8292683678".into(),
335 last_refreshed_at: None,
338 let inserted_user = User_::create(&conn, &new_user).unwrap();
340 let new_community = CommunityForm {
341 name: "test community_3".to_string(),
342 title: "nada".to_owned(),
345 creator_id: inserted_user.id,
350 actor_id: "changeme_8223262378".into(),
354 last_refreshed_at: None,
360 let inserted_community = Community::create(&conn, &new_community).unwrap();
362 let new_post = PostForm {
363 name: "A test post".into(),
366 creator_id: inserted_user.id,
367 community_id: inserted_community.id,
375 embed_description: None,
378 ap_id: "http://fake.com".into(),
383 let inserted_post = Post::create(&conn, &new_post).unwrap();
385 let expected_post = Post {
386 id: inserted_post.id,
387 name: "A test post".into(),
390 creator_id: inserted_user.id,
391 community_id: inserted_community.id,
392 published: inserted_post.published,
400 embed_description: None,
403 ap_id: "http://fake.com".into(),
408 let post_like_form = PostLikeForm {
409 post_id: inserted_post.id,
410 user_id: inserted_user.id,
414 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
416 let expected_post_like = PostLike {
417 id: inserted_post_like.id,
418 post_id: inserted_post.id,
419 user_id: inserted_user.id,
420 published: inserted_post_like.published,
425 let post_saved_form = PostSavedForm {
426 post_id: inserted_post.id,
427 user_id: inserted_user.id,
430 let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
432 let expected_post_saved = PostSaved {
433 id: inserted_post_saved.id,
434 post_id: inserted_post.id,
435 user_id: inserted_user.id,
436 published: inserted_post_saved.published,
440 let post_read_form = PostReadForm {
441 post_id: inserted_post.id,
442 user_id: inserted_user.id,
445 let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
447 let expected_post_read = PostRead {
448 id: inserted_post_read.id,
449 post_id: inserted_post.id,
450 user_id: inserted_user.id,
451 published: inserted_post_read.published,
454 let read_post = Post::read(&conn, inserted_post.id).unwrap();
455 let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
456 let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
457 let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
458 let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
459 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
460 Community::delete(&conn, inserted_community.id).unwrap();
461 User_::delete(&conn, inserted_user.id).unwrap();
463 assert_eq!(expected_post, read_post);
464 assert_eq!(expected_post, inserted_post);
465 assert_eq!(expected_post, updated_post);
466 assert_eq!(expected_post_like, inserted_post_like);
467 assert_eq!(expected_post_saved, inserted_post_saved);
468 assert_eq!(expected_post_read, inserted_post_read);
469 assert_eq!(1, like_removed);
470 assert_eq!(1, saved_removed);
471 assert_eq!(1, read_removed);
472 assert_eq!(1, num_deleted);