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, ApiError, 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;
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(|e| ApiError::err("community_follower_already_exists", e))?;
96 move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
97 blocking(context.pool(), unfollow)
99 .map_err(|e| ApiError::err("community_follower_already_exists", e))?;
101 } else if data.follow {
102 // Dont actually add to the community followers here, because you need
103 // to wait for the accept
104 FollowCommunityApub::send(&local_user_view.person.clone().into(), &community, context)
107 UndoFollowCommunity::send(&local_user_view.person.clone().into(), &community, context)
109 let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
110 blocking(context.pool(), unfollow)
112 .map_err(|e| ApiError::err("community_follower_already_exists", e))?;
115 let community_id = data.community_id;
116 let person_id = local_user_view.person.id;
117 let mut community_view = blocking(context.pool(), move |conn| {
118 CommunityView::read(conn, community_id, Some(person_id))
122 // TODO: this needs to return a "pending" state, until Accept is received from the remote server
123 // For now, just assume that remote follows are accepted.
124 // Otherwise, the subscribed will be null
125 if !community.local {
126 community_view.subscribed = data.follow;
129 Ok(CommunityResponse { community_view })
133 #[async_trait::async_trait(?Send)]
134 impl Perform for BlockCommunity {
135 type Response = BlockCommunityResponse;
139 context: &Data<LemmyContext>,
140 _websocket_id: Option<ConnectionId>,
141 ) -> Result<BlockCommunityResponse, LemmyError> {
142 let data: &BlockCommunity = self;
143 let local_user_view =
144 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
146 let community_id = data.community_id;
147 let person_id = local_user_view.person.id;
148 let community_block_form = CommunityBlockForm {
154 let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
155 blocking(context.pool(), block)
157 .map_err(|e| ApiError::err("community_block_already_exists", e))?;
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(|e| ApiError::err("community_block_already_exists", e))?;
182 let community_view = blocking(context.pool(), move |conn| {
183 CommunityView::read(conn, community_id, Some(person_id))
187 Ok(BlockCommunityResponse {
194 #[async_trait::async_trait(?Send)]
195 impl Perform for BanFromCommunity {
196 type Response = BanFromCommunityResponse;
200 context: &Data<LemmyContext>,
201 websocket_id: Option<ConnectionId>,
202 ) -> Result<BanFromCommunityResponse, LemmyError> {
203 let data: &BanFromCommunity = self;
204 let local_user_view =
205 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
207 let community_id = data.community_id;
208 let banned_person_id = data.person_id;
210 // Verify that only mods or admins can ban
211 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
213 let community_user_ban_form = CommunityPersonBanForm {
214 community_id: data.community_id,
215 person_id: data.person_id,
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| ApiError::err("community_user_already_banned", e))?;
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)
247 BlockUserFromCommunity::send(
250 &local_user_view.person.clone().into(),
255 let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
256 blocking(context.pool(), unban)
258 .map_err(|e| ApiError::err("community_user_already_banned", e))?;
259 UndoBlockUserFromCommunity::send(
262 &local_user_view.person.clone().into(),
268 // Remove/Restore their data if that's desired
269 if data.remove_data.unwrap_or(false) {
271 blocking(context.pool(), move |conn: &'_ _| {
272 Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true)
277 // TODO Diesel doesn't allow updates with joins, so this has to be a loop
278 let comments = blocking(context.pool(), move |conn| {
279 CommentQueryBuilder::create(conn)
280 .creator_id(banned_person_id)
281 .community_id(community_id)
282 .limit(std::i64::MAX)
287 for comment_view in &comments {
288 let comment_id = comment_view.comment.id;
289 blocking(context.pool(), move |conn: &'_ _| {
290 Comment::update_removed(conn, comment_id, true)
297 // TODO eventually do correct expires
298 let expires = data.expires.map(naive_from_unix);
300 let form = ModBanFromCommunityForm {
301 mod_person_id: local_user_view.person.id,
302 other_person_id: data.person_id,
303 community_id: data.community_id,
304 reason: data.reason.to_owned(),
305 banned: Some(data.ban),
308 blocking(context.pool(), move |conn| {
309 ModBanFromCommunity::create(conn, &form)
313 let person_id = data.person_id;
314 let person_view = blocking(context.pool(), move |conn| {
315 PersonViewSafe::read(conn, person_id)
319 let res = BanFromCommunityResponse {
324 context.chat_server().do_send(SendCommunityRoomMessage {
325 op: UserOperation::BanFromCommunity,
326 response: res.clone(),
335 #[async_trait::async_trait(?Send)]
336 impl Perform for AddModToCommunity {
337 type Response = AddModToCommunityResponse;
341 context: &Data<LemmyContext>,
342 websocket_id: Option<ConnectionId>,
343 ) -> Result<AddModToCommunityResponse, LemmyError> {
344 let data: &AddModToCommunity = self;
345 let local_user_view =
346 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
348 let community_id = data.community_id;
350 // Verify that only mods or admins can add mod
351 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
353 // Update in local database
354 let community_moderator_form = CommunityModeratorForm {
355 community_id: data.community_id,
356 person_id: data.person_id,
359 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
360 blocking(context.pool(), join)
362 .map_err(|e| ApiError::err("community_moderator_already_exists", e))?;
364 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
365 blocking(context.pool(), leave)
367 .map_err(|e| ApiError::err("community_moderator_already_exists", e))?;
371 let form = ModAddCommunityForm {
372 mod_person_id: local_user_view.person.id,
373 other_person_id: data.person_id,
374 community_id: data.community_id,
375 removed: Some(!data.added),
377 blocking(context.pool(), move |conn| {
378 ModAddCommunity::create(conn, &form)
382 // Send to federated instances
383 let updated_mod_id = data.person_id;
384 let updated_mod: ApubPerson = blocking(context.pool(), move |conn| {
385 Person::read(conn, updated_mod_id)
389 let community: ApubCommunity = blocking(context.pool(), move |conn| {
390 Community::read(conn, community_id)
398 &local_user_view.person.into(),
406 &local_user_view.person.into(),
412 // Note: in case a remote mod is added, this returns the old moderators list, it will only get
413 // updated once we receive an activity from the community (like `Announce/Add/Moderator`)
414 let community_id = data.community_id;
415 let moderators = blocking(context.pool(), move |conn| {
416 CommunityModeratorView::for_community(conn, community_id)
420 let res = AddModToCommunityResponse { moderators };
421 context.chat_server().do_send(SendCommunityRoomMessage {
422 op: UserOperation::AddModToCommunity,
423 response: res.clone(),
431 // TODO: we dont do anything for federation here, it should be updated the next time the community
432 // gets fetched. i hope we can get rid of the community creator role soon.
433 #[async_trait::async_trait(?Send)]
434 impl Perform for TransferCommunity {
435 type Response = GetCommunityResponse;
439 context: &Data<LemmyContext>,
440 _websocket_id: Option<ConnectionId>,
441 ) -> Result<GetCommunityResponse, LemmyError> {
442 let data: &TransferCommunity = self;
443 let local_user_view =
444 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
446 let site_creator_id = blocking(context.pool(), move |conn| {
447 Site::read(conn, 1).map(|s| s.creator_id)
451 let mut admins = blocking(context.pool(), PersonViewSafe::admins).await??;
453 // Making sure the site creator, if an admin, is at the top
454 let creator_index = admins
456 .position(|r| r.person.id == site_creator_id)
457 .context(location_info!())?;
458 let creator_person = admins.remove(creator_index);
459 admins.insert(0, creator_person);
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(ApiError::err_plain("not_an_admin").into());
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(|e| ApiError::err("community_moderator_already_exists", e))?;
509 let form = ModTransferCommunityForm {
510 mod_person_id: local_user_view.person.id,
511 other_person_id: data.person_id,
512 community_id: data.community_id,
513 removed: Some(false),
515 blocking(context.pool(), move |conn| {
516 ModTransferCommunity::create(conn, &form)
520 let community_id = data.community_id;
521 let person_id = local_user_view.person.id;
522 let community_view = blocking(context.pool(), move |conn| {
523 CommunityView::read(conn, community_id, Some(person_id))
526 .map_err(|e| ApiError::err("couldnt_find_community", e))?;
528 let community_id = data.community_id;
529 let moderators = blocking(context.pool(), move |conn| {
530 CommunityModeratorView::for_community(conn, community_id)
533 .map_err(|e| ApiError::err("couldnt_find_community", e))?;
536 Ok(GetCommunityResponse {