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