4 schema::{comment, comment_like, comment_saved},
9 use diesel::{dsl::*, result::Error, *};
10 use url::{ParseError, Url};
12 // WITH RECURSIVE MyTree AS (
13 // SELECT * FROM comment WHERE parent_id IS NULL
15 // SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id
17 // SELECT * FROM MyTree;
19 #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug)]
21 #[table_name = "comment"]
26 pub parent_id: Option<i32>,
29 pub read: bool, // Whether the recipient has read the comment or not
30 pub published: chrono::NaiveDateTime,
31 pub updated: Option<chrono::NaiveDateTime>,
37 #[derive(Insertable, AsChangeset, Clone)]
38 #[table_name = "comment"]
39 pub struct CommentForm {
42 pub parent_id: Option<i32>,
44 pub removed: Option<bool>,
45 pub read: Option<bool>,
46 pub published: Option<chrono::NaiveDateTime>,
47 pub updated: Option<chrono::NaiveDateTime>,
48 pub deleted: Option<bool>,
49 pub ap_id: Option<String>,
54 pub fn get_ap_id(&self) -> Result<Url, ParseError> {
55 Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string()))
59 impl Crud<CommentForm> for Comment {
60 fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
61 use crate::schema::comment::dsl::*;
62 comment.find(comment_id).first::<Self>(conn)
65 fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
66 use crate::schema::comment::dsl::*;
67 diesel::delete(comment.find(comment_id)).execute(conn)
70 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
71 use crate::schema::comment::dsl::*;
74 .get_result::<Self>(conn)
80 comment_form: &CommentForm,
81 ) -> Result<Self, Error> {
82 use crate::schema::comment::dsl::*;
83 diesel::update(comment.find(comment_id))
85 .get_result::<Self>(conn)
94 ) -> Result<Self, Error> {
95 use crate::schema::comment::dsl::*;
97 diesel::update(comment.find(comment_id))
98 .set(ap_id.eq(apub_id))
99 .get_result::<Self>(conn)
102 pub fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
103 use crate::schema::comment::dsl::*;
104 comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
107 pub fn permadelete_for_creator(
110 ) -> Result<Vec<Self>, Error> {
111 use crate::schema::comment::dsl::*;
112 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
114 content.eq("*Permananently Deleted*"),
116 updated.eq(naive_now()),
118 .get_results::<Self>(conn)
121 pub fn update_deleted(
125 ) -> Result<Self, Error> {
126 use crate::schema::comment::dsl::*;
127 diesel::update(comment.find(comment_id))
128 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
129 .get_result::<Self>(conn)
132 pub fn update_removed(
136 ) -> Result<Self, Error> {
137 use crate::schema::comment::dsl::*;
138 diesel::update(comment.find(comment_id))
139 .set((removed.eq(new_removed), updated.eq(naive_now())))
140 .get_result::<Self>(conn)
143 pub fn update_removed_for_creator(
147 ) -> Result<Vec<Self>, Error> {
148 use crate::schema::comment::dsl::*;
149 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
150 .set((removed.eq(new_removed), updated.eq(naive_now())))
151 .get_results::<Self>(conn)
154 pub fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Self, Error> {
155 use crate::schema::comment::dsl::*;
156 diesel::update(comment.find(comment_id))
157 .set(read.eq(new_read))
158 .get_result::<Self>(conn)
161 pub fn update_content(
165 ) -> Result<Self, Error> {
166 use crate::schema::comment::dsl::*;
167 diesel::update(comment.find(comment_id))
168 .set((content.eq(new_content), updated.eq(naive_now())))
169 .get_result::<Self>(conn)
172 pub fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
173 use crate::schema::comment::dsl::*;
175 .values(comment_form)
179 .get_result::<Self>(conn)
183 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
184 #[belongs_to(Comment)]
185 #[table_name = "comment_like"]
186 pub struct CommentLike {
192 pub published: chrono::NaiveDateTime,
195 #[derive(Insertable, AsChangeset, Clone)]
196 #[table_name = "comment_like"]
197 pub struct CommentLikeForm {
204 impl Likeable<CommentLikeForm> for CommentLike {
205 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
206 use crate::schema::comment_like::dsl::*;
207 insert_into(comment_like)
208 .values(comment_like_form)
209 .get_result::<Self>(conn)
211 fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result<usize, Error> {
212 use crate::schema::comment_like::dsl;
215 .filter(dsl::comment_id.eq(comment_id))
216 .filter(dsl::user_id.eq(user_id)),
222 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
223 #[belongs_to(Comment)]
224 #[table_name = "comment_saved"]
225 pub struct CommentSaved {
229 pub published: chrono::NaiveDateTime,
232 #[derive(Insertable, AsChangeset)]
233 #[table_name = "comment_saved"]
234 pub struct CommentSavedForm {
239 impl Saveable<CommentSavedForm> for CommentSaved {
240 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
241 use crate::schema::comment_saved::dsl::*;
242 insert_into(comment_saved)
243 .values(comment_saved_form)
244 .get_result::<Self>(conn)
246 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
247 use crate::schema::comment_saved::dsl::*;
250 .filter(comment_id.eq(comment_saved_form.comment_id))
251 .filter(user_id.eq(comment_saved_form.user_id)),
263 tests::establish_unpooled_connection,
272 let conn = establish_unpooled_connection();
274 let new_user = UserForm {
275 name: "terry".into(),
276 preferred_username: None,
277 password_encrypted: "nope".into(),
279 matrix_user_id: None,
286 theme: "darkly".into(),
287 default_sort_type: SortType::Hot as i16,
288 default_listing_type: ListingType::Subscribed as i16,
289 lang: "browser".into(),
291 send_notifications_to_email: false,
297 last_refreshed_at: None,
300 let inserted_user = User_::create(&conn, &new_user).unwrap();
302 let new_community = CommunityForm {
303 name: "test community".to_string(),
304 title: "nada".to_owned(),
307 creator_id: inserted_user.id,
316 last_refreshed_at: None,
322 let inserted_community = Community::create(&conn, &new_community).unwrap();
324 let new_post = PostForm {
325 name: "A test post".into(),
326 creator_id: inserted_user.id,
329 community_id: inserted_community.id,
337 embed_description: None,
345 let inserted_post = Post::create(&conn, &new_post).unwrap();
347 let comment_form = CommentForm {
348 content: "A test comment".into(),
349 creator_id: inserted_user.id,
350 post_id: inserted_post.id,
361 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
363 let expected_comment = Comment {
364 id: inserted_comment.id,
365 content: "A test comment".into(),
366 creator_id: inserted_user.id,
367 post_id: inserted_post.id,
372 published: inserted_comment.published,
374 ap_id: inserted_comment.ap_id.to_owned(),
378 let child_comment_form = CommentForm {
379 content: "A child comment".into(),
380 creator_id: inserted_user.id,
381 post_id: inserted_post.id,
382 parent_id: Some(inserted_comment.id),
392 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
395 let comment_like_form = CommentLikeForm {
396 comment_id: inserted_comment.id,
397 post_id: inserted_post.id,
398 user_id: inserted_user.id,
402 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
404 let expected_comment_like = CommentLike {
405 id: inserted_comment_like.id,
406 comment_id: inserted_comment.id,
407 post_id: inserted_post.id,
408 user_id: inserted_user.id,
409 published: inserted_comment_like.published,
414 let comment_saved_form = CommentSavedForm {
415 comment_id: inserted_comment.id,
416 user_id: inserted_user.id,
419 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
421 let expected_comment_saved = CommentSaved {
422 id: inserted_comment_saved.id,
423 comment_id: inserted_comment.id,
424 user_id: inserted_user.id,
425 published: inserted_comment_saved.published,
428 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
429 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
430 let like_removed = CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
431 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
432 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
433 Comment::delete(&conn, inserted_child_comment.id).unwrap();
434 Post::delete(&conn, inserted_post.id).unwrap();
435 Community::delete(&conn, inserted_community.id).unwrap();
436 User_::delete(&conn, inserted_user.id).unwrap();
438 assert_eq!(expected_comment, read_comment);
439 assert_eq!(expected_comment, inserted_comment);
440 assert_eq!(expected_comment, updated_comment);
441 assert_eq!(expected_comment_like, inserted_comment_like);
442 assert_eq!(expected_comment_saved, inserted_comment_saved);
445 inserted_child_comment.parent_id.unwrap()
447 assert_eq!(1, like_removed);
448 assert_eq!(1, saved_removed);
449 assert_eq!(1, num_deleted);