4 schema::{comment, comment_like, comment_saved},
10 use diesel::{dsl::*, result::Error, *};
11 use url::{ParseError, Url};
13 // WITH RECURSIVE MyTree AS (
14 // SELECT * FROM comment WHERE parent_id IS NULL
16 // SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id
18 // SELECT * FROM MyTree;
20 #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug)]
22 #[table_name = "comment"]
27 pub parent_id: Option<i32>,
30 pub read: bool, // Whether the recipient has read the comment or not
31 pub published: chrono::NaiveDateTime,
32 pub updated: Option<chrono::NaiveDateTime>,
38 #[derive(Insertable, AsChangeset, Clone)]
39 #[table_name = "comment"]
40 pub struct CommentForm {
43 pub parent_id: Option<i32>,
45 pub removed: Option<bool>,
46 pub read: Option<bool>,
47 pub published: Option<chrono::NaiveDateTime>,
48 pub updated: Option<chrono::NaiveDateTime>,
49 pub deleted: Option<bool>,
50 pub ap_id: Option<String>,
55 pub fn get_ap_id(&self) -> Result<Url, ParseError> {
56 Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string()))
60 impl Crud<CommentForm> for Comment {
61 fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
62 use crate::schema::comment::dsl::*;
63 comment.find(comment_id).first::<Self>(conn)
66 fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
67 use crate::schema::comment::dsl::*;
68 diesel::delete(comment.find(comment_id)).execute(conn)
71 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
72 use crate::schema::comment::dsl::*;
75 .get_result::<Self>(conn)
81 comment_form: &CommentForm,
82 ) -> Result<Self, Error> {
83 use crate::schema::comment::dsl::*;
84 diesel::update(comment.find(comment_id))
86 .get_result::<Self>(conn)
90 impl ApubObject<CommentForm> for Comment {
91 fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
92 use crate::schema::comment::dsl::*;
93 comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
96 fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
97 use crate::schema::comment::dsl::*;
103 .get_result::<Self>(conn)
112 ) -> Result<Self, Error> {
113 use crate::schema::comment::dsl::*;
115 diesel::update(comment.find(comment_id))
116 .set(ap_id.eq(apub_id))
117 .get_result::<Self>(conn)
120 pub fn permadelete_for_creator(
123 ) -> Result<Vec<Self>, Error> {
124 use crate::schema::comment::dsl::*;
125 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
127 content.eq("*Permananently Deleted*"),
129 updated.eq(naive_now()),
131 .get_results::<Self>(conn)
134 pub fn update_deleted(
138 ) -> Result<Self, Error> {
139 use crate::schema::comment::dsl::*;
140 diesel::update(comment.find(comment_id))
141 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
142 .get_result::<Self>(conn)
145 pub fn update_removed(
149 ) -> Result<Self, Error> {
150 use crate::schema::comment::dsl::*;
151 diesel::update(comment.find(comment_id))
152 .set((removed.eq(new_removed), updated.eq(naive_now())))
153 .get_result::<Self>(conn)
156 pub fn update_removed_for_creator(
160 ) -> Result<Vec<Self>, Error> {
161 use crate::schema::comment::dsl::*;
162 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
163 .set((removed.eq(new_removed), updated.eq(naive_now())))
164 .get_results::<Self>(conn)
167 pub fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Self, Error> {
168 use crate::schema::comment::dsl::*;
169 diesel::update(comment.find(comment_id))
170 .set(read.eq(new_read))
171 .get_result::<Self>(conn)
174 pub fn update_content(
178 ) -> Result<Self, Error> {
179 use crate::schema::comment::dsl::*;
180 diesel::update(comment.find(comment_id))
181 .set((content.eq(new_content), updated.eq(naive_now())))
182 .get_result::<Self>(conn)
186 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
187 #[belongs_to(Comment)]
188 #[table_name = "comment_like"]
189 pub struct CommentLike {
193 pub post_id: i32, // TODO this is redundant
195 pub published: chrono::NaiveDateTime,
198 #[derive(Insertable, AsChangeset, Clone)]
199 #[table_name = "comment_like"]
200 pub struct CommentLikeForm {
203 pub post_id: i32, // TODO this is redundant
207 impl Likeable<CommentLikeForm> for CommentLike {
208 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
209 use crate::schema::comment_like::dsl::*;
210 insert_into(comment_like)
211 .values(comment_like_form)
212 .get_result::<Self>(conn)
214 fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result<usize, Error> {
215 use crate::schema::comment_like::dsl;
218 .filter(dsl::comment_id.eq(comment_id))
219 .filter(dsl::user_id.eq(user_id)),
225 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
226 #[belongs_to(Comment)]
227 #[table_name = "comment_saved"]
228 pub struct CommentSaved {
232 pub published: chrono::NaiveDateTime,
235 #[derive(Insertable, AsChangeset)]
236 #[table_name = "comment_saved"]
237 pub struct CommentSavedForm {
242 impl Saveable<CommentSavedForm> for CommentSaved {
243 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
244 use crate::schema::comment_saved::dsl::*;
245 insert_into(comment_saved)
246 .values(comment_saved_form)
247 .get_result::<Self>(conn)
249 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
250 use crate::schema::comment_saved::dsl::*;
253 .filter(comment_id.eq(comment_saved_form.comment_id))
254 .filter(user_id.eq(comment_saved_form.user_id)),
266 tests::establish_unpooled_connection,
275 let conn = establish_unpooled_connection();
277 let new_user = UserForm {
278 name: "terry".into(),
279 preferred_username: None,
280 password_encrypted: "nope".into(),
282 matrix_user_id: None,
290 theme: "browser".into(),
291 default_sort_type: SortType::Hot as i16,
292 default_listing_type: ListingType::Subscribed as i16,
293 lang: "browser".into(),
295 send_notifications_to_email: false,
301 last_refreshed_at: None,
304 let inserted_user = User_::create(&conn, &new_user).unwrap();
306 let new_community = CommunityForm {
307 name: "test community".to_string(),
308 title: "nada".to_owned(),
311 creator_id: inserted_user.id,
320 last_refreshed_at: None,
326 let inserted_community = Community::create(&conn, &new_community).unwrap();
328 let new_post = PostForm {
329 name: "A test post".into(),
330 creator_id: inserted_user.id,
333 community_id: inserted_community.id,
341 embed_description: None,
349 let inserted_post = Post::create(&conn, &new_post).unwrap();
351 let comment_form = CommentForm {
352 content: "A test comment".into(),
353 creator_id: inserted_user.id,
354 post_id: inserted_post.id,
365 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
367 let expected_comment = Comment {
368 id: inserted_comment.id,
369 content: "A test comment".into(),
370 creator_id: inserted_user.id,
371 post_id: inserted_post.id,
376 published: inserted_comment.published,
378 ap_id: inserted_comment.ap_id.to_owned(),
382 let child_comment_form = CommentForm {
383 content: "A child comment".into(),
384 creator_id: inserted_user.id,
385 post_id: inserted_post.id,
386 parent_id: Some(inserted_comment.id),
396 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
399 let comment_like_form = CommentLikeForm {
400 comment_id: inserted_comment.id,
401 post_id: inserted_post.id,
402 user_id: inserted_user.id,
406 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
408 let expected_comment_like = CommentLike {
409 id: inserted_comment_like.id,
410 comment_id: inserted_comment.id,
411 post_id: inserted_post.id,
412 user_id: inserted_user.id,
413 published: inserted_comment_like.published,
418 let comment_saved_form = CommentSavedForm {
419 comment_id: inserted_comment.id,
420 user_id: inserted_user.id,
423 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
425 let expected_comment_saved = CommentSaved {
426 id: inserted_comment_saved.id,
427 comment_id: inserted_comment.id,
428 user_id: inserted_user.id,
429 published: inserted_comment_saved.published,
432 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
433 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
434 let like_removed = CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
435 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
436 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
437 Comment::delete(&conn, inserted_child_comment.id).unwrap();
438 Post::delete(&conn, inserted_post.id).unwrap();
439 Community::delete(&conn, inserted_community.id).unwrap();
440 User_::delete(&conn, inserted_user.id).unwrap();
442 assert_eq!(expected_comment, read_comment);
443 assert_eq!(expected_comment, inserted_comment);
444 assert_eq!(expected_comment, updated_comment);
445 assert_eq!(expected_comment_like, inserted_comment_like);
446 assert_eq!(expected_comment_saved, inserted_comment_saved);
449 inserted_child_comment.parent_id.unwrap()
451 assert_eq!(1, like_removed);
452 assert_eq!(1, saved_removed);
453 assert_eq!(1, num_deleted);