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