]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/person_mention.rs
Done with user->person migrations, now to code.
[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::*;
4
5 impl Crud<PersonMentionForm> for PersonMention {
6   fn read(conn: &PgConnection, person_mention_id: i32) -> 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: i32,
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: i32,
39     new_read: bool,
40   ) -> Result<PersonMention, Error>;
41   fn mark_all_as_read(
42     conn: &PgConnection,
43     for_recipient_id: i32,
44   ) -> Result<Vec<PersonMention>, Error>;
45 }
46
47 impl PersonMention_ for PersonMention {
48   fn update_read(
49     conn: &PgConnection,
50     person_mention_id: i32,
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: i32,
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, ListingType, SortType};
77   use lemmy_db_schema::source::{
78     comment::*,
79     community::{Community, CommunityForm},
80     post::*,
81     person::*,
82     person_mention::*,
83   };
84
85   #[test]
86   fn test_crud() {
87     let conn = establish_unpooled_connection();
88
89     let new_person = PersonForm {
90       name: "terrylake".into(),
91       preferred_username: None,
92       avatar: None,
93       banner: None,
94       banned: Some(false),
95       deleted: false,
96       published: None,
97       updated: None,
98       actor_id: None,
99       bio: None,
100       local: true,
101       private_key: None,
102       public_key: None,
103       last_refreshed_at: None,
104       inbox_url: None,
105       shared_inbox_url: None,
106     };
107
108     let inserted_person = Person::create(&conn, &new_person).unwrap();
109
110     let recipient_form = PersonForm {
111       name: "terrylakes recipient".into(),
112       preferred_username: None,
113       avatar: None,
114       banner: None,
115       banned: Some(false),
116       deleted: false,
117       published: None,
118       updated: None,
119       actor_id: None,
120       bio: None,
121       local: true,
122       private_key: None,
123       public_key: None,
124       last_refreshed_at: None,
125       inbox_url: None,
126       shared_inbox_url: None,
127     };
128
129     let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
130
131     let new_community = CommunityForm {
132       name: "test community lake".to_string(),
133       title: "nada".to_owned(),
134       description: None,
135       creator_id: inserted_person.id,
136       removed: None,
137       deleted: None,
138       updated: None,
139       nsfw: false,
140       actor_id: None,
141       local: true,
142       private_key: None,
143       public_key: None,
144       last_refreshed_at: None,
145       published: None,
146       icon: None,
147       banner: None,
148       followers_url: None,
149       inbox_url: None,
150       shared_inbox_url: None,
151     };
152
153     let inserted_community = Community::create(&conn, &new_community).unwrap();
154
155     let new_post = PostForm {
156       name: "A test post".into(),
157       creator_id: inserted_person.id,
158       url: None,
159       body: None,
160       community_id: inserted_community.id,
161       removed: None,
162       deleted: None,
163       locked: None,
164       stickied: None,
165       updated: None,
166       nsfw: false,
167       embed_title: None,
168       embed_description: None,
169       embed_html: None,
170       thumbnail_url: None,
171       ap_id: None,
172       local: true,
173       published: None,
174     };
175
176     let inserted_post = Post::create(&conn, &new_post).unwrap();
177
178     let comment_form = CommentForm {
179       content: "A test comment".into(),
180       creator_id: inserted_person.id,
181       post_id: inserted_post.id,
182       removed: None,
183       deleted: None,
184       read: None,
185       parent_id: None,
186       published: None,
187       updated: None,
188       ap_id: None,
189       local: true,
190     };
191
192     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
193
194     let person_mention_form = PersonMentionForm {
195       recipient_id: inserted_recipient.id,
196       comment_id: inserted_comment.id,
197       read: None,
198     };
199
200     let inserted_mention = PersonMention::create(&conn, &person_mention_form).unwrap();
201
202     let expected_mention = PersonMention {
203       id: inserted_mention.id,
204       recipient_id: inserted_mention.recipient_id,
205       comment_id: inserted_mention.comment_id,
206       read: false,
207       published: inserted_mention.published,
208     };
209
210     let read_mention = PersonMention::read(&conn, inserted_mention.id).unwrap();
211     let updated_mention =
212       PersonMention::update(&conn, inserted_mention.id, &person_mention_form).unwrap();
213     Comment::delete(&conn, inserted_comment.id).unwrap();
214     Post::delete(&conn, inserted_post.id).unwrap();
215     Community::delete(&conn, inserted_community.id).unwrap();
216     Person::delete(&conn, inserted_person.id).unwrap();
217     Person::delete(&conn, inserted_recipient.id).unwrap();
218
219     assert_eq!(expected_mention, read_mention);
220     assert_eq!(expected_mention, inserted_mention);
221     assert_eq!(expected_mention, updated_mention);
222   }
223 }