]> Untitled Git - lemmy.git/blob - crates/api/src/community.rs
Removing the site creator, adding leave_admin. Fixes #1808 (#2052)
[lemmy.git] / crates / api / src / community.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use anyhow::Context;
4 use lemmy_api_common::{
5   blocking,
6   check_community_ban,
7   check_community_deleted_or_removed,
8   community::*,
9   get_local_user_view_from_jwt,
10   is_mod_or_admin,
11 };
12 use lemmy_apub::{
13   objects::{community::ApubCommunity, person::ApubPerson},
14   protocol::activities::{
15     community::{
16       add_mod::AddMod,
17       block_user::BlockUserFromCommunity,
18       remove_mod::RemoveMod,
19       undo_block_user::UndoBlockUserFromCommunity,
20     },
21     following::{follow::FollowCommunity as FollowCommunityApub, undo_follow::UndoFollowCommunity},
22   },
23 };
24 use lemmy_db_schema::{
25   source::{
26     comment::Comment,
27     community::{
28       Community,
29       CommunityFollower,
30       CommunityFollowerForm,
31       CommunityModerator,
32       CommunityModeratorForm,
33       CommunityPersonBan,
34       CommunityPersonBanForm,
35     },
36     community_block::{CommunityBlock, CommunityBlockForm},
37     moderator::{
38       ModAddCommunity,
39       ModAddCommunityForm,
40       ModBanFromCommunity,
41       ModBanFromCommunityForm,
42       ModTransferCommunity,
43       ModTransferCommunityForm,
44     },
45     person::Person,
46     post::Post,
47   },
48   traits::{Bannable, Blockable, Crud, Followable, Joinable},
49 };
50 use lemmy_db_views::comment_view::CommentQueryBuilder;
51 use lemmy_db_views_actor::{
52   community_moderator_view::CommunityModeratorView,
53   community_view::CommunityView,
54   person_view::PersonViewSafe,
55 };
56 use lemmy_utils::{location_info, utils::naive_from_unix, ConnectionId, LemmyError};
57 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
58
59 #[async_trait::async_trait(?Send)]
60 impl Perform for FollowCommunity {
61   type Response = CommunityResponse;
62
63   #[tracing::instrument(skip(context, _websocket_id))]
64   async fn perform(
65     &self,
66     context: &Data<LemmyContext>,
67     _websocket_id: Option<ConnectionId>,
68   ) -> Result<CommunityResponse, LemmyError> {
69     let data: &FollowCommunity = self;
70     let local_user_view =
71       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
72
73     let community_id = data.community_id;
74     let community: ApubCommunity = blocking(context.pool(), move |conn| {
75       Community::read(conn, community_id)
76     })
77     .await??
78     .into();
79     let community_follower_form = CommunityFollowerForm {
80       community_id: data.community_id,
81       person_id: local_user_view.person.id,
82       pending: false,
83     };
84
85     if community.local {
86       if data.follow {
87         check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
88         check_community_deleted_or_removed(community_id, context.pool()).await?;
89
90         let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
91         blocking(context.pool(), follow)
92           .await?
93           .map_err(LemmyError::from)
94           .map_err(|e| e.with_message("community_follower_already_exists"))?;
95       } else {
96         let unfollow =
97           move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
98         blocking(context.pool(), unfollow)
99           .await?
100           .map_err(LemmyError::from)
101           .map_err(|e| e.with_message("community_follower_already_exists"))?;
102       }
103     } else if data.follow {
104       // Dont actually add to the community followers here, because you need
105       // to wait for the accept
106       FollowCommunityApub::send(&local_user_view.person.clone().into(), &community, context)
107         .await?;
108     } else {
109       UndoFollowCommunity::send(&local_user_view.person.clone().into(), &community, context)
110         .await?;
111       let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
112       blocking(context.pool(), unfollow)
113         .await?
114         .map_err(LemmyError::from)
115         .map_err(|e| e.with_message("community_follower_already_exists"))?;
116     }
117
118     let community_id = data.community_id;
119     let person_id = local_user_view.person.id;
120     let mut community_view = blocking(context.pool(), move |conn| {
121       CommunityView::read(conn, community_id, Some(person_id))
122     })
123     .await??;
124
125     // TODO: this needs to return a "pending" state, until Accept is received from the remote server
126     // For now, just assume that remote follows are accepted.
127     // Otherwise, the subscribed will be null
128     if !community.local {
129       community_view.subscribed = data.follow;
130     }
131
132     Ok(CommunityResponse { community_view })
133   }
134 }
135
136 #[async_trait::async_trait(?Send)]
137 impl Perform for BlockCommunity {
138   type Response = BlockCommunityResponse;
139
140   #[tracing::instrument(skip(context, _websocket_id))]
141   async fn perform(
142     &self,
143     context: &Data<LemmyContext>,
144     _websocket_id: Option<ConnectionId>,
145   ) -> Result<BlockCommunityResponse, LemmyError> {
146     let data: &BlockCommunity = self;
147     let local_user_view =
148       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
149
150     let community_id = data.community_id;
151     let person_id = local_user_view.person.id;
152     let community_block_form = CommunityBlockForm {
153       person_id,
154       community_id,
155     };
156
157     if data.block {
158       let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
159       blocking(context.pool(), block)
160         .await?
161         .map_err(LemmyError::from)
162         .map_err(|e| e.with_message("community_block_already_exists"))?;
163
164       // Also, unfollow the community, and send a federated unfollow
165       let community_follower_form = CommunityFollowerForm {
166         community_id: data.community_id,
167         person_id,
168         pending: false,
169       };
170       blocking(context.pool(), move |conn: &'_ _| {
171         CommunityFollower::unfollow(conn, &community_follower_form)
172       })
173       .await?
174       .ok();
175       let community = blocking(context.pool(), move |conn| {
176         Community::read(conn, community_id)
177       })
178       .await??;
179       UndoFollowCommunity::send(&local_user_view.person.into(), &community.into(), context).await?;
180     } else {
181       let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
182       blocking(context.pool(), unblock)
183         .await?
184         .map_err(LemmyError::from)
185         .map_err(|e| e.with_message("community_block_already_exists"))?;
186     }
187
188     let community_view = blocking(context.pool(), move |conn| {
189       CommunityView::read(conn, community_id, Some(person_id))
190     })
191     .await??;
192
193     Ok(BlockCommunityResponse {
194       blocked: data.block,
195       community_view,
196     })
197   }
198 }
199
200 #[async_trait::async_trait(?Send)]
201 impl Perform for BanFromCommunity {
202   type Response = BanFromCommunityResponse;
203
204   #[tracing::instrument(skip(context, websocket_id))]
205   async fn perform(
206     &self,
207     context: &Data<LemmyContext>,
208     websocket_id: Option<ConnectionId>,
209   ) -> Result<BanFromCommunityResponse, LemmyError> {
210     let data: &BanFromCommunity = self;
211     let local_user_view =
212       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
213
214     let community_id = data.community_id;
215     let banned_person_id = data.person_id;
216     let expires = data.expires.map(naive_from_unix);
217
218     // Verify that only mods or admins can ban
219     is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
220
221     let community_user_ban_form = CommunityPersonBanForm {
222       community_id: data.community_id,
223       person_id: data.person_id,
224       expires: Some(expires),
225     };
226
227     let community: ApubCommunity = blocking(context.pool(), move |conn: &'_ _| {
228       Community::read(conn, community_id)
229     })
230     .await??
231     .into();
232     let banned_person: ApubPerson = blocking(context.pool(), move |conn: &'_ _| {
233       Person::read(conn, banned_person_id)
234     })
235     .await??
236     .into();
237
238     if data.ban {
239       let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
240       blocking(context.pool(), ban)
241         .await?
242         .map_err(LemmyError::from)
243         .map_err(|e| e.with_message("community_user_already_banned"))?;
244
245       // Also unsubscribe them from the community, if they are subscribed
246       let community_follower_form = CommunityFollowerForm {
247         community_id: data.community_id,
248         person_id: banned_person_id,
249         pending: false,
250       };
251       blocking(context.pool(), move |conn: &'_ _| {
252         CommunityFollower::unfollow(conn, &community_follower_form)
253       })
254       .await?
255       .ok();
256
257       BlockUserFromCommunity::send(
258         &community,
259         &banned_person,
260         &local_user_view.person.clone().into(),
261         expires,
262         context,
263       )
264       .await?;
265     } else {
266       let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
267       blocking(context.pool(), unban)
268         .await?
269         .map_err(LemmyError::from)
270         .map_err(|e| e.with_message("community_user_already_banned"))?;
271       UndoBlockUserFromCommunity::send(
272         &community,
273         &banned_person,
274         &local_user_view.person.clone().into(),
275         context,
276       )
277       .await?;
278     }
279
280     // Remove/Restore their data if that's desired
281     if data.remove_data.unwrap_or(false) {
282       // Posts
283       blocking(context.pool(), move |conn: &'_ _| {
284         Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true)
285       })
286       .await??;
287
288       // Comments
289       // TODO Diesel doesn't allow updates with joins, so this has to be a loop
290       let comments = blocking(context.pool(), move |conn| {
291         CommentQueryBuilder::create(conn)
292           .creator_id(banned_person_id)
293           .community_id(community_id)
294           .limit(std::i64::MAX)
295           .list()
296       })
297       .await??;
298
299       for comment_view in &comments {
300         let comment_id = comment_view.comment.id;
301         blocking(context.pool(), move |conn: &'_ _| {
302           Comment::update_removed(conn, comment_id, true)
303         })
304         .await??;
305       }
306     }
307
308     // Mod tables
309     let form = ModBanFromCommunityForm {
310       mod_person_id: local_user_view.person.id,
311       other_person_id: data.person_id,
312       community_id: data.community_id,
313       reason: data.reason.to_owned(),
314       banned: Some(data.ban),
315       expires,
316     };
317     blocking(context.pool(), move |conn| {
318       ModBanFromCommunity::create(conn, &form)
319     })
320     .await??;
321
322     let person_id = data.person_id;
323     let person_view = blocking(context.pool(), move |conn| {
324       PersonViewSafe::read(conn, person_id)
325     })
326     .await??;
327
328     let res = BanFromCommunityResponse {
329       person_view,
330       banned: data.ban,
331     };
332
333     context.chat_server().do_send(SendCommunityRoomMessage {
334       op: UserOperation::BanFromCommunity,
335       response: res.clone(),
336       community_id,
337       websocket_id,
338     });
339
340     Ok(res)
341   }
342 }
343
344 #[async_trait::async_trait(?Send)]
345 impl Perform for AddModToCommunity {
346   type Response = AddModToCommunityResponse;
347
348   #[tracing::instrument(skip(context, websocket_id))]
349   async fn perform(
350     &self,
351     context: &Data<LemmyContext>,
352     websocket_id: Option<ConnectionId>,
353   ) -> Result<AddModToCommunityResponse, LemmyError> {
354     let data: &AddModToCommunity = self;
355     let local_user_view =
356       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
357
358     let community_id = data.community_id;
359
360     // Verify that only mods or admins can add mod
361     is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
362
363     // Update in local database
364     let community_moderator_form = CommunityModeratorForm {
365       community_id: data.community_id,
366       person_id: data.person_id,
367     };
368     if data.added {
369       let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
370       blocking(context.pool(), join)
371         .await?
372         .map_err(LemmyError::from)
373         .map_err(|e| e.with_message("community_moderator_already_exists"))?;
374     } else {
375       let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
376       blocking(context.pool(), leave)
377         .await?
378         .map_err(LemmyError::from)
379         .map_err(|e| e.with_message("community_moderator_already_exists"))?;
380     }
381
382     // Mod tables
383     let form = ModAddCommunityForm {
384       mod_person_id: local_user_view.person.id,
385       other_person_id: data.person_id,
386       community_id: data.community_id,
387       removed: Some(!data.added),
388     };
389     blocking(context.pool(), move |conn| {
390       ModAddCommunity::create(conn, &form)
391     })
392     .await??;
393
394     // Send to federated instances
395     let updated_mod_id = data.person_id;
396     let updated_mod: ApubPerson = blocking(context.pool(), move |conn| {
397       Person::read(conn, updated_mod_id)
398     })
399     .await??
400     .into();
401     let community: ApubCommunity = blocking(context.pool(), move |conn| {
402       Community::read(conn, community_id)
403     })
404     .await??
405     .into();
406     if data.added {
407       AddMod::send(
408         &community,
409         &updated_mod,
410         &local_user_view.person.into(),
411         context,
412       )
413       .await?;
414     } else {
415       RemoveMod::send(
416         &community,
417         &updated_mod,
418         &local_user_view.person.into(),
419         context,
420       )
421       .await?;
422     }
423
424     // Note: in case a remote mod is added, this returns the old moderators list, it will only get
425     //       updated once we receive an activity from the community (like `Announce/Add/Moderator`)
426     let community_id = data.community_id;
427     let moderators = blocking(context.pool(), move |conn| {
428       CommunityModeratorView::for_community(conn, community_id)
429     })
430     .await??;
431
432     let res = AddModToCommunityResponse { moderators };
433     context.chat_server().do_send(SendCommunityRoomMessage {
434       op: UserOperation::AddModToCommunity,
435       response: res.clone(),
436       community_id,
437       websocket_id,
438     });
439     Ok(res)
440   }
441 }
442
443 // TODO: we dont do anything for federation here, it should be updated the next time the community
444 //       gets fetched. i hope we can get rid of the community creator role soon.
445 #[async_trait::async_trait(?Send)]
446 impl Perform for TransferCommunity {
447   type Response = GetCommunityResponse;
448
449   #[tracing::instrument(skip(context, _websocket_id))]
450   async fn perform(
451     &self,
452     context: &Data<LemmyContext>,
453     _websocket_id: Option<ConnectionId>,
454   ) -> Result<GetCommunityResponse, LemmyError> {
455     let data: &TransferCommunity = self;
456     let local_user_view =
457       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
458
459     let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
460
461     // Fetch the community mods
462     let community_id = data.community_id;
463     let mut community_mods = blocking(context.pool(), move |conn| {
464       CommunityModeratorView::for_community(conn, community_id)
465     })
466     .await??;
467
468     // Make sure transferrer is either the top community mod, or an admin
469     if local_user_view.person.id != community_mods[0].moderator.id
470       && !admins
471         .iter()
472         .map(|a| a.person.id)
473         .any(|x| x == local_user_view.person.id)
474     {
475       return Err(LemmyError::from_message("not_an_admin"));
476     }
477
478     // You have to re-do the community_moderator table, reordering it.
479     // Add the transferee to the top
480     let creator_index = community_mods
481       .iter()
482       .position(|r| r.moderator.id == data.person_id)
483       .context(location_info!())?;
484     let creator_person = community_mods.remove(creator_index);
485     community_mods.insert(0, creator_person);
486
487     // Delete all the mods
488     let community_id = data.community_id;
489     blocking(context.pool(), move |conn| {
490       CommunityModerator::delete_for_community(conn, community_id)
491     })
492     .await??;
493
494     // TODO: this should probably be a bulk operation
495     // Re-add the mods, in the new order
496     for cmod in &community_mods {
497       let community_moderator_form = CommunityModeratorForm {
498         community_id: cmod.community.id,
499         person_id: cmod.moderator.id,
500       };
501
502       let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
503       blocking(context.pool(), join)
504         .await?
505         .map_err(LemmyError::from)
506         .map_err(|e| e.with_message("community_moderator_already_exists"))?;
507     }
508
509     // Mod tables
510     let form = ModTransferCommunityForm {
511       mod_person_id: local_user_view.person.id,
512       other_person_id: data.person_id,
513       community_id: data.community_id,
514       removed: Some(false),
515     };
516     blocking(context.pool(), move |conn| {
517       ModTransferCommunity::create(conn, &form)
518     })
519     .await??;
520
521     let community_id = data.community_id;
522     let person_id = local_user_view.person.id;
523     let community_view = blocking(context.pool(), move |conn| {
524       CommunityView::read(conn, community_id, Some(person_id))
525     })
526     .await?
527     .map_err(LemmyError::from)
528     .map_err(|e| e.with_message("couldnt_find_community"))?;
529
530     let community_id = data.community_id;
531     let moderators = blocking(context.pool(), move |conn| {
532       CommunityModeratorView::for_community(conn, community_id)
533     })
534     .await?
535     .map_err(LemmyError::from)
536     .map_err(|e| e.with_message("couldnt_find_community"))?;
537
538     // Return the jwt
539     Ok(GetCommunityResponse {
540       community_view,
541       moderators,
542       online: 0,
543     })
544   }
545 }