1 use super::comment::Comment;
2 use crate::{schema::user_mention, Crud};
3 use diesel::{dsl::*, result::Error, *};
4 use serde::{Deserialize, Serialize};
6 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
8 #[table_name = "user_mention"]
9 pub struct UserMention {
11 pub recipient_id: i32,
14 pub published: chrono::NaiveDateTime,
17 #[derive(Insertable, AsChangeset, Clone)]
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 delete(conn: &PgConnection, user_mention_id: i32) -> Result<usize, Error> {
32 use crate::schema::user_mention::dsl::*;
33 diesel::delete(user_mention.find(user_mention_id)).execute(conn)
36 fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
37 use crate::schema::user_mention::dsl::*;
38 insert_into(user_mention)
39 .values(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)
85 tests::establish_unpooled_connection,
94 let conn = establish_unpooled_connection();
96 let new_user = UserForm {
97 name: "terrylake".into(),
98 preferred_username: None,
99 password_encrypted: "nope".into(),
101 matrix_user_id: None,
108 theme: "darkly".into(),
109 default_sort_type: SortType::Hot as i16,
110 default_listing_type: ListingType::Subscribed as i16,
111 lang: "browser".into(),
113 send_notifications_to_email: false,
114 actor_id: "changeme_628763".into(),
119 last_refreshed_at: None,
122 let inserted_user = User_::create(&conn, &new_user).unwrap();
124 let recipient_form = UserForm {
125 name: "terrylakes recipient".into(),
126 preferred_username: None,
127 password_encrypted: "nope".into(),
129 matrix_user_id: None,
136 theme: "darkly".into(),
137 default_sort_type: SortType::Hot as i16,
138 default_listing_type: ListingType::Subscribed as i16,
139 lang: "browser".into(),
141 send_notifications_to_email: false,
142 actor_id: "changeme_927389278".into(),
147 last_refreshed_at: None,
150 let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
152 let new_community = CommunityForm {
153 name: "test community lake".to_string(),
154 title: "nada".to_owned(),
157 creator_id: inserted_user.id,
162 actor_id: "changeme_876238".into(),
166 last_refreshed_at: None,
172 let inserted_community = Community::create(&conn, &new_community).unwrap();
174 let new_post = PostForm {
175 name: "A test post".into(),
176 creator_id: inserted_user.id,
179 community_id: inserted_community.id,
187 embed_description: None,
190 ap_id: "http://fake.com".into(),
195 let inserted_post = Post::create(&conn, &new_post).unwrap();
197 let comment_form = CommentForm {
198 content: "A test comment".into(),
199 creator_id: inserted_user.id,
200 post_id: inserted_post.id,
207 ap_id: "http://fake.com".into(),
211 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
213 let user_mention_form = UserMentionForm {
214 recipient_id: inserted_recipient.id,
215 comment_id: inserted_comment.id,
219 let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
221 let expected_mention = UserMention {
222 id: inserted_mention.id,
223 recipient_id: inserted_mention.recipient_id,
224 comment_id: inserted_mention.comment_id,
226 published: inserted_mention.published,
229 let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
230 let updated_mention =
231 UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
232 let num_deleted = UserMention::delete(&conn, inserted_mention.id).unwrap();
233 Comment::delete(&conn, inserted_comment.id).unwrap();
234 Post::delete(&conn, inserted_post.id).unwrap();
235 Community::delete(&conn, inserted_community.id).unwrap();
236 User_::delete(&conn, inserted_user.id).unwrap();
237 User_::delete(&conn, inserted_recipient.id).unwrap();
239 assert_eq!(expected_mention, read_mention);
240 assert_eq!(expected_mention, inserted_mention);
241 assert_eq!(expected_mention, updated_mention);
242 assert_eq!(1, num_deleted);