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