]> Untitled Git - lemmy.git/blob - crates/api/src/community.rs
d2e149448a4b60a2aab15f5e1708bc22e4c2bdac
[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     let community = blocking(context.pool(), move |conn| {
339       Community::read(conn, community_id)
340     })
341     .await??;
342     if local_user_view.person.admin && !community.local {
343       return Err(LemmyError::from_message("not_a_moderator"));
344     }
345
346     // Update in local database
347     let community_moderator_form = CommunityModeratorForm {
348       community_id: data.community_id,
349       person_id: data.person_id,
350     };
351     if data.added {
352       let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
353       blocking(context.pool(), join)
354         .await?
355         .map_err(LemmyError::from)
356         .map_err(|e| e.with_message("community_moderator_already_exists"))?;
357     } else {
358       let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
359       blocking(context.pool(), leave)
360         .await?
361         .map_err(LemmyError::from)
362         .map_err(|e| e.with_message("community_moderator_already_exists"))?;
363     }
364
365     // Mod tables
366     let form = ModAddCommunityForm {
367       mod_person_id: local_user_view.person.id,
368       other_person_id: data.person_id,
369       community_id: data.community_id,
370       removed: Some(!data.added),
371     };
372     blocking(context.pool(), move |conn| {
373       ModAddCommunity::create(conn, &form)
374     })
375     .await??;
376
377     // Send to federated instances
378     let updated_mod_id = data.person_id;
379     let updated_mod: ApubPerson = blocking(context.pool(), move |conn| {
380       Person::read(conn, updated_mod_id)
381     })
382     .await??
383     .into();
384     let community: ApubCommunity = community.into();
385     if data.added {
386       AddMod::send(
387         &community,
388         &updated_mod,
389         &local_user_view.person.into(),
390         context,
391       )
392       .await?;
393     } else {
394       RemoveMod::send(
395         &community,
396         &updated_mod,
397         &local_user_view.person.into(),
398         context,
399       )
400       .await?;
401     }
402
403     // Note: in case a remote mod is added, this returns the old moderators list, it will only get
404     //       updated once we receive an activity from the community (like `Announce/Add/Moderator`)
405     let community_id = data.community_id;
406     let moderators = blocking(context.pool(), move |conn| {
407       CommunityModeratorView::for_community(conn, community_id)
408     })
409     .await??;
410
411     let res = AddModToCommunityResponse { moderators };
412     context.chat_server().do_send(SendCommunityRoomMessage {
413       op: UserOperation::AddModToCommunity,
414       response: res.clone(),
415       community_id,
416       websocket_id,
417     });
418     Ok(res)
419   }
420 }
421
422 // TODO: we dont do anything for federation here, it should be updated the next time the community
423 //       gets fetched. i hope we can get rid of the community creator role soon.
424 #[async_trait::async_trait(?Send)]
425 impl Perform for TransferCommunity {
426   type Response = GetCommunityResponse;
427
428   #[tracing::instrument(skip(context, _websocket_id))]
429   async fn perform(
430     &self,
431     context: &Data<LemmyContext>,
432     _websocket_id: Option<ConnectionId>,
433   ) -> Result<GetCommunityResponse, LemmyError> {
434     let data: &TransferCommunity = self;
435     let local_user_view =
436       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
437
438     let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
439
440     // Fetch the community mods
441     let community_id = data.community_id;
442     let mut community_mods = blocking(context.pool(), move |conn| {
443       CommunityModeratorView::for_community(conn, community_id)
444     })
445     .await??;
446
447     // Make sure transferrer is either the top community mod, or an admin
448     if local_user_view.person.id != community_mods[0].moderator.id
449       && !admins
450         .iter()
451         .map(|a| a.person.id)
452         .any(|x| x == local_user_view.person.id)
453     {
454       return Err(LemmyError::from_message("not_an_admin"));
455     }
456
457     // You have to re-do the community_moderator table, reordering it.
458     // Add the transferee to the top
459     let creator_index = community_mods
460       .iter()
461       .position(|r| r.moderator.id == data.person_id)
462       .context(location_info!())?;
463     let creator_person = community_mods.remove(creator_index);
464     community_mods.insert(0, creator_person);
465
466     // Delete all the mods
467     let community_id = data.community_id;
468     blocking(context.pool(), move |conn| {
469       CommunityModerator::delete_for_community(conn, community_id)
470     })
471     .await??;
472
473     // TODO: this should probably be a bulk operation
474     // Re-add the mods, in the new order
475     for cmod in &community_mods {
476       let community_moderator_form = CommunityModeratorForm {
477         community_id: cmod.community.id,
478         person_id: cmod.moderator.id,
479       };
480
481       let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
482       blocking(context.pool(), join)
483         .await?
484         .map_err(LemmyError::from)
485         .map_err(|e| e.with_message("community_moderator_already_exists"))?;
486     }
487
488     // Mod tables
489     let form = ModTransferCommunityForm {
490       mod_person_id: local_user_view.person.id,
491       other_person_id: data.person_id,
492       community_id: data.community_id,
493       removed: Some(false),
494     };
495     blocking(context.pool(), move |conn| {
496       ModTransferCommunity::create(conn, &form)
497     })
498     .await??;
499
500     let community_id = data.community_id;
501     let person_id = local_user_view.person.id;
502     let community_view = blocking(context.pool(), move |conn| {
503       CommunityView::read(conn, community_id, Some(person_id))
504     })
505     .await?
506     .map_err(LemmyError::from)
507     .map_err(|e| e.with_message("couldnt_find_community"))?;
508
509     let community_id = data.community_id;
510     let moderators = blocking(context.pool(), move |conn| {
511       CommunityModeratorView::for_community(conn, community_id)
512     })
513     .await?
514     .map_err(LemmyError::from)
515     .map_err(|e| e.with_message("couldnt_find_community"))?;
516
517     // Return the jwt
518     Ok(GetCommunityResponse {
519       community_view,
520       moderators,
521       online: 0,
522     })
523   }
524 }