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