]> Untitled Git - lemmy.git/blob - server/lemmy_db/src/user_mention.rs
routes.api: fix get_captcha endpoint (#1135)
[lemmy.git] / server / lemmy_db / src / user_mention.rs
1 use super::comment::Comment;
2 use crate::{schema::user_mention, Crud};
3 use diesel::{dsl::*, result::Error, *};
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug)]
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)]
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 create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
31     use crate::schema::user_mention::dsl::*;
32     insert_into(user_mention)
33       .values(user_mention_form)
34       .get_result::<Self>(conn)
35   }
36
37   fn update(
38     conn: &PgConnection,
39     user_mention_id: i32,
40     user_mention_form: &UserMentionForm,
41   ) -> Result<Self, Error> {
42     use crate::schema::user_mention::dsl::*;
43     diesel::update(user_mention.find(user_mention_id))
44       .set(user_mention_form)
45       .get_result::<Self>(conn)
46   }
47 }
48
49 impl UserMention {
50   pub fn update_read(
51     conn: &PgConnection,
52     user_mention_id: i32,
53     new_read: bool,
54   ) -> Result<Self, Error> {
55     use crate::schema::user_mention::dsl::*;
56     diesel::update(user_mention.find(user_mention_id))
57       .set(read.eq(new_read))
58       .get_result::<Self>(conn)
59   }
60
61   pub fn mark_all_as_read(conn: &PgConnection, for_recipient_id: i32) -> Result<Vec<Self>, Error> {
62     use crate::schema::user_mention::dsl::*;
63     diesel::update(
64       user_mention
65         .filter(recipient_id.eq(for_recipient_id))
66         .filter(read.eq(false)),
67     )
68     .set(read.eq(true))
69     .get_results::<Self>(conn)
70   }
71 }
72
73 #[cfg(test)]
74 mod tests {
75   use crate::{
76     comment::*,
77     community::*,
78     post::*,
79     tests::establish_unpooled_connection,
80     user::*,
81     user_mention::*,
82     ListingType,
83     SortType,
84   };
85
86   #[test]
87   fn test_crud() {
88     let conn = establish_unpooled_connection();
89
90     let new_user = UserForm {
91       name: "terrylake".into(),
92       preferred_username: None,
93       password_encrypted: "nope".into(),
94       email: None,
95       matrix_user_id: None,
96       avatar: None,
97       banner: None,
98       admin: false,
99       banned: false,
100       updated: None,
101       show_nsfw: false,
102       theme: "darkly".into(),
103       default_sort_type: SortType::Hot as i16,
104       default_listing_type: ListingType::Subscribed as i16,
105       lang: "browser".into(),
106       show_avatars: true,
107       send_notifications_to_email: false,
108       actor_id: None,
109       bio: None,
110       local: true,
111       private_key: None,
112       public_key: None,
113       last_refreshed_at: None,
114     };
115
116     let inserted_user = User_::create(&conn, &new_user).unwrap();
117
118     let recipient_form = UserForm {
119       name: "terrylakes recipient".into(),
120       preferred_username: None,
121       password_encrypted: "nope".into(),
122       email: None,
123       matrix_user_id: None,
124       avatar: None,
125       banner: None,
126       admin: false,
127       banned: false,
128       updated: None,
129       show_nsfw: false,
130       theme: "darkly".into(),
131       default_sort_type: SortType::Hot as i16,
132       default_listing_type: ListingType::Subscribed as i16,
133       lang: "browser".into(),
134       show_avatars: true,
135       send_notifications_to_email: false,
136       actor_id: None,
137       bio: None,
138       local: true,
139       private_key: None,
140       public_key: None,
141       last_refreshed_at: None,
142     };
143
144     let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
145
146     let new_community = CommunityForm {
147       name: "test community lake".to_string(),
148       title: "nada".to_owned(),
149       description: None,
150       category_id: 1,
151       creator_id: inserted_user.id,
152       removed: None,
153       deleted: None,
154       updated: None,
155       nsfw: false,
156       actor_id: None,
157       local: true,
158       private_key: None,
159       public_key: None,
160       last_refreshed_at: None,
161       published: None,
162       icon: None,
163       banner: None,
164     };
165
166     let inserted_community = Community::create(&conn, &new_community).unwrap();
167
168     let new_post = PostForm {
169       name: "A test post".into(),
170       creator_id: inserted_user.id,
171       url: None,
172       body: None,
173       community_id: inserted_community.id,
174       removed: None,
175       deleted: None,
176       locked: None,
177       stickied: None,
178       updated: None,
179       nsfw: false,
180       embed_title: None,
181       embed_description: None,
182       embed_html: None,
183       thumbnail_url: None,
184       ap_id: None,
185       local: true,
186       published: None,
187     };
188
189     let inserted_post = Post::create(&conn, &new_post).unwrap();
190
191     let comment_form = CommentForm {
192       content: "A test comment".into(),
193       creator_id: inserted_user.id,
194       post_id: inserted_post.id,
195       removed: None,
196       deleted: None,
197       read: None,
198       parent_id: None,
199       published: None,
200       updated: None,
201       ap_id: None,
202       local: true,
203     };
204
205     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
206
207     let user_mention_form = UserMentionForm {
208       recipient_id: inserted_recipient.id,
209       comment_id: inserted_comment.id,
210       read: None,
211     };
212
213     let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
214
215     let expected_mention = UserMention {
216       id: inserted_mention.id,
217       recipient_id: inserted_mention.recipient_id,
218       comment_id: inserted_mention.comment_id,
219       read: false,
220       published: inserted_mention.published,
221     };
222
223     let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
224     let updated_mention =
225       UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
226     Comment::delete(&conn, inserted_comment.id).unwrap();
227     Post::delete(&conn, inserted_post.id).unwrap();
228     Community::delete(&conn, inserted_community.id).unwrap();
229     User_::delete(&conn, inserted_user.id).unwrap();
230     User_::delete(&conn, inserted_recipient.id).unwrap();
231
232     assert_eq!(expected_mention, read_mention);
233     assert_eq!(expected_mention, inserted_mention);
234     assert_eq!(expected_mention, updated_mention);
235   }
236 }