2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{source::person_mention::*, PersonId, PersonMentionId};
5 impl Crud<PersonMentionForm, PersonMentionId> for PersonMention {
6 fn read(conn: &PgConnection, person_mention_id: PersonMentionId) -> Result<Self, Error> {
7 use lemmy_db_schema::schema::person_mention::dsl::*;
8 person_mention.find(person_mention_id).first::<Self>(conn)
11 fn create(conn: &PgConnection, person_mention_form: &PersonMentionForm) -> Result<Self, Error> {
12 use lemmy_db_schema::schema::person_mention::dsl::*;
13 // since the return here isnt utilized, we dont need to do an update
14 // but get_result doesnt return the existing row here
15 insert_into(person_mention)
16 .values(person_mention_form)
17 .on_conflict((recipient_id, comment_id))
19 .set(person_mention_form)
20 .get_result::<Self>(conn)
25 person_mention_id: PersonMentionId,
26 person_mention_form: &PersonMentionForm,
27 ) -> Result<Self, Error> {
28 use lemmy_db_schema::schema::person_mention::dsl::*;
29 diesel::update(person_mention.find(person_mention_id))
30 .set(person_mention_form)
31 .get_result::<Self>(conn)
35 pub trait PersonMention_ {
38 person_mention_id: PersonMentionId,
40 ) -> Result<PersonMention, Error>;
43 for_recipient_id: PersonId,
44 ) -> Result<Vec<PersonMention>, Error>;
47 impl PersonMention_ for PersonMention {
50 person_mention_id: PersonMentionId,
52 ) -> Result<PersonMention, Error> {
53 use lemmy_db_schema::schema::person_mention::dsl::*;
54 diesel::update(person_mention.find(person_mention_id))
55 .set(read.eq(new_read))
56 .get_result::<Self>(conn)
61 for_recipient_id: PersonId,
62 ) -> Result<Vec<PersonMention>, Error> {
63 use lemmy_db_schema::schema::person_mention::dsl::*;
66 .filter(recipient_id.eq(for_recipient_id))
67 .filter(read.eq(false)),
70 .get_results::<Self>(conn)
76 use crate::{establish_unpooled_connection, Crud};
77 use lemmy_db_schema::source::{
79 community::{Community, CommunityForm},
84 use serial_test::serial;
89 let conn = establish_unpooled_connection();
91 let new_person = PersonForm {
92 name: "terrylake".into(),
93 preferred_username: None,
105 last_refreshed_at: None,
107 shared_inbox_url: None,
108 matrix_user_id: None,
111 let inserted_person = Person::create(&conn, &new_person).unwrap();
113 let recipient_form = PersonForm {
114 name: "terrylakes recipient".into(),
115 preferred_username: None,
127 last_refreshed_at: None,
129 shared_inbox_url: None,
130 matrix_user_id: None,
133 let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
135 let new_community = CommunityForm {
136 name: "test community lake".to_string(),
137 title: "nada".to_owned(),
139 creator_id: inserted_person.id,
148 last_refreshed_at: None,
154 shared_inbox_url: None,
157 let inserted_community = Community::create(&conn, &new_community).unwrap();
159 let new_post = PostForm {
160 name: "A test post".into(),
161 creator_id: inserted_person.id,
164 community_id: inserted_community.id,
172 embed_description: None,
180 let inserted_post = Post::create(&conn, &new_post).unwrap();
182 let comment_form = CommentForm {
183 content: "A test comment".into(),
184 creator_id: inserted_person.id,
185 post_id: inserted_post.id,
196 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
198 let person_mention_form = PersonMentionForm {
199 recipient_id: inserted_recipient.id,
200 comment_id: inserted_comment.id,
204 let inserted_mention = PersonMention::create(&conn, &person_mention_form).unwrap();
206 let expected_mention = PersonMention {
207 id: inserted_mention.id,
208 recipient_id: inserted_mention.recipient_id,
209 comment_id: inserted_mention.comment_id,
211 published: inserted_mention.published,
214 let read_mention = PersonMention::read(&conn, inserted_mention.id).unwrap();
215 let updated_mention =
216 PersonMention::update(&conn, inserted_mention.id, &person_mention_form).unwrap();
217 Comment::delete(&conn, inserted_comment.id).unwrap();
218 Post::delete(&conn, inserted_post.id).unwrap();
219 Community::delete(&conn, inserted_community.id).unwrap();
220 Person::delete(&conn, inserted_person.id).unwrap();
221 Person::delete(&conn, inserted_recipient.id).unwrap();
223 assert_eq!(expected_mention, read_mention);
224 assert_eq!(expected_mention, inserted_mention);
225 assert_eq!(expected_mention, updated_mention);