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(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
91 move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
92 blocking(context.pool(), unfollow)
94 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
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)
102 UndoFollowCommunity::send(&local_user_view.person.clone().into(), &community, context)
104 let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
105 blocking(context.pool(), unfollow)
107 .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
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))
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;
124 Ok(CommunityResponse { community_view })
128 #[async_trait::async_trait(?Send)]
129 impl Perform for BlockCommunity {
130 type Response = BlockCommunityResponse;
132 #[tracing::instrument(skip(context, _websocket_id))]
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?;
142 let community_id = data.community_id;
143 let person_id = local_user_view.person.id;
144 let community_block_form = CommunityBlockForm {
150 let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
151 blocking(context.pool(), block)
153 .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
155 // Also, unfollow the community, and send a federated unfollow
156 let community_follower_form = CommunityFollowerForm {
157 community_id: data.community_id,
161 blocking(context.pool(), move |conn: &'_ _| {
162 CommunityFollower::unfollow(conn, &community_follower_form)
166 let community = blocking(context.pool(), move |conn| {
167 Community::read(conn, community_id)
170 UndoFollowCommunity::send(&local_user_view.person.into(), &community.into(), context).await?;
172 let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
173 blocking(context.pool(), unblock)
175 .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
178 let community_view = blocking(context.pool(), move |conn| {
179 CommunityView::read(conn, community_id, Some(person_id))
183 Ok(BlockCommunityResponse {
190 #[async_trait::async_trait(?Send)]
191 impl Perform for BanFromCommunity {
192 type Response = BanFromCommunityResponse;
194 #[tracing::instrument(skip(context, websocket_id))]
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?;
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);
209 // Verify that only mods or admins can ban
210 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
212 let community_user_ban_form = CommunityPersonBanForm {
213 community_id: data.community_id,
214 person_id: data.person_id,
215 expires: Some(expires),
218 let community: ApubCommunity = blocking(context.pool(), move |conn: &'_ _| {
219 Community::read(conn, community_id)
223 let banned_person: ApubPerson = blocking(context.pool(), move |conn: &'_ _| {
224 Person::read(conn, banned_person_id)
230 let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
231 blocking(context.pool(), ban)
233 .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?;
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,
241 blocking(context.pool(), move |conn: &'_ _| {
242 CommunityFollower::unfollow(conn, &community_follower_form)
248 &SiteOrCommunity::Community(community),
250 &local_user_view.person.clone().into(),
258 let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
259 blocking(context.pool(), unban)
261 .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?;
263 &SiteOrCommunity::Community(community),
265 &local_user_view.person.clone().into(),
272 // Remove/Restore their data if that's desired
274 remove_user_data_in_community(community_id, banned_person_id, context.pool()).await?;
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),
286 blocking(context.pool(), move |conn| {
287 ModBanFromCommunity::create(conn, &form)
291 let person_id = data.person_id;
292 let person_view = blocking(context.pool(), move |conn| {
293 PersonViewSafe::read(conn, person_id)
297 let res = BanFromCommunityResponse {
302 context.chat_server().do_send(SendCommunityRoomMessage {
303 op: UserOperation::BanFromCommunity,
304 response: res.clone(),
313 #[async_trait::async_trait(?Send)]
314 impl Perform for AddModToCommunity {
315 type Response = AddModToCommunityResponse;
317 #[tracing::instrument(skip(context, websocket_id))]
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?;
327 let community_id = data.community_id;
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)
335 if local_user_view.person.admin && !community.local {
336 return Err(LemmyError::from_message("not_a_moderator"));
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(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
350 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
351 blocking(context.pool(), leave)
353 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
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),
363 blocking(context.pool(), move |conn| {
364 ModAddCommunity::create(conn, &form)
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)
375 let community: ApubCommunity = community.into();
380 &local_user_view.person.into(),
388 &local_user_view.person.into(),
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)
402 let res = AddModToCommunityResponse { moderators };
403 context.chat_server().do_send(SendCommunityRoomMessage {
404 op: UserOperation::AddModToCommunity,
405 response: res.clone(),
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;
419 #[tracing::instrument(skip(context, _websocket_id))]
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?;
429 let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
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)
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
442 .map(|a| a.person.id)
443 .any(|x| x == local_user_view.person.id)
445 return Err(LemmyError::from_message("not_an_admin"));
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
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);
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)
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,
472 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
473 blocking(context.pool(), join)
475 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
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),
485 blocking(context.pool(), move |conn| {
486 ModTransferCommunity::create(conn, &form)
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))
496 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
498 let community_id = data.community_id;
499 let moderators = blocking(context.pool(), move |conn| {
500 CommunityModeratorView::for_community(conn, community_id)
503 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
506 Ok(GetCommunityResponse {