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?;
339 // Update in local database
340 let community_moderator_form = CommunityModeratorForm {
341 community_id: data.community_id,
342 person_id: data.person_id,
345 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
346 blocking(context.pool(), join)
348 .map_err(LemmyError::from)
349 .map_err(|e| e.with_message("community_moderator_already_exists"))?;
351 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
352 blocking(context.pool(), leave)
354 .map_err(LemmyError::from)
355 .map_err(|e| e.with_message("community_moderator_already_exists"))?;
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),
365 blocking(context.pool(), move |conn| {
366 ModAddCommunity::create(conn, &form)
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)
377 let community: ApubCommunity = blocking(context.pool(), move |conn| {
378 Community::read(conn, community_id)
386 &local_user_view.person.into(),
394 &local_user_view.person.into(),
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)
408 let res = AddModToCommunityResponse { moderators };
409 context.chat_server().do_send(SendCommunityRoomMessage {
410 op: UserOperation::AddModToCommunity,
411 response: res.clone(),
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;
425 #[tracing::instrument(skip(context, _websocket_id))]
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?;
435 let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
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)
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
448 .map(|a| a.person.id)
449 .any(|x| x == local_user_view.person.id)
451 return Err(LemmyError::from_message("not_an_admin"));
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
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);
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)
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,
478 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
479 blocking(context.pool(), join)
481 .map_err(LemmyError::from)
482 .map_err(|e| e.with_message("community_moderator_already_exists"))?;
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),
492 blocking(context.pool(), move |conn| {
493 ModTransferCommunity::create(conn, &form)
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))
503 .map_err(LemmyError::from)
504 .map_err(|e| e.with_message("couldnt_find_community"))?;
506 let community_id = data.community_id;
507 let moderators = blocking(context.pool(), move |conn| {
508 CommunityModeratorView::for_community(conn, community_id)
511 .map_err(LemmyError::from)
512 .map_err(|e| e.with_message("couldnt_find_community"))?;
515 Ok(GetCommunityResponse {