2 newtypes::{CommentId, PersonId, PersonMentionId},
3 source::person_mention::*,
6 use diesel::{dsl::*, result::Error, *};
8 impl Crud for PersonMention {
9 type Form = PersonMentionForm;
10 type IdType = PersonMentionId;
11 fn read(conn: &mut PgConnection, person_mention_id: PersonMentionId) -> Result<Self, Error> {
12 use crate::schema::person_mention::dsl::*;
13 person_mention.find(person_mention_id).first::<Self>(conn)
17 conn: &mut PgConnection,
18 person_mention_form: &PersonMentionForm,
19 ) -> Result<Self, Error> {
20 use crate::schema::person_mention::dsl::*;
21 // since the return here isnt utilized, we dont need to do an update
22 // but get_result doesnt return the existing row here
23 insert_into(person_mention)
24 .values(person_mention_form)
25 .on_conflict((recipient_id, comment_id))
27 .set(person_mention_form)
28 .get_result::<Self>(conn)
32 conn: &mut PgConnection,
33 person_mention_id: PersonMentionId,
34 person_mention_form: &PersonMentionForm,
35 ) -> Result<Self, Error> {
36 use crate::schema::person_mention::dsl::*;
37 diesel::update(person_mention.find(person_mention_id))
38 .set(person_mention_form)
39 .get_result::<Self>(conn)
45 conn: &mut PgConnection,
46 person_mention_id: PersonMentionId,
48 ) -> Result<PersonMention, Error> {
49 use crate::schema::person_mention::dsl::*;
50 diesel::update(person_mention.find(person_mention_id))
51 .set(read.eq(new_read))
52 .get_result::<Self>(conn)
55 pub fn mark_all_as_read(
56 conn: &mut PgConnection,
57 for_recipient_id: PersonId,
58 ) -> Result<Vec<PersonMention>, Error> {
59 use crate::schema::person_mention::dsl::*;
62 .filter(recipient_id.eq(for_recipient_id))
63 .filter(read.eq(false)),
66 .get_results::<Self>(conn)
68 pub fn read_by_comment_and_person(
69 conn: &mut PgConnection,
70 for_comment_id: CommentId,
71 for_recipient_id: PersonId,
72 ) -> Result<Self, Error> {
73 use crate::schema::person_mention::dsl::*;
75 .filter(comment_id.eq(for_comment_id))
76 .filter(recipient_id.eq(for_recipient_id))
86 community::{Community, CommunityForm},
92 utils::establish_unpooled_connection,
94 use serial_test::serial;
99 let conn = &mut establish_unpooled_connection();
101 let new_person = PersonForm {
102 name: "terrylake".into(),
103 public_key: Some("pubkey".to_string()),
104 ..PersonForm::default()
107 let inserted_person = Person::create(conn, &new_person).unwrap();
109 let recipient_form = PersonForm {
110 name: "terrylakes recipient".into(),
111 public_key: Some("pubkey".to_string()),
112 ..PersonForm::default()
115 let inserted_recipient = Person::create(conn, &recipient_form).unwrap();
117 let new_community = CommunityForm {
118 name: "test community lake".to_string(),
119 title: "nada".to_owned(),
120 public_key: Some("pubkey".to_string()),
121 ..CommunityForm::default()
124 let inserted_community = Community::create(conn, &new_community).unwrap();
126 let new_post = PostForm {
127 name: "A test post".into(),
128 creator_id: inserted_person.id,
129 community_id: inserted_community.id,
130 ..PostForm::default()
133 let inserted_post = Post::create(conn, &new_post).unwrap();
135 let comment_form = CommentForm {
136 content: "A test comment".into(),
137 creator_id: inserted_person.id,
138 post_id: inserted_post.id,
139 ..CommentForm::default()
142 let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
144 let person_mention_form = PersonMentionForm {
145 recipient_id: inserted_recipient.id,
146 comment_id: inserted_comment.id,
150 let inserted_mention = PersonMention::create(conn, &person_mention_form).unwrap();
152 let expected_mention = PersonMention {
153 id: inserted_mention.id,
154 recipient_id: inserted_mention.recipient_id,
155 comment_id: inserted_mention.comment_id,
157 published: inserted_mention.published,
160 let read_mention = PersonMention::read(conn, inserted_mention.id).unwrap();
161 let updated_mention =
162 PersonMention::update(conn, inserted_mention.id, &person_mention_form).unwrap();
163 Comment::delete(conn, inserted_comment.id).unwrap();
164 Post::delete(conn, inserted_post.id).unwrap();
165 Community::delete(conn, inserted_community.id).unwrap();
166 Person::delete(conn, inserted_person.id).unwrap();
167 Person::delete(conn, inserted_recipient.id).unwrap();
169 assert_eq!(expected_mention, read_mention);
170 assert_eq!(expected_mention, inserted_mention);
171 assert_eq!(expected_mention, updated_mention);