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