]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/comment_reply.rs
Cache & Optimize Woodpecker CI (#3450)
[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   #![allow(clippy::unwrap_used)]
85   #![allow(clippy::indexing_slicing)]
86
87   use crate::{
88     source::{
89       comment::{Comment, CommentInsertForm},
90       comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm},
91       community::{Community, CommunityInsertForm},
92       instance::Instance,
93       person::{Person, PersonInsertForm},
94       post::{Post, PostInsertForm},
95     },
96     traits::Crud,
97     utils::build_db_pool_for_tests,
98   };
99   use serial_test::serial;
100
101   #[tokio::test]
102   #[serial]
103   async fn test_crud() {
104     let pool = &build_db_pool_for_tests().await;
105     let pool = &mut pool.into();
106
107     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
108       .await
109       .unwrap();
110
111     let new_person = PersonInsertForm::builder()
112       .name("terrylake".into())
113       .public_key("pubkey".to_string())
114       .instance_id(inserted_instance.id)
115       .build();
116
117     let inserted_person = Person::create(pool, &new_person).await.unwrap();
118
119     let recipient_form = PersonInsertForm::builder()
120       .name("terrylakes recipient".into())
121       .public_key("pubkey".to_string())
122       .instance_id(inserted_instance.id)
123       .build();
124
125     let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
126
127     let new_community = CommunityInsertForm::builder()
128       .name("test community lake".to_string())
129       .title("nada".to_owned())
130       .public_key("pubkey".to_string())
131       .instance_id(inserted_instance.id)
132       .build();
133
134     let inserted_community = Community::create(pool, &new_community).await.unwrap();
135
136     let new_post = PostInsertForm::builder()
137       .name("A test post".into())
138       .creator_id(inserted_person.id)
139       .community_id(inserted_community.id)
140       .build();
141
142     let inserted_post = Post::create(pool, &new_post).await.unwrap();
143
144     let comment_form = CommentInsertForm::builder()
145       .content("A test comment".into())
146       .creator_id(inserted_person.id)
147       .post_id(inserted_post.id)
148       .build();
149
150     let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
151
152     let comment_reply_form = CommentReplyInsertForm {
153       recipient_id: inserted_recipient.id,
154       comment_id: inserted_comment.id,
155       read: None,
156     };
157
158     let inserted_reply = CommentReply::create(pool, &comment_reply_form)
159       .await
160       .unwrap();
161
162     let expected_reply = CommentReply {
163       id: inserted_reply.id,
164       recipient_id: inserted_reply.recipient_id,
165       comment_id: inserted_reply.comment_id,
166       read: false,
167       published: inserted_reply.published,
168     };
169
170     let read_reply = CommentReply::read(pool, inserted_reply.id).await.unwrap();
171
172     let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) };
173     let updated_reply = CommentReply::update(pool, inserted_reply.id, &comment_reply_update_form)
174       .await
175       .unwrap();
176
177     Comment::delete(pool, inserted_comment.id).await.unwrap();
178     Post::delete(pool, inserted_post.id).await.unwrap();
179     Community::delete(pool, inserted_community.id)
180       .await
181       .unwrap();
182     Person::delete(pool, inserted_person.id).await.unwrap();
183     Person::delete(pool, inserted_recipient.id).await.unwrap();
184     Instance::delete(pool, inserted_instance.id).await.unwrap();
185
186     assert_eq!(expected_reply, read_reply);
187     assert_eq!(expected_reply, inserted_reply);
188     assert_eq!(expected_reply, updated_reply);
189   }
190 }