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;
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))
28 .set(person_mention_form)
29 .get_result::<Self>(conn)
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)
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?;
54 .filter(recipient_id.eq(for_recipient_id))
55 .filter(read.eq(false)),
58 .get_results::<Self>(conn)
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?;
69 .filter(comment_id.eq(for_comment_id))
70 .filter(recipient_id.eq(for_recipient_id))
78 #![allow(clippy::unwrap_used)]
79 #![allow(clippy::indexing_slicing)]
83 comment::{Comment, CommentInsertForm},
84 community::{Community, CommunityInsertForm},
86 person::{Person, PersonInsertForm},
87 person_mention::{PersonMention, PersonMentionInsertForm, PersonMentionUpdateForm},
88 post::{Post, PostInsertForm},
91 utils::build_db_pool_for_tests,
93 use serial_test::serial;
97 async fn test_crud() {
98 let pool = &build_db_pool_for_tests().await;
99 let pool = &mut pool.into();
101 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
105 let new_person = PersonInsertForm::builder()
106 .name("terrylake".into())
107 .public_key("pubkey".to_string())
108 .instance_id(inserted_instance.id)
111 let inserted_person = Person::create(pool, &new_person).await.unwrap();
113 let recipient_form = PersonInsertForm::builder()
114 .name("terrylakes recipient".into())
115 .public_key("pubkey".to_string())
116 .instance_id(inserted_instance.id)
119 let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
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)
128 let inserted_community = Community::create(pool, &new_community).await.unwrap();
130 let new_post = PostInsertForm::builder()
131 .name("A test post".into())
132 .creator_id(inserted_person.id)
133 .community_id(inserted_community.id)
136 let inserted_post = Post::create(pool, &new_post).await.unwrap();
138 let comment_form = CommentInsertForm::builder()
139 .content("A test comment".into())
140 .creator_id(inserted_person.id)
141 .post_id(inserted_post.id)
144 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
146 let person_mention_form = PersonMentionInsertForm {
147 recipient_id: inserted_recipient.id,
148 comment_id: inserted_comment.id,
152 let inserted_mention = PersonMention::create(pool, &person_mention_form)
156 let expected_mention = PersonMention {
157 id: inserted_mention.id,
158 recipient_id: inserted_mention.recipient_id,
159 comment_id: inserted_mention.comment_id,
161 published: inserted_mention.published,
164 let read_mention = PersonMention::read(pool, inserted_mention.id)
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)
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)
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();
182 assert_eq!(expected_mention, read_mention);
183 assert_eq!(expected_mention, inserted_mention);
184 assert_eq!(expected_mention, updated_mention);