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