2 newtypes::{CommentId, PersonId, PersonMentionId},
3 schema::person_mention::dsl::{comment_id, person_mention, read, recipient_id},
4 source::person_mention::{PersonMention, PersonMentionInsertForm, PersonMentionUpdateForm},
6 utils::{get_conn, DbPool},
8 use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
9 use diesel_async::RunQueryDsl;
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?;
19 .find(person_mention_id)
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))
35 .set(person_mention_form)
36 .get_result::<Self>(conn)
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)
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?;
61 .filter(recipient_id.eq(for_recipient_id))
62 .filter(read.eq(false)),
65 .get_results::<Self>(conn)
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?;
76 .filter(comment_id.eq(for_comment_id))
77 .filter(recipient_id.eq(for_recipient_id))
85 #![allow(clippy::unwrap_used)]
86 #![allow(clippy::indexing_slicing)]
90 comment::{Comment, CommentInsertForm},
91 community::{Community, CommunityInsertForm},
93 person::{Person, PersonInsertForm},
94 person_mention::{PersonMention, PersonMentionInsertForm, PersonMentionUpdateForm},
95 post::{Post, PostInsertForm},
98 utils::build_db_pool_for_tests,
100 use serial_test::serial;
104 async fn test_crud() {
105 let pool = &build_db_pool_for_tests().await;
106 let pool = &mut pool.into();
108 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
112 let new_person = PersonInsertForm::builder()
113 .name("terrylake".into())
114 .public_key("pubkey".to_string())
115 .instance_id(inserted_instance.id)
118 let inserted_person = Person::create(pool, &new_person).await.unwrap();
120 let recipient_form = PersonInsertForm::builder()
121 .name("terrylakes recipient".into())
122 .public_key("pubkey".to_string())
123 .instance_id(inserted_instance.id)
126 let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
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)
135 let inserted_community = Community::create(pool, &new_community).await.unwrap();
137 let new_post = PostInsertForm::builder()
138 .name("A test post".into())
139 .creator_id(inserted_person.id)
140 .community_id(inserted_community.id)
143 let inserted_post = Post::create(pool, &new_post).await.unwrap();
145 let comment_form = CommentInsertForm::builder()
146 .content("A test comment".into())
147 .creator_id(inserted_person.id)
148 .post_id(inserted_post.id)
151 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
153 let person_mention_form = PersonMentionInsertForm {
154 recipient_id: inserted_recipient.id,
155 comment_id: inserted_comment.id,
159 let inserted_mention = PersonMention::create(pool, &person_mention_form)
163 let expected_mention = PersonMention {
164 id: inserted_mention.id,
165 recipient_id: inserted_mention.recipient_id,
166 comment_id: inserted_mention.comment_id,
168 published: inserted_mention.published,
171 let read_mention = PersonMention::read(pool, inserted_mention.id)
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)
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)
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();
189 assert_eq!(expected_mention, read_mention);
190 assert_eq!(expected_mention, inserted_mention);
191 assert_eq!(expected_mention, updated_mention);