2 newtypes::{CommentId, DbUrl, PersonId},
11 traits::{Crud, DeleteableOrRemoveable, Likeable, Saveable},
14 use diesel::{dsl::*, result::Error, *};
15 use diesel_ltree::Ltree;
20 conn: &mut PgConnection,
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(
32 conn: &mut PgConnection,
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(
46 conn: &mut PgConnection,
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(
57 conn: &mut PgConnection,
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(
68 conn: &mut PgConnection,
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)
79 conn: &mut PgConnection,
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: &mut 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: &mut 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: &mut 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: &mut PgConnection, _comment_form: &CommentForm) -> Result<Self, Error> {
189 conn: &mut PgConnection,
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: &mut 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)
213 conn: &mut PgConnection,
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: &mut 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)
239 conn: &mut PgConnection,
240 comment_saved_form: &CommentSavedForm,
241 ) -> Result<usize, Error> {
242 use crate::schema::comment_saved::dsl::*;
245 .filter(comment_id.eq(comment_saved_form.comment_id))
246 .filter(person_id.eq(comment_saved_form.person_id)),
252 impl DeleteableOrRemoveable for Comment {
253 fn blank_out_deleted_or_removed_info(mut self) -> Self {
254 self.content = "".into();
262 newtypes::LanguageId,
265 community::{Community, CommunityForm},
266 person::{Person, PersonForm},
269 traits::{Crud, Likeable, Saveable},
270 utils::establish_unpooled_connection,
272 use diesel_ltree::Ltree;
273 use serial_test::serial;
278 let conn = &mut establish_unpooled_connection();
280 let new_person = PersonForm {
281 name: "terry".into(),
282 public_key: Some("pubkey".to_string()),
283 ..PersonForm::default()
286 let inserted_person = Person::create(conn, &new_person).unwrap();
288 let new_community = CommunityForm {
289 name: "test community".to_string(),
290 title: "nada".to_owned(),
291 public_key: Some("pubkey".to_string()),
292 ..CommunityForm::default()
295 let inserted_community = Community::create(conn, &new_community).unwrap();
297 let new_post = PostForm {
298 name: "A test post".into(),
299 creator_id: inserted_person.id,
300 community_id: inserted_community.id,
301 ..PostForm::default()
304 let inserted_post = Post::create(conn, &new_post).unwrap();
306 let comment_form = CommentForm {
307 content: "A test comment".into(),
308 creator_id: inserted_person.id,
309 post_id: inserted_post.id,
310 ..CommentForm::default()
313 let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
315 let expected_comment = Comment {
316 id: inserted_comment.id,
317 content: "A test comment".into(),
318 creator_id: inserted_person.id,
319 post_id: inserted_post.id,
322 path: Ltree(format!("0.{}", inserted_comment.id)),
323 published: inserted_comment.published,
325 ap_id: inserted_comment.ap_id.to_owned(),
326 distinguished: false,
328 language_id: LanguageId::default(),
331 let child_comment_form = CommentForm {
332 content: "A child comment".into(),
333 creator_id: inserted_person.id,
334 post_id: inserted_post.id,
335 // path: Some(text2ltree(inserted_comment.id),
336 ..CommentForm::default()
339 let inserted_child_comment =
340 Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
343 let comment_like_form = CommentLikeForm {
344 comment_id: inserted_comment.id,
345 post_id: inserted_post.id,
346 person_id: inserted_person.id,
350 let inserted_comment_like = CommentLike::like(conn, &comment_like_form).unwrap();
352 let expected_comment_like = CommentLike {
353 id: inserted_comment_like.id,
354 comment_id: inserted_comment.id,
355 post_id: inserted_post.id,
356 person_id: inserted_person.id,
357 published: inserted_comment_like.published,
362 let comment_saved_form = CommentSavedForm {
363 comment_id: inserted_comment.id,
364 person_id: inserted_person.id,
367 let inserted_comment_saved = CommentSaved::save(conn, &comment_saved_form).unwrap();
369 let expected_comment_saved = CommentSaved {
370 id: inserted_comment_saved.id,
371 comment_id: inserted_comment.id,
372 person_id: inserted_person.id,
373 published: inserted_comment_saved.published,
376 let read_comment = Comment::read(conn, inserted_comment.id).unwrap();
377 let updated_comment = Comment::update(conn, inserted_comment.id, &comment_form).unwrap();
378 let like_removed = CommentLike::remove(conn, inserted_person.id, inserted_comment.id).unwrap();
379 let saved_removed = CommentSaved::unsave(conn, &comment_saved_form).unwrap();
380 let num_deleted = Comment::delete(conn, inserted_comment.id).unwrap();
381 Comment::delete(conn, inserted_child_comment.id).unwrap();
382 Post::delete(conn, inserted_post.id).unwrap();
383 Community::delete(conn, inserted_community.id).unwrap();
384 Person::delete(conn, inserted_person.id).unwrap();
386 assert_eq!(expected_comment, read_comment);
387 assert_eq!(expected_comment, inserted_comment);
388 assert_eq!(expected_comment, updated_comment);
389 assert_eq!(expected_comment_like, inserted_comment_like);
390 assert_eq!(expected_comment_saved, inserted_comment_saved);
392 format!("0.{}.{}", expected_comment.id, inserted_child_comment.id),
393 inserted_child_comment.path.0,
395 assert_eq!(1, like_removed);
396 assert_eq!(1, saved_removed);
397 assert_eq!(1, num_deleted);