]> Untitled Git - lemmy.git/blob - server/src/db/user_mention.rs
Merge branch 'dev' into federation
[lemmy.git] / server / src / db / user_mention.rs
1 use super::comment::Comment;
2 use super::*;
3 use crate::schema::user_mention;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
6 #[belongs_to(Comment)]
7 #[table_name = "user_mention"]
8 pub struct UserMention {
9   pub id: i32,
10   pub recipient_id: i32,
11   pub comment_id: i32,
12   pub read: bool,
13   pub published: chrono::NaiveDateTime,
14 }
15
16 #[derive(Insertable, AsChangeset, Clone)]
17 #[table_name = "user_mention"]
18 pub struct UserMentionForm {
19   pub recipient_id: i32,
20   pub comment_id: i32,
21   pub read: Option<bool>,
22 }
23
24 impl Crud<UserMentionForm> for UserMention {
25   fn read(conn: &PgConnection, user_mention_id: i32) -> Result<Self, Error> {
26     use crate::schema::user_mention::dsl::*;
27     user_mention.find(user_mention_id).first::<Self>(conn)
28   }
29
30   fn delete(conn: &PgConnection, user_mention_id: i32) -> Result<usize, Error> {
31     use crate::schema::user_mention::dsl::*;
32     diesel::delete(user_mention.find(user_mention_id)).execute(conn)
33   }
34
35   fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
36     use crate::schema::user_mention::dsl::*;
37     insert_into(user_mention)
38       .values(user_mention_form)
39       .get_result::<Self>(conn)
40   }
41
42   fn update(
43     conn: &PgConnection,
44     user_mention_id: i32,
45     user_mention_form: &UserMentionForm,
46   ) -> Result<Self, Error> {
47     use crate::schema::user_mention::dsl::*;
48     diesel::update(user_mention.find(user_mention_id))
49       .set(user_mention_form)
50       .get_result::<Self>(conn)
51   }
52 }
53
54 #[cfg(test)]
55 mod tests {
56   use super::super::comment::*;
57   use super::super::community::*;
58   use super::super::post::*;
59   use super::super::user::*;
60   use super::*;
61   #[test]
62   fn test_crud() {
63     let conn = establish_unpooled_connection();
64
65     let new_user = UserForm {
66       name: "terrylake".into(),
67       preferred_username: None,
68       password_encrypted: "nope".into(),
69       email: None,
70       matrix_user_id: None,
71       avatar: None,
72       admin: false,
73       banned: false,
74       updated: None,
75       show_nsfw: false,
76       theme: "darkly".into(),
77       default_sort_type: SortType::Hot as i16,
78       default_listing_type: ListingType::Subscribed as i16,
79       lang: "browser".into(),
80       show_avatars: true,
81       send_notifications_to_email: false,
82       actor_id: "changeme".into(),
83       bio: None,
84       local: true,
85       private_key: None,
86       public_key: None,
87       last_refreshed_at: None,
88     };
89
90     let inserted_user = User_::create(&conn, &new_user).unwrap();
91
92     let recipient_form = UserForm {
93       name: "terrylakes recipient".into(),
94       preferred_username: None,
95       password_encrypted: "nope".into(),
96       email: None,
97       matrix_user_id: None,
98       avatar: None,
99       admin: false,
100       banned: false,
101       updated: None,
102       show_nsfw: false,
103       theme: "darkly".into(),
104       default_sort_type: SortType::Hot as i16,
105       default_listing_type: ListingType::Subscribed as i16,
106       lang: "browser".into(),
107       show_avatars: true,
108       send_notifications_to_email: false,
109       actor_id: "changeme".into(),
110       bio: None,
111       local: true,
112       private_key: None,
113       public_key: None,
114       last_refreshed_at: None,
115     };
116
117     let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
118
119     let new_community = CommunityForm {
120       name: "test community lake".to_string(),
121       title: "nada".to_owned(),
122       description: None,
123       category_id: 1,
124       creator_id: inserted_user.id,
125       removed: None,
126       deleted: None,
127       updated: None,
128       nsfw: false,
129       actor_id: "changeme".into(),
130       local: true,
131       private_key: None,
132       public_key: None,
133       last_refreshed_at: None,
134       published: None,
135     };
136
137     let inserted_community = Community::create(&conn, &new_community).unwrap();
138
139     let new_post = PostForm {
140       name: "A test post".into(),
141       creator_id: inserted_user.id,
142       url: None,
143       body: None,
144       community_id: inserted_community.id,
145       removed: None,
146       deleted: None,
147       locked: None,
148       stickied: None,
149       updated: None,
150       nsfw: false,
151       embed_title: None,
152       embed_description: None,
153       embed_html: None,
154       thumbnail_url: None,
155       ap_id: "changeme".into(),
156       local: true,
157       published: None,
158     };
159
160     let inserted_post = Post::create(&conn, &new_post).unwrap();
161
162     let comment_form = CommentForm {
163       content: "A test comment".into(),
164       creator_id: inserted_user.id,
165       post_id: inserted_post.id,
166       removed: None,
167       deleted: None,
168       read: None,
169       parent_id: None,
170       updated: None,
171       ap_id: "changeme".into(),
172       local: true,
173     };
174
175     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
176
177     let user_mention_form = UserMentionForm {
178       recipient_id: inserted_recipient.id,
179       comment_id: inserted_comment.id,
180       read: None,
181     };
182
183     let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
184
185     let expected_mention = UserMention {
186       id: inserted_mention.id,
187       recipient_id: inserted_mention.recipient_id,
188       comment_id: inserted_mention.comment_id,
189       read: false,
190       published: inserted_mention.published,
191     };
192
193     let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
194     let updated_mention =
195       UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
196     let num_deleted = UserMention::delete(&conn, inserted_mention.id).unwrap();
197     Comment::delete(&conn, inserted_comment.id).unwrap();
198     Post::delete(&conn, inserted_post.id).unwrap();
199     Community::delete(&conn, inserted_community.id).unwrap();
200     User_::delete(&conn, inserted_user.id).unwrap();
201     User_::delete(&conn, inserted_recipient.id).unwrap();
202
203     assert_eq!(expected_mention, read_mention);
204     assert_eq!(expected_mention, inserted_mention);
205     assert_eq!(expected_mention, updated_mention);
206     assert_eq!(1, num_deleted);
207   }
208 }