2 use actix_web::web::Data;
4 use lemmy_api_common::{
7 check_community_deleted_or_removed,
9 get_local_user_view_from_jwt,
13 objects::{community::ApubCommunity, person::ApubPerson},
14 protocol::activities::{
17 block_user::BlockUserFromCommunity,
18 remove_mod::RemoveMod,
19 undo_block_user::UndoBlockUserFromCommunity,
21 following::{follow::FollowCommunity as FollowCommunityApub, undo_follow::UndoFollowCommunity},
24 use lemmy_db_schema::{
30 CommunityFollowerForm,
32 CommunityModeratorForm,
34 CommunityPersonBanForm,
36 community_block::{CommunityBlock, CommunityBlockForm},
41 ModBanFromCommunityForm,
43 ModTransferCommunityForm,
48 traits::{Bannable, Blockable, Crud, Followable, Joinable},
50 use lemmy_db_views::comment_view::CommentQueryBuilder;
51 use lemmy_db_views_actor::{
52 community_moderator_view::CommunityModeratorView,
53 community_view::CommunityView,
54 person_view::PersonViewSafe,
56 use lemmy_utils::{location_info, utils::naive_from_unix, ConnectionId, LemmyError};
57 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
59 #[async_trait::async_trait(?Send)]
60 impl Perform for FollowCommunity {
61 type Response = CommunityResponse;
63 #[tracing::instrument(skip(context, _websocket_id))]
66 context: &Data<LemmyContext>,
67 _websocket_id: Option<ConnectionId>,
68 ) -> Result<CommunityResponse, LemmyError> {
69 let data: &FollowCommunity = self;
71 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
73 let community_id = data.community_id;
74 let community: ApubCommunity = blocking(context.pool(), move |conn| {
75 Community::read(conn, community_id)
79 let community_follower_form = CommunityFollowerForm {
80 community_id: data.community_id,
81 person_id: local_user_view.person.id,
87 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
88 check_community_deleted_or_removed(community_id, context.pool()).await?;
90 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
91 blocking(context.pool(), follow)
93 .map_err(LemmyError::from)
94 .map_err(|e| e.with_message("community_follower_already_exists"))?;
97 move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
98 blocking(context.pool(), unfollow)
100 .map_err(LemmyError::from)
101 .map_err(|e| e.with_message("community_follower_already_exists"))?;
103 } else if data.follow {
104 // Dont actually add to the community followers here, because you need
105 // to wait for the accept
106 FollowCommunityApub::send(&local_user_view.person.clone().into(), &community, context)
109 UndoFollowCommunity::send(&local_user_view.person.clone().into(), &community, context)
111 let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
112 blocking(context.pool(), unfollow)
114 .map_err(LemmyError::from)
115 .map_err(|e| e.with_message("community_follower_already_exists"))?;
118 let community_id = data.community_id;
119 let person_id = local_user_view.person.id;
120 let mut community_view = blocking(context.pool(), move |conn| {
121 CommunityView::read(conn, community_id, Some(person_id))
125 // TODO: this needs to return a "pending" state, until Accept is received from the remote server
126 // For now, just assume that remote follows are accepted.
127 // Otherwise, the subscribed will be null
128 if !community.local {
129 community_view.subscribed = data.follow;
132 Ok(CommunityResponse { community_view })
136 #[async_trait::async_trait(?Send)]
137 impl Perform for BlockCommunity {
138 type Response = BlockCommunityResponse;
140 #[tracing::instrument(skip(context, _websocket_id))]
143 context: &Data<LemmyContext>,
144 _websocket_id: Option<ConnectionId>,
145 ) -> Result<BlockCommunityResponse, LemmyError> {
146 let data: &BlockCommunity = self;
147 let local_user_view =
148 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
150 let community_id = data.community_id;
151 let person_id = local_user_view.person.id;
152 let community_block_form = CommunityBlockForm {
158 let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
159 blocking(context.pool(), block)
161 .map_err(LemmyError::from)
162 .map_err(|e| e.with_message("community_block_already_exists"))?;
164 // Also, unfollow the community, and send a federated unfollow
165 let community_follower_form = CommunityFollowerForm {
166 community_id: data.community_id,
170 blocking(context.pool(), move |conn: &'_ _| {
171 CommunityFollower::unfollow(conn, &community_follower_form)
175 let community = blocking(context.pool(), move |conn| {
176 Community::read(conn, community_id)
179 UndoFollowCommunity::send(&local_user_view.person.into(), &community.into(), context).await?;
181 let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
182 blocking(context.pool(), unblock)
184 .map_err(LemmyError::from)
185 .map_err(|e| e.with_message("community_block_already_exists"))?;
188 let community_view = blocking(context.pool(), move |conn| {
189 CommunityView::read(conn, community_id, Some(person_id))
193 Ok(BlockCommunityResponse {
200 #[async_trait::async_trait(?Send)]
201 impl Perform for BanFromCommunity {
202 type Response = BanFromCommunityResponse;
204 #[tracing::instrument(skip(context, websocket_id))]
207 context: &Data<LemmyContext>,
208 websocket_id: Option<ConnectionId>,
209 ) -> Result<BanFromCommunityResponse, LemmyError> {
210 let data: &BanFromCommunity = self;
211 let local_user_view =
212 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
214 let community_id = data.community_id;
215 let banned_person_id = data.person_id;
216 let expires = data.expires.map(naive_from_unix);
218 // Verify that only mods or admins can ban
219 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
221 let community_user_ban_form = CommunityPersonBanForm {
222 community_id: data.community_id,
223 person_id: data.person_id,
224 expires: Some(expires),
227 let community: ApubCommunity = blocking(context.pool(), move |conn: &'_ _| {
228 Community::read(conn, community_id)
232 let banned_person: ApubPerson = blocking(context.pool(), move |conn: &'_ _| {
233 Person::read(conn, banned_person_id)
239 let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
240 blocking(context.pool(), ban)
242 .map_err(LemmyError::from)
243 .map_err(|e| e.with_message("community_user_already_banned"))?;
245 // Also unsubscribe them from the community, if they are subscribed
246 let community_follower_form = CommunityFollowerForm {
247 community_id: data.community_id,
248 person_id: banned_person_id,
251 blocking(context.pool(), move |conn: &'_ _| {
252 CommunityFollower::unfollow(conn, &community_follower_form)
257 BlockUserFromCommunity::send(
260 &local_user_view.person.clone().into(),
266 let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
267 blocking(context.pool(), unban)
269 .map_err(LemmyError::from)
270 .map_err(|e| e.with_message("community_user_already_banned"))?;
271 UndoBlockUserFromCommunity::send(
274 &local_user_view.person.clone().into(),
280 // Remove/Restore their data if that's desired
281 if data.remove_data.unwrap_or(false) {
283 blocking(context.pool(), move |conn: &'_ _| {
284 Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true)
289 // TODO Diesel doesn't allow updates with joins, so this has to be a loop
290 let comments = blocking(context.pool(), move |conn| {
291 CommentQueryBuilder::create(conn)
292 .creator_id(banned_person_id)
293 .community_id(community_id)
294 .limit(std::i64::MAX)
299 for comment_view in &comments {
300 let comment_id = comment_view.comment.id;
301 blocking(context.pool(), move |conn: &'_ _| {
302 Comment::update_removed(conn, comment_id, true)
309 let form = ModBanFromCommunityForm {
310 mod_person_id: local_user_view.person.id,
311 other_person_id: data.person_id,
312 community_id: data.community_id,
313 reason: data.reason.to_owned(),
314 banned: Some(data.ban),
317 blocking(context.pool(), move |conn| {
318 ModBanFromCommunity::create(conn, &form)
322 let person_id = data.person_id;
323 let person_view = blocking(context.pool(), move |conn| {
324 PersonViewSafe::read(conn, person_id)
328 let res = BanFromCommunityResponse {
333 context.chat_server().do_send(SendCommunityRoomMessage {
334 op: UserOperation::BanFromCommunity,
335 response: res.clone(),
344 #[async_trait::async_trait(?Send)]
345 impl Perform for AddModToCommunity {
346 type Response = AddModToCommunityResponse;
348 #[tracing::instrument(skip(context, websocket_id))]
351 context: &Data<LemmyContext>,
352 websocket_id: Option<ConnectionId>,
353 ) -> Result<AddModToCommunityResponse, LemmyError> {
354 let data: &AddModToCommunity = self;
355 let local_user_view =
356 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
358 let community_id = data.community_id;
360 // Verify that only mods or admins can add mod
361 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
363 // Update in local database
364 let community_moderator_form = CommunityModeratorForm {
365 community_id: data.community_id,
366 person_id: data.person_id,
369 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
370 blocking(context.pool(), join)
372 .map_err(LemmyError::from)
373 .map_err(|e| e.with_message("community_moderator_already_exists"))?;
375 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
376 blocking(context.pool(), leave)
378 .map_err(LemmyError::from)
379 .map_err(|e| e.with_message("community_moderator_already_exists"))?;
383 let form = ModAddCommunityForm {
384 mod_person_id: local_user_view.person.id,
385 other_person_id: data.person_id,
386 community_id: data.community_id,
387 removed: Some(!data.added),
389 blocking(context.pool(), move |conn| {
390 ModAddCommunity::create(conn, &form)
394 // Send to federated instances
395 let updated_mod_id = data.person_id;
396 let updated_mod: ApubPerson = blocking(context.pool(), move |conn| {
397 Person::read(conn, updated_mod_id)
401 let community: ApubCommunity = blocking(context.pool(), move |conn| {
402 Community::read(conn, community_id)
410 &local_user_view.person.into(),
418 &local_user_view.person.into(),
424 // Note: in case a remote mod is added, this returns the old moderators list, it will only get
425 // updated once we receive an activity from the community (like `Announce/Add/Moderator`)
426 let community_id = data.community_id;
427 let moderators = blocking(context.pool(), move |conn| {
428 CommunityModeratorView::for_community(conn, community_id)
432 let res = AddModToCommunityResponse { moderators };
433 context.chat_server().do_send(SendCommunityRoomMessage {
434 op: UserOperation::AddModToCommunity,
435 response: res.clone(),
443 // TODO: we dont do anything for federation here, it should be updated the next time the community
444 // gets fetched. i hope we can get rid of the community creator role soon.
445 #[async_trait::async_trait(?Send)]
446 impl Perform for TransferCommunity {
447 type Response = GetCommunityResponse;
449 #[tracing::instrument(skip(context, _websocket_id))]
452 context: &Data<LemmyContext>,
453 _websocket_id: Option<ConnectionId>,
454 ) -> Result<GetCommunityResponse, LemmyError> {
455 let data: &TransferCommunity = self;
456 let local_user_view =
457 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
459 let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
461 // Fetch the community mods
462 let community_id = data.community_id;
463 let mut community_mods = blocking(context.pool(), move |conn| {
464 CommunityModeratorView::for_community(conn, community_id)
468 // Make sure transferrer is either the top community mod, or an admin
469 if local_user_view.person.id != community_mods[0].moderator.id
472 .map(|a| a.person.id)
473 .any(|x| x == local_user_view.person.id)
475 return Err(LemmyError::from_message("not_an_admin"));
478 // You have to re-do the community_moderator table, reordering it.
479 // Add the transferee to the top
480 let creator_index = community_mods
482 .position(|r| r.moderator.id == data.person_id)
483 .context(location_info!())?;
484 let creator_person = community_mods.remove(creator_index);
485 community_mods.insert(0, creator_person);
487 // Delete all the mods
488 let community_id = data.community_id;
489 blocking(context.pool(), move |conn| {
490 CommunityModerator::delete_for_community(conn, community_id)
494 // TODO: this should probably be a bulk operation
495 // Re-add the mods, in the new order
496 for cmod in &community_mods {
497 let community_moderator_form = CommunityModeratorForm {
498 community_id: cmod.community.id,
499 person_id: cmod.moderator.id,
502 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
503 blocking(context.pool(), join)
505 .map_err(LemmyError::from)
506 .map_err(|e| e.with_message("community_moderator_already_exists"))?;
510 let form = ModTransferCommunityForm {
511 mod_person_id: local_user_view.person.id,
512 other_person_id: data.person_id,
513 community_id: data.community_id,
514 removed: Some(false),
516 blocking(context.pool(), move |conn| {
517 ModTransferCommunity::create(conn, &form)
521 let community_id = data.community_id;
522 let person_id = local_user_view.person.id;
523 let community_view = blocking(context.pool(), move |conn| {
524 CommunityView::read(conn, community_id, Some(person_id))
527 .map_err(LemmyError::from)
528 .map_err(|e| e.with_message("couldnt_find_community"))?;
530 let community_id = data.community_id;
531 let moderators = blocking(context.pool(), move |conn| {
532 CommunityModeratorView::for_community(conn, community_id)
535 .map_err(LemmyError::from)
536 .map_err(|e| e.with_message("couldnt_find_community"))?;
539 Ok(GetCommunityResponse {