]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/person_mention.rs
Rewrite delete activities (#1699)
[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 for PersonMention {
6   type Form = PersonMentionForm;
7   type IdType = PersonMentionId;
8   fn read(conn: &PgConnection, person_mention_id: PersonMentionId) -> Result<Self, Error> {
9     use lemmy_db_schema::schema::person_mention::dsl::*;
10     person_mention.find(person_mention_id).first::<Self>(conn)
11   }
12
13   fn create(conn: &PgConnection, person_mention_form: &PersonMentionForm) -> Result<Self, Error> {
14     use lemmy_db_schema::schema::person_mention::dsl::*;
15     // since the return here isnt utilized, we dont need to do an update
16     // but get_result doesnt return the existing row here
17     insert_into(person_mention)
18       .values(person_mention_form)
19       .on_conflict((recipient_id, comment_id))
20       .do_update()
21       .set(person_mention_form)
22       .get_result::<Self>(conn)
23   }
24
25   fn update(
26     conn: &PgConnection,
27     person_mention_id: PersonMentionId,
28     person_mention_form: &PersonMentionForm,
29   ) -> Result<Self, Error> {
30     use lemmy_db_schema::schema::person_mention::dsl::*;
31     diesel::update(person_mention.find(person_mention_id))
32       .set(person_mention_form)
33       .get_result::<Self>(conn)
34   }
35 }
36
37 pub trait PersonMention_ {
38   fn update_read(
39     conn: &PgConnection,
40     person_mention_id: PersonMentionId,
41     new_read: bool,
42   ) -> Result<PersonMention, Error>;
43   fn mark_all_as_read(
44     conn: &PgConnection,
45     for_recipient_id: PersonId,
46   ) -> Result<Vec<PersonMention>, Error>;
47 }
48
49 impl PersonMention_ for PersonMention {
50   fn update_read(
51     conn: &PgConnection,
52     person_mention_id: PersonMentionId,
53     new_read: bool,
54   ) -> Result<PersonMention, Error> {
55     use lemmy_db_schema::schema::person_mention::dsl::*;
56     diesel::update(person_mention.find(person_mention_id))
57       .set(read.eq(new_read))
58       .get_result::<Self>(conn)
59   }
60
61   fn mark_all_as_read(
62     conn: &PgConnection,
63     for_recipient_id: PersonId,
64   ) -> Result<Vec<PersonMention>, Error> {
65     use lemmy_db_schema::schema::person_mention::dsl::*;
66     diesel::update(
67       person_mention
68         .filter(recipient_id.eq(for_recipient_id))
69         .filter(read.eq(false)),
70     )
71     .set(read.eq(true))
72     .get_results::<Self>(conn)
73   }
74 }
75
76 #[cfg(test)]
77 mod tests {
78   use crate::{establish_unpooled_connection, Crud};
79   use lemmy_db_schema::source::{
80     comment::*,
81     community::{Community, CommunityForm},
82     person::*,
83     person_mention::*,
84     post::*,
85   };
86   use serial_test::serial;
87
88   #[test]
89   #[serial]
90   fn test_crud() {
91     let conn = establish_unpooled_connection();
92
93     let new_person = PersonForm {
94       name: "terrylake".into(),
95       ..PersonForm::default()
96     };
97
98     let inserted_person = Person::create(&conn, &new_person).unwrap();
99
100     let recipient_form = PersonForm {
101       name: "terrylakes recipient".into(),
102       ..PersonForm::default()
103     };
104
105     let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
106
107     let new_community = CommunityForm {
108       name: "test community lake".to_string(),
109       title: "nada".to_owned(),
110       ..CommunityForm::default()
111     };
112
113     let inserted_community = Community::create(&conn, &new_community).unwrap();
114
115     let new_post = PostForm {
116       name: "A test post".into(),
117       creator_id: inserted_person.id,
118       community_id: inserted_community.id,
119       ..PostForm::default()
120     };
121
122     let inserted_post = Post::create(&conn, &new_post).unwrap();
123
124     let comment_form = CommentForm {
125       content: "A test comment".into(),
126       creator_id: inserted_person.id,
127       post_id: inserted_post.id,
128       ..CommentForm::default()
129     };
130
131     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
132
133     let person_mention_form = PersonMentionForm {
134       recipient_id: inserted_recipient.id,
135       comment_id: inserted_comment.id,
136       read: None,
137     };
138
139     let inserted_mention = PersonMention::create(&conn, &person_mention_form).unwrap();
140
141     let expected_mention = PersonMention {
142       id: inserted_mention.id,
143       recipient_id: inserted_mention.recipient_id,
144       comment_id: inserted_mention.comment_id,
145       read: false,
146       published: inserted_mention.published,
147     };
148
149     let read_mention = PersonMention::read(&conn, inserted_mention.id).unwrap();
150     let updated_mention =
151       PersonMention::update(&conn, inserted_mention.id, &person_mention_form).unwrap();
152     Comment::delete(&conn, inserted_comment.id).unwrap();
153     Post::delete(&conn, inserted_post.id).unwrap();
154     Community::delete(&conn, inserted_community.id).unwrap();
155     Person::delete(&conn, inserted_person.id).unwrap();
156     Person::delete(&conn, inserted_recipient.id).unwrap();
157
158     assert_eq!(expected_mention, read_mention);
159     assert_eq!(expected_mention, inserted_mention);
160     assert_eq!(expected_mention, updated_mention);
161   }
162 }