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,
107 theme: "darkly".into(),
108 default_sort_type: SortType::Hot as i16,
109 default_listing_type: ListingType::Subscribed as i16,
110 lang: "browser".into(),
112 send_notifications_to_email: false,
113 actor_id: "changeme_628763".into(),
118 last_refreshed_at: None,
121 let inserted_user = User_::create(&conn, &new_user).unwrap();
123 let recipient_form = UserForm {
124 name: "terrylakes recipient".into(),
125 preferred_username: None,
126 password_encrypted: "nope".into(),
128 matrix_user_id: None,
134 theme: "darkly".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,
140 actor_id: "changeme_927389278".into(),
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,
160 actor_id: "changeme_876238".into(),
164 last_refreshed_at: None,
168 let inserted_community = Community::create(&conn, &new_community).unwrap();
170 let new_post = PostForm {
171 name: "A test post".into(),
172 creator_id: inserted_user.id,
175 community_id: inserted_community.id,
183 embed_description: None,
186 ap_id: "http://fake.com".into(),
191 let inserted_post = Post::create(&conn, &new_post).unwrap();
193 let comment_form = CommentForm {
194 content: "A test comment".into(),
195 creator_id: inserted_user.id,
196 post_id: inserted_post.id,
203 ap_id: "http://fake.com".into(),
207 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
209 let user_mention_form = UserMentionForm {
210 recipient_id: inserted_recipient.id,
211 comment_id: inserted_comment.id,
215 let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
217 let expected_mention = UserMention {
218 id: inserted_mention.id,
219 recipient_id: inserted_mention.recipient_id,
220 comment_id: inserted_mention.comment_id,
222 published: inserted_mention.published,
225 let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
226 let updated_mention =
227 UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
228 let num_deleted = UserMention::delete(&conn, inserted_mention.id).unwrap();
229 Comment::delete(&conn, inserted_comment.id).unwrap();
230 Post::delete(&conn, inserted_post.id).unwrap();
231 Community::delete(&conn, inserted_community.id).unwrap();
232 User_::delete(&conn, inserted_user.id).unwrap();
233 User_::delete(&conn, inserted_recipient.id).unwrap();
235 assert_eq!(expected_mention, read_mention);
236 assert_eq!(expected_mention, inserted_mention);
237 assert_eq!(expected_mention, updated_mention);
238 assert_eq!(1, num_deleted);