4 #[derive(Serialize, Deserialize)]
5 pub struct GetCommunity {
11 #[derive(Serialize, Deserialize)]
12 pub struct GetCommunityResponse {
14 community: CommunityView,
15 moderators: Vec<CommunityModeratorView>,
16 admins: Vec<UserView>,
19 #[derive(Serialize, Deserialize)]
20 pub struct CreateCommunity {
23 description: Option<String>,
29 #[derive(Serialize, Deserialize, Clone)]
30 pub struct CommunityResponse {
32 pub community: CommunityView,
35 #[derive(Serialize, Deserialize)]
36 pub struct ListCommunities {
43 #[derive(Serialize, Deserialize)]
44 pub struct ListCommunitiesResponse {
46 communities: Vec<CommunityView>,
49 #[derive(Serialize, Deserialize, Clone)]
50 pub struct BanFromCommunity {
51 pub community_id: i32,
54 reason: Option<String>,
59 #[derive(Serialize, Deserialize)]
60 pub struct BanFromCommunityResponse {
66 #[derive(Serialize, Deserialize)]
67 pub struct AddModToCommunity {
68 pub community_id: i32,
74 #[derive(Serialize, Deserialize)]
75 pub struct AddModToCommunityResponse {
77 moderators: Vec<CommunityModeratorView>,
80 #[derive(Serialize, Deserialize)]
81 pub struct EditCommunity {
85 description: Option<String>,
87 removed: Option<bool>,
88 deleted: Option<bool>,
90 reason: Option<String>,
95 #[derive(Serialize, Deserialize)]
96 pub struct FollowCommunity {
102 #[derive(Serialize, Deserialize)]
103 pub struct GetFollowedCommunities {
107 #[derive(Serialize, Deserialize)]
108 pub struct GetFollowedCommunitiesResponse {
110 communities: Vec<CommunityFollowerView>,
113 #[derive(Serialize, Deserialize)]
114 pub struct TransferCommunity {
120 impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
121 fn perform(&self) -> Result<GetCommunityResponse, Error> {
122 let data: &GetCommunity = &self.data;
123 let conn = establish_connection();
125 let user_id: Option<i32> = match &data.auth {
126 Some(auth) => match Claims::decode(&auth) {
128 let user_id = claims.claims.id;
136 let community_id = match data.id {
139 Community::read_from_name(&conn, data.name.to_owned().unwrap_or("main".to_string()))?.id
143 let community_view = match CommunityView::read(&conn, community_id, user_id) {
144 Ok(community) => community,
145 Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?,
148 let moderators = match CommunityModeratorView::for_community(&conn, community_id) {
149 Ok(moderators) => moderators,
150 Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?,
153 let site_creator_id = Site::read(&conn, 1)?.creator_id;
154 let mut admins = UserView::admins(&conn)?;
155 let creator_index = admins.iter().position(|r| r.id == site_creator_id).unwrap();
156 let creator_user = admins.remove(creator_index);
157 admins.insert(0, creator_user);
160 Ok(GetCommunityResponse {
161 op: self.op.to_string(),
162 community: community_view,
163 moderators: moderators,
169 impl Perform<CommunityResponse> for Oper<CreateCommunity> {
170 fn perform(&self) -> Result<CommunityResponse, Error> {
171 let data: &CreateCommunity = &self.data;
172 let conn = establish_connection();
174 let claims = match Claims::decode(&data.auth) {
175 Ok(claims) => claims.claims,
176 Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
179 if has_slurs(&data.name)
180 || has_slurs(&data.title)
181 || (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap()))
183 return Err(APIError::err(&self.op, "no_slurs"))?;
186 let user_id = claims.id;
188 // Check for a site ban
189 if UserView::read(&conn, user_id)?.banned {
190 return Err(APIError::err(&self.op, "site_ban"))?;
193 // When you create a community, make sure the user becomes a moderator and a follower
194 let community_form = CommunityForm {
195 name: data.name.to_owned(),
196 title: data.title.to_owned(),
197 description: data.description.to_owned(),
198 category_id: data.category_id,
206 let inserted_community = match Community::create(&conn, &community_form) {
207 Ok(community) => community,
208 Err(_e) => return Err(APIError::err(&self.op, "community_already_exists"))?,
211 let community_moderator_form = CommunityModeratorForm {
212 community_id: inserted_community.id,
216 let _inserted_community_moderator =
217 match CommunityModerator::join(&conn, &community_moderator_form) {
220 return Err(APIError::err(
222 "community_moderator_already_exists",
227 let community_follower_form = CommunityFollowerForm {
228 community_id: inserted_community.id,
232 let _inserted_community_follower =
233 match CommunityFollower::follow(&conn, &community_follower_form) {
235 Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists"))?,
238 let community_view = CommunityView::read(&conn, inserted_community.id, Some(user_id))?;
240 Ok(CommunityResponse {
241 op: self.op.to_string(),
242 community: community_view,
247 impl Perform<CommunityResponse> for Oper<EditCommunity> {
248 fn perform(&self) -> Result<CommunityResponse, Error> {
249 let data: &EditCommunity = &self.data;
251 if has_slurs(&data.name) || has_slurs(&data.title) {
252 return Err(APIError::err(&self.op, "no_slurs"))?;
255 let conn = establish_connection();
257 let claims = match Claims::decode(&data.auth) {
258 Ok(claims) => claims.claims,
259 Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
262 let user_id = claims.id;
264 // Check for a site ban
265 if UserView::read(&conn, user_id)?.banned {
266 return Err(APIError::err(&self.op, "site_ban"))?;
270 let mut editors: Vec<i32> = Vec::new();
272 &mut CommunityModeratorView::for_community(&conn, data.edit_id)?
277 editors.append(&mut UserView::admins(&conn)?.into_iter().map(|a| a.id).collect());
278 if !editors.contains(&user_id) {
279 return Err(APIError::err(&self.op, "no_community_edit_allowed"))?;
282 let community_form = CommunityForm {
283 name: data.name.to_owned(),
284 title: data.title.to_owned(),
285 description: data.description.to_owned(),
286 category_id: data.category_id.to_owned(),
288 removed: data.removed.to_owned(),
289 deleted: data.deleted.to_owned(),
291 updated: Some(naive_now()),
294 let _updated_community = match Community::update(&conn, data.edit_id, &community_form) {
295 Ok(community) => community,
296 Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_community"))?,
300 if let Some(removed) = data.removed.to_owned() {
301 let expires = match data.expires {
302 Some(time) => Some(naive_from_unix(time)),
305 let form = ModRemoveCommunityForm {
306 mod_user_id: user_id,
307 community_id: data.edit_id,
308 removed: Some(removed),
309 reason: data.reason.to_owned(),
312 ModRemoveCommunity::create(&conn, &form)?;
315 let community_view = CommunityView::read(&conn, data.edit_id, Some(user_id))?;
317 Ok(CommunityResponse {
318 op: self.op.to_string(),
319 community: community_view,
324 impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
325 fn perform(&self) -> Result<ListCommunitiesResponse, Error> {
326 let data: &ListCommunities = &self.data;
327 let conn = establish_connection();
329 let user_claims: Option<Claims> = match &data.auth {
330 Some(auth) => match Claims::decode(&auth) {
331 Ok(claims) => Some(claims.claims),
337 let user_id = match &user_claims {
338 Some(claims) => Some(claims.id),
342 let show_nsfw = match &user_claims {
343 Some(claims) => claims.show_nsfw,
347 let sort = SortType::from_str(&data.sort)?;
349 let communities: Vec<CommunityView> = CommunityView::list(
350 &conn, &sort, user_id, show_nsfw, None, data.page, data.limit,
354 Ok(ListCommunitiesResponse {
355 op: self.op.to_string(),
356 communities: communities,
361 impl Perform<CommunityResponse> for Oper<FollowCommunity> {
362 fn perform(&self) -> Result<CommunityResponse, Error> {
363 let data: &FollowCommunity = &self.data;
364 let conn = establish_connection();
366 let claims = match Claims::decode(&data.auth) {
367 Ok(claims) => claims.claims,
368 Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
371 let user_id = claims.id;
373 let community_follower_form = CommunityFollowerForm {
374 community_id: data.community_id,
379 match CommunityFollower::follow(&conn, &community_follower_form) {
381 Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists"))?,
384 match CommunityFollower::ignore(&conn, &community_follower_form) {
386 Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists"))?,
390 let community_view = CommunityView::read(&conn, data.community_id, Some(user_id))?;
392 Ok(CommunityResponse {
393 op: self.op.to_string(),
394 community: community_view,
399 impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
400 fn perform(&self) -> Result<GetFollowedCommunitiesResponse, Error> {
401 let data: &GetFollowedCommunities = &self.data;
402 let conn = establish_connection();
404 let claims = match Claims::decode(&data.auth) {
405 Ok(claims) => claims.claims,
406 Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
409 let user_id = claims.id;
411 let communities: Vec<CommunityFollowerView> =
412 match CommunityFollowerView::for_user(&conn, user_id) {
413 Ok(communities) => communities,
414 Err(_e) => return Err(APIError::err(&self.op, "system_err_login"))?,
418 Ok(GetFollowedCommunitiesResponse {
419 op: self.op.to_string(),
420 communities: communities,
425 impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
426 fn perform(&self) -> Result<BanFromCommunityResponse, Error> {
427 let data: &BanFromCommunity = &self.data;
428 let conn = establish_connection();
430 let claims = match Claims::decode(&data.auth) {
431 Ok(claims) => claims.claims,
432 Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
435 let user_id = claims.id;
437 let community_user_ban_form = CommunityUserBanForm {
438 community_id: data.community_id,
439 user_id: data.user_id,
443 match CommunityUserBan::ban(&conn, &community_user_ban_form) {
445 Err(_e) => return Err(APIError::err(&self.op, "community_user_already_banned"))?,
448 match CommunityUserBan::unban(&conn, &community_user_ban_form) {
450 Err(_e) => return Err(APIError::err(&self.op, "community_user_already_banned"))?,
455 let expires = match data.expires {
456 Some(time) => Some(naive_from_unix(time)),
460 let form = ModBanFromCommunityForm {
461 mod_user_id: user_id,
462 other_user_id: data.user_id,
463 community_id: data.community_id,
464 reason: data.reason.to_owned(),
465 banned: Some(data.ban),
468 ModBanFromCommunity::create(&conn, &form)?;
470 let user_view = UserView::read(&conn, data.user_id)?;
472 Ok(BanFromCommunityResponse {
473 op: self.op.to_string(),
480 impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> {
481 fn perform(&self) -> Result<AddModToCommunityResponse, Error> {
482 let data: &AddModToCommunity = &self.data;
483 let conn = establish_connection();
485 let claims = match Claims::decode(&data.auth) {
486 Ok(claims) => claims.claims,
487 Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
490 let user_id = claims.id;
492 let community_moderator_form = CommunityModeratorForm {
493 community_id: data.community_id,
494 user_id: data.user_id,
498 match CommunityModerator::join(&conn, &community_moderator_form) {
501 return Err(APIError::err(
503 "community_moderator_already_exists",
508 match CommunityModerator::leave(&conn, &community_moderator_form) {
511 return Err(APIError::err(
513 "community_moderator_already_exists",
520 let form = ModAddCommunityForm {
521 mod_user_id: user_id,
522 other_user_id: data.user_id,
523 community_id: data.community_id,
524 removed: Some(!data.added),
526 ModAddCommunity::create(&conn, &form)?;
528 let moderators = CommunityModeratorView::for_community(&conn, data.community_id)?;
530 Ok(AddModToCommunityResponse {
531 op: self.op.to_string(),
532 moderators: moderators,
537 impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
538 fn perform(&self) -> Result<GetCommunityResponse, Error> {
539 let data: &TransferCommunity = &self.data;
540 let conn = establish_connection();
542 let claims = match Claims::decode(&data.auth) {
543 Ok(claims) => claims.claims,
544 Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
547 let user_id = claims.id;
549 let read_community = Community::read(&conn, data.community_id)?;
551 let site_creator_id = Site::read(&conn, 1)?.creator_id;
552 let mut admins = UserView::admins(&conn)?;
553 let creator_index = admins.iter().position(|r| r.id == site_creator_id).unwrap();
554 let creator_user = admins.remove(creator_index);
555 admins.insert(0, creator_user);
557 // Make sure user is the creator, or an admin
558 if user_id != read_community.creator_id
562 .collect::<Vec<i32>>()
565 return Err(APIError::err(&self.op, "not_an_admin"))?;
568 let community_form = CommunityForm {
569 name: read_community.name,
570 title: read_community.title,
571 description: read_community.description,
572 category_id: read_community.category_id,
573 creator_id: data.user_id,
576 nsfw: read_community.nsfw,
577 updated: Some(naive_now()),
580 let _updated_community = match Community::update(&conn, data.community_id, &community_form) {
581 Ok(community) => community,
582 Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_community"))?,
585 // You also have to re-do the community_moderator table, reordering it.
586 let mut community_mods = CommunityModeratorView::for_community(&conn, data.community_id)?;
587 let creator_index = community_mods
589 .position(|r| r.user_id == data.user_id)
591 let creator_user = community_mods.remove(creator_index);
592 community_mods.insert(0, creator_user);
594 CommunityModerator::delete_for_community(&conn, data.community_id)?;
596 for cmod in &community_mods {
597 let community_moderator_form = CommunityModeratorForm {
598 community_id: cmod.community_id,
599 user_id: cmod.user_id,
602 let _inserted_community_moderator =
603 match CommunityModerator::join(&conn, &community_moderator_form) {
606 return Err(APIError::err(
608 "community_moderator_already_exists",
615 let form = ModAddCommunityForm {
616 mod_user_id: user_id,
617 other_user_id: data.user_id,
618 community_id: data.community_id,
619 removed: Some(false),
621 ModAddCommunity::create(&conn, &form)?;
623 let community_view = match CommunityView::read(&conn, data.community_id, Some(user_id)) {
624 Ok(community) => community,
625 Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?,
628 let moderators = match CommunityModeratorView::for_community(&conn, data.community_id) {
629 Ok(moderators) => moderators,
630 Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?,
634 Ok(GetCommunityResponse {
635 op: self.op.to_string(),
636 community: community_view,
637 moderators: moderators,