2 newtypes::{CommentId, PersonId, PersonMentionId},
3 schema::person_mention::dsl::*,
4 source::person_mention::*,
6 utils::{get_conn, DbPool},
8 use diesel::{dsl::*, result::Error, *};
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: &DbPool, person_mention_id: PersonMentionId) -> Result<Self, Error> {
17 let conn = &mut get_conn(pool).await?;
19 .find(person_mention_id)
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))
32 .set(person_mention_form)
33 .get_result::<Self>(conn)
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)
51 pub async fn mark_all_as_read(
53 for_recipient_id: PersonId,
54 ) -> Result<Vec<PersonMention>, Error> {
55 let conn = &mut get_conn(pool).await?;
58 .filter(recipient_id.eq(for_recipient_id))
59 .filter(read.eq(false)),
62 .get_results::<Self>(conn)
66 pub async fn read_by_comment_and_person(
68 for_comment_id: CommentId,
69 for_recipient_id: PersonId,
70 ) -> Result<Self, Error> {
71 let conn = &mut get_conn(pool).await?;
73 .filter(comment_id.eq(for_comment_id))
74 .filter(recipient_id.eq(for_recipient_id))
85 community::{Community, CommunityInsertForm},
92 utils::build_db_pool_for_tests,
94 use serial_test::serial;
98 async fn test_crud() {
99 let pool = &build_db_pool_for_tests().await;
101 let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
103 let new_person = PersonInsertForm::builder()
104 .name("terrylake".into())
105 .public_key("pubkey".to_string())
106 .instance_id(inserted_instance.id)
109 let inserted_person = Person::create(pool, &new_person).await.unwrap();
111 let recipient_form = PersonInsertForm::builder()
112 .name("terrylakes recipient".into())
113 .public_key("pubkey".to_string())
114 .instance_id(inserted_instance.id)
117 let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
119 let new_community = CommunityInsertForm::builder()
120 .name("test community lake".to_string())
121 .title("nada".to_owned())
122 .public_key("pubkey".to_string())
123 .instance_id(inserted_instance.id)
126 let inserted_community = Community::create(pool, &new_community).await.unwrap();
128 let new_post = PostInsertForm::builder()
129 .name("A test post".into())
130 .creator_id(inserted_person.id)
131 .community_id(inserted_community.id)
134 let inserted_post = Post::create(pool, &new_post).await.unwrap();
136 let comment_form = CommentInsertForm::builder()
137 .content("A test comment".into())
138 .creator_id(inserted_person.id)
139 .post_id(inserted_post.id)
142 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
144 let person_mention_form = PersonMentionInsertForm {
145 recipient_id: inserted_recipient.id,
146 comment_id: inserted_comment.id,
150 let inserted_mention = PersonMention::create(pool, &person_mention_form)
154 let expected_mention = PersonMention {
155 id: inserted_mention.id,
156 recipient_id: inserted_mention.recipient_id,
157 comment_id: inserted_mention.comment_id,
159 published: inserted_mention.published,
162 let read_mention = PersonMention::read(pool, inserted_mention.id)
166 let person_mention_update_form = PersonMentionUpdateForm { read: Some(false) };
167 let updated_mention =
168 PersonMention::update(pool, inserted_mention.id, &person_mention_update_form)
171 Comment::delete(pool, inserted_comment.id).await.unwrap();
172 Post::delete(pool, inserted_post.id).await.unwrap();
173 Community::delete(pool, inserted_community.id)
176 Person::delete(pool, inserted_person.id).await.unwrap();
177 Person::delete(pool, inserted_recipient.id).await.unwrap();
178 Instance::delete(pool, inserted_instance.id).await.unwrap();
180 assert_eq!(expected_mention, read_mention);
181 assert_eq!(expected_mention, inserted_mention);
182 assert_eq!(expected_mention, updated_mention);