]> Untitled Git - lemmy.git/blob - lemmy_db/src/source/user_mention.rs
Merge branch 'main' into move_views_to_diesel
[lemmy.git] / lemmy_db / src / source / user_mention.rs
1 use super::comment::Comment;
2 use crate::{schema::user_mention, Crud};
3 use diesel::{dsl::*, result::Error, *};
4 use serde::Serialize;
5
6 #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
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)]
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 create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
32     use crate::schema::user_mention::dsl::*;
33     // since the return here isnt utilized, we dont need to do an update
34     // but get_result doesnt return the existing row here
35     insert_into(user_mention)
36       .values(user_mention_form)
37       .on_conflict((recipient_id, comment_id))
38       .do_update()
39       .set(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     source::{comment::*, community::*, post::*, user::*, user_mention::*},
83     tests::establish_unpooled_connection,
84     ListingType,
85     SortType,
86   };
87
88   #[test]
89   fn test_crud() {
90     let conn = establish_unpooled_connection();
91
92     let new_user = UserForm {
93       name: "terrylake".into(),
94       preferred_username: None,
95       password_encrypted: "nope".into(),
96       email: None,
97       matrix_user_id: None,
98       avatar: None,
99       banner: None,
100       admin: false,
101       banned: Some(false),
102       published: None,
103       updated: None,
104       show_nsfw: false,
105       theme: "browser".into(),
106       default_sort_type: SortType::Hot as i16,
107       default_listing_type: ListingType::Subscribed as i16,
108       lang: "browser".into(),
109       show_avatars: true,
110       send_notifications_to_email: false,
111       actor_id: None,
112       bio: None,
113       local: true,
114       private_key: None,
115       public_key: None,
116       last_refreshed_at: None,
117     };
118
119     let inserted_user = User_::create(&conn, &new_user).unwrap();
120
121     let recipient_form = UserForm {
122       name: "terrylakes recipient".into(),
123       preferred_username: None,
124       password_encrypted: "nope".into(),
125       email: None,
126       matrix_user_id: None,
127       avatar: None,
128       banner: None,
129       admin: false,
130       banned: Some(false),
131       published: None,
132       updated: None,
133       show_nsfw: false,
134       theme: "browser".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: None,
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: None,
161       local: true,
162       private_key: None,
163       public_key: None,
164       last_refreshed_at: None,
165       published: None,
166       icon: None,
167       banner: None,
168     };
169
170     let inserted_community = Community::create(&conn, &new_community).unwrap();
171
172     let new_post = PostForm {
173       name: "A test post".into(),
174       creator_id: inserted_user.id,
175       url: None,
176       body: None,
177       community_id: inserted_community.id,
178       removed: None,
179       deleted: None,
180       locked: None,
181       stickied: None,
182       updated: None,
183       nsfw: false,
184       embed_title: None,
185       embed_description: None,
186       embed_html: None,
187       thumbnail_url: None,
188       ap_id: None,
189       local: true,
190       published: None,
191     };
192
193     let inserted_post = Post::create(&conn, &new_post).unwrap();
194
195     let comment_form = CommentForm {
196       content: "A test comment".into(),
197       creator_id: inserted_user.id,
198       post_id: inserted_post.id,
199       removed: None,
200       deleted: None,
201       read: None,
202       parent_id: None,
203       published: None,
204       updated: None,
205       ap_id: None,
206       local: true,
207     };
208
209     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
210
211     let user_mention_form = UserMentionForm {
212       recipient_id: inserted_recipient.id,
213       comment_id: inserted_comment.id,
214       read: None,
215     };
216
217     let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
218
219     let expected_mention = UserMention {
220       id: inserted_mention.id,
221       recipient_id: inserted_mention.recipient_id,
222       comment_id: inserted_mention.comment_id,
223       read: false,
224       published: inserted_mention.published,
225     };
226
227     let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
228     let updated_mention =
229       UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
230     Comment::delete(&conn, inserted_comment.id).unwrap();
231     Post::delete(&conn, inserted_post.id).unwrap();
232     Community::delete(&conn, inserted_community.id).unwrap();
233     User_::delete(&conn, inserted_user.id).unwrap();
234     User_::delete(&conn, inserted_recipient.id).unwrap();
235
236     assert_eq!(expected_mention, read_mention);
237     assert_eq!(expected_mention, inserted_mention);
238     assert_eq!(expected_mention, updated_mention);
239   }
240 }