]> Untitled Git - lemmy.git/blob - server/src/db/user_mention.rs
Merge remote-tracking branch 'upstream/master'
[lemmy.git] / server / src / db / user_mention.rs
1 use super::comment::Comment;
2 use super::*;
3 use crate::schema::user_mention;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
6 #[belongs_to(Comment)]
7 #[table_name = "user_mention"]
8 pub struct UserMention {
9   pub id: i32,
10   pub recipient_id: i32,
11   pub comment_id: i32,
12   pub read: bool,
13   pub published: chrono::NaiveDateTime,
14 }
15
16 #[derive(Insertable, AsChangeset, Clone)]
17 #[table_name = "user_mention"]
18 pub struct UserMentionForm {
19   pub recipient_id: i32,
20   pub comment_id: i32,
21   pub read: Option<bool>,
22 }
23
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)
28   }
29
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)
33   }
34
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)
40   }
41
42   fn update(
43     conn: &PgConnection,
44     user_mention_id: i32,
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)
51   }
52 }
53
54 #[cfg(test)]
55 mod tests {
56   use super::super::comment::*;
57   use super::super::community::*;
58   use super::super::post::*;
59   use super::super::user::*;
60   use super::*;
61   #[test]
62   fn test_crud() {
63     let conn = establish_unpooled_connection();
64
65     let new_user = UserForm {
66       name: "terrylake".into(),
67       fedi_name: "rrf".into(),
68       preferred_username: None,
69       password_encrypted: "nope".into(),
70       email: None,
71       matrix_user_id: None,
72       avatar: None,
73       admin: false,
74       banned: false,
75       updated: None,
76       show_nsfw: false,
77       theme: "darkly".into(),
78       default_sort_type: SortType::Hot as i16,
79       default_listing_type: ListingType::Subscribed as i16,
80       lang: "browser".into(),
81       show_avatars: true,
82       send_notifications_to_email: false,
83     };
84
85     let inserted_user = User_::create(&conn, &new_user).unwrap();
86
87     let recipient_form = UserForm {
88       name: "terrylakes recipient".into(),
89       fedi_name: "rrf".into(),
90       preferred_username: None,
91       password_encrypted: "nope".into(),
92       email: None,
93       matrix_user_id: None,
94       avatar: None,
95       admin: false,
96       banned: false,
97       updated: None,
98       show_nsfw: false,
99       theme: "darkly".into(),
100       default_sort_type: SortType::Hot as i16,
101       default_listing_type: ListingType::Subscribed as i16,
102       lang: "browser".into(),
103       show_avatars: true,
104       send_notifications_to_email: false,
105     };
106
107     let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
108
109     let new_community = CommunityForm {
110       name: "test community lake".to_string(),
111       title: "nada".to_owned(),
112       description: None,
113       category_id: 1,
114       creator_id: inserted_user.id,
115       removed: None,
116       deleted: None,
117       updated: None,
118       nsfw: false,
119     };
120
121     let inserted_community = Community::create(&conn, &new_community).unwrap();
122
123     let new_post = PostForm {
124       name: "A test post".into(),
125       creator_id: inserted_user.id,
126       url: None,
127       body: None,
128       community_id: inserted_community.id,
129       removed: None,
130       deleted: None,
131       locked: None,
132       stickied: None,
133       updated: None,
134       nsfw: false,
135     };
136
137     let inserted_post = Post::create(&conn, &new_post).unwrap();
138
139     let comment_form = CommentForm {
140       content: "A test comment".into(),
141       creator_id: inserted_user.id,
142       post_id: inserted_post.id,
143       removed: None,
144       deleted: None,
145       read: None,
146       parent_id: None,
147       updated: None,
148     };
149
150     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
151
152     let user_mention_form = UserMentionForm {
153       recipient_id: inserted_recipient.id,
154       comment_id: inserted_comment.id,
155       read: None,
156     };
157
158     let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
159
160     let expected_mention = UserMention {
161       id: inserted_mention.id,
162       recipient_id: inserted_mention.recipient_id,
163       comment_id: inserted_mention.comment_id,
164       read: false,
165       published: inserted_mention.published,
166     };
167
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();
177
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);
182   }
183 }