]> Untitled Git - lemmy.git/blob - server/src/db/community.rs
Adding emoji support.
[lemmy.git] / server / src / db / community.rs
1 use crate::schema::{community, community_moderator, community_follower, community_user_ban, site};
2 use super::*;
3
4 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
5 #[table_name="community"]
6 pub struct Community {
7   pub id: i32,
8   pub name: String,
9   pub title: String,
10   pub description: Option<String>,
11   pub category_id: i32,
12   pub creator_id: i32,
13   pub removed: bool,
14   pub published: chrono::NaiveDateTime,
15   pub updated: Option<chrono::NaiveDateTime>,
16   pub deleted: bool,
17   pub nsfw: bool,
18 }
19
20 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
21 #[table_name="community"]
22 pub struct CommunityForm {
23   pub name: String,
24   pub title: String,
25   pub description: Option<String>,
26   pub category_id: i32,
27   pub creator_id: i32,
28   pub removed: Option<bool>,
29   pub updated: Option<chrono::NaiveDateTime>,
30   pub deleted: Option<bool>,
31   pub nsfw: bool,
32 }
33
34 impl Crud<CommunityForm> for Community {
35   fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
36     use crate::schema::community::dsl::*;
37     community.find(community_id)
38       .first::<Self>(conn)
39   }
40
41   fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
42     use crate::schema::community::dsl::*;
43     diesel::delete(community.find(community_id))
44       .execute(conn)
45   }
46
47   fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
48     use crate::schema::community::dsl::*;
49       insert_into(community)
50         .values(new_community)
51         .get_result::<Self>(conn)
52   }
53
54   fn update(conn: &PgConnection, community_id: i32, new_community: &CommunityForm) -> Result<Self, Error> {
55     use crate::schema::community::dsl::*;
56     diesel::update(community.find(community_id))
57       .set(new_community)
58       .get_result::<Self>(conn)
59   }
60 }
61
62 impl Community {
63   pub fn read_from_name(conn: &PgConnection, community_name: String) -> Result<Self, Error> {
64     use crate::schema::community::dsl::*;
65     community.filter(name.eq(community_name))
66       .first::<Self>(conn)
67   }
68 }
69
70 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
71 #[belongs_to(Community)]
72 #[table_name = "community_moderator"]
73 pub struct CommunityModerator {
74   pub id: i32,
75   pub community_id: i32,
76   pub user_id: i32,
77   pub published: chrono::NaiveDateTime,
78 }
79
80 #[derive(Insertable, AsChangeset, Clone)]
81 #[table_name="community_moderator"]
82 pub struct CommunityModeratorForm {
83   pub community_id: i32,
84   pub user_id: i32,
85 }
86
87 impl Joinable<CommunityModeratorForm> for CommunityModerator {
88   fn join(conn: &PgConnection, community_user_form: &CommunityModeratorForm) -> Result<Self, Error> {
89     use crate::schema::community_moderator::dsl::*;
90     insert_into(community_moderator)
91       .values(community_user_form)
92       .get_result::<Self>(conn)
93   }
94
95   fn leave(conn: &PgConnection, community_user_form: &CommunityModeratorForm) -> Result<usize, Error> {
96     use crate::schema::community_moderator::dsl::*;
97     diesel::delete(community_moderator
98       .filter(community_id.eq(community_user_form.community_id))
99       .filter(user_id.eq(community_user_form.user_id)))
100       .execute(conn)
101   }
102 }
103
104 impl CommunityModerator {
105   pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
106     use crate::schema::community_moderator::dsl::*;
107     diesel::delete(
108       community_moderator
109       .filter(community_id.eq(for_community_id)))
110       .execute(conn)
111   }
112 }
113
114 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
115 #[belongs_to(Community)]
116 #[table_name = "community_user_ban"]
117 pub struct CommunityUserBan {
118   pub id: i32,
119   pub community_id: i32,
120   pub user_id: i32,
121   pub published: chrono::NaiveDateTime,
122 }
123
124 #[derive(Insertable, AsChangeset, Clone)]
125 #[table_name="community_user_ban"]
126 pub struct CommunityUserBanForm {
127   pub community_id: i32,
128   pub user_id: i32,
129 }
130
131 impl Bannable<CommunityUserBanForm> for CommunityUserBan {
132   fn ban(conn: &PgConnection, community_user_ban_form: &CommunityUserBanForm) -> Result<Self, Error> {
133     use crate::schema::community_user_ban::dsl::*;
134     insert_into(community_user_ban)
135       .values(community_user_ban_form)
136       .get_result::<Self>(conn)
137   }
138
139   fn unban(conn: &PgConnection, community_user_ban_form: &CommunityUserBanForm) -> Result<usize, Error> {
140     use crate::schema::community_user_ban::dsl::*;
141     diesel::delete(community_user_ban
142       .filter(community_id.eq(community_user_ban_form.community_id))
143       .filter(user_id.eq(community_user_ban_form.user_id)))
144       .execute(conn)
145   }
146 }
147
148 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
149 #[belongs_to(Community)]
150 #[table_name = "community_follower"]
151 pub struct CommunityFollower {
152   pub id: i32,
153   pub community_id: i32,
154   pub user_id: i32,
155   pub published: chrono::NaiveDateTime,
156 }
157
158 #[derive(Insertable, AsChangeset, Clone)]
159 #[table_name="community_follower"]
160 pub struct CommunityFollowerForm {
161   pub community_id: i32,
162   pub user_id: i32,
163 }
164
165 impl Followable<CommunityFollowerForm> for CommunityFollower {
166   fn follow(conn: &PgConnection, community_follower_form: &CommunityFollowerForm) -> Result<Self, Error> {
167     use crate::schema::community_follower::dsl::*;
168     insert_into(community_follower)
169       .values(community_follower_form)
170       .get_result::<Self>(conn)
171   }
172   fn ignore(conn: &PgConnection, community_follower_form: &CommunityFollowerForm) -> Result<usize, Error> {
173     use crate::schema::community_follower::dsl::*;
174     diesel::delete(community_follower
175       .filter(community_id.eq(&community_follower_form.community_id))
176       .filter(user_id.eq(&community_follower_form.user_id)))
177       .execute(conn)
178   }
179 }
180
181 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
182 #[table_name="site"]
183 pub struct Site {
184   pub id: i32,
185   pub name: String,
186   pub description: Option<String>,
187   pub creator_id: i32,
188   pub published: chrono::NaiveDateTime,
189   pub updated: Option<chrono::NaiveDateTime>
190 }
191
192 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
193 #[table_name="site"]
194 pub struct SiteForm {
195   pub name: String,
196   pub description: Option<String>,
197   pub creator_id: i32,
198   pub updated: Option<chrono::NaiveDateTime>
199 }
200
201 impl Crud<SiteForm> for Site {
202   fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> {
203     use crate::schema::site::dsl::*;
204     site.first::<Self>(conn)
205   }
206
207   fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> {
208     use crate::schema::site::dsl::*;
209     diesel::delete(site.find(site_id))
210       .execute(conn)
211   }
212
213   fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> {
214     use crate::schema::site::dsl::*;
215       insert_into(site)
216         .values(new_site)
217         .get_result::<Self>(conn)
218   }
219
220   fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result<Self, Error> {
221     use crate::schema::site::dsl::*;
222     diesel::update(site.find(site_id))
223       .set(new_site)
224       .get_result::<Self>(conn)
225   }
226 }
227
228 #[cfg(test)]
229 mod tests {
230   use super::*;
231   use super::super::user::*;
232  #[test]
233   fn test_crud() {
234     let conn = establish_connection();
235
236     let new_user = UserForm {
237       name: "bobbee".into(),
238       fedi_name: "rrf".into(),
239       preferred_username: None,
240       password_encrypted: "nope".into(),
241       email: None,
242       admin: false,
243       banned: false,
244       updated: None,
245       show_nsfw: false,
246     };
247
248     let inserted_user = User_::create(&conn, &new_user).unwrap();
249
250     let new_community = CommunityForm {
251       name: "TIL".into(),
252       creator_id: inserted_user.id,
253       title: "nada".to_owned(),
254       description: None,
255       category_id: 1,
256       nsfw: false,
257       removed: None,
258       deleted: None,
259       updated: None,
260     };
261
262     let inserted_community = Community::create(&conn, &new_community).unwrap();
263
264     let expected_community = Community {
265       id: inserted_community.id,
266       creator_id: inserted_user.id,
267       name: "TIL".into(),
268       title: "nada".to_owned(),
269       description: None,
270       category_id: 1,
271       nsfw: false,
272       removed: false,
273       deleted: false,
274       published: inserted_community.published,
275       updated: None
276     };
277
278     let community_follower_form = CommunityFollowerForm {
279       community_id: inserted_community.id,
280       user_id: inserted_user.id
281     };
282
283     let inserted_community_follower = CommunityFollower::follow(&conn, &community_follower_form).unwrap();
284
285
286     let expected_community_follower = CommunityFollower {
287       id: inserted_community_follower.id,
288       community_id: inserted_community.id,
289       user_id: inserted_user.id,
290       published: inserted_community_follower.published
291     };
292     
293     let community_user_form = CommunityModeratorForm {
294       community_id: inserted_community.id,
295       user_id: inserted_user.id
296     };
297
298     let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
299
300     let expected_community_user = CommunityModerator {
301       id: inserted_community_user.id,
302       community_id: inserted_community.id,
303       user_id: inserted_user.id,
304       published: inserted_community_user.published
305     };
306
307     let community_user_ban_form = CommunityUserBanForm {
308       community_id: inserted_community.id,
309       user_id: inserted_user.id
310     };
311
312     let inserted_community_user_ban = CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
313
314     let expected_community_user_ban = CommunityUserBan {
315       id: inserted_community_user_ban.id,
316       community_id: inserted_community.id,
317       user_id: inserted_user.id,
318       published: inserted_community_user_ban.published
319     };
320
321     let read_community = Community::read(&conn, inserted_community.id).unwrap();
322     let updated_community = Community::update(&conn, inserted_community.id, &new_community).unwrap();
323     let ignored_community = CommunityFollower::ignore(&conn, &community_follower_form).unwrap();
324     let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
325     let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
326     let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
327     User_::delete(&conn, inserted_user.id).unwrap();
328
329     assert_eq!(expected_community, read_community);
330     assert_eq!(expected_community, inserted_community);
331     assert_eq!(expected_community, updated_community);
332     assert_eq!(expected_community_follower, inserted_community_follower);
333     assert_eq!(expected_community_user, inserted_community_user);
334     assert_eq!(expected_community_user_ban, inserted_community_user_ban);
335     assert_eq!(1, ignored_community);
336     assert_eq!(1, left_community);
337     assert_eq!(1, unban);
338     // assert_eq!(2, loaded_count);
339     assert_eq!(1, num_deleted);
340
341   }
342 }