1 use super::{post::Post, *};
2 use crate::schema::{comment, comment_like, comment_saved};
3 use url::{ParseError, Url};
5 // WITH RECURSIVE MyTree AS (
6 // SELECT * FROM comment WHERE parent_id IS NULL
8 // SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id
10 // SELECT * FROM MyTree;
12 #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
14 #[table_name = "comment"]
19 pub parent_id: Option<i32>,
22 pub read: bool, // Whether the recipient has read the comment or not
23 pub published: chrono::NaiveDateTime,
24 pub updated: Option<chrono::NaiveDateTime>,
30 #[derive(Insertable, AsChangeset, Clone)]
31 #[table_name = "comment"]
32 pub struct CommentForm {
35 pub parent_id: Option<i32>,
37 pub removed: Option<bool>,
38 pub read: Option<bool>,
39 pub published: Option<chrono::NaiveDateTime>,
40 pub updated: Option<chrono::NaiveDateTime>,
41 pub deleted: Option<bool>,
47 pub fn get_ap_id(&self) -> Result<Url, ParseError> {
48 Url::parse(&self.ap_id)
52 impl Crud<CommentForm> for Comment {
53 fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
54 use crate::schema::comment::dsl::*;
55 comment.find(comment_id).first::<Self>(conn)
58 fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
59 use crate::schema::comment::dsl::*;
60 diesel::delete(comment.find(comment_id)).execute(conn)
63 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
64 use crate::schema::comment::dsl::*;
67 .get_result::<Self>(conn)
73 comment_form: &CommentForm,
74 ) -> Result<Self, Error> {
75 use crate::schema::comment::dsl::*;
76 diesel::update(comment.find(comment_id))
78 .get_result::<Self>(conn)
87 ) -> Result<Self, Error> {
88 use crate::schema::comment::dsl::*;
90 diesel::update(comment.find(comment_id))
91 .set(ap_id.eq(apub_id))
92 .get_result::<Self>(conn)
95 pub fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
96 use crate::schema::comment::dsl::*;
97 comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
100 pub fn permadelete(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
101 use crate::schema::comment::dsl::*;
103 diesel::update(comment.find(comment_id))
105 content.eq("*Permananently Deleted*"),
107 updated.eq(naive_now()),
109 .get_result::<Self>(conn)
112 pub fn update_deleted(
116 ) -> Result<Self, Error> {
117 use crate::schema::comment::dsl::*;
118 diesel::update(comment.find(comment_id))
120 deleted.eq(new_deleted),
121 updated.eq(naive_now())
123 .get_result::<Self>(conn)
126 pub fn update_removed(
130 ) -> Result<Self, Error> {
131 use crate::schema::comment::dsl::*;
132 diesel::update(comment.find(comment_id))
134 removed.eq(new_removed),
135 updated.eq(naive_now())
137 .get_result::<Self>(conn)
140 pub fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Self, Error> {
141 use crate::schema::comment::dsl::*;
142 diesel::update(comment.find(comment_id))
143 .set(read.eq(new_read))
144 .get_result::<Self>(conn)
147 pub fn update_content(
151 ) -> Result<Self, Error> {
152 use crate::schema::comment::dsl::*;
153 diesel::update(comment.find(comment_id))
154 .set((content.eq(new_content), updated.eq(naive_now())))
155 .get_result::<Self>(conn)
159 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
160 #[belongs_to(Comment)]
161 #[table_name = "comment_like"]
162 pub struct CommentLike {
168 pub published: chrono::NaiveDateTime,
171 #[derive(Insertable, AsChangeset, Clone)]
172 #[table_name = "comment_like"]
173 pub struct CommentLikeForm {
180 impl Likeable<CommentLikeForm> for CommentLike {
181 fn read(conn: &PgConnection, comment_id_from: i32) -> Result<Vec<Self>, Error> {
182 use crate::schema::comment_like::dsl::*;
184 .filter(comment_id.eq(comment_id_from))
188 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
189 use crate::schema::comment_like::dsl::*;
190 insert_into(comment_like)
191 .values(comment_like_form)
192 .get_result::<Self>(conn)
194 fn remove(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<usize, Error> {
195 use crate::schema::comment_like::dsl::*;
198 .filter(comment_id.eq(comment_like_form.comment_id))
199 .filter(user_id.eq(comment_like_form.user_id)),
206 pub fn from_post(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> {
207 use crate::schema::comment_like::dsl::*;
209 .filter(post_id.eq(post_id_from))
214 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
215 #[belongs_to(Comment)]
216 #[table_name = "comment_saved"]
217 pub struct CommentSaved {
221 pub published: chrono::NaiveDateTime,
224 #[derive(Insertable, AsChangeset, Clone)]
225 #[table_name = "comment_saved"]
226 pub struct CommentSavedForm {
231 impl Saveable<CommentSavedForm> for CommentSaved {
232 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
233 use crate::schema::comment_saved::dsl::*;
234 insert_into(comment_saved)
235 .values(comment_saved_form)
236 .get_result::<Self>(conn)
238 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
239 use crate::schema::comment_saved::dsl::*;
242 .filter(comment_id.eq(comment_saved_form.comment_id))
243 .filter(user_id.eq(comment_saved_form.user_id)),
251 use crate::{comment::*, community::*, post::*, tests::establish_unpooled_connection, user::*};
255 let conn = establish_unpooled_connection();
257 let new_user = UserForm {
258 name: "terry".into(),
259 preferred_username: None,
260 password_encrypted: "nope".into(),
262 matrix_user_id: None,
268 theme: "darkly".into(),
269 default_sort_type: SortType::Hot as i16,
270 default_listing_type: ListingType::Subscribed as i16,
271 lang: "browser".into(),
273 send_notifications_to_email: false,
274 actor_id: "changeme_283687".into(),
279 last_refreshed_at: None,
282 let inserted_user = User_::create(&conn, &new_user).unwrap();
284 let new_community = CommunityForm {
285 name: "test community".to_string(),
286 title: "nada".to_owned(),
289 creator_id: inserted_user.id,
294 actor_id: "changeme_928738972".into(),
298 last_refreshed_at: None,
302 let inserted_community = Community::create(&conn, &new_community).unwrap();
304 let new_post = PostForm {
305 name: "A test post".into(),
306 creator_id: inserted_user.id,
309 community_id: inserted_community.id,
317 embed_description: None,
320 ap_id: "http://fake.com".into(),
325 let inserted_post = Post::create(&conn, &new_post).unwrap();
327 let comment_form = CommentForm {
328 content: "A test comment".into(),
329 creator_id: inserted_user.id,
330 post_id: inserted_post.id,
337 ap_id: "http://fake.com".into(),
341 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
343 let expected_comment = Comment {
344 id: inserted_comment.id,
345 content: "A test comment".into(),
346 creator_id: inserted_user.id,
347 post_id: inserted_post.id,
352 published: inserted_comment.published,
354 ap_id: "http://fake.com".into(),
358 let child_comment_form = CommentForm {
359 content: "A child comment".into(),
360 creator_id: inserted_user.id,
361 post_id: inserted_post.id,
362 parent_id: Some(inserted_comment.id),
368 ap_id: "http://fake.com".into(),
372 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
375 let comment_like_form = CommentLikeForm {
376 comment_id: inserted_comment.id,
377 post_id: inserted_post.id,
378 user_id: inserted_user.id,
382 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
384 let expected_comment_like = CommentLike {
385 id: inserted_comment_like.id,
386 comment_id: inserted_comment.id,
387 post_id: inserted_post.id,
388 user_id: inserted_user.id,
389 published: inserted_comment_like.published,
394 let comment_saved_form = CommentSavedForm {
395 comment_id: inserted_comment.id,
396 user_id: inserted_user.id,
399 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
401 let expected_comment_saved = CommentSaved {
402 id: inserted_comment_saved.id,
403 comment_id: inserted_comment.id,
404 user_id: inserted_user.id,
405 published: inserted_comment_saved.published,
408 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
409 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
410 let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap();
411 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
412 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
413 Comment::delete(&conn, inserted_child_comment.id).unwrap();
414 Post::delete(&conn, inserted_post.id).unwrap();
415 Community::delete(&conn, inserted_community.id).unwrap();
416 User_::delete(&conn, inserted_user.id).unwrap();
418 assert_eq!(expected_comment, read_comment);
419 assert_eq!(expected_comment, inserted_comment);
420 assert_eq!(expected_comment, updated_comment);
421 assert_eq!(expected_comment_like, inserted_comment_like);
422 assert_eq!(expected_comment_saved, inserted_comment_saved);
425 inserted_child_comment.parent_id.unwrap()
427 assert_eq!(1, like_removed);
428 assert_eq!(1, saved_removed);
429 assert_eq!(1, num_deleted);