4 schema::{comment, comment_alias_1, comment_like, comment_saved},
10 use diesel::{dsl::*, result::Error, *};
12 use url::{ParseError, Url};
14 // WITH RECURSIVE MyTree AS (
15 // SELECT * FROM comment WHERE parent_id IS NULL
17 // SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id
19 // SELECT * FROM MyTree;
21 #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
23 #[table_name = "comment"]
28 pub parent_id: Option<i32>,
31 pub read: bool, // Whether the recipient has read the comment or not
32 pub published: chrono::NaiveDateTime,
33 pub updated: Option<chrono::NaiveDateTime>,
39 #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
41 #[table_name = "comment_alias_1"]
42 pub struct CommentAlias1 {
46 pub parent_id: Option<i32>,
49 pub read: bool, // Whether the recipient has read the comment or not
50 pub published: chrono::NaiveDateTime,
51 pub updated: Option<chrono::NaiveDateTime>,
57 #[derive(Insertable, AsChangeset, Clone)]
58 #[table_name = "comment"]
59 pub struct CommentForm {
62 pub parent_id: Option<i32>,
64 pub removed: Option<bool>,
65 pub read: Option<bool>,
66 pub published: Option<chrono::NaiveDateTime>,
67 pub updated: Option<chrono::NaiveDateTime>,
68 pub deleted: Option<bool>,
69 pub ap_id: Option<String>,
74 pub fn get_ap_id(&self) -> Result<Url, ParseError> {
75 Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string()))
79 impl Crud<CommentForm> for Comment {
80 fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
81 use crate::schema::comment::dsl::*;
82 comment.find(comment_id).first::<Self>(conn)
85 fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
86 use crate::schema::comment::dsl::*;
87 diesel::delete(comment.find(comment_id)).execute(conn)
90 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
91 use crate::schema::comment::dsl::*;
94 .get_result::<Self>(conn)
100 comment_form: &CommentForm,
101 ) -> Result<Self, Error> {
102 use crate::schema::comment::dsl::*;
103 diesel::update(comment.find(comment_id))
105 .get_result::<Self>(conn)
109 impl ApubObject<CommentForm> for Comment {
110 fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
111 use crate::schema::comment::dsl::*;
112 comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
115 fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
116 use crate::schema::comment::dsl::*;
118 .values(comment_form)
122 .get_result::<Self>(conn)
131 ) -> Result<Self, Error> {
132 use crate::schema::comment::dsl::*;
134 diesel::update(comment.find(comment_id))
135 .set(ap_id.eq(apub_id))
136 .get_result::<Self>(conn)
139 pub fn permadelete_for_creator(
142 ) -> Result<Vec<Self>, Error> {
143 use crate::schema::comment::dsl::*;
144 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
146 content.eq("*Permananently Deleted*"),
148 updated.eq(naive_now()),
150 .get_results::<Self>(conn)
153 pub fn update_deleted(
157 ) -> Result<Self, Error> {
158 use crate::schema::comment::dsl::*;
159 diesel::update(comment.find(comment_id))
160 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
161 .get_result::<Self>(conn)
164 pub fn update_removed(
168 ) -> Result<Self, Error> {
169 use crate::schema::comment::dsl::*;
170 diesel::update(comment.find(comment_id))
171 .set((removed.eq(new_removed), updated.eq(naive_now())))
172 .get_result::<Self>(conn)
175 pub fn update_removed_for_creator(
179 ) -> Result<Vec<Self>, Error> {
180 use crate::schema::comment::dsl::*;
181 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
182 .set((removed.eq(new_removed), updated.eq(naive_now())))
183 .get_results::<Self>(conn)
186 pub fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Self, Error> {
187 use crate::schema::comment::dsl::*;
188 diesel::update(comment.find(comment_id))
189 .set(read.eq(new_read))
190 .get_result::<Self>(conn)
193 pub fn update_content(
197 ) -> Result<Self, Error> {
198 use crate::schema::comment::dsl::*;
199 diesel::update(comment.find(comment_id))
200 .set((content.eq(new_content), updated.eq(naive_now())))
201 .get_result::<Self>(conn)
205 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
206 #[belongs_to(Comment)]
207 #[table_name = "comment_like"]
208 pub struct CommentLike {
212 pub post_id: i32, // TODO this is redundant
214 pub published: chrono::NaiveDateTime,
217 #[derive(Insertable, AsChangeset, Clone)]
218 #[table_name = "comment_like"]
219 pub struct CommentLikeForm {
222 pub post_id: i32, // TODO this is redundant
226 impl Likeable<CommentLikeForm> for CommentLike {
227 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
228 use crate::schema::comment_like::dsl::*;
229 insert_into(comment_like)
230 .values(comment_like_form)
231 .get_result::<Self>(conn)
233 fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result<usize, Error> {
234 use crate::schema::comment_like::dsl;
237 .filter(dsl::comment_id.eq(comment_id))
238 .filter(dsl::user_id.eq(user_id)),
244 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
245 #[belongs_to(Comment)]
246 #[table_name = "comment_saved"]
247 pub struct CommentSaved {
251 pub published: chrono::NaiveDateTime,
254 #[derive(Insertable, AsChangeset)]
255 #[table_name = "comment_saved"]
256 pub struct CommentSavedForm {
261 impl Saveable<CommentSavedForm> for CommentSaved {
262 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
263 use crate::schema::comment_saved::dsl::*;
264 insert_into(comment_saved)
265 .values(comment_saved_form)
266 .get_result::<Self>(conn)
268 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
269 use crate::schema::comment_saved::dsl::*;
272 .filter(comment_id.eq(comment_saved_form.comment_id))
273 .filter(user_id.eq(comment_saved_form.user_id)),
282 source::{comment::*, community::*, post::*, user::*},
283 tests::establish_unpooled_connection,
291 let conn = establish_unpooled_connection();
293 let new_user = UserForm {
294 name: "terry".into(),
295 preferred_username: None,
296 password_encrypted: "nope".into(),
298 matrix_user_id: None,
306 theme: "browser".into(),
307 default_sort_type: SortType::Hot as i16,
308 default_listing_type: ListingType::Subscribed as i16,
309 lang: "browser".into(),
311 send_notifications_to_email: false,
317 last_refreshed_at: None,
320 let inserted_user = User_::create(&conn, &new_user).unwrap();
322 let new_community = CommunityForm {
323 name: "test community".to_string(),
324 title: "nada".to_owned(),
327 creator_id: inserted_user.id,
336 last_refreshed_at: None,
342 let inserted_community = Community::create(&conn, &new_community).unwrap();
344 let new_post = PostForm {
345 name: "A test post".into(),
346 creator_id: inserted_user.id,
349 community_id: inserted_community.id,
357 embed_description: None,
365 let inserted_post = Post::create(&conn, &new_post).unwrap();
367 let comment_form = CommentForm {
368 content: "A test comment".into(),
369 creator_id: inserted_user.id,
370 post_id: inserted_post.id,
381 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
383 let expected_comment = Comment {
384 id: inserted_comment.id,
385 content: "A test comment".into(),
386 creator_id: inserted_user.id,
387 post_id: inserted_post.id,
392 published: inserted_comment.published,
394 ap_id: inserted_comment.ap_id.to_owned(),
398 let child_comment_form = CommentForm {
399 content: "A child comment".into(),
400 creator_id: inserted_user.id,
401 post_id: inserted_post.id,
402 parent_id: Some(inserted_comment.id),
412 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
415 let comment_like_form = CommentLikeForm {
416 comment_id: inserted_comment.id,
417 post_id: inserted_post.id,
418 user_id: inserted_user.id,
422 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
424 let expected_comment_like = CommentLike {
425 id: inserted_comment_like.id,
426 comment_id: inserted_comment.id,
427 post_id: inserted_post.id,
428 user_id: inserted_user.id,
429 published: inserted_comment_like.published,
434 let comment_saved_form = CommentSavedForm {
435 comment_id: inserted_comment.id,
436 user_id: inserted_user.id,
439 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
441 let expected_comment_saved = CommentSaved {
442 id: inserted_comment_saved.id,
443 comment_id: inserted_comment.id,
444 user_id: inserted_user.id,
445 published: inserted_comment_saved.published,
448 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
449 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
450 let like_removed = CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
451 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
452 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
453 Comment::delete(&conn, inserted_child_comment.id).unwrap();
454 Post::delete(&conn, inserted_post.id).unwrap();
455 Community::delete(&conn, inserted_community.id).unwrap();
456 User_::delete(&conn, inserted_user.id).unwrap();
458 assert_eq!(expected_comment, read_comment);
459 assert_eq!(expected_comment, inserted_comment);
460 assert_eq!(expected_comment, updated_comment);
461 assert_eq!(expected_comment_like, inserted_comment_like);
462 assert_eq!(expected_comment_saved, inserted_comment_saved);
465 inserted_child_comment.parent_id.unwrap()
467 assert_eq!(1, like_removed);
468 assert_eq!(1, saved_removed);
469 assert_eq!(1, num_deleted);