1 use super::comment::Comment;
2 use crate::{schema::user_mention, Crud};
3 use diesel::{dsl::*, result::Error, *};
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug)]
7 #[table_name = "user_mention"]
8 pub struct UserMention {
10 pub recipient_id: i32,
13 pub published: chrono::NaiveDateTime,
16 #[derive(Insertable, AsChangeset)]
17 #[table_name = "user_mention"]
18 pub struct UserMentionForm {
19 pub recipient_id: i32,
21 pub read: Option<bool>,
24 impl Crud<UserMentionForm> for UserMention {
25 fn read(conn: &PgConnection, user_mention_id: i32) -> Result<Self, Error> {
26 use crate::schema::user_mention::dsl::*;
27 user_mention.find(user_mention_id).first::<Self>(conn)
30 fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
31 use crate::schema::user_mention::dsl::*;
32 insert_into(user_mention)
33 .values(user_mention_form)
34 .get_result::<Self>(conn)
40 user_mention_form: &UserMentionForm,
41 ) -> Result<Self, Error> {
42 use crate::schema::user_mention::dsl::*;
43 diesel::update(user_mention.find(user_mention_id))
44 .set(user_mention_form)
45 .get_result::<Self>(conn)
54 ) -> Result<Self, Error> {
55 use crate::schema::user_mention::dsl::*;
56 diesel::update(user_mention.find(user_mention_id))
57 .set(read.eq(new_read))
58 .get_result::<Self>(conn)
61 pub fn mark_all_as_read(conn: &PgConnection, for_recipient_id: i32) -> Result<Vec<Self>, Error> {
62 use crate::schema::user_mention::dsl::*;
65 .filter(recipient_id.eq(for_recipient_id))
66 .filter(read.eq(false)),
69 .get_results::<Self>(conn)
79 tests::establish_unpooled_connection,
88 let conn = establish_unpooled_connection();
90 let new_user = UserForm {
91 name: "terrylake".into(),
92 preferred_username: None,
93 password_encrypted: "nope".into(),
102 theme: "darkly".into(),
103 default_sort_type: SortType::Hot as i16,
104 default_listing_type: ListingType::Subscribed as i16,
105 lang: "browser".into(),
107 send_notifications_to_email: false,
113 last_refreshed_at: None,
116 let inserted_user = User_::create(&conn, &new_user).unwrap();
118 let recipient_form = UserForm {
119 name: "terrylakes recipient".into(),
120 preferred_username: None,
121 password_encrypted: "nope".into(),
123 matrix_user_id: None,
130 theme: "darkly".into(),
131 default_sort_type: SortType::Hot as i16,
132 default_listing_type: ListingType::Subscribed as i16,
133 lang: "browser".into(),
135 send_notifications_to_email: false,
141 last_refreshed_at: None,
144 let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
146 let new_community = CommunityForm {
147 name: "test community lake".to_string(),
148 title: "nada".to_owned(),
151 creator_id: inserted_user.id,
160 last_refreshed_at: None,
166 let inserted_community = Community::create(&conn, &new_community).unwrap();
168 let new_post = PostForm {
169 name: "A test post".into(),
170 creator_id: inserted_user.id,
173 community_id: inserted_community.id,
181 embed_description: None,
189 let inserted_post = Post::create(&conn, &new_post).unwrap();
191 let comment_form = CommentForm {
192 content: "A test comment".into(),
193 creator_id: inserted_user.id,
194 post_id: inserted_post.id,
205 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
207 let user_mention_form = UserMentionForm {
208 recipient_id: inserted_recipient.id,
209 comment_id: inserted_comment.id,
213 let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
215 let expected_mention = UserMention {
216 id: inserted_mention.id,
217 recipient_id: inserted_mention.recipient_id,
218 comment_id: inserted_mention.comment_id,
220 published: inserted_mention.published,
223 let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
224 let updated_mention =
225 UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
226 Comment::delete(&conn, inserted_comment.id).unwrap();
227 Post::delete(&conn, inserted_post.id).unwrap();
228 Community::delete(&conn, inserted_community.id).unwrap();
229 User_::delete(&conn, inserted_user.id).unwrap();
230 User_::delete(&conn, inserted_recipient.id).unwrap();
232 assert_eq!(expected_mention, read_mention);
233 assert_eq!(expected_mention, inserted_mention);
234 assert_eq!(expected_mention, updated_mention);