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