]> Untitled Git - lemmy.git/blob - crates/api/src/community.rs
Merge pull request #1399 from LemmyNet/dont_let_banned_users_follow
[lemmy.git] / crates / api / src / community.rs
1 use crate::{
2   check_community_ban,
3   check_optional_url,
4   get_user_from_jwt,
5   get_user_from_jwt_opt,
6   is_admin,
7   is_mod_or_admin,
8   Perform,
9 };
10 use actix_web::web::Data;
11 use anyhow::Context;
12 use lemmy_apub::ActorType;
13 use lemmy_db_queries::{
14   diesel_option_overwrite,
15   source::{
16     comment::Comment_,
17     community::{CommunityModerator_, Community_},
18     post::Post_,
19   },
20   ApubObject,
21   Bannable,
22   Crud,
23   Followable,
24   Joinable,
25   ListingType,
26   SortType,
27 };
28 use lemmy_db_schema::{
29   naive_now,
30   source::{comment::Comment, community::*, moderator::*, post::Post, site::*},
31 };
32 use lemmy_db_views::comment_view::CommentQueryBuilder;
33 use lemmy_db_views_actor::{
34   community_follower_view::CommunityFollowerView,
35   community_moderator_view::CommunityModeratorView,
36   community_view::{CommunityQueryBuilder, CommunityView},
37   user_view::UserViewSafe,
38 };
39 use lemmy_structs::{blocking, community::*};
40 use lemmy_utils::{
41   apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
42   location_info,
43   utils::{check_slurs, check_slurs_opt, is_valid_community_name, naive_from_unix},
44   APIError,
45   ConnectionId,
46   LemmyError,
47 };
48 use lemmy_websocket::{
49   messages::{GetCommunityUsersOnline, SendCommunityRoomMessage},
50   LemmyContext,
51   UserOperation,
52 };
53 use std::str::FromStr;
54
55 #[async_trait::async_trait(?Send)]
56 impl Perform for GetCommunity {
57   type Response = GetCommunityResponse;
58
59   async fn perform(
60     &self,
61     context: &Data<LemmyContext>,
62     _websocket_id: Option<ConnectionId>,
63   ) -> Result<GetCommunityResponse, LemmyError> {
64     let data: &GetCommunity = &self;
65     let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
66     let user_id = user.map(|u| u.id);
67
68     let community_id = match data.id {
69       Some(id) => id,
70       None => {
71         let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
72         match blocking(context.pool(), move |conn| {
73           Community::read_from_name(conn, &name)
74         })
75         .await?
76         {
77           Ok(community) => community,
78           Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
79         }
80         .id
81       }
82     };
83
84     let community_view = match blocking(context.pool(), move |conn| {
85       CommunityView::read(conn, community_id, user_id)
86     })
87     .await?
88     {
89       Ok(community) => community,
90       Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
91     };
92
93     let moderators: Vec<CommunityModeratorView> = match blocking(context.pool(), move |conn| {
94       CommunityModeratorView::for_community(conn, community_id)
95     })
96     .await?
97     {
98       Ok(moderators) => moderators,
99       Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
100     };
101
102     let online = context
103       .chat_server()
104       .send(GetCommunityUsersOnline { community_id })
105       .await
106       .unwrap_or(1);
107
108     let res = GetCommunityResponse {
109       community_view,
110       moderators,
111       online,
112     };
113
114     // Return the jwt
115     Ok(res)
116   }
117 }
118
119 #[async_trait::async_trait(?Send)]
120 impl Perform for CreateCommunity {
121   type Response = CommunityResponse;
122
123   async fn perform(
124     &self,
125     context: &Data<LemmyContext>,
126     _websocket_id: Option<ConnectionId>,
127   ) -> Result<CommunityResponse, LemmyError> {
128     let data: &CreateCommunity = &self;
129     let user = get_user_from_jwt(&data.auth, context.pool()).await?;
130
131     check_slurs(&data.name)?;
132     check_slurs(&data.title)?;
133     check_slurs_opt(&data.description)?;
134
135     if !is_valid_community_name(&data.name) {
136       return Err(APIError::err("invalid_community_name").into());
137     }
138
139     // Double check for duplicate community actor_ids
140     let actor_id = make_apub_endpoint(EndpointType::Community, &data.name);
141     let actor_id_cloned = actor_id.to_owned();
142     let community_dupe = blocking(context.pool(), move |conn| {
143       Community::read_from_apub_id(conn, &actor_id_cloned.into())
144     })
145     .await?;
146     if community_dupe.is_ok() {
147       return Err(APIError::err("community_already_exists").into());
148     }
149
150     // Check to make sure the icon and banners are urls
151     let icon = diesel_option_overwrite(&data.icon);
152     let banner = diesel_option_overwrite(&data.banner);
153
154     check_optional_url(&icon)?;
155     check_optional_url(&banner)?;
156
157     // When you create a community, make sure the user becomes a moderator and a follower
158     let keypair = generate_actor_keypair()?;
159
160     let community_form = CommunityForm {
161       name: data.name.to_owned(),
162       title: data.title.to_owned(),
163       description: data.description.to_owned(),
164       icon,
165       banner,
166       category_id: data.category_id,
167       creator_id: user.id,
168       removed: None,
169       deleted: None,
170       nsfw: data.nsfw,
171       updated: None,
172       actor_id: Some(actor_id.into()),
173       local: true,
174       private_key: Some(keypair.private_key),
175       public_key: Some(keypair.public_key),
176       last_refreshed_at: None,
177       published: None,
178     };
179
180     let inserted_community = match blocking(context.pool(), move |conn| {
181       Community::create(conn, &community_form)
182     })
183     .await?
184     {
185       Ok(community) => community,
186       Err(_e) => return Err(APIError::err("community_already_exists").into()),
187     };
188
189     // The community creator becomes a moderator
190     let community_moderator_form = CommunityModeratorForm {
191       community_id: inserted_community.id,
192       user_id: user.id,
193     };
194
195     let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
196     if blocking(context.pool(), join).await?.is_err() {
197       return Err(APIError::err("community_moderator_already_exists").into());
198     }
199
200     // Follow your own community
201     let community_follower_form = CommunityFollowerForm {
202       community_id: inserted_community.id,
203       user_id: user.id,
204       pending: false,
205     };
206
207     let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
208     if blocking(context.pool(), follow).await?.is_err() {
209       return Err(APIError::err("community_follower_already_exists").into());
210     }
211
212     let user_id = user.id;
213     let community_view = blocking(context.pool(), move |conn| {
214       CommunityView::read(conn, inserted_community.id, Some(user_id))
215     })
216     .await??;
217
218     Ok(CommunityResponse { community_view })
219   }
220 }
221
222 #[async_trait::async_trait(?Send)]
223 impl Perform for EditCommunity {
224   type Response = CommunityResponse;
225
226   async fn perform(
227     &self,
228     context: &Data<LemmyContext>,
229     websocket_id: Option<ConnectionId>,
230   ) -> Result<CommunityResponse, LemmyError> {
231     let data: &EditCommunity = &self;
232     let user = get_user_from_jwt(&data.auth, context.pool()).await?;
233
234     check_slurs(&data.title)?;
235     check_slurs_opt(&data.description)?;
236
237     // Verify its a mod (only mods can edit it)
238     let community_id = data.community_id;
239     let mods: Vec<i32> = blocking(context.pool(), move |conn| {
240       CommunityModeratorView::for_community(conn, community_id)
241         .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
242     })
243     .await??;
244     if !mods.contains(&user.id) {
245       return Err(APIError::err("not_a_moderator").into());
246     }
247
248     let community_id = data.community_id;
249     let read_community = blocking(context.pool(), move |conn| {
250       Community::read(conn, community_id)
251     })
252     .await??;
253
254     let icon = diesel_option_overwrite(&data.icon);
255     let banner = diesel_option_overwrite(&data.banner);
256
257     check_optional_url(&icon)?;
258     check_optional_url(&banner)?;
259
260     let community_form = CommunityForm {
261       name: read_community.name,
262       title: data.title.to_owned(),
263       description: data.description.to_owned(),
264       icon,
265       banner,
266       category_id: data.category_id.to_owned(),
267       creator_id: read_community.creator_id,
268       removed: Some(read_community.removed),
269       deleted: Some(read_community.deleted),
270       nsfw: data.nsfw,
271       updated: Some(naive_now()),
272       actor_id: Some(read_community.actor_id),
273       local: read_community.local,
274       private_key: read_community.private_key,
275       public_key: read_community.public_key,
276       last_refreshed_at: None,
277       published: None,
278     };
279
280     let community_id = data.community_id;
281     match blocking(context.pool(), move |conn| {
282       Community::update(conn, community_id, &community_form)
283     })
284     .await?
285     {
286       Ok(community) => community,
287       Err(_e) => return Err(APIError::err("couldnt_update_community").into()),
288     };
289
290     // TODO there needs to be some kind of an apub update
291     // process for communities and users
292
293     let community_id = data.community_id;
294     let user_id = user.id;
295     let community_view = blocking(context.pool(), move |conn| {
296       CommunityView::read(conn, community_id, Some(user_id))
297     })
298     .await??;
299
300     let res = CommunityResponse { community_view };
301
302     send_community_websocket(&res, context, websocket_id, UserOperation::EditCommunity);
303
304     Ok(res)
305   }
306 }
307
308 #[async_trait::async_trait(?Send)]
309 impl Perform for DeleteCommunity {
310   type Response = CommunityResponse;
311
312   async fn perform(
313     &self,
314     context: &Data<LemmyContext>,
315     websocket_id: Option<ConnectionId>,
316   ) -> Result<CommunityResponse, LemmyError> {
317     let data: &DeleteCommunity = &self;
318     let user = get_user_from_jwt(&data.auth, context.pool()).await?;
319
320     // Verify its the creator (only a creator can delete the community)
321     let community_id = data.community_id;
322     let read_community = blocking(context.pool(), move |conn| {
323       Community::read(conn, community_id)
324     })
325     .await??;
326     if read_community.creator_id != user.id {
327       return Err(APIError::err("no_community_edit_allowed").into());
328     }
329
330     // Do the delete
331     let community_id = data.community_id;
332     let deleted = data.deleted;
333     let updated_community = match blocking(context.pool(), move |conn| {
334       Community::update_deleted(conn, community_id, deleted)
335     })
336     .await?
337     {
338       Ok(community) => community,
339       Err(_e) => return Err(APIError::err("couldnt_update_community").into()),
340     };
341
342     // Send apub messages
343     if deleted {
344       updated_community.send_delete(context).await?;
345     } else {
346       updated_community.send_undo_delete(context).await?;
347     }
348
349     let community_id = data.community_id;
350     let user_id = user.id;
351     let community_view = blocking(context.pool(), move |conn| {
352       CommunityView::read(conn, community_id, Some(user_id))
353     })
354     .await??;
355
356     let res = CommunityResponse { community_view };
357
358     send_community_websocket(&res, context, websocket_id, UserOperation::DeleteCommunity);
359
360     Ok(res)
361   }
362 }
363
364 #[async_trait::async_trait(?Send)]
365 impl Perform for RemoveCommunity {
366   type Response = CommunityResponse;
367
368   async fn perform(
369     &self,
370     context: &Data<LemmyContext>,
371     websocket_id: Option<ConnectionId>,
372   ) -> Result<CommunityResponse, LemmyError> {
373     let data: &RemoveCommunity = &self;
374     let user = get_user_from_jwt(&data.auth, context.pool()).await?;
375
376     // Verify its an admin (only an admin can remove a community)
377     is_admin(context.pool(), user.id).await?;
378
379     // Do the remove
380     let community_id = data.community_id;
381     let removed = data.removed;
382     let updated_community = match blocking(context.pool(), move |conn| {
383       Community::update_removed(conn, community_id, removed)
384     })
385     .await?
386     {
387       Ok(community) => community,
388       Err(_e) => return Err(APIError::err("couldnt_update_community").into()),
389     };
390
391     // Mod tables
392     let expires = match data.expires {
393       Some(time) => Some(naive_from_unix(time)),
394       None => None,
395     };
396     let form = ModRemoveCommunityForm {
397       mod_user_id: user.id,
398       community_id: data.community_id,
399       removed: Some(removed),
400       reason: data.reason.to_owned(),
401       expires,
402     };
403     blocking(context.pool(), move |conn| {
404       ModRemoveCommunity::create(conn, &form)
405     })
406     .await??;
407
408     // Apub messages
409     if removed {
410       updated_community.send_remove(context).await?;
411     } else {
412       updated_community.send_undo_remove(context).await?;
413     }
414
415     let community_id = data.community_id;
416     let user_id = user.id;
417     let community_view = blocking(context.pool(), move |conn| {
418       CommunityView::read(conn, community_id, Some(user_id))
419     })
420     .await??;
421
422     let res = CommunityResponse { community_view };
423
424     send_community_websocket(&res, context, websocket_id, UserOperation::RemoveCommunity);
425
426     Ok(res)
427   }
428 }
429
430 #[async_trait::async_trait(?Send)]
431 impl Perform for ListCommunities {
432   type Response = ListCommunitiesResponse;
433
434   async fn perform(
435     &self,
436     context: &Data<LemmyContext>,
437     _websocket_id: Option<ConnectionId>,
438   ) -> Result<ListCommunitiesResponse, LemmyError> {
439     let data: &ListCommunities = &self;
440     let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
441
442     let user_id = match &user {
443       Some(user) => Some(user.id),
444       None => None,
445     };
446
447     let show_nsfw = match &user {
448       Some(user) => user.show_nsfw,
449       None => false,
450     };
451
452     let type_ = ListingType::from_str(&data.type_)?;
453     let sort = SortType::from_str(&data.sort)?;
454
455     let page = data.page;
456     let limit = data.limit;
457     let communities = blocking(context.pool(), move |conn| {
458       CommunityQueryBuilder::create(conn)
459         .listing_type(&type_)
460         .sort(&sort)
461         .show_nsfw(show_nsfw)
462         .my_user_id(user_id)
463         .page(page)
464         .limit(limit)
465         .list()
466     })
467     .await??;
468
469     // Return the jwt
470     Ok(ListCommunitiesResponse { communities })
471   }
472 }
473
474 #[async_trait::async_trait(?Send)]
475 impl Perform for FollowCommunity {
476   type Response = CommunityResponse;
477
478   async fn perform(
479     &self,
480     context: &Data<LemmyContext>,
481     _websocket_id: Option<ConnectionId>,
482   ) -> Result<CommunityResponse, LemmyError> {
483     let data: &FollowCommunity = &self;
484     let user = get_user_from_jwt(&data.auth, context.pool()).await?;
485
486     let community_id = data.community_id;
487     let community = blocking(context.pool(), move |conn| {
488       Community::read(conn, community_id)
489     })
490     .await??;
491     let community_follower_form = CommunityFollowerForm {
492       community_id: data.community_id,
493       user_id: user.id,
494       pending: false,
495     };
496
497     if community.local {
498       if data.follow {
499         check_community_ban(user.id, community_id, context.pool()).await?;
500
501         let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
502         if blocking(context.pool(), follow).await?.is_err() {
503           return Err(APIError::err("community_follower_already_exists").into());
504         }
505       } else {
506         let unfollow =
507           move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
508         if blocking(context.pool(), unfollow).await?.is_err() {
509           return Err(APIError::err("community_follower_already_exists").into());
510         }
511       }
512     } else if data.follow {
513       // Dont actually add to the community followers here, because you need
514       // to wait for the accept
515       user.send_follow(&community.actor_id(), context).await?;
516     } else {
517       user.send_unfollow(&community.actor_id(), context).await?;
518       let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
519       if blocking(context.pool(), unfollow).await?.is_err() {
520         return Err(APIError::err("community_follower_already_exists").into());
521       }
522     }
523
524     let community_id = data.community_id;
525     let user_id = user.id;
526     let mut community_view = blocking(context.pool(), move |conn| {
527       CommunityView::read(conn, community_id, Some(user_id))
528     })
529     .await??;
530
531     // TODO: this needs to return a "pending" state, until Accept is received from the remote server
532     // For now, just assume that remote follows are accepted.
533     // Otherwise, the subscribed will be null
534     if !community.local {
535       community_view.subscribed = data.follow;
536     }
537
538     Ok(CommunityResponse { community_view })
539   }
540 }
541
542 #[async_trait::async_trait(?Send)]
543 impl Perform for GetFollowedCommunities {
544   type Response = GetFollowedCommunitiesResponse;
545
546   async fn perform(
547     &self,
548     context: &Data<LemmyContext>,
549     _websocket_id: Option<ConnectionId>,
550   ) -> Result<GetFollowedCommunitiesResponse, LemmyError> {
551     let data: &GetFollowedCommunities = &self;
552     let user = get_user_from_jwt(&data.auth, context.pool()).await?;
553
554     let user_id = user.id;
555     let communities = match blocking(context.pool(), move |conn| {
556       CommunityFollowerView::for_user(conn, user_id)
557     })
558     .await?
559     {
560       Ok(communities) => communities,
561       _ => return Err(APIError::err("system_err_login").into()),
562     };
563
564     // Return the jwt
565     Ok(GetFollowedCommunitiesResponse { communities })
566   }
567 }
568
569 #[async_trait::async_trait(?Send)]
570 impl Perform for BanFromCommunity {
571   type Response = BanFromCommunityResponse;
572
573   async fn perform(
574     &self,
575     context: &Data<LemmyContext>,
576     websocket_id: Option<ConnectionId>,
577   ) -> Result<BanFromCommunityResponse, LemmyError> {
578     let data: &BanFromCommunity = &self;
579     let user = get_user_from_jwt(&data.auth, context.pool()).await?;
580
581     let community_id = data.community_id;
582     let banned_user_id = data.user_id;
583
584     // Verify that only mods or admins can ban
585     is_mod_or_admin(context.pool(), user.id, community_id).await?;
586
587     let community_user_ban_form = CommunityUserBanForm {
588       community_id: data.community_id,
589       user_id: data.user_id,
590     };
591
592     if data.ban {
593       let ban = move |conn: &'_ _| CommunityUserBan::ban(conn, &community_user_ban_form);
594       if blocking(context.pool(), ban).await?.is_err() {
595         return Err(APIError::err("community_user_already_banned").into());
596       }
597
598       // Also unsubscribe them from the community, if they are subscribed
599       let community_follower_form = CommunityFollowerForm {
600         community_id: data.community_id,
601         user_id: banned_user_id,
602         pending: false,
603       };
604       blocking(context.pool(), move |conn: &'_ _| {
605         CommunityFollower::unfollow(conn, &community_follower_form)
606       })
607       .await?
608       .ok();
609     } else {
610       let unban = move |conn: &'_ _| CommunityUserBan::unban(conn, &community_user_ban_form);
611       if blocking(context.pool(), unban).await?.is_err() {
612         return Err(APIError::err("community_user_already_banned").into());
613       }
614     }
615
616     // Remove/Restore their data if that's desired
617     if data.remove_data {
618       // Posts
619       blocking(context.pool(), move |conn: &'_ _| {
620         Post::update_removed_for_creator(conn, banned_user_id, Some(community_id), true)
621       })
622       .await??;
623
624       // Comments
625       // TODO Diesel doesn't allow updates with joins, so this has to be a loop
626       let comments = blocking(context.pool(), move |conn| {
627         CommentQueryBuilder::create(conn)
628           .creator_id(banned_user_id)
629           .community_id(community_id)
630           .limit(std::i64::MAX)
631           .list()
632       })
633       .await??;
634
635       for comment_view in &comments {
636         let comment_id = comment_view.comment.id;
637         blocking(context.pool(), move |conn: &'_ _| {
638           Comment::update_removed(conn, comment_id, true)
639         })
640         .await??;
641       }
642     }
643
644     // Mod tables
645     // TODO eventually do correct expires
646     let expires = match data.expires {
647       Some(time) => Some(naive_from_unix(time)),
648       None => None,
649     };
650
651     let form = ModBanFromCommunityForm {
652       mod_user_id: user.id,
653       other_user_id: data.user_id,
654       community_id: data.community_id,
655       reason: data.reason.to_owned(),
656       banned: Some(data.ban),
657       expires,
658     };
659     blocking(context.pool(), move |conn| {
660       ModBanFromCommunity::create(conn, &form)
661     })
662     .await??;
663
664     let user_id = data.user_id;
665     let user_view = blocking(context.pool(), move |conn| {
666       UserViewSafe::read(conn, user_id)
667     })
668     .await??;
669
670     let res = BanFromCommunityResponse {
671       user_view,
672       banned: data.ban,
673     };
674
675     context.chat_server().do_send(SendCommunityRoomMessage {
676       op: UserOperation::BanFromCommunity,
677       response: res.clone(),
678       community_id,
679       websocket_id,
680     });
681
682     Ok(res)
683   }
684 }
685
686 #[async_trait::async_trait(?Send)]
687 impl Perform for AddModToCommunity {
688   type Response = AddModToCommunityResponse;
689
690   async fn perform(
691     &self,
692     context: &Data<LemmyContext>,
693     websocket_id: Option<ConnectionId>,
694   ) -> Result<AddModToCommunityResponse, LemmyError> {
695     let data: &AddModToCommunity = &self;
696     let user = get_user_from_jwt(&data.auth, context.pool()).await?;
697
698     let community_moderator_form = CommunityModeratorForm {
699       community_id: data.community_id,
700       user_id: data.user_id,
701     };
702
703     let community_id = data.community_id;
704
705     // Verify that only mods or admins can add mod
706     is_mod_or_admin(context.pool(), user.id, community_id).await?;
707
708     if data.added {
709       let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
710       if blocking(context.pool(), join).await?.is_err() {
711         return Err(APIError::err("community_moderator_already_exists").into());
712       }
713     } else {
714       let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
715       if blocking(context.pool(), leave).await?.is_err() {
716         return Err(APIError::err("community_moderator_already_exists").into());
717       }
718     }
719
720     // Mod tables
721     let form = ModAddCommunityForm {
722       mod_user_id: user.id,
723       other_user_id: data.user_id,
724       community_id: data.community_id,
725       removed: Some(!data.added),
726     };
727     blocking(context.pool(), move |conn| {
728       ModAddCommunity::create(conn, &form)
729     })
730     .await??;
731
732     let community_id = data.community_id;
733     let moderators = blocking(context.pool(), move |conn| {
734       CommunityModeratorView::for_community(conn, community_id)
735     })
736     .await??;
737
738     let res = AddModToCommunityResponse { moderators };
739
740     context.chat_server().do_send(SendCommunityRoomMessage {
741       op: UserOperation::AddModToCommunity,
742       response: res.clone(),
743       community_id,
744       websocket_id,
745     });
746
747     Ok(res)
748   }
749 }
750
751 #[async_trait::async_trait(?Send)]
752 impl Perform for TransferCommunity {
753   type Response = GetCommunityResponse;
754
755   async fn perform(
756     &self,
757     context: &Data<LemmyContext>,
758     _websocket_id: Option<ConnectionId>,
759   ) -> Result<GetCommunityResponse, LemmyError> {
760     let data: &TransferCommunity = &self;
761     let user = get_user_from_jwt(&data.auth, context.pool()).await?;
762
763     let community_id = data.community_id;
764     let read_community = blocking(context.pool(), move |conn| {
765       Community::read(conn, community_id)
766     })
767     .await??;
768
769     let site_creator_id = blocking(context.pool(), move |conn| {
770       Site::read(conn, 1).map(|s| s.creator_id)
771     })
772     .await??;
773
774     let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
775
776     // Making sure the creator, if an admin, is at the top
777     let creator_index = admins
778       .iter()
779       .position(|r| r.user.id == site_creator_id)
780       .context(location_info!())?;
781     let creator_user = admins.remove(creator_index);
782     admins.insert(0, creator_user);
783
784     // Make sure user is the creator, or an admin
785     if user.id != read_community.creator_id
786       && !admins.iter().map(|a| a.user.id).any(|x| x == user.id)
787     {
788       return Err(APIError::err("not_an_admin").into());
789     }
790
791     let community_id = data.community_id;
792     let new_creator = data.user_id;
793     let update = move |conn: &'_ _| Community::update_creator(conn, community_id, new_creator);
794     if blocking(context.pool(), update).await?.is_err() {
795       return Err(APIError::err("couldnt_update_community").into());
796     };
797
798     // You also have to re-do the community_moderator table, reordering it.
799     let community_id = data.community_id;
800     let mut community_mods = blocking(context.pool(), move |conn| {
801       CommunityModeratorView::for_community(conn, community_id)
802     })
803     .await??;
804     let creator_index = community_mods
805       .iter()
806       .position(|r| r.moderator.id == data.user_id)
807       .context(location_info!())?;
808     let creator_user = community_mods.remove(creator_index);
809     community_mods.insert(0, creator_user);
810
811     let community_id = data.community_id;
812     blocking(context.pool(), move |conn| {
813       CommunityModerator::delete_for_community(conn, community_id)
814     })
815     .await??;
816
817     // TODO: this should probably be a bulk operation
818     for cmod in &community_mods {
819       let community_moderator_form = CommunityModeratorForm {
820         community_id: cmod.community.id,
821         user_id: cmod.moderator.id,
822       };
823
824       let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
825       if blocking(context.pool(), join).await?.is_err() {
826         return Err(APIError::err("community_moderator_already_exists").into());
827       }
828     }
829
830     // Mod tables
831     let form = ModAddCommunityForm {
832       mod_user_id: user.id,
833       other_user_id: data.user_id,
834       community_id: data.community_id,
835       removed: Some(false),
836     };
837     blocking(context.pool(), move |conn| {
838       ModAddCommunity::create(conn, &form)
839     })
840     .await??;
841
842     let community_id = data.community_id;
843     let user_id = user.id;
844     let community_view = match blocking(context.pool(), move |conn| {
845       CommunityView::read(conn, community_id, Some(user_id))
846     })
847     .await?
848     {
849       Ok(community) => community,
850       Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
851     };
852
853     let community_id = data.community_id;
854     let moderators = match blocking(context.pool(), move |conn| {
855       CommunityModeratorView::for_community(conn, community_id)
856     })
857     .await?
858     {
859       Ok(moderators) => moderators,
860       Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
861     };
862
863     // Return the jwt
864     Ok(GetCommunityResponse {
865       community_view,
866       moderators,
867       online: 0,
868     })
869   }
870 }
871
872 fn send_community_websocket(
873   res: &CommunityResponse,
874   context: &Data<LemmyContext>,
875   websocket_id: Option<ConnectionId>,
876   op: UserOperation,
877 ) {
878   // Strip out the user id and subscribed when sending to others
879   let mut res_sent = res.clone();
880   res_sent.community_view.subscribed = false;
881
882   context.chat_server().do_send(SendCommunityRoomMessage {
883     op,
884     response: res_sent,
885     community_id: res.community_view.community.id,
886     websocket_id,
887   });
888 }