]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/person_mention.rs
Dont upsert Instance row every apub fetch (#2771)
[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: &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::{Comment, CommentInsertForm},
85       community::{Community, CommunityInsertForm},
86       instance::Instance,
87       person::{Person, PersonInsertForm},
88       person_mention::{PersonMention, PersonMentionInsertForm, PersonMentionUpdateForm},
89       post::{Post, PostInsertForm},
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::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 }