]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/user_mention.rs
Merge pull request #1328 from LemmyNet/move_views_to_diesel
[lemmy.git] / crates / db_queries / src / source / user_mention.rs
1 use crate::Crud;
2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::source::user_mention::*;
4
5 impl Crud<UserMentionForm> for UserMention {
6   fn read(conn: &PgConnection, user_mention_id: i32) -> Result<Self, Error> {
7     use lemmy_db_schema::schema::user_mention::dsl::*;
8     user_mention.find(user_mention_id).first::<Self>(conn)
9   }
10
11   fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
12     use lemmy_db_schema::schema::user_mention::dsl::*;
13     // since the return here isnt utilized, we dont need to do an update
14     // but get_result doesnt return the existing row here
15     insert_into(user_mention)
16       .values(user_mention_form)
17       .on_conflict((recipient_id, comment_id))
18       .do_update()
19       .set(user_mention_form)
20       .get_result::<Self>(conn)
21   }
22
23   fn update(
24     conn: &PgConnection,
25     user_mention_id: i32,
26     user_mention_form: &UserMentionForm,
27   ) -> Result<Self, Error> {
28     use lemmy_db_schema::schema::user_mention::dsl::*;
29     diesel::update(user_mention.find(user_mention_id))
30       .set(user_mention_form)
31       .get_result::<Self>(conn)
32   }
33 }
34
35 pub trait UserMention_ {
36   fn update_read(
37     conn: &PgConnection,
38     user_mention_id: i32,
39     new_read: bool,
40   ) -> Result<UserMention, Error>;
41   fn mark_all_as_read(
42     conn: &PgConnection,
43     for_recipient_id: i32,
44   ) -> Result<Vec<UserMention>, Error>;
45 }
46
47 impl UserMention_ for UserMention {
48   fn update_read(
49     conn: &PgConnection,
50     user_mention_id: i32,
51     new_read: bool,
52   ) -> Result<UserMention, Error> {
53     use lemmy_db_schema::schema::user_mention::dsl::*;
54     diesel::update(user_mention.find(user_mention_id))
55       .set(read.eq(new_read))
56       .get_result::<Self>(conn)
57   }
58
59   fn mark_all_as_read(
60     conn: &PgConnection,
61     for_recipient_id: i32,
62   ) -> Result<Vec<UserMention>, Error> {
63     use lemmy_db_schema::schema::user_mention::dsl::*;
64     diesel::update(
65       user_mention
66         .filter(recipient_id.eq(for_recipient_id))
67         .filter(read.eq(false)),
68     )
69     .set(read.eq(true))
70     .get_results::<Self>(conn)
71   }
72 }
73
74 #[cfg(test)]
75 mod tests {
76   use crate::{establish_unpooled_connection, Crud, ListingType, SortType};
77   use lemmy_db_schema::source::{
78     comment::*,
79     community::{Community, CommunityForm},
80     post::*,
81     user::*,
82     user_mention::*,
83   };
84
85   #[test]
86   fn test_crud() {
87     let conn = establish_unpooled_connection();
88
89     let new_user = UserForm {
90       name: "terrylake".into(),
91       preferred_username: None,
92       password_encrypted: "nope".into(),
93       email: None,
94       matrix_user_id: None,
95       avatar: None,
96       banner: None,
97       admin: false,
98       banned: Some(false),
99       published: None,
100       updated: None,
101       show_nsfw: false,
102       theme: "browser".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: Some(false),
128       published: None,
129       updated: None,
130       show_nsfw: false,
131       theme: "browser".into(),
132       default_sort_type: SortType::Hot as i16,
133       default_listing_type: ListingType::Subscribed as i16,
134       lang: "browser".into(),
135       show_avatars: true,
136       send_notifications_to_email: false,
137       actor_id: None,
138       bio: None,
139       local: true,
140       private_key: None,
141       public_key: None,
142       last_refreshed_at: None,
143     };
144
145     let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
146
147     let new_community = CommunityForm {
148       name: "test community lake".to_string(),
149       title: "nada".to_owned(),
150       description: None,
151       category_id: 1,
152       creator_id: inserted_user.id,
153       removed: None,
154       deleted: None,
155       updated: None,
156       nsfw: false,
157       actor_id: None,
158       local: true,
159       private_key: None,
160       public_key: None,
161       last_refreshed_at: None,
162       published: None,
163       icon: None,
164       banner: None,
165     };
166
167     let inserted_community = Community::create(&conn, &new_community).unwrap();
168
169     let new_post = PostForm {
170       name: "A test post".into(),
171       creator_id: inserted_user.id,
172       url: None,
173       body: None,
174       community_id: inserted_community.id,
175       removed: None,
176       deleted: None,
177       locked: None,
178       stickied: None,
179       updated: None,
180       nsfw: false,
181       embed_title: None,
182       embed_description: None,
183       embed_html: None,
184       thumbnail_url: None,
185       ap_id: None,
186       local: true,
187       published: None,
188     };
189
190     let inserted_post = Post::create(&conn, &new_post).unwrap();
191
192     let comment_form = CommentForm {
193       content: "A test comment".into(),
194       creator_id: inserted_user.id,
195       post_id: inserted_post.id,
196       removed: None,
197       deleted: None,
198       read: None,
199       parent_id: None,
200       published: None,
201       updated: None,
202       ap_id: None,
203       local: true,
204     };
205
206     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
207
208     let user_mention_form = UserMentionForm {
209       recipient_id: inserted_recipient.id,
210       comment_id: inserted_comment.id,
211       read: None,
212     };
213
214     let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
215
216     let expected_mention = UserMention {
217       id: inserted_mention.id,
218       recipient_id: inserted_mention.recipient_id,
219       comment_id: inserted_mention.comment_id,
220       read: false,
221       published: inserted_mention.published,
222     };
223
224     let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
225     let updated_mention =
226       UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
227     Comment::delete(&conn, inserted_comment.id).unwrap();
228     Post::delete(&conn, inserted_post.id).unwrap();
229     Community::delete(&conn, inserted_community.id).unwrap();
230     User_::delete(&conn, inserted_user.id).unwrap();
231     User_::delete(&conn, inserted_recipient.id).unwrap();
232
233     assert_eq!(expected_mention, read_mention);
234     assert_eq!(expected_mention, inserted_mention);
235     assert_eq!(expected_mention, updated_mention);
236   }
237 }