1 use super::comment::Comment;
3 use crate::schema::user_mention;
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
7 #[table_name = "user_mention"]
8 pub struct UserMention {
10 pub recipient_id: i32,
13 pub published: chrono::NaiveDateTime,
16 #[derive(Insertable, AsChangeset, Clone)]
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 delete(conn: &PgConnection, user_mention_id: i32) -> Result<usize, Error> {
31 use crate::schema::user_mention::dsl::*;
32 diesel::delete(user_mention.find(user_mention_id)).execute(conn)
35 fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
36 use crate::schema::user_mention::dsl::*;
37 insert_into(user_mention)
38 .values(user_mention_form)
39 .get_result::<Self>(conn)
45 user_mention_form: &UserMentionForm,
46 ) -> Result<Self, Error> {
47 use crate::schema::user_mention::dsl::*;
48 diesel::update(user_mention.find(user_mention_id))
49 .set(user_mention_form)
50 .get_result::<Self>(conn)
56 use super::super::comment::*;
57 use super::super::community::*;
58 use super::super::post::*;
59 use super::super::user::*;
63 let conn = establish_unpooled_connection();
65 let new_user = UserForm {
66 name: "terrylake".into(),
67 preferred_username: None,
68 password_encrypted: "nope".into(),
76 theme: "darkly".into(),
77 default_sort_type: SortType::Hot as i16,
78 default_listing_type: ListingType::Subscribed as i16,
79 lang: "browser".into(),
81 send_notifications_to_email: false,
82 actor_id: "changeme".into(),
87 last_refreshed_at: None,
90 let inserted_user = User_::create(&conn, &new_user).unwrap();
92 let recipient_form = UserForm {
93 name: "terrylakes recipient".into(),
94 preferred_username: None,
95 password_encrypted: "nope".into(),
103 theme: "darkly".into(),
104 default_sort_type: SortType::Hot as i16,
105 default_listing_type: ListingType::Subscribed as i16,
106 lang: "browser".into(),
108 send_notifications_to_email: false,
109 actor_id: "changeme".into(),
114 last_refreshed_at: None,
117 let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
119 let new_community = CommunityForm {
120 name: "test community lake".to_string(),
121 title: "nada".to_owned(),
124 creator_id: inserted_user.id,
129 actor_id: "changeme".into(),
133 last_refreshed_at: None,
137 let inserted_community = Community::create(&conn, &new_community).unwrap();
139 let new_post = PostForm {
140 name: "A test post".into(),
141 creator_id: inserted_user.id,
144 community_id: inserted_community.id,
152 embed_description: None,
155 ap_id: "changeme".into(),
160 let inserted_post = Post::create(&conn, &new_post).unwrap();
162 let comment_form = CommentForm {
163 content: "A test comment".into(),
164 creator_id: inserted_user.id,
165 post_id: inserted_post.id,
171 ap_id: "changeme".into(),
175 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
177 let user_mention_form = UserMentionForm {
178 recipient_id: inserted_recipient.id,
179 comment_id: inserted_comment.id,
183 let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
185 let expected_mention = UserMention {
186 id: inserted_mention.id,
187 recipient_id: inserted_mention.recipient_id,
188 comment_id: inserted_mention.comment_id,
190 published: inserted_mention.published,
193 let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
194 let updated_mention =
195 UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
196 let num_deleted = UserMention::delete(&conn, inserted_mention.id).unwrap();
197 Comment::delete(&conn, inserted_comment.id).unwrap();
198 Post::delete(&conn, inserted_post.id).unwrap();
199 Community::delete(&conn, inserted_community.id).unwrap();
200 User_::delete(&conn, inserted_user.id).unwrap();
201 User_::delete(&conn, inserted_recipient.id).unwrap();
203 assert_eq!(expected_mention, read_mention);
204 assert_eq!(expected_mention, inserted_mention);
205 assert_eq!(expected_mention, updated_mention);
206 assert_eq!(1, num_deleted);