]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/person_mention.rs
Moving matrix_user_id to person table. #1438
[lemmy.git] / crates / db_queries / src / source / person_mention.rs
1 use crate::Crud;
2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{source::person_mention::*, PersonId, PersonMentionId};
4
5 impl Crud<PersonMentionForm, PersonMentionId> for PersonMention {
6   fn read(conn: &PgConnection, person_mention_id: PersonMentionId) -> Result<Self, Error> {
7     use lemmy_db_schema::schema::person_mention::dsl::*;
8     person_mention.find(person_mention_id).first::<Self>(conn)
9   }
10
11   fn create(conn: &PgConnection, person_mention_form: &PersonMentionForm) -> Result<Self, Error> {
12     use lemmy_db_schema::schema::person_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(person_mention)
16       .values(person_mention_form)
17       .on_conflict((recipient_id, comment_id))
18       .do_update()
19       .set(person_mention_form)
20       .get_result::<Self>(conn)
21   }
22
23   fn update(
24     conn: &PgConnection,
25     person_mention_id: PersonMentionId,
26     person_mention_form: &PersonMentionForm,
27   ) -> Result<Self, Error> {
28     use lemmy_db_schema::schema::person_mention::dsl::*;
29     diesel::update(person_mention.find(person_mention_id))
30       .set(person_mention_form)
31       .get_result::<Self>(conn)
32   }
33 }
34
35 pub trait PersonMention_ {
36   fn update_read(
37     conn: &PgConnection,
38     person_mention_id: PersonMentionId,
39     new_read: bool,
40   ) -> Result<PersonMention, Error>;
41   fn mark_all_as_read(
42     conn: &PgConnection,
43     for_recipient_id: PersonId,
44   ) -> Result<Vec<PersonMention>, Error>;
45 }
46
47 impl PersonMention_ for PersonMention {
48   fn update_read(
49     conn: &PgConnection,
50     person_mention_id: PersonMentionId,
51     new_read: bool,
52   ) -> Result<PersonMention, Error> {
53     use lemmy_db_schema::schema::person_mention::dsl::*;
54     diesel::update(person_mention.find(person_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: PersonId,
62   ) -> Result<Vec<PersonMention>, Error> {
63     use lemmy_db_schema::schema::person_mention::dsl::*;
64     diesel::update(
65       person_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};
77   use lemmy_db_schema::source::{
78     comment::*,
79     community::{Community, CommunityForm},
80     person::*,
81     person_mention::*,
82     post::*,
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_person = PersonForm {
92       name: "terrylake".into(),
93       preferred_username: None,
94       avatar: None,
95       banner: None,
96       banned: None,
97       deleted: None,
98       published: None,
99       updated: None,
100       actor_id: None,
101       bio: None,
102       local: None,
103       private_key: None,
104       public_key: None,
105       last_refreshed_at: None,
106       inbox_url: None,
107       shared_inbox_url: None,
108       matrix_user_id: None,
109     };
110
111     let inserted_person = Person::create(&conn, &new_person).unwrap();
112
113     let recipient_form = PersonForm {
114       name: "terrylakes recipient".into(),
115       preferred_username: None,
116       avatar: None,
117       banner: None,
118       banned: None,
119       deleted: None,
120       published: None,
121       updated: None,
122       actor_id: None,
123       bio: None,
124       local: None,
125       private_key: None,
126       public_key: None,
127       last_refreshed_at: None,
128       inbox_url: None,
129       shared_inbox_url: None,
130       matrix_user_id: None,
131     };
132
133     let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
134
135     let new_community = CommunityForm {
136       name: "test community lake".to_string(),
137       title: "nada".to_owned(),
138       description: None,
139       creator_id: inserted_person.id,
140       removed: None,
141       deleted: None,
142       updated: None,
143       nsfw: false,
144       actor_id: None,
145       local: true,
146       private_key: None,
147       public_key: None,
148       last_refreshed_at: None,
149       published: None,
150       icon: None,
151       banner: None,
152       followers_url: None,
153       inbox_url: None,
154       shared_inbox_url: None,
155     };
156
157     let inserted_community = Community::create(&conn, &new_community).unwrap();
158
159     let new_post = PostForm {
160       name: "A test post".into(),
161       creator_id: inserted_person.id,
162       url: None,
163       body: None,
164       community_id: inserted_community.id,
165       removed: None,
166       deleted: None,
167       locked: None,
168       stickied: None,
169       updated: None,
170       nsfw: false,
171       embed_title: None,
172       embed_description: None,
173       embed_html: None,
174       thumbnail_url: None,
175       ap_id: None,
176       local: true,
177       published: None,
178     };
179
180     let inserted_post = Post::create(&conn, &new_post).unwrap();
181
182     let comment_form = CommentForm {
183       content: "A test comment".into(),
184       creator_id: inserted_person.id,
185       post_id: inserted_post.id,
186       removed: None,
187       deleted: None,
188       read: None,
189       parent_id: None,
190       published: None,
191       updated: None,
192       ap_id: None,
193       local: true,
194     };
195
196     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
197
198     let person_mention_form = PersonMentionForm {
199       recipient_id: inserted_recipient.id,
200       comment_id: inserted_comment.id,
201       read: None,
202     };
203
204     let inserted_mention = PersonMention::create(&conn, &person_mention_form).unwrap();
205
206     let expected_mention = PersonMention {
207       id: inserted_mention.id,
208       recipient_id: inserted_mention.recipient_id,
209       comment_id: inserted_mention.comment_id,
210       read: false,
211       published: inserted_mention.published,
212     };
213
214     let read_mention = PersonMention::read(&conn, inserted_mention.id).unwrap();
215     let updated_mention =
216       PersonMention::update(&conn, inserted_mention.id, &person_mention_form).unwrap();
217     Comment::delete(&conn, inserted_comment.id).unwrap();
218     Post::delete(&conn, inserted_post.id).unwrap();
219     Community::delete(&conn, inserted_community.id).unwrap();
220     Person::delete(&conn, inserted_person.id).unwrap();
221     Person::delete(&conn, inserted_recipient.id).unwrap();
222
223     assert_eq!(expected_mention, read_mention);
224     assert_eq!(expected_mention, inserted_mention);
225     assert_eq!(expected_mention, updated_mention);
226   }
227 }