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