2 newtypes::{CommentId, DbUrl, PersonId},
11 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();
214 community::{Community, CommunityForm},
215 person::{Person, PersonForm},
218 traits::{Crud, Likeable, Saveable},
219 utils::establish_unpooled_connection,
221 use serial_test::serial;
226 let conn = establish_unpooled_connection();
228 let new_person = PersonForm {
229 name: "terry".into(),
230 public_key: Some("pubkey".to_string()),
231 ..PersonForm::default()
234 let inserted_person = Person::create(&conn, &new_person).unwrap();
236 let new_community = CommunityForm {
237 name: "test community".to_string(),
238 title: "nada".to_owned(),
239 public_key: Some("pubkey".to_string()),
240 ..CommunityForm::default()
243 let inserted_community = Community::create(&conn, &new_community).unwrap();
245 let new_post = PostForm {
246 name: "A test post".into(),
247 creator_id: inserted_person.id,
248 community_id: inserted_community.id,
249 ..PostForm::default()
252 let inserted_post = Post::create(&conn, &new_post).unwrap();
254 let comment_form = CommentForm {
255 content: "A test comment".into(),
256 creator_id: inserted_person.id,
257 post_id: inserted_post.id,
258 ..CommentForm::default()
261 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
263 let expected_comment = Comment {
264 id: inserted_comment.id,
265 content: "A test comment".into(),
266 creator_id: inserted_person.id,
267 post_id: inserted_post.id,
272 published: inserted_comment.published,
274 ap_id: inserted_comment.ap_id.to_owned(),
278 let child_comment_form = CommentForm {
279 content: "A child comment".into(),
280 creator_id: inserted_person.id,
281 post_id: inserted_post.id,
282 parent_id: Some(inserted_comment.id),
283 ..CommentForm::default()
286 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
289 let comment_like_form = CommentLikeForm {
290 comment_id: inserted_comment.id,
291 post_id: inserted_post.id,
292 person_id: inserted_person.id,
296 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
298 let expected_comment_like = CommentLike {
299 id: inserted_comment_like.id,
300 comment_id: inserted_comment.id,
301 post_id: inserted_post.id,
302 person_id: inserted_person.id,
303 published: inserted_comment_like.published,
308 let comment_saved_form = CommentSavedForm {
309 comment_id: inserted_comment.id,
310 person_id: inserted_person.id,
313 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
315 let expected_comment_saved = CommentSaved {
316 id: inserted_comment_saved.id,
317 comment_id: inserted_comment.id,
318 person_id: inserted_person.id,
319 published: inserted_comment_saved.published,
322 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
323 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
324 let like_removed = CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
325 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
326 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
327 Comment::delete(&conn, inserted_child_comment.id).unwrap();
328 Post::delete(&conn, inserted_post.id).unwrap();
329 Community::delete(&conn, inserted_community.id).unwrap();
330 Person::delete(&conn, inserted_person.id).unwrap();
332 assert_eq!(expected_comment, read_comment);
333 assert_eq!(expected_comment, inserted_comment);
334 assert_eq!(expected_comment, updated_comment);
335 assert_eq!(expected_comment_like, inserted_comment_like);
336 assert_eq!(expected_comment_saved, inserted_comment_saved);
339 inserted_child_comment.parent_id.unwrap()
341 assert_eq!(1, like_removed);
342 assert_eq!(1, saved_removed);
343 assert_eq!(1, num_deleted);