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 fedi_name: "rrf".into(),
68 preferred_username: None,
69 password_encrypted: "nope".into(),
77 theme: "darkly".into(),
78 default_sort_type: SortType::Hot as i16,
79 default_listing_type: ListingType::Subscribed as i16,
80 lang: "browser".into(),
82 send_notifications_to_email: false,
85 let inserted_user = User_::create(&conn, &new_user).unwrap();
87 let recipient_form = UserForm {
88 name: "terrylakes recipient".into(),
89 fedi_name: "rrf".into(),
90 preferred_username: None,
91 password_encrypted: "nope".into(),
99 theme: "darkly".into(),
100 default_sort_type: SortType::Hot as i16,
101 default_listing_type: ListingType::Subscribed as i16,
102 lang: "browser".into(),
104 send_notifications_to_email: false,
107 let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
109 let new_community = CommunityForm {
110 name: "test community lake".to_string(),
111 title: "nada".to_owned(),
114 creator_id: inserted_user.id,
121 let inserted_community = Community::create(&conn, &new_community).unwrap();
123 let new_post = PostForm {
124 name: "A test post".into(),
125 creator_id: inserted_user.id,
128 community_id: inserted_community.id,
137 let inserted_post = Post::create(&conn, &new_post).unwrap();
139 let comment_form = CommentForm {
140 content: "A test comment".into(),
141 creator_id: inserted_user.id,
142 post_id: inserted_post.id,
150 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
152 let user_mention_form = UserMentionForm {
153 recipient_id: inserted_recipient.id,
154 comment_id: inserted_comment.id,
158 let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
160 let expected_mention = UserMention {
161 id: inserted_mention.id,
162 recipient_id: inserted_mention.recipient_id,
163 comment_id: inserted_mention.comment_id,
165 published: inserted_mention.published,
168 let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
169 let updated_mention =
170 UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
171 let num_deleted = UserMention::delete(&conn, inserted_mention.id).unwrap();
172 Comment::delete(&conn, inserted_comment.id).unwrap();
173 Post::delete(&conn, inserted_post.id).unwrap();
174 Community::delete(&conn, inserted_community.id).unwrap();
175 User_::delete(&conn, inserted_user.id).unwrap();
176 User_::delete(&conn, inserted_recipient.id).unwrap();
178 assert_eq!(expected_mention, read_mention);
179 assert_eq!(expected_mention, inserted_mention);
180 assert_eq!(expected_mention, updated_mention);
181 assert_eq!(1, num_deleted);