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