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,
49 traits::{Bannable, Blockable, Crud, Followable, Joinable},
51 use lemmy_db_views::comment_view::CommentQueryBuilder;
52 use lemmy_db_views_actor::{
53 community_moderator_view::CommunityModeratorView,
54 community_view::CommunityView,
55 person_view::PersonViewSafe,
57 use lemmy_utils::{location_info, utils::naive_from_unix, ConnectionId, LemmyError};
58 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
60 #[async_trait::async_trait(?Send)]
61 impl Perform for FollowCommunity {
62 type Response = CommunityResponse;
64 #[tracing::instrument(skip(context, _websocket_id))]
67 context: &Data<LemmyContext>,
68 _websocket_id: Option<ConnectionId>,
69 ) -> Result<CommunityResponse, LemmyError> {
70 let data: &FollowCommunity = self;
72 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
74 let community_id = data.community_id;
75 let community: ApubCommunity = blocking(context.pool(), move |conn| {
76 Community::read(conn, community_id)
80 let community_follower_form = CommunityFollowerForm {
81 community_id: data.community_id,
82 person_id: local_user_view.person.id,
88 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
89 check_community_deleted_or_removed(community_id, context.pool()).await?;
91 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
92 blocking(context.pool(), follow)
94 .map_err(LemmyError::from)
95 .map_err(|e| e.with_message("community_follower_already_exists"))?;
98 move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
99 blocking(context.pool(), unfollow)
101 .map_err(LemmyError::from)
102 .map_err(|e| e.with_message("community_follower_already_exists"))?;
104 } else if data.follow {
105 // Dont actually add to the community followers here, because you need
106 // to wait for the accept
107 FollowCommunityApub::send(&local_user_view.person.clone().into(), &community, context)
110 UndoFollowCommunity::send(&local_user_view.person.clone().into(), &community, context)
112 let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
113 blocking(context.pool(), unfollow)
115 .map_err(LemmyError::from)
116 .map_err(|e| e.with_message("community_follower_already_exists"))?;
119 let community_id = data.community_id;
120 let person_id = local_user_view.person.id;
121 let mut community_view = blocking(context.pool(), move |conn| {
122 CommunityView::read(conn, community_id, Some(person_id))
126 // TODO: this needs to return a "pending" state, until Accept is received from the remote server
127 // For now, just assume that remote follows are accepted.
128 // Otherwise, the subscribed will be null
129 if !community.local {
130 community_view.subscribed = data.follow;
133 Ok(CommunityResponse { community_view })
137 #[async_trait::async_trait(?Send)]
138 impl Perform for BlockCommunity {
139 type Response = BlockCommunityResponse;
141 #[tracing::instrument(skip(context, _websocket_id))]
144 context: &Data<LemmyContext>,
145 _websocket_id: Option<ConnectionId>,
146 ) -> Result<BlockCommunityResponse, LemmyError> {
147 let data: &BlockCommunity = self;
148 let local_user_view =
149 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
151 let community_id = data.community_id;
152 let person_id = local_user_view.person.id;
153 let community_block_form = CommunityBlockForm {
159 let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
160 blocking(context.pool(), block)
162 .map_err(LemmyError::from)
163 .map_err(|e| e.with_message("community_block_already_exists"))?;
165 // Also, unfollow the community, and send a federated unfollow
166 let community_follower_form = CommunityFollowerForm {
167 community_id: data.community_id,
171 blocking(context.pool(), move |conn: &'_ _| {
172 CommunityFollower::unfollow(conn, &community_follower_form)
176 let community = blocking(context.pool(), move |conn| {
177 Community::read(conn, community_id)
180 UndoFollowCommunity::send(&local_user_view.person.into(), &community.into(), context).await?;
182 let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
183 blocking(context.pool(), unblock)
185 .map_err(LemmyError::from)
186 .map_err(|e| e.with_message("community_block_already_exists"))?;
189 let community_view = blocking(context.pool(), move |conn| {
190 CommunityView::read(conn, community_id, Some(person_id))
194 Ok(BlockCommunityResponse {
201 #[async_trait::async_trait(?Send)]
202 impl Perform for BanFromCommunity {
203 type Response = BanFromCommunityResponse;
205 #[tracing::instrument(skip(context, websocket_id))]
208 context: &Data<LemmyContext>,
209 websocket_id: Option<ConnectionId>,
210 ) -> Result<BanFromCommunityResponse, LemmyError> {
211 let data: &BanFromCommunity = self;
212 let local_user_view =
213 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
215 let community_id = data.community_id;
216 let banned_person_id = data.person_id;
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,
226 let community: ApubCommunity = blocking(context.pool(), move |conn: &'_ _| {
227 Community::read(conn, community_id)
231 let banned_person: ApubPerson = blocking(context.pool(), move |conn: &'_ _| {
232 Person::read(conn, banned_person_id)
238 let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
239 blocking(context.pool(), ban)
241 .map_err(LemmyError::from)
242 .map_err(|e| e.with_message("community_user_already_banned"))?;
244 // Also unsubscribe them from the community, if they are subscribed
245 let community_follower_form = CommunityFollowerForm {
246 community_id: data.community_id,
247 person_id: banned_person_id,
250 blocking(context.pool(), move |conn: &'_ _| {
251 CommunityFollower::unfollow(conn, &community_follower_form)
256 BlockUserFromCommunity::send(
259 &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"))?;
269 UndoBlockUserFromCommunity::send(
272 &local_user_view.person.clone().into(),
278 // Remove/Restore their data if that's desired
279 if data.remove_data.unwrap_or(false) {
281 blocking(context.pool(), move |conn: &'_ _| {
282 Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true)
287 // TODO Diesel doesn't allow updates with joins, so this has to be a loop
288 let comments = blocking(context.pool(), move |conn| {
289 CommentQueryBuilder::create(conn)
290 .creator_id(banned_person_id)
291 .community_id(community_id)
292 .limit(std::i64::MAX)
297 for comment_view in &comments {
298 let comment_id = comment_view.comment.id;
299 blocking(context.pool(), move |conn: &'_ _| {
300 Comment::update_removed(conn, comment_id, true)
307 // TODO eventually do correct expires
308 let expires = data.expires.map(naive_from_unix);
310 let form = ModBanFromCommunityForm {
311 mod_person_id: local_user_view.person.id,
312 other_person_id: data.person_id,
313 community_id: data.community_id,
314 reason: data.reason.to_owned(),
315 banned: Some(data.ban),
318 blocking(context.pool(), move |conn| {
319 ModBanFromCommunity::create(conn, &form)
323 let person_id = data.person_id;
324 let person_view = blocking(context.pool(), move |conn| {
325 PersonViewSafe::read(conn, person_id)
329 let res = BanFromCommunityResponse {
334 context.chat_server().do_send(SendCommunityRoomMessage {
335 op: UserOperation::BanFromCommunity,
336 response: res.clone(),
345 #[async_trait::async_trait(?Send)]
346 impl Perform for AddModToCommunity {
347 type Response = AddModToCommunityResponse;
349 #[tracing::instrument(skip(context, websocket_id))]
352 context: &Data<LemmyContext>,
353 websocket_id: Option<ConnectionId>,
354 ) -> Result<AddModToCommunityResponse, LemmyError> {
355 let data: &AddModToCommunity = self;
356 let local_user_view =
357 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
359 let community_id = data.community_id;
361 // Verify that only mods or admins can add mod
362 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
364 // Update in local database
365 let community_moderator_form = CommunityModeratorForm {
366 community_id: data.community_id,
367 person_id: data.person_id,
370 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
371 blocking(context.pool(), join)
373 .map_err(LemmyError::from)
374 .map_err(|e| e.with_message("community_moderator_already_exists"))?;
376 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
377 blocking(context.pool(), leave)
379 .map_err(LemmyError::from)
380 .map_err(|e| e.with_message("community_moderator_already_exists"))?;
384 let form = ModAddCommunityForm {
385 mod_person_id: local_user_view.person.id,
386 other_person_id: data.person_id,
387 community_id: data.community_id,
388 removed: Some(!data.added),
390 blocking(context.pool(), move |conn| {
391 ModAddCommunity::create(conn, &form)
395 // Send to federated instances
396 let updated_mod_id = data.person_id;
397 let updated_mod: ApubPerson = blocking(context.pool(), move |conn| {
398 Person::read(conn, updated_mod_id)
402 let community: ApubCommunity = blocking(context.pool(), move |conn| {
403 Community::read(conn, community_id)
411 &local_user_view.person.into(),
419 &local_user_view.person.into(),
425 // Note: in case a remote mod is added, this returns the old moderators list, it will only get
426 // updated once we receive an activity from the community (like `Announce/Add/Moderator`)
427 let community_id = data.community_id;
428 let moderators = blocking(context.pool(), move |conn| {
429 CommunityModeratorView::for_community(conn, community_id)
433 let res = AddModToCommunityResponse { moderators };
434 context.chat_server().do_send(SendCommunityRoomMessage {
435 op: UserOperation::AddModToCommunity,
436 response: res.clone(),
444 // TODO: we dont do anything for federation here, it should be updated the next time the community
445 // gets fetched. i hope we can get rid of the community creator role soon.
446 #[async_trait::async_trait(?Send)]
447 impl Perform for TransferCommunity {
448 type Response = GetCommunityResponse;
450 #[tracing::instrument(skip(context, _websocket_id))]
453 context: &Data<LemmyContext>,
454 _websocket_id: Option<ConnectionId>,
455 ) -> Result<GetCommunityResponse, LemmyError> {
456 let data: &TransferCommunity = self;
457 let local_user_view =
458 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
460 let site_creator_id = blocking(context.pool(), move |conn| {
461 Site::read(conn, 1).map(|s| s.creator_id)
465 let mut admins = blocking(context.pool(), PersonViewSafe::admins).await??;
467 // Making sure the site creator, if an admin, is at the top
468 let creator_index = admins
470 .position(|r| r.person.id == site_creator_id)
471 .context(location_info!())?;
472 let creator_person = admins.remove(creator_index);
473 admins.insert(0, creator_person);
475 // Fetch the community mods
476 let community_id = data.community_id;
477 let mut community_mods = blocking(context.pool(), move |conn| {
478 CommunityModeratorView::for_community(conn, community_id)
482 // Make sure transferrer is either the top community mod, or an admin
483 if local_user_view.person.id != community_mods[0].moderator.id
486 .map(|a| a.person.id)
487 .any(|x| x == local_user_view.person.id)
489 return Err(LemmyError::from_message("not_an_admin"));
492 // You have to re-do the community_moderator table, reordering it.
493 // Add the transferee to the top
494 let creator_index = community_mods
496 .position(|r| r.moderator.id == data.person_id)
497 .context(location_info!())?;
498 let creator_person = community_mods.remove(creator_index);
499 community_mods.insert(0, creator_person);
501 // Delete all the mods
502 let community_id = data.community_id;
503 blocking(context.pool(), move |conn| {
504 CommunityModerator::delete_for_community(conn, community_id)
508 // TODO: this should probably be a bulk operation
509 // Re-add the mods, in the new order
510 for cmod in &community_mods {
511 let community_moderator_form = CommunityModeratorForm {
512 community_id: cmod.community.id,
513 person_id: cmod.moderator.id,
516 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
517 blocking(context.pool(), join)
519 .map_err(LemmyError::from)
520 .map_err(|e| e.with_message("community_moderator_already_exists"))?;
524 let form = ModTransferCommunityForm {
525 mod_person_id: local_user_view.person.id,
526 other_person_id: data.person_id,
527 community_id: data.community_id,
528 removed: Some(false),
530 blocking(context.pool(), move |conn| {
531 ModTransferCommunity::create(conn, &form)
535 let community_id = data.community_id;
536 let person_id = local_user_view.person.id;
537 let community_view = blocking(context.pool(), move |conn| {
538 CommunityView::read(conn, community_id, Some(person_id))
541 .map_err(LemmyError::from)
542 .map_err(|e| e.with_message("couldnt_find_community"))?;
544 let community_id = data.community_id;
545 let moderators = blocking(context.pool(), move |conn| {
546 CommunityModeratorView::for_community(conn, community_id)
549 .map_err(LemmyError::from)
550 .map_err(|e| e.with_message("couldnt_find_community"))?;
553 Ok(GetCommunityResponse {