]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/comment_reply.rs
add enable_federated_downvotes site option
[lemmy.git] / crates / db_schema / src / impls / comment_reply.rs
1 use crate::{
2   newtypes::{CommentId, CommentReplyId, PersonId},
3   schema::comment_reply::dsl::{comment_id, comment_reply, read, recipient_id},
4   source::comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm},
5   traits::Crud,
6   utils::{get_conn, DbPool},
7 };
8 use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
9 use diesel_async::RunQueryDsl;
10
11 #[async_trait]
12 impl Crud for CommentReply {
13   type InsertForm = CommentReplyInsertForm;
14   type UpdateForm = CommentReplyUpdateForm;
15   type IdType = CommentReplyId;
16
17   async fn create(
18     pool: &mut DbPool<'_>,
19     comment_reply_form: &Self::InsertForm,
20   ) -> Result<Self, Error> {
21     let conn = &mut get_conn(pool).await?;
22
23     // since the return here isnt utilized, we dont need to do an update
24     // but get_result doesnt return the existing row here
25     insert_into(comment_reply)
26       .values(comment_reply_form)
27       .on_conflict((recipient_id, comment_id))
28       .do_update()
29       .set(comment_reply_form)
30       .get_result::<Self>(conn)
31       .await
32   }
33
34   async fn update(
35     pool: &mut DbPool<'_>,
36     comment_reply_id: CommentReplyId,
37     comment_reply_form: &Self::UpdateForm,
38   ) -> Result<Self, Error> {
39     let conn = &mut get_conn(pool).await?;
40     diesel::update(comment_reply.find(comment_reply_id))
41       .set(comment_reply_form)
42       .get_result::<Self>(conn)
43       .await
44   }
45 }
46
47 impl CommentReply {
48   pub async fn mark_all_as_read(
49     pool: &mut DbPool<'_>,
50     for_recipient_id: PersonId,
51   ) -> Result<Vec<CommentReply>, Error> {
52     let conn = &mut get_conn(pool).await?;
53     diesel::update(
54       comment_reply
55         .filter(recipient_id.eq(for_recipient_id))
56         .filter(read.eq(false)),
57     )
58     .set(read.eq(true))
59     .get_results::<Self>(conn)
60     .await
61   }
62
63   pub async fn read_by_comment(
64     pool: &mut DbPool<'_>,
65     for_comment_id: CommentId,
66   ) -> Result<Self, Error> {
67     let conn = &mut get_conn(pool).await?;
68     comment_reply
69       .filter(comment_id.eq(for_comment_id))
70       .first::<Self>(conn)
71       .await
72   }
73 }
74
75 #[cfg(test)]
76 mod tests {
77   #![allow(clippy::unwrap_used)]
78   #![allow(clippy::indexing_slicing)]
79
80   use crate::{
81     source::{
82       comment::{Comment, CommentInsertForm},
83       comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm},
84       community::{Community, CommunityInsertForm},
85       instance::Instance,
86       person::{Person, PersonInsertForm},
87       post::{Post, PostInsertForm},
88     },
89     traits::Crud,
90     utils::build_db_pool_for_tests,
91   };
92   use serial_test::serial;
93
94   #[tokio::test]
95   #[serial]
96   async fn test_crud() {
97     let pool = &build_db_pool_for_tests().await;
98     let pool = &mut pool.into();
99
100     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
101       .await
102       .unwrap();
103
104     let new_person = PersonInsertForm::builder()
105       .name("terrylake".into())
106       .public_key("pubkey".to_string())
107       .instance_id(inserted_instance.id)
108       .build();
109
110     let inserted_person = Person::create(pool, &new_person).await.unwrap();
111
112     let recipient_form = PersonInsertForm::builder()
113       .name("terrylakes recipient".into())
114       .public_key("pubkey".to_string())
115       .instance_id(inserted_instance.id)
116       .build();
117
118     let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
119
120     let new_community = CommunityInsertForm::builder()
121       .name("test community lake".to_string())
122       .title("nada".to_owned())
123       .public_key("pubkey".to_string())
124       .instance_id(inserted_instance.id)
125       .build();
126
127     let inserted_community = Community::create(pool, &new_community).await.unwrap();
128
129     let new_post = PostInsertForm::builder()
130       .name("A test post".into())
131       .creator_id(inserted_person.id)
132       .community_id(inserted_community.id)
133       .build();
134
135     let inserted_post = Post::create(pool, &new_post).await.unwrap();
136
137     let comment_form = CommentInsertForm::builder()
138       .content("A test comment".into())
139       .creator_id(inserted_person.id)
140       .post_id(inserted_post.id)
141       .build();
142
143     let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
144
145     let comment_reply_form = CommentReplyInsertForm {
146       recipient_id: inserted_recipient.id,
147       comment_id: inserted_comment.id,
148       read: None,
149     };
150
151     let inserted_reply = CommentReply::create(pool, &comment_reply_form)
152       .await
153       .unwrap();
154
155     let expected_reply = CommentReply {
156       id: inserted_reply.id,
157       recipient_id: inserted_reply.recipient_id,
158       comment_id: inserted_reply.comment_id,
159       read: false,
160       published: inserted_reply.published,
161     };
162
163     let read_reply = CommentReply::read(pool, inserted_reply.id).await.unwrap();
164
165     let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) };
166     let updated_reply = CommentReply::update(pool, inserted_reply.id, &comment_reply_update_form)
167       .await
168       .unwrap();
169
170     Comment::delete(pool, inserted_comment.id).await.unwrap();
171     Post::delete(pool, inserted_post.id).await.unwrap();
172     Community::delete(pool, inserted_community.id)
173       .await
174       .unwrap();
175     Person::delete(pool, inserted_person.id).await.unwrap();
176     Person::delete(pool, inserted_recipient.id).await.unwrap();
177     Instance::delete(pool, inserted_instance.id).await.unwrap();
178
179     assert_eq!(expected_reply, read_reply);
180     assert_eq!(expected_reply, inserted_reply);
181     assert_eq!(expected_reply, updated_reply);
182   }
183 }