1 use super::comment::Comment;
2 use crate::{schema::user_mention, Crud};
3 use diesel::{dsl::*, result::Error, *};
6 #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
8 #[table_name = "user_mention"]
9 pub struct UserMention {
11 pub recipient_id: i32,
14 pub published: chrono::NaiveDateTime,
17 #[derive(Insertable, AsChangeset)]
18 #[table_name = "user_mention"]
19 pub struct UserMentionForm {
20 pub recipient_id: i32,
22 pub read: Option<bool>,
25 impl Crud<UserMentionForm> for UserMention {
26 fn read(conn: &PgConnection, user_mention_id: i32) -> Result<Self, Error> {
27 use crate::schema::user_mention::dsl::*;
28 user_mention.find(user_mention_id).first::<Self>(conn)
31 fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
32 use crate::schema::user_mention::dsl::*;
33 // since the return here isnt utilized, we dont need to do an update
34 // but get_result doesnt return the existing row here
35 insert_into(user_mention)
36 .values(user_mention_form)
37 .on_conflict((recipient_id, comment_id))
39 .set(user_mention_form)
40 .get_result::<Self>(conn)
46 user_mention_form: &UserMentionForm,
47 ) -> Result<Self, Error> {
48 use crate::schema::user_mention::dsl::*;
49 diesel::update(user_mention.find(user_mention_id))
50 .set(user_mention_form)
51 .get_result::<Self>(conn)
60 ) -> Result<Self, Error> {
61 use crate::schema::user_mention::dsl::*;
62 diesel::update(user_mention.find(user_mention_id))
63 .set(read.eq(new_read))
64 .get_result::<Self>(conn)
67 pub fn mark_all_as_read(conn: &PgConnection, for_recipient_id: i32) -> Result<Vec<Self>, Error> {
68 use crate::schema::user_mention::dsl::*;
71 .filter(recipient_id.eq(for_recipient_id))
72 .filter(read.eq(false)),
75 .get_results::<Self>(conn)
82 source::{comment::*, community::*, post::*, user::*, user_mention::*},
83 tests::establish_unpooled_connection,
90 let conn = establish_unpooled_connection();
92 let new_user = UserForm {
93 name: "terrylake".into(),
94 preferred_username: None,
95 password_encrypted: "nope".into(),
105 theme: "browser".into(),
106 default_sort_type: SortType::Hot as i16,
107 default_listing_type: ListingType::Subscribed as i16,
108 lang: "browser".into(),
110 send_notifications_to_email: false,
116 last_refreshed_at: None,
119 let inserted_user = User_::create(&conn, &new_user).unwrap();
121 let recipient_form = UserForm {
122 name: "terrylakes recipient".into(),
123 preferred_username: None,
124 password_encrypted: "nope".into(),
126 matrix_user_id: None,
134 theme: "browser".into(),
135 default_sort_type: SortType::Hot as i16,
136 default_listing_type: ListingType::Subscribed as i16,
137 lang: "browser".into(),
139 send_notifications_to_email: false,
145 last_refreshed_at: None,
148 let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
150 let new_community = CommunityForm {
151 name: "test community lake".to_string(),
152 title: "nada".to_owned(),
155 creator_id: inserted_user.id,
164 last_refreshed_at: None,
170 let inserted_community = Community::create(&conn, &new_community).unwrap();
172 let new_post = PostForm {
173 name: "A test post".into(),
174 creator_id: inserted_user.id,
177 community_id: inserted_community.id,
185 embed_description: None,
193 let inserted_post = Post::create(&conn, &new_post).unwrap();
195 let comment_form = CommentForm {
196 content: "A test comment".into(),
197 creator_id: inserted_user.id,
198 post_id: inserted_post.id,
209 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
211 let user_mention_form = UserMentionForm {
212 recipient_id: inserted_recipient.id,
213 comment_id: inserted_comment.id,
217 let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
219 let expected_mention = UserMention {
220 id: inserted_mention.id,
221 recipient_id: inserted_mention.recipient_id,
222 comment_id: inserted_mention.comment_id,
224 published: inserted_mention.published,
227 let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
228 let updated_mention =
229 UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
230 Comment::delete(&conn, inserted_comment.id).unwrap();
231 Post::delete(&conn, inserted_post.id).unwrap();
232 Community::delete(&conn, inserted_community.id).unwrap();
233 User_::delete(&conn, inserted_user.id).unwrap();
234 User_::delete(&conn, inserted_recipient.id).unwrap();
236 assert_eq!(expected_mention, read_mention);
237 assert_eq!(expected_mention, inserted_mention);
238 assert_eq!(expected_mention, updated_mention);