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