2 use crate::schema::{post, post_like, post_read, post_saved};
4 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
9 pub url: Option<String>,
10 pub body: Option<String>,
12 pub community_id: i32,
15 pub published: chrono::NaiveDateTime,
16 pub updated: Option<chrono::NaiveDateTime>,
22 #[derive(Insertable, AsChangeset, Clone)]
23 #[table_name = "post"]
26 pub url: Option<String>,
27 pub body: Option<String>,
29 pub community_id: i32,
30 pub removed: Option<bool>,
31 pub locked: Option<bool>,
32 pub updated: Option<chrono::NaiveDateTime>,
33 pub deleted: Option<bool>,
35 pub stickied: Option<bool>,
38 impl Crud<PostForm> for Post {
39 fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
40 use crate::schema::post::dsl::*;
41 post.find(post_id).first::<Self>(conn)
44 fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
45 use crate::schema::post::dsl::*;
46 diesel::delete(post.find(post_id)).execute(conn)
49 fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
50 use crate::schema::post::dsl::*;
51 insert_into(post).values(new_post).get_result::<Self>(conn)
54 fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
55 use crate::schema::post::dsl::*;
56 diesel::update(post.find(post_id))
58 .get_result::<Self>(conn)
62 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
64 #[table_name = "post_like"]
70 pub published: chrono::NaiveDateTime,
73 #[derive(Insertable, AsChangeset, Clone)]
74 #[table_name = "post_like"]
75 pub struct PostLikeForm {
81 impl Likeable<PostLikeForm> for PostLike {
82 fn read(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> {
83 use crate::schema::post_like::dsl::*;
85 .filter(post_id.eq(post_id_from))
88 fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
89 use crate::schema::post_like::dsl::*;
90 insert_into(post_like)
91 .values(post_like_form)
92 .get_result::<Self>(conn)
94 fn remove(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<usize, Error> {
95 use crate::schema::post_like::dsl::*;
98 .filter(post_id.eq(post_like_form.post_id))
99 .filter(user_id.eq(post_like_form.user_id)),
105 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
107 #[table_name = "post_saved"]
108 pub struct PostSaved {
112 pub published: chrono::NaiveDateTime,
115 #[derive(Insertable, AsChangeset, Clone)]
116 #[table_name = "post_saved"]
117 pub struct PostSavedForm {
122 impl Saveable<PostSavedForm> for PostSaved {
123 fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
124 use crate::schema::post_saved::dsl::*;
125 insert_into(post_saved)
126 .values(post_saved_form)
127 .get_result::<Self>(conn)
129 fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
130 use crate::schema::post_saved::dsl::*;
133 .filter(post_id.eq(post_saved_form.post_id))
134 .filter(user_id.eq(post_saved_form.user_id)),
140 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
142 #[table_name = "post_read"]
143 pub struct PostRead {
147 pub published: chrono::NaiveDateTime,
150 #[derive(Insertable, AsChangeset, Clone)]
151 #[table_name = "post_read"]
152 pub struct PostReadForm {
157 impl Readable<PostReadForm> for PostRead {
158 fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
159 use crate::schema::post_read::dsl::*;
160 insert_into(post_read)
161 .values(post_read_form)
162 .get_result::<Self>(conn)
164 fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
165 use crate::schema::post_read::dsl::*;
168 .filter(post_id.eq(post_read_form.post_id))
169 .filter(user_id.eq(post_read_form.user_id)),
177 use super::super::community::*;
178 use super::super::user::*;
182 let conn = establish_unpooled_connection();
184 let new_user = UserForm {
186 fedi_name: "rrf".into(),
187 preferred_username: None,
188 password_encrypted: "nope".into(),
190 matrix_user_id: None,
196 theme: "darkly".into(),
197 default_sort_type: SortType::Hot as i16,
198 default_listing_type: ListingType::Subscribed as i16,
199 lang: "browser".into(),
201 send_notifications_to_email: false,
204 let inserted_user = User_::create(&conn, &new_user).unwrap();
206 let new_community = CommunityForm {
207 name: "test community_3".to_string(),
208 title: "nada".to_owned(),
211 creator_id: inserted_user.id,
218 let inserted_community = Community::create(&conn, &new_community).unwrap();
220 let new_post = PostForm {
221 name: "A test post".into(),
224 creator_id: inserted_user.id,
225 community_id: inserted_community.id,
234 let inserted_post = Post::create(&conn, &new_post).unwrap();
236 let expected_post = Post {
237 id: inserted_post.id,
238 name: "A test post".into(),
241 creator_id: inserted_user.id,
242 community_id: inserted_community.id,
243 published: inserted_post.published,
253 let post_like_form = PostLikeForm {
254 post_id: inserted_post.id,
255 user_id: inserted_user.id,
259 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
261 let expected_post_like = PostLike {
262 id: inserted_post_like.id,
263 post_id: inserted_post.id,
264 user_id: inserted_user.id,
265 published: inserted_post_like.published,
270 let post_saved_form = PostSavedForm {
271 post_id: inserted_post.id,
272 user_id: inserted_user.id,
275 let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
277 let expected_post_saved = PostSaved {
278 id: inserted_post_saved.id,
279 post_id: inserted_post.id,
280 user_id: inserted_user.id,
281 published: inserted_post_saved.published,
285 let post_read_form = PostReadForm {
286 post_id: inserted_post.id,
287 user_id: inserted_user.id,
290 let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
292 let expected_post_read = PostRead {
293 id: inserted_post_read.id,
294 post_id: inserted_post.id,
295 user_id: inserted_user.id,
296 published: inserted_post_read.published,
299 let read_post = Post::read(&conn, inserted_post.id).unwrap();
300 let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
301 let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
302 let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
303 let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
304 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
305 Community::delete(&conn, inserted_community.id).unwrap();
306 User_::delete(&conn, inserted_user.id).unwrap();
308 assert_eq!(expected_post, read_post);
309 assert_eq!(expected_post, inserted_post);
310 assert_eq!(expected_post, updated_post);
311 assert_eq!(expected_post_like, inserted_post_like);
312 assert_eq!(expected_post_saved, inserted_post_saved);
313 assert_eq!(expected_post_read, inserted_post_read);
314 assert_eq!(1, like_removed);
315 assert_eq!(1, saved_removed);
316 assert_eq!(1, read_removed);
317 assert_eq!(1, num_deleted);