]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/user_mention.rs
b0c97572f691193e03ed38399b1f7a7390e6ac9c
[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       inbox_url: None,
115       shared_inbox_url: None,
116     };
117
118     let inserted_user = User_::create(&conn, &new_user).unwrap();
119
120     let recipient_form = UserForm {
121       name: "terrylakes recipient".into(),
122       preferred_username: None,
123       password_encrypted: "nope".into(),
124       email: None,
125       matrix_user_id: None,
126       avatar: None,
127       banner: None,
128       admin: false,
129       banned: Some(false),
130       published: None,
131       updated: None,
132       show_nsfw: false,
133       theme: "browser".into(),
134       default_sort_type: SortType::Hot as i16,
135       default_listing_type: ListingType::Subscribed as i16,
136       lang: "browser".into(),
137       show_avatars: true,
138       send_notifications_to_email: false,
139       actor_id: None,
140       bio: None,
141       local: true,
142       private_key: None,
143       public_key: None,
144       last_refreshed_at: None,
145       inbox_url: None,
146       shared_inbox_url: None,
147     };
148
149     let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
150
151     let new_community = CommunityForm {
152       name: "test community lake".to_string(),
153       title: "nada".to_owned(),
154       description: None,
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       followers_url: None,
169       inbox_url: None,
170       shared_inbox_url: None,
171     };
172
173     let inserted_community = Community::create(&conn, &new_community).unwrap();
174
175     let new_post = PostForm {
176       name: "A test post".into(),
177       creator_id: inserted_user.id,
178       url: None,
179       body: None,
180       community_id: inserted_community.id,
181       removed: None,
182       deleted: None,
183       locked: None,
184       stickied: None,
185       updated: None,
186       nsfw: false,
187       embed_title: None,
188       embed_description: None,
189       embed_html: None,
190       thumbnail_url: None,
191       ap_id: None,
192       local: true,
193       published: None,
194     };
195
196     let inserted_post = Post::create(&conn, &new_post).unwrap();
197
198     let comment_form = CommentForm {
199       content: "A test comment".into(),
200       creator_id: inserted_user.id,
201       post_id: inserted_post.id,
202       removed: None,
203       deleted: None,
204       read: None,
205       parent_id: None,
206       published: None,
207       updated: None,
208       ap_id: None,
209       local: true,
210     };
211
212     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
213
214     let user_mention_form = UserMentionForm {
215       recipient_id: inserted_recipient.id,
216       comment_id: inserted_comment.id,
217       read: None,
218     };
219
220     let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
221
222     let expected_mention = UserMention {
223       id: inserted_mention.id,
224       recipient_id: inserted_mention.recipient_id,
225       comment_id: inserted_mention.comment_id,
226       read: false,
227       published: inserted_mention.published,
228     };
229
230     let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
231     let updated_mention =
232       UserMention::update(&conn, inserted_mention.id, &user_mention_form).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   }
243 }