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