]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/user_mention.rs
Support plain `cargo test` and disable unused doctests for speed
[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   use serial_test::serial;
85
86   #[test]
87   #[serial]
88   fn test_crud() {
89     let conn = establish_unpooled_connection();
90
91     let new_user = UserForm {
92       name: "terrylake".into(),
93       preferred_username: None,
94       password_encrypted: "nope".into(),
95       email: None,
96       matrix_user_id: None,
97       avatar: None,
98       banner: None,
99       admin: false,
100       banned: Some(false),
101       published: None,
102       updated: None,
103       show_nsfw: false,
104       theme: "browser".into(),
105       default_sort_type: SortType::Hot as i16,
106       default_listing_type: ListingType::Subscribed as i16,
107       lang: "browser".into(),
108       show_avatars: true,
109       send_notifications_to_email: false,
110       actor_id: None,
111       bio: None,
112       local: true,
113       private_key: None,
114       public_key: None,
115       last_refreshed_at: None,
116       inbox_url: None,
117       shared_inbox_url: None,
118     };
119
120     let inserted_user = User_::create(&conn, &new_user).unwrap();
121
122     let recipient_form = UserForm {
123       name: "terrylakes recipient".into(),
124       preferred_username: None,
125       password_encrypted: "nope".into(),
126       email: None,
127       matrix_user_id: None,
128       avatar: None,
129       banner: None,
130       admin: false,
131       banned: Some(false),
132       published: None,
133       updated: None,
134       show_nsfw: false,
135       theme: "browser".into(),
136       default_sort_type: SortType::Hot as i16,
137       default_listing_type: ListingType::Subscribed as i16,
138       lang: "browser".into(),
139       show_avatars: true,
140       send_notifications_to_email: false,
141       actor_id: None,
142       bio: None,
143       local: true,
144       private_key: None,
145       public_key: None,
146       last_refreshed_at: None,
147       inbox_url: None,
148       shared_inbox_url: None,
149     };
150
151     let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
152
153     let new_community = CommunityForm {
154       name: "test community lake".to_string(),
155       title: "nada".to_owned(),
156       description: None,
157       creator_id: inserted_user.id,
158       removed: None,
159       deleted: None,
160       updated: None,
161       nsfw: false,
162       actor_id: None,
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       followers_url: None,
171       inbox_url: None,
172       shared_inbox_url: None,
173     };
174
175     let inserted_community = Community::create(&conn, &new_community).unwrap();
176
177     let new_post = PostForm {
178       name: "A test post".into(),
179       creator_id: inserted_user.id,
180       url: None,
181       body: None,
182       community_id: inserted_community.id,
183       removed: None,
184       deleted: None,
185       locked: None,
186       stickied: None,
187       updated: None,
188       nsfw: false,
189       embed_title: None,
190       embed_description: None,
191       embed_html: None,
192       thumbnail_url: None,
193       ap_id: None,
194       local: true,
195       published: None,
196     };
197
198     let inserted_post = Post::create(&conn, &new_post).unwrap();
199
200     let comment_form = CommentForm {
201       content: "A test comment".into(),
202       creator_id: inserted_user.id,
203       post_id: inserted_post.id,
204       removed: None,
205       deleted: None,
206       read: None,
207       parent_id: None,
208       published: None,
209       updated: None,
210       ap_id: None,
211       local: true,
212     };
213
214     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
215
216     let user_mention_form = UserMentionForm {
217       recipient_id: inserted_recipient.id,
218       comment_id: inserted_comment.id,
219       read: None,
220     };
221
222     let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
223
224     let expected_mention = UserMention {
225       id: inserted_mention.id,
226       recipient_id: inserted_mention.recipient_id,
227       comment_id: inserted_mention.comment_id,
228       read: false,
229       published: inserted_mention.published,
230     };
231
232     let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
233     let updated_mention =
234       UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
235     Comment::delete(&conn, inserted_comment.id).unwrap();
236     Post::delete(&conn, inserted_post.id).unwrap();
237     Community::delete(&conn, inserted_community.id).unwrap();
238     User_::delete(&conn, inserted_user.id).unwrap();
239     User_::delete(&conn, inserted_recipient.id).unwrap();
240
241     assert_eq!(expected_mention, read_mention);
242     assert_eq!(expected_mention, inserted_mention);
243     assert_eq!(expected_mention, updated_mention);
244   }
245 }