2 newtypes::{CommentId, DbUrl, PersonId},
11 traits::{Crud, DeleteableOrRemoveable, Likeable, Saveable},
14 use diesel::{dsl::*, result::Error, *};
15 use diesel_ltree::Ltree;
21 comment_id: CommentId,
23 ) -> Result<Self, Error> {
24 use crate::schema::comment::dsl::*;
26 diesel::update(comment.find(comment_id))
27 .set(ap_id.eq(apub_id))
28 .get_result::<Self>(conn)
31 pub fn permadelete_for_creator(
33 for_creator_id: PersonId,
34 ) -> Result<Vec<Self>, Error> {
35 use crate::schema::comment::dsl::*;
36 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
38 content.eq("*Permananently Deleted*"),
40 updated.eq(naive_now()),
42 .get_results::<Self>(conn)
45 pub fn update_deleted(
47 comment_id: CommentId,
49 ) -> Result<Self, Error> {
50 use crate::schema::comment::dsl::*;
51 diesel::update(comment.find(comment_id))
52 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
53 .get_result::<Self>(conn)
56 pub fn update_removed(
58 comment_id: CommentId,
60 ) -> Result<Self, Error> {
61 use crate::schema::comment::dsl::*;
62 diesel::update(comment.find(comment_id))
63 .set((removed.eq(new_removed), updated.eq(naive_now())))
64 .get_result::<Self>(conn)
67 pub fn update_removed_for_creator(
69 for_creator_id: PersonId,
71 ) -> Result<Vec<Self>, Error> {
72 use crate::schema::comment::dsl::*;
73 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
74 .set((removed.eq(new_removed), updated.eq(naive_now())))
75 .get_results::<Self>(conn)
80 comment_form: &CommentForm,
81 parent_path: Option<&Ltree>,
82 ) -> Result<Comment, Error> {
83 use crate::schema::comment::dsl::*;
85 // Insert, to get the id
86 let inserted_comment = insert_into(comment)
91 .get_result::<Self>(conn);
93 if let Ok(comment_insert) = inserted_comment {
94 let comment_id = comment_insert.id;
96 // You need to update the ltree column
97 let ltree = Ltree(if let Some(parent_path) = parent_path {
98 // The previous parent will already have 0 in it
99 // Append this comment id
100 format!("{}.{}", parent_path.0, comment_id)
102 // '0' is always the first path, append to that
103 format!("{}.{}", 0, comment_id)
106 let updated_comment = diesel::update(comment.find(comment_id))
108 .get_result::<Self>(conn);
110 // Update the child count for the parent comment_aggregates
111 // You could do this with a trigger, but since you have to do this manually anyway,
112 // you can just have it here
113 if let Some(parent_path) = parent_path {
114 // You have to update counts for all parents, not just the immediate one
115 // TODO if the performance of this is terrible, it might be better to do this as part of a
116 // scheduled query... although the counts would often be wrong.
118 // The child_count query for reference:
119 // select c.id, c.path, count(c2.id) as child_count from comment c
120 // left join comment c2 on c2.path <@ c.path and c2.path != c.path
123 let top_parent = format!("0.{}", parent_path.0.split('.').collect::<Vec<&str>>()[1]);
124 let update_child_count_stmt = format!(
126 update comment_aggregates ca set child_count = c.child_count
128 select c.id, c.path, count(c2.id) as child_count from comment c
129 join comment c2 on c2.path <@ c.path and c2.path != c.path
133 where ca.comment_id = c.id",
137 sql_query(update_child_count_stmt).execute(conn)?;
144 pub fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
145 use crate::schema::comment::dsl::*;
146 let object_id: DbUrl = object_id.into();
149 .filter(ap_id.eq(object_id))
150 .first::<Comment>(conn)
156 pub fn parent_comment_id(&self) -> Option<CommentId> {
157 let mut ltree_split: Vec<&str> = self.path.0.split('.').collect();
158 ltree_split.remove(0); // The first is always 0
159 if ltree_split.len() > 1 {
160 ltree_split[ltree_split.len() - 2]
170 impl Crud for Comment {
171 type Form = CommentForm;
172 type IdType = CommentId;
173 fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
174 use crate::schema::comment::dsl::*;
175 comment.find(comment_id).first::<Self>(conn)
178 fn delete(conn: &PgConnection, comment_id: CommentId) -> Result<usize, Error> {
179 use crate::schema::comment::dsl::*;
180 diesel::delete(comment.find(comment_id)).execute(conn)
183 /// This is unimplemented, use [[Comment::create]]
184 fn create(_conn: &PgConnection, _comment_form: &CommentForm) -> Result<Self, Error> {
190 comment_id: CommentId,
191 comment_form: &CommentForm,
192 ) -> Result<Self, Error> {
193 use crate::schema::comment::dsl::*;
194 diesel::update(comment.find(comment_id))
196 .get_result::<Self>(conn)
200 impl Likeable for CommentLike {
201 type Form = CommentLikeForm;
202 type IdType = CommentId;
203 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
204 use crate::schema::comment_like::dsl::*;
205 insert_into(comment_like)
206 .values(comment_like_form)
207 .on_conflict((comment_id, person_id))
209 .set(comment_like_form)
210 .get_result::<Self>(conn)
215 comment_id: CommentId,
216 ) -> Result<usize, Error> {
217 use crate::schema::comment_like::dsl;
220 .filter(dsl::comment_id.eq(comment_id))
221 .filter(dsl::person_id.eq(person_id)),
227 impl Saveable for CommentSaved {
228 type Form = CommentSavedForm;
229 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
230 use crate::schema::comment_saved::dsl::*;
231 insert_into(comment_saved)
232 .values(comment_saved_form)
233 .on_conflict((comment_id, person_id))
235 .set(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(person_id.eq(comment_saved_form.person_id)),
249 impl DeleteableOrRemoveable for Comment {
250 fn blank_out_deleted_or_removed_info(mut self) -> Self {
251 self.content = "".into();
259 newtypes::LanguageId,
262 community::{Community, CommunityForm},
263 person::{Person, PersonForm},
266 traits::{Crud, Likeable, Saveable},
267 utils::establish_unpooled_connection,
269 use diesel_ltree::Ltree;
270 use serial_test::serial;
275 let conn = establish_unpooled_connection();
277 let new_person = PersonForm {
278 name: "terry".into(),
279 public_key: Some("pubkey".to_string()),
280 ..PersonForm::default()
283 let inserted_person = Person::create(&conn, &new_person).unwrap();
285 let new_community = CommunityForm {
286 name: "test community".to_string(),
287 title: "nada".to_owned(),
288 public_key: Some("pubkey".to_string()),
289 ..CommunityForm::default()
292 let inserted_community = Community::create(&conn, &new_community).unwrap();
294 let new_post = PostForm {
295 name: "A test post".into(),
296 creator_id: inserted_person.id,
297 community_id: inserted_community.id,
298 ..PostForm::default()
301 let inserted_post = Post::create(&conn, &new_post).unwrap();
303 let comment_form = CommentForm {
304 content: "A test comment".into(),
305 creator_id: inserted_person.id,
306 post_id: inserted_post.id,
307 ..CommentForm::default()
310 let inserted_comment = Comment::create(&conn, &comment_form, None).unwrap();
312 let expected_comment = Comment {
313 id: inserted_comment.id,
314 content: "A test comment".into(),
315 creator_id: inserted_person.id,
316 post_id: inserted_post.id,
319 path: Ltree(format!("0.{}", inserted_comment.id)),
320 published: inserted_comment.published,
322 ap_id: inserted_comment.ap_id.to_owned(),
323 distinguished: false,
325 language_id: LanguageId::default(),
328 let child_comment_form = CommentForm {
329 content: "A child comment".into(),
330 creator_id: inserted_person.id,
331 post_id: inserted_post.id,
332 // path: Some(text2ltree(inserted_comment.id),
333 ..CommentForm::default()
336 let inserted_child_comment =
337 Comment::create(&conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
340 let comment_like_form = CommentLikeForm {
341 comment_id: inserted_comment.id,
342 post_id: inserted_post.id,
343 person_id: inserted_person.id,
347 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
349 let expected_comment_like = CommentLike {
350 id: inserted_comment_like.id,
351 comment_id: inserted_comment.id,
352 post_id: inserted_post.id,
353 person_id: inserted_person.id,
354 published: inserted_comment_like.published,
359 let comment_saved_form = CommentSavedForm {
360 comment_id: inserted_comment.id,
361 person_id: inserted_person.id,
364 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
366 let expected_comment_saved = CommentSaved {
367 id: inserted_comment_saved.id,
368 comment_id: inserted_comment.id,
369 person_id: inserted_person.id,
370 published: inserted_comment_saved.published,
373 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
374 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
375 let like_removed = CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
376 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
377 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
378 Comment::delete(&conn, inserted_child_comment.id).unwrap();
379 Post::delete(&conn, inserted_post.id).unwrap();
380 Community::delete(&conn, inserted_community.id).unwrap();
381 Person::delete(&conn, inserted_person.id).unwrap();
383 assert_eq!(expected_comment, read_comment);
384 assert_eq!(expected_comment, inserted_comment);
385 assert_eq!(expected_comment, updated_comment);
386 assert_eq!(expected_comment_like, inserted_comment_like);
387 assert_eq!(expected_comment_saved, inserted_comment_saved);
389 format!("0.{}.{}", expected_comment.id, inserted_child_comment.id),
390 inserted_child_comment.path.0,
392 assert_eq!(1, like_removed);
393 assert_eq!(1, saved_removed);
394 assert_eq!(1, num_deleted);