]> Untitled Git - lemmy.git/blob - server/lemmy_db/src/user_mention.rs
Translated using Weblate (Italian)
[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       admin: false,
104       banned: false,
105       updated: None,
106       show_nsfw: false,
107       theme: "darkly".into(),
108       default_sort_type: SortType::Hot as i16,
109       default_listing_type: ListingType::Subscribed as i16,
110       lang: "browser".into(),
111       show_avatars: true,
112       send_notifications_to_email: false,
113       actor_id: "changeme_628763".into(),
114       bio: None,
115       local: true,
116       private_key: None,
117       public_key: None,
118       last_refreshed_at: None,
119     };
120
121     let inserted_user = User_::create(&conn, &new_user).unwrap();
122
123     let recipient_form = UserForm {
124       name: "terrylakes recipient".into(),
125       preferred_username: None,
126       password_encrypted: "nope".into(),
127       email: None,
128       matrix_user_id: None,
129       avatar: None,
130       admin: false,
131       banned: false,
132       updated: None,
133       show_nsfw: false,
134       theme: "darkly".into(),
135       default_sort_type: SortType::Hot as i16,
136       default_listing_type: ListingType::Subscribed as i16,
137       lang: "browser".into(),
138       show_avatars: true,
139       send_notifications_to_email: false,
140       actor_id: "changeme_927389278".into(),
141       bio: None,
142       local: true,
143       private_key: None,
144       public_key: None,
145       last_refreshed_at: None,
146     };
147
148     let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
149
150     let new_community = CommunityForm {
151       name: "test community lake".to_string(),
152       title: "nada".to_owned(),
153       description: None,
154       category_id: 1,
155       creator_id: inserted_user.id,
156       removed: None,
157       deleted: None,
158       updated: None,
159       nsfw: false,
160       actor_id: "changeme_876238".into(),
161       local: true,
162       private_key: None,
163       public_key: None,
164       last_refreshed_at: None,
165       published: None,
166     };
167
168     let inserted_community = Community::create(&conn, &new_community).unwrap();
169
170     let new_post = PostForm {
171       name: "A test post".into(),
172       creator_id: inserted_user.id,
173       url: None,
174       body: None,
175       community_id: inserted_community.id,
176       removed: None,
177       deleted: None,
178       locked: None,
179       stickied: None,
180       updated: None,
181       nsfw: false,
182       embed_title: None,
183       embed_description: None,
184       embed_html: None,
185       thumbnail_url: None,
186       ap_id: "http://fake.com".into(),
187       local: true,
188       published: None,
189     };
190
191     let inserted_post = Post::create(&conn, &new_post).unwrap();
192
193     let comment_form = CommentForm {
194       content: "A test comment".into(),
195       creator_id: inserted_user.id,
196       post_id: inserted_post.id,
197       removed: None,
198       deleted: None,
199       read: None,
200       parent_id: None,
201       published: None,
202       updated: None,
203       ap_id: "http://fake.com".into(),
204       local: true,
205     };
206
207     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
208
209     let user_mention_form = UserMentionForm {
210       recipient_id: inserted_recipient.id,
211       comment_id: inserted_comment.id,
212       read: None,
213     };
214
215     let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
216
217     let expected_mention = UserMention {
218       id: inserted_mention.id,
219       recipient_id: inserted_mention.recipient_id,
220       comment_id: inserted_mention.comment_id,
221       read: false,
222       published: inserted_mention.published,
223     };
224
225     let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
226     let updated_mention =
227       UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
228     let num_deleted = UserMention::delete(&conn, inserted_mention.id).unwrap();
229     Comment::delete(&conn, inserted_comment.id).unwrap();
230     Post::delete(&conn, inserted_post.id).unwrap();
231     Community::delete(&conn, inserted_community.id).unwrap();
232     User_::delete(&conn, inserted_user.id).unwrap();
233     User_::delete(&conn, inserted_recipient.id).unwrap();
234
235     assert_eq!(expected_mention, read_mention);
236     assert_eq!(expected_mention, inserted_mention);
237     assert_eq!(expected_mention, updated_mention);
238     assert_eq!(1, num_deleted);
239   }
240 }