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