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