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 .on_conflict((comment_id, user_id))
233 .set(comment_like_form)
234 .get_result::<Self>(conn)
236 fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result<usize, Error> {
237 use crate::schema::comment_like::dsl;
240 .filter(dsl::comment_id.eq(comment_id))
241 .filter(dsl::user_id.eq(user_id)),
247 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
248 #[belongs_to(Comment)]
249 #[table_name = "comment_saved"]
250 pub struct CommentSaved {
254 pub published: chrono::NaiveDateTime,
257 #[derive(Insertable, AsChangeset)]
258 #[table_name = "comment_saved"]
259 pub struct CommentSavedForm {
264 impl Saveable<CommentSavedForm> for CommentSaved {
265 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
266 use crate::schema::comment_saved::dsl::*;
267 insert_into(comment_saved)
268 .values(comment_saved_form)
269 .on_conflict((comment_id, user_id))
271 .set(comment_saved_form)
272 .get_result::<Self>(conn)
274 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
275 use crate::schema::comment_saved::dsl::*;
278 .filter(comment_id.eq(comment_saved_form.comment_id))
279 .filter(user_id.eq(comment_saved_form.user_id)),
288 source::{comment::*, community::*, post::*, user::*},
289 tests::establish_unpooled_connection,
297 let conn = establish_unpooled_connection();
299 let new_user = UserForm {
300 name: "terry".into(),
301 preferred_username: None,
302 password_encrypted: "nope".into(),
304 matrix_user_id: None,
312 theme: "browser".into(),
313 default_sort_type: SortType::Hot as i16,
314 default_listing_type: ListingType::Subscribed as i16,
315 lang: "browser".into(),
317 send_notifications_to_email: false,
323 last_refreshed_at: None,
326 let inserted_user = User_::create(&conn, &new_user).unwrap();
328 let new_community = CommunityForm {
329 name: "test community".to_string(),
330 title: "nada".to_owned(),
333 creator_id: inserted_user.id,
342 last_refreshed_at: None,
348 let inserted_community = Community::create(&conn, &new_community).unwrap();
350 let new_post = PostForm {
351 name: "A test post".into(),
352 creator_id: inserted_user.id,
355 community_id: inserted_community.id,
363 embed_description: None,
371 let inserted_post = Post::create(&conn, &new_post).unwrap();
373 let comment_form = CommentForm {
374 content: "A test comment".into(),
375 creator_id: inserted_user.id,
376 post_id: inserted_post.id,
387 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
389 let expected_comment = Comment {
390 id: inserted_comment.id,
391 content: "A test comment".into(),
392 creator_id: inserted_user.id,
393 post_id: inserted_post.id,
398 published: inserted_comment.published,
400 ap_id: inserted_comment.ap_id.to_owned(),
404 let child_comment_form = CommentForm {
405 content: "A child comment".into(),
406 creator_id: inserted_user.id,
407 post_id: inserted_post.id,
408 parent_id: Some(inserted_comment.id),
418 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
421 let comment_like_form = CommentLikeForm {
422 comment_id: inserted_comment.id,
423 post_id: inserted_post.id,
424 user_id: inserted_user.id,
428 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
430 let expected_comment_like = CommentLike {
431 id: inserted_comment_like.id,
432 comment_id: inserted_comment.id,
433 post_id: inserted_post.id,
434 user_id: inserted_user.id,
435 published: inserted_comment_like.published,
440 let comment_saved_form = CommentSavedForm {
441 comment_id: inserted_comment.id,
442 user_id: inserted_user.id,
445 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
447 let expected_comment_saved = CommentSaved {
448 id: inserted_comment_saved.id,
449 comment_id: inserted_comment.id,
450 user_id: inserted_user.id,
451 published: inserted_comment_saved.published,
454 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
455 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
456 let like_removed = CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
457 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
458 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
459 Comment::delete(&conn, inserted_child_comment.id).unwrap();
460 Post::delete(&conn, inserted_post.id).unwrap();
461 Community::delete(&conn, inserted_community.id).unwrap();
462 User_::delete(&conn, inserted_user.id).unwrap();
464 assert_eq!(expected_comment, read_comment);
465 assert_eq!(expected_comment, inserted_comment);
466 assert_eq!(expected_comment, updated_comment);
467 assert_eq!(expected_comment_like, inserted_comment_like);
468 assert_eq!(expected_comment_saved, inserted_comment_saved);
471 inserted_child_comment.parent_id.unwrap()
473 assert_eq!(1, like_removed);
474 assert_eq!(1, saved_removed);
475 assert_eq!(1, num_deleted);