2 use actix_web::web::Data;
4 use lemmy_api_common::{
7 check_community_deleted_or_removed,
9 get_local_user_view_from_jwt,
12 use lemmy_apub::activities::{
15 block_user::BlockUserFromCommunity,
16 remove_mod::RemoveMod,
17 undo_block_user::UndoBlockUserFromCommunity,
19 following::{follow::FollowCommunity as FollowCommunityApub, undo::UndoFollowCommunity},
21 use lemmy_db_schema::{
27 CommunityFollowerForm,
29 CommunityModeratorForm,
31 CommunityPersonBanForm,
33 community_block::{CommunityBlock, CommunityBlockForm},
38 ModBanFromCommunityForm,
40 ModTransferCommunityForm,
46 traits::{Bannable, Blockable, Crud, Followable, Joinable},
48 use lemmy_db_views::comment_view::CommentQueryBuilder;
49 use lemmy_db_views_actor::{
50 community_moderator_view::CommunityModeratorView,
51 community_view::CommunityView,
52 person_view::PersonViewSafe,
54 use lemmy_utils::{location_info, utils::naive_from_unix, ApiError, ConnectionId, LemmyError};
55 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
57 #[async_trait::async_trait(?Send)]
58 impl Perform for FollowCommunity {
59 type Response = CommunityResponse;
63 context: &Data<LemmyContext>,
64 _websocket_id: Option<ConnectionId>,
65 ) -> Result<CommunityResponse, LemmyError> {
66 let data: &FollowCommunity = self;
68 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
70 let community_id = data.community_id;
71 let community = blocking(context.pool(), move |conn| {
72 Community::read(conn, community_id)
75 let community_follower_form = CommunityFollowerForm {
76 community_id: data.community_id,
77 person_id: local_user_view.person.id,
83 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
84 check_community_deleted_or_removed(community_id, context.pool()).await?;
86 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
87 blocking(context.pool(), follow)
89 .map_err(|e| ApiError::err("community_follower_already_exists", e))?;
92 move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
93 blocking(context.pool(), unfollow)
95 .map_err(|e| ApiError::err("community_follower_already_exists", e))?;
97 } else if data.follow {
98 // Dont actually add to the community followers here, because you need
99 // to wait for the accept
100 FollowCommunityApub::send(&local_user_view.person, &community, context).await?;
102 UndoFollowCommunity::send(&local_user_view.person, &community, context).await?;
103 let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
104 blocking(context.pool(), unfollow)
106 .map_err(|e| ApiError::err("community_follower_already_exists", e))?;
109 let community_id = data.community_id;
110 let person_id = local_user_view.person.id;
111 let mut community_view = blocking(context.pool(), move |conn| {
112 CommunityView::read(conn, community_id, Some(person_id))
116 // TODO: this needs to return a "pending" state, until Accept is received from the remote server
117 // For now, just assume that remote follows are accepted.
118 // Otherwise, the subscribed will be null
119 if !community.local {
120 community_view.subscribed = data.follow;
123 Ok(CommunityResponse { community_view })
127 #[async_trait::async_trait(?Send)]
128 impl Perform for BlockCommunity {
129 type Response = BlockCommunityResponse;
133 context: &Data<LemmyContext>,
134 _websocket_id: Option<ConnectionId>,
135 ) -> Result<BlockCommunityResponse, LemmyError> {
136 let data: &BlockCommunity = self;
137 let local_user_view =
138 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
140 let community_id = data.community_id;
141 let person_id = local_user_view.person.id;
142 let community_block_form = CommunityBlockForm {
148 let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
149 blocking(context.pool(), block)
151 .map_err(|e| ApiError::err("community_block_already_exists", e))?;
153 // Also, unfollow the community, and send a federated unfollow
154 let community_follower_form = CommunityFollowerForm {
155 community_id: data.community_id,
159 blocking(context.pool(), move |conn: &'_ _| {
160 CommunityFollower::unfollow(conn, &community_follower_form)
164 let community = blocking(context.pool(), move |conn| {
165 Community::read(conn, community_id)
168 UndoFollowCommunity::send(&local_user_view.person, &community, context).await?;
170 let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
171 blocking(context.pool(), unblock)
173 .map_err(|e| ApiError::err("community_block_already_exists", e))?;
176 let community_view = blocking(context.pool(), move |conn| {
177 CommunityView::read(conn, community_id, Some(person_id))
181 Ok(BlockCommunityResponse {
188 #[async_trait::async_trait(?Send)]
189 impl Perform for BanFromCommunity {
190 type Response = BanFromCommunityResponse;
194 context: &Data<LemmyContext>,
195 websocket_id: Option<ConnectionId>,
196 ) -> Result<BanFromCommunityResponse, LemmyError> {
197 let data: &BanFromCommunity = self;
198 let local_user_view =
199 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
201 let community_id = data.community_id;
202 let banned_person_id = data.person_id;
204 // Verify that only mods or admins can ban
205 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
207 let community_user_ban_form = CommunityPersonBanForm {
208 community_id: data.community_id,
209 person_id: data.person_id,
212 let community = blocking(context.pool(), move |conn: &'_ _| {
213 Community::read(conn, community_id)
216 let banned_person = blocking(context.pool(), move |conn: &'_ _| {
217 Person::read(conn, banned_person_id)
222 let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
223 blocking(context.pool(), ban)
225 .map_err(|e| ApiError::err("community_user_already_banned", e))?;
227 // Also unsubscribe them from the community, if they are subscribed
228 let community_follower_form = CommunityFollowerForm {
229 community_id: data.community_id,
230 person_id: banned_person_id,
233 blocking(context.pool(), move |conn: &'_ _| {
234 CommunityFollower::unfollow(conn, &community_follower_form)
239 BlockUserFromCommunity::send(&community, &banned_person, &local_user_view.person, context)
242 let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
243 blocking(context.pool(), unban)
245 .map_err(|e| ApiError::err("community_user_already_banned", e))?;
246 UndoBlockUserFromCommunity::send(
249 &local_user_view.person,
255 // Remove/Restore their data if that's desired
256 if data.remove_data.unwrap_or(false) {
258 blocking(context.pool(), move |conn: &'_ _| {
259 Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true)
264 // TODO Diesel doesn't allow updates with joins, so this has to be a loop
265 let comments = blocking(context.pool(), move |conn| {
266 CommentQueryBuilder::create(conn)
267 .creator_id(banned_person_id)
268 .community_id(community_id)
269 .limit(std::i64::MAX)
274 for comment_view in &comments {
275 let comment_id = comment_view.comment.id;
276 blocking(context.pool(), move |conn: &'_ _| {
277 Comment::update_removed(conn, comment_id, true)
284 // TODO eventually do correct expires
285 let expires = data.expires.map(naive_from_unix);
287 let form = ModBanFromCommunityForm {
288 mod_person_id: local_user_view.person.id,
289 other_person_id: data.person_id,
290 community_id: data.community_id,
291 reason: data.reason.to_owned(),
292 banned: Some(data.ban),
295 blocking(context.pool(), move |conn| {
296 ModBanFromCommunity::create(conn, &form)
300 let person_id = data.person_id;
301 let person_view = blocking(context.pool(), move |conn| {
302 PersonViewSafe::read(conn, person_id)
306 let res = BanFromCommunityResponse {
311 context.chat_server().do_send(SendCommunityRoomMessage {
312 op: UserOperation::BanFromCommunity,
313 response: res.clone(),
322 #[async_trait::async_trait(?Send)]
323 impl Perform for AddModToCommunity {
324 type Response = AddModToCommunityResponse;
328 context: &Data<LemmyContext>,
329 websocket_id: Option<ConnectionId>,
330 ) -> Result<AddModToCommunityResponse, LemmyError> {
331 let data: &AddModToCommunity = self;
332 let local_user_view =
333 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
335 let community_id = data.community_id;
337 // Verify that only mods or admins can add mod
338 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
340 // Update in local database
341 let community_moderator_form = CommunityModeratorForm {
342 community_id: data.community_id,
343 person_id: data.person_id,
346 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
347 blocking(context.pool(), join)
349 .map_err(|e| ApiError::err("community_moderator_already_exists", e))?;
351 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
352 blocking(context.pool(), leave)
354 .map_err(|e| ApiError::err("community_moderator_already_exists", e))?;
358 let form = ModAddCommunityForm {
359 mod_person_id: local_user_view.person.id,
360 other_person_id: data.person_id,
361 community_id: data.community_id,
362 removed: Some(!data.added),
364 blocking(context.pool(), move |conn| {
365 ModAddCommunity::create(conn, &form)
369 // Send to federated instances
370 let updated_mod_id = data.person_id;
371 let updated_mod = blocking(context.pool(), move |conn| {
372 Person::read(conn, updated_mod_id)
375 let community = blocking(context.pool(), move |conn| {
376 Community::read(conn, community_id)
380 AddMod::send(&community, &updated_mod, &local_user_view.person, context).await?;
382 RemoveMod::send(&community, &updated_mod, &local_user_view.person, context).await?;
385 // Note: in case a remote mod is added, this returns the old moderators list, it will only get
386 // updated once we receive an activity from the community (like `Announce/Add/Moderator`)
387 let community_id = data.community_id;
388 let moderators = blocking(context.pool(), move |conn| {
389 CommunityModeratorView::for_community(conn, community_id)
393 let res = AddModToCommunityResponse { moderators };
394 context.chat_server().do_send(SendCommunityRoomMessage {
395 op: UserOperation::AddModToCommunity,
396 response: res.clone(),
404 // TODO: we dont do anything for federation here, it should be updated the next time the community
405 // gets fetched. i hope we can get rid of the community creator role soon.
406 #[async_trait::async_trait(?Send)]
407 impl Perform for TransferCommunity {
408 type Response = GetCommunityResponse;
412 context: &Data<LemmyContext>,
413 _websocket_id: Option<ConnectionId>,
414 ) -> Result<GetCommunityResponse, LemmyError> {
415 let data: &TransferCommunity = self;
416 let local_user_view =
417 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
419 let site_creator_id = blocking(context.pool(), move |conn| {
420 Site::read(conn, 1).map(|s| s.creator_id)
424 let mut admins = blocking(context.pool(), PersonViewSafe::admins).await??;
426 // Making sure the site creator, if an admin, is at the top
427 let creator_index = admins
429 .position(|r| r.person.id == site_creator_id)
430 .context(location_info!())?;
431 let creator_person = admins.remove(creator_index);
432 admins.insert(0, creator_person);
434 // Fetch the community mods
435 let community_id = data.community_id;
436 let mut community_mods = blocking(context.pool(), move |conn| {
437 CommunityModeratorView::for_community(conn, community_id)
441 // Make sure transferrer is either the top community mod, or an admin
442 if local_user_view.person.id != community_mods[0].moderator.id
445 .map(|a| a.person.id)
446 .any(|x| x == local_user_view.person.id)
448 return Err(ApiError::err_plain("not_an_admin").into());
451 // You have to re-do the community_moderator table, reordering it.
452 // Add the transferee to the top
453 let creator_index = community_mods
455 .position(|r| r.moderator.id == data.person_id)
456 .context(location_info!())?;
457 let creator_person = community_mods.remove(creator_index);
458 community_mods.insert(0, creator_person);
460 // Delete all the mods
461 let community_id = data.community_id;
462 blocking(context.pool(), move |conn| {
463 CommunityModerator::delete_for_community(conn, community_id)
467 // TODO: this should probably be a bulk operation
468 // Re-add the mods, in the new order
469 for cmod in &community_mods {
470 let community_moderator_form = CommunityModeratorForm {
471 community_id: cmod.community.id,
472 person_id: cmod.moderator.id,
475 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
476 blocking(context.pool(), join)
478 .map_err(|e| ApiError::err("community_moderator_already_exists", e))?;
482 let form = ModTransferCommunityForm {
483 mod_person_id: local_user_view.person.id,
484 other_person_id: data.person_id,
485 community_id: data.community_id,
486 removed: Some(false),
488 blocking(context.pool(), move |conn| {
489 ModTransferCommunity::create(conn, &form)
493 let community_id = data.community_id;
494 let person_id = local_user_view.person.id;
495 let community_view = blocking(context.pool(), move |conn| {
496 CommunityView::read(conn, community_id, Some(person_id))
499 .map_err(|e| ApiError::err("couldnt_find_community", e))?;
501 let community_id = data.community_id;
502 let moderators = blocking(context.pool(), move |conn| {
503 CommunityModeratorView::for_community(conn, community_id)
506 .map_err(|e| ApiError::err("couldnt_find_community", e))?;
509 Ok(GetCommunityResponse {