2 use actix_web::web::Data;
4 use lemmy_api_common::{
7 check_community_deleted_or_removed,
9 get_local_user_view_from_jwt,
11 remove_user_data_in_community,
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},
22 use lemmy_db_schema::{
27 CommunityFollowerForm,
29 CommunityModeratorForm,
31 CommunityPersonBanForm,
33 community_block::{CommunityBlock, CommunityBlockForm},
38 ModBanFromCommunityForm,
40 ModTransferCommunityForm,
44 traits::{Bannable, Blockable, Crud, Followable, Joinable},
46 use lemmy_db_views_actor::{
47 community_moderator_view::CommunityModeratorView,
48 community_view::CommunityView,
49 person_view::PersonViewSafe,
51 use lemmy_utils::{location_info, utils::naive_from_unix, ConnectionId, LemmyError};
52 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
54 #[async_trait::async_trait(?Send)]
55 impl Perform for FollowCommunity {
56 type Response = CommunityResponse;
58 #[tracing::instrument(skip(context, _websocket_id))]
61 context: &Data<LemmyContext>,
62 _websocket_id: Option<ConnectionId>,
63 ) -> Result<CommunityResponse, LemmyError> {
64 let data: &FollowCommunity = self;
66 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
68 let community_id = data.community_id;
69 let community: ApubCommunity = blocking(context.pool(), move |conn| {
70 Community::read(conn, community_id)
74 let community_follower_form = CommunityFollowerForm {
75 community_id: data.community_id,
76 person_id: local_user_view.person.id,
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?;
85 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
86 blocking(context.pool(), follow)
88 .map_err(LemmyError::from)
89 .map_err(|e| e.with_message("community_follower_already_exists"))?;
92 move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
93 blocking(context.pool(), unfollow)
95 .map_err(LemmyError::from)
96 .map_err(|e| e.with_message("community_follower_already_exists"))?;
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)
104 UndoFollowCommunity::send(&local_user_view.person.clone().into(), &community, context)
106 let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
107 blocking(context.pool(), unfollow)
109 .map_err(LemmyError::from)
110 .map_err(|e| e.with_message("community_follower_already_exists"))?;
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))
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;
127 Ok(CommunityResponse { community_view })
131 #[async_trait::async_trait(?Send)]
132 impl Perform for BlockCommunity {
133 type Response = BlockCommunityResponse;
135 #[tracing::instrument(skip(context, _websocket_id))]
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?;
145 let community_id = data.community_id;
146 let person_id = local_user_view.person.id;
147 let community_block_form = CommunityBlockForm {
153 let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
154 blocking(context.pool(), block)
156 .map_err(LemmyError::from)
157 .map_err(|e| e.with_message("community_block_already_exists"))?;
159 // Also, unfollow the community, and send a federated unfollow
160 let community_follower_form = CommunityFollowerForm {
161 community_id: data.community_id,
165 blocking(context.pool(), move |conn: &'_ _| {
166 CommunityFollower::unfollow(conn, &community_follower_form)
170 let community = blocking(context.pool(), move |conn| {
171 Community::read(conn, community_id)
174 UndoFollowCommunity::send(&local_user_view.person.into(), &community.into(), context).await?;
176 let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
177 blocking(context.pool(), unblock)
179 .map_err(LemmyError::from)
180 .map_err(|e| e.with_message("community_block_already_exists"))?;
183 let community_view = blocking(context.pool(), move |conn| {
184 CommunityView::read(conn, community_id, Some(person_id))
188 Ok(BlockCommunityResponse {
195 #[async_trait::async_trait(?Send)]
196 impl Perform for BanFromCommunity {
197 type Response = BanFromCommunityResponse;
199 #[tracing::instrument(skip(context, websocket_id))]
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?;
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);
214 // Verify that only mods or admins can ban
215 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
217 let community_user_ban_form = CommunityPersonBanForm {
218 community_id: data.community_id,
219 person_id: data.person_id,
220 expires: Some(expires),
223 let community: ApubCommunity = blocking(context.pool(), move |conn: &'_ _| {
224 Community::read(conn, community_id)
228 let banned_person: ApubPerson = blocking(context.pool(), move |conn: &'_ _| {
229 Person::read(conn, banned_person_id)
235 let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
236 blocking(context.pool(), ban)
238 .map_err(LemmyError::from)
239 .map_err(|e| e.with_message("community_user_already_banned"))?;
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,
247 blocking(context.pool(), move |conn: &'_ _| {
248 CommunityFollower::unfollow(conn, &community_follower_form)
254 &SiteOrCommunity::Community(community),
256 &local_user_view.person.clone().into(),
264 let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
265 blocking(context.pool(), unban)
267 .map_err(LemmyError::from)
268 .map_err(|e| e.with_message("community_user_already_banned"))?;
270 &SiteOrCommunity::Community(community),
272 &local_user_view.person.clone().into(),
279 // Remove/Restore their data if that's desired
281 remove_user_data_in_community(community_id, banned_person_id, context.pool()).await?;
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),
293 blocking(context.pool(), move |conn| {
294 ModBanFromCommunity::create(conn, &form)
298 let person_id = data.person_id;
299 let person_view = blocking(context.pool(), move |conn| {
300 PersonViewSafe::read(conn, person_id)
304 let res = BanFromCommunityResponse {
309 context.chat_server().do_send(SendCommunityRoomMessage {
310 op: UserOperation::BanFromCommunity,
311 response: res.clone(),
320 #[async_trait::async_trait(?Send)]
321 impl Perform for AddModToCommunity {
322 type Response = AddModToCommunityResponse;
324 #[tracing::instrument(skip(context, websocket_id))]
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?;
334 let community_id = data.community_id;
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)
342 if local_user_view.person.admin && !community.local {
343 return Err(LemmyError::from_message("not_a_moderator"));
346 // Update in local database
347 let community_moderator_form = CommunityModeratorForm {
348 community_id: data.community_id,
349 person_id: data.person_id,
352 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
353 blocking(context.pool(), join)
355 .map_err(LemmyError::from)
356 .map_err(|e| e.with_message("community_moderator_already_exists"))?;
358 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
359 blocking(context.pool(), leave)
361 .map_err(LemmyError::from)
362 .map_err(|e| e.with_message("community_moderator_already_exists"))?;
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),
372 blocking(context.pool(), move |conn| {
373 ModAddCommunity::create(conn, &form)
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)
384 let community: ApubCommunity = community.into();
389 &local_user_view.person.into(),
397 &local_user_view.person.into(),
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)
411 let res = AddModToCommunityResponse { moderators };
412 context.chat_server().do_send(SendCommunityRoomMessage {
413 op: UserOperation::AddModToCommunity,
414 response: res.clone(),
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;
428 #[tracing::instrument(skip(context, _websocket_id))]
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?;
438 let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
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)
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
451 .map(|a| a.person.id)
452 .any(|x| x == local_user_view.person.id)
454 return Err(LemmyError::from_message("not_an_admin"));
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
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);
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)
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,
481 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
482 blocking(context.pool(), join)
484 .map_err(LemmyError::from)
485 .map_err(|e| e.with_message("community_moderator_already_exists"))?;
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),
495 blocking(context.pool(), move |conn| {
496 ModTransferCommunity::create(conn, &form)
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))
506 .map_err(LemmyError::from)
507 .map_err(|e| e.with_message("couldnt_find_community"))?;
509 let community_id = data.community_id;
510 let moderators = blocking(context.pool(), move |conn| {
511 CommunityModeratorView::for_community(conn, community_id)
514 .map_err(LemmyError::from)
515 .map_err(|e| e.with_message("couldnt_find_community"))?;
518 Ok(GetCommunityResponse {