3 newtypes::{CommentId, DbUrl, PersonId},
12 traits::{Crud, DeleteableOrRemoveable, Likeable, Saveable},
14 use diesel::{dsl::*, result::Error, *};
20 comment_id: CommentId,
22 ) -> Result<Self, Error> {
23 use crate::schema::comment::dsl::*;
25 diesel::update(comment.find(comment_id))
26 .set(ap_id.eq(apub_id))
27 .get_result::<Self>(conn)
30 pub fn permadelete_for_creator(
32 for_creator_id: PersonId,
33 ) -> Result<Vec<Self>, Error> {
34 use crate::schema::comment::dsl::*;
35 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
37 content.eq("*Permananently Deleted*"),
39 updated.eq(naive_now()),
41 .get_results::<Self>(conn)
44 pub fn update_deleted(
46 comment_id: CommentId,
48 ) -> Result<Self, Error> {
49 use crate::schema::comment::dsl::*;
50 diesel::update(comment.find(comment_id))
51 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
52 .get_result::<Self>(conn)
55 pub fn update_removed(
57 comment_id: CommentId,
59 ) -> Result<Self, Error> {
60 use crate::schema::comment::dsl::*;
61 diesel::update(comment.find(comment_id))
62 .set((removed.eq(new_removed), updated.eq(naive_now())))
63 .get_result::<Self>(conn)
66 pub fn update_removed_for_creator(
68 for_creator_id: PersonId,
70 ) -> Result<Vec<Self>, Error> {
71 use crate::schema::comment::dsl::*;
72 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
73 .set((removed.eq(new_removed), updated.eq(naive_now())))
74 .get_results::<Self>(conn)
79 comment_id: CommentId,
81 ) -> Result<Self, Error> {
82 use crate::schema::comment::dsl::*;
83 diesel::update(comment.find(comment_id))
84 .set(read.eq(new_read))
85 .get_result::<Self>(conn)
88 pub fn update_content(
90 comment_id: CommentId,
92 ) -> Result<Self, Error> {
93 use crate::schema::comment::dsl::*;
94 diesel::update(comment.find(comment_id))
95 .set((content.eq(new_content), updated.eq(naive_now())))
96 .get_result::<Self>(conn)
99 pub fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Comment, Error> {
100 use crate::schema::comment::dsl::*;
102 .values(comment_form)
106 .get_result::<Self>(conn)
108 pub fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
109 use crate::schema::comment::dsl::*;
110 let object_id: DbUrl = object_id.into();
113 .filter(ap_id.eq(object_id))
114 .first::<Comment>(conn)
121 impl Crud for Comment {
122 type Form = CommentForm;
123 type IdType = CommentId;
124 fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
125 use crate::schema::comment::dsl::*;
126 comment.find(comment_id).first::<Self>(conn)
129 fn delete(conn: &PgConnection, comment_id: CommentId) -> Result<usize, Error> {
130 use crate::schema::comment::dsl::*;
131 diesel::delete(comment.find(comment_id)).execute(conn)
134 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
135 use crate::schema::comment::dsl::*;
137 .values(comment_form)
138 .get_result::<Self>(conn)
143 comment_id: CommentId,
144 comment_form: &CommentForm,
145 ) -> Result<Self, Error> {
146 use crate::schema::comment::dsl::*;
147 diesel::update(comment.find(comment_id))
149 .get_result::<Self>(conn)
153 impl Likeable for CommentLike {
154 type Form = CommentLikeForm;
155 type IdType = CommentId;
156 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
157 use crate::schema::comment_like::dsl::*;
158 insert_into(comment_like)
159 .values(comment_like_form)
160 .on_conflict((comment_id, person_id))
162 .set(comment_like_form)
163 .get_result::<Self>(conn)
168 comment_id: CommentId,
169 ) -> Result<usize, Error> {
170 use crate::schema::comment_like::dsl;
173 .filter(dsl::comment_id.eq(comment_id))
174 .filter(dsl::person_id.eq(person_id)),
180 impl Saveable for CommentSaved {
181 type Form = CommentSavedForm;
182 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
183 use crate::schema::comment_saved::dsl::*;
184 insert_into(comment_saved)
185 .values(comment_saved_form)
186 .on_conflict((comment_id, person_id))
188 .set(comment_saved_form)
189 .get_result::<Self>(conn)
191 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
192 use crate::schema::comment_saved::dsl::*;
195 .filter(comment_id.eq(comment_saved_form.comment_id))
196 .filter(person_id.eq(comment_saved_form.person_id)),
202 impl DeleteableOrRemoveable for Comment {
203 fn blank_out_deleted_or_removed_info(mut self) -> Self {
204 self.content = "".into();
212 establish_unpooled_connection,
215 community::{Community, CommunityForm},
216 person::{Person, PersonForm},
219 traits::{Crud, Likeable, Saveable},
221 use serial_test::serial;
226 let conn = establish_unpooled_connection();
228 let new_person = PersonForm {
229 name: "terry".into(),
230 ..PersonForm::default()
233 let inserted_person = Person::create(&conn, &new_person).unwrap();
235 let new_community = CommunityForm {
236 name: "test community".to_string(),
237 title: "nada".to_owned(),
238 ..CommunityForm::default()
241 let inserted_community = Community::create(&conn, &new_community).unwrap();
243 let new_post = PostForm {
244 name: "A test post".into(),
245 creator_id: inserted_person.id,
246 community_id: inserted_community.id,
247 ..PostForm::default()
250 let inserted_post = Post::create(&conn, &new_post).unwrap();
252 let comment_form = CommentForm {
253 content: "A test comment".into(),
254 creator_id: inserted_person.id,
255 post_id: inserted_post.id,
256 ..CommentForm::default()
259 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
261 let expected_comment = Comment {
262 id: inserted_comment.id,
263 content: "A test comment".into(),
264 creator_id: inserted_person.id,
265 post_id: inserted_post.id,
270 published: inserted_comment.published,
272 ap_id: inserted_comment.ap_id.to_owned(),
276 let child_comment_form = CommentForm {
277 content: "A child comment".into(),
278 creator_id: inserted_person.id,
279 post_id: inserted_post.id,
280 parent_id: Some(inserted_comment.id),
281 ..CommentForm::default()
284 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
287 let comment_like_form = CommentLikeForm {
288 comment_id: inserted_comment.id,
289 post_id: inserted_post.id,
290 person_id: inserted_person.id,
294 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
296 let expected_comment_like = CommentLike {
297 id: inserted_comment_like.id,
298 comment_id: inserted_comment.id,
299 post_id: inserted_post.id,
300 person_id: inserted_person.id,
301 published: inserted_comment_like.published,
306 let comment_saved_form = CommentSavedForm {
307 comment_id: inserted_comment.id,
308 person_id: inserted_person.id,
311 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
313 let expected_comment_saved = CommentSaved {
314 id: inserted_comment_saved.id,
315 comment_id: inserted_comment.id,
316 person_id: inserted_person.id,
317 published: inserted_comment_saved.published,
320 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
321 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
322 let like_removed = CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
323 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
324 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
325 Comment::delete(&conn, inserted_child_comment.id).unwrap();
326 Post::delete(&conn, inserted_post.id).unwrap();
327 Community::delete(&conn, inserted_community.id).unwrap();
328 Person::delete(&conn, inserted_person.id).unwrap();
330 assert_eq!(expected_comment, read_comment);
331 assert_eq!(expected_comment, inserted_comment);
332 assert_eq!(expected_comment, updated_comment);
333 assert_eq!(expected_comment_like, inserted_comment_like);
334 assert_eq!(expected_comment_saved, inserted_comment_saved);
337 inserted_child_comment.parent_id.unwrap()
339 assert_eq!(1, like_removed);
340 assert_eq!(1, saved_removed);
341 assert_eq!(1, num_deleted);