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))
87 comment::{Comment, CommentInsertForm},
88 community::{Community, CommunityInsertForm},
90 person::{Person, PersonInsertForm},
91 person_mention::{PersonMention, PersonMentionInsertForm, PersonMentionUpdateForm},
92 post::{Post, PostInsertForm},
95 utils::build_db_pool_for_tests,
97 use serial_test::serial;
101 async fn test_crud() {
102 let pool = &build_db_pool_for_tests().await;
103 let pool = &mut pool.into();
105 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
109 let new_person = PersonInsertForm::builder()
110 .name("terrylake".into())
111 .public_key("pubkey".to_string())
112 .instance_id(inserted_instance.id)
115 let inserted_person = Person::create(pool, &new_person).await.unwrap();
117 let recipient_form = PersonInsertForm::builder()
118 .name("terrylakes recipient".into())
119 .public_key("pubkey".to_string())
120 .instance_id(inserted_instance.id)
123 let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
125 let new_community = CommunityInsertForm::builder()
126 .name("test community lake".to_string())
127 .title("nada".to_owned())
128 .public_key("pubkey".to_string())
129 .instance_id(inserted_instance.id)
132 let inserted_community = Community::create(pool, &new_community).await.unwrap();
134 let new_post = PostInsertForm::builder()
135 .name("A test post".into())
136 .creator_id(inserted_person.id)
137 .community_id(inserted_community.id)
140 let inserted_post = Post::create(pool, &new_post).await.unwrap();
142 let comment_form = CommentInsertForm::builder()
143 .content("A test comment".into())
144 .creator_id(inserted_person.id)
145 .post_id(inserted_post.id)
148 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
150 let person_mention_form = PersonMentionInsertForm {
151 recipient_id: inserted_recipient.id,
152 comment_id: inserted_comment.id,
156 let inserted_mention = PersonMention::create(pool, &person_mention_form)
160 let expected_mention = PersonMention {
161 id: inserted_mention.id,
162 recipient_id: inserted_mention.recipient_id,
163 comment_id: inserted_mention.comment_id,
165 published: inserted_mention.published,
168 let read_mention = PersonMention::read(pool, inserted_mention.id)
172 let person_mention_update_form = PersonMentionUpdateForm { read: Some(false) };
173 let updated_mention =
174 PersonMention::update(pool, inserted_mention.id, &person_mention_update_form)
177 Comment::delete(pool, inserted_comment.id).await.unwrap();
178 Post::delete(pool, inserted_post.id).await.unwrap();
179 Community::delete(pool, inserted_community.id)
182 Person::delete(pool, inserted_person.id).await.unwrap();
183 Person::delete(pool, inserted_recipient.id).await.unwrap();
184 Instance::delete(pool, inserted_instance.id).await.unwrap();
186 assert_eq!(expected_mention, read_mention);
187 assert_eq!(expected_mention, inserted_mention);
188 assert_eq!(expected_mention, updated_mention);