2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::source::person_mention::*;
5 impl Crud<PersonMentionForm> for PersonMention {
6 fn read(conn: &PgConnection, person_mention_id: i32) -> 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: i32,
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: i32,
40 ) -> Result<PersonMention, Error>;
43 for_recipient_id: i32,
44 ) -> Result<Vec<PersonMention>, Error>;
47 impl PersonMention_ for PersonMention {
50 person_mention_id: i32,
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: i32,
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, ListingType, SortType};
77 use lemmy_db_schema::source::{
79 community::{Community, CommunityForm},
87 let conn = establish_unpooled_connection();
89 let new_person = PersonForm {
90 name: "terrylake".into(),
91 preferred_username: None,
103 last_refreshed_at: None,
105 shared_inbox_url: None,
108 let inserted_person = Person::create(&conn, &new_person).unwrap();
110 let recipient_form = PersonForm {
111 name: "terrylakes recipient".into(),
112 preferred_username: None,
124 last_refreshed_at: None,
126 shared_inbox_url: None,
129 let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
131 let new_community = CommunityForm {
132 name: "test community lake".to_string(),
133 title: "nada".to_owned(),
135 creator_id: inserted_person.id,
144 last_refreshed_at: None,
150 shared_inbox_url: None,
153 let inserted_community = Community::create(&conn, &new_community).unwrap();
155 let new_post = PostForm {
156 name: "A test post".into(),
157 creator_id: inserted_person.id,
160 community_id: inserted_community.id,
168 embed_description: None,
176 let inserted_post = Post::create(&conn, &new_post).unwrap();
178 let comment_form = CommentForm {
179 content: "A test comment".into(),
180 creator_id: inserted_person.id,
181 post_id: inserted_post.id,
192 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
194 let person_mention_form = PersonMentionForm {
195 recipient_id: inserted_recipient.id,
196 comment_id: inserted_comment.id,
200 let inserted_mention = PersonMention::create(&conn, &person_mention_form).unwrap();
202 let expected_mention = PersonMention {
203 id: inserted_mention.id,
204 recipient_id: inserted_mention.recipient_id,
205 comment_id: inserted_mention.comment_id,
207 published: inserted_mention.published,
210 let read_mention = PersonMention::read(&conn, inserted_mention.id).unwrap();
211 let updated_mention =
212 PersonMention::update(&conn, inserted_mention.id, &person_mention_form).unwrap();
213 Comment::delete(&conn, inserted_comment.id).unwrap();
214 Post::delete(&conn, inserted_post.id).unwrap();
215 Community::delete(&conn, inserted_community.id).unwrap();
216 Person::delete(&conn, inserted_person.id).unwrap();
217 Person::delete(&conn, inserted_recipient.id).unwrap();
219 assert_eq!(expected_mention, read_mention);
220 assert_eq!(expected_mention, inserted_mention);
221 assert_eq!(expected_mention, updated_mention);