2 use actix_web::web::Data;
4 use lemmy_api_common::{
8 get_local_user_view_from_jwt,
11 use lemmy_apub::activities::{
14 block_user::BlockUserFromCommunity,
15 remove_mod::RemoveMod,
16 undo_block_user::UndoBlockUserFromCommunity,
18 following::{follow::FollowCommunity as FollowCommunityApub, undo::UndoFollowCommunity},
20 use lemmy_db_queries::{
21 source::{comment::Comment_, community::CommunityModerator_, post::Post_},
28 use lemmy_db_schema::source::{
31 community_block::{CommunityBlock, CommunityBlockForm},
37 use lemmy_db_views::comment_view::CommentQueryBuilder;
38 use lemmy_db_views_actor::{
39 community_moderator_view::CommunityModeratorView,
40 community_view::CommunityView,
41 person_view::PersonViewSafe,
43 use lemmy_utils::{location_info, utils::naive_from_unix, ApiError, ConnectionId, LemmyError};
44 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
46 #[async_trait::async_trait(?Send)]
47 impl Perform for FollowCommunity {
48 type Response = CommunityResponse;
52 context: &Data<LemmyContext>,
53 _websocket_id: Option<ConnectionId>,
54 ) -> Result<CommunityResponse, LemmyError> {
55 let data: &FollowCommunity = self;
57 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
59 let community_id = data.community_id;
60 let community = blocking(context.pool(), move |conn| {
61 Community::read(conn, community_id)
64 let community_follower_form = CommunityFollowerForm {
65 community_id: data.community_id,
66 person_id: local_user_view.person.id,
72 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
74 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
75 if blocking(context.pool(), follow).await?.is_err() {
76 return Err(ApiError::err("community_follower_already_exists").into());
80 move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
81 if blocking(context.pool(), unfollow).await?.is_err() {
82 return Err(ApiError::err("community_follower_already_exists").into());
85 } else if data.follow {
86 // Dont actually add to the community followers here, because you need
87 // to wait for the accept
88 FollowCommunityApub::send(&local_user_view.person, &community, context).await?;
90 UndoFollowCommunity::send(&local_user_view.person, &community, context).await?;
91 let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
92 if blocking(context.pool(), unfollow).await?.is_err() {
93 return Err(ApiError::err("community_follower_already_exists").into());
97 let community_id = data.community_id;
98 let person_id = local_user_view.person.id;
99 let mut community_view = blocking(context.pool(), move |conn| {
100 CommunityView::read(conn, community_id, Some(person_id))
104 // TODO: this needs to return a "pending" state, until Accept is received from the remote server
105 // For now, just assume that remote follows are accepted.
106 // Otherwise, the subscribed will be null
107 if !community.local {
108 community_view.subscribed = data.follow;
111 Ok(CommunityResponse { community_view })
115 #[async_trait::async_trait(?Send)]
116 impl Perform for BlockCommunity {
117 type Response = BlockCommunityResponse;
121 context: &Data<LemmyContext>,
122 _websocket_id: Option<ConnectionId>,
123 ) -> Result<BlockCommunityResponse, LemmyError> {
124 let data: &BlockCommunity = self;
125 let local_user_view =
126 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
128 let community_id = data.community_id;
129 let person_id = local_user_view.person.id;
130 let community_block_form = CommunityBlockForm {
136 let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
137 if blocking(context.pool(), block).await?.is_err() {
138 return Err(ApiError::err("community_block_already_exists").into());
141 // Also, unfollow the community, and send a federated unfollow
142 let community_follower_form = CommunityFollowerForm {
143 community_id: data.community_id,
147 blocking(context.pool(), move |conn: &'_ _| {
148 CommunityFollower::unfollow(conn, &community_follower_form)
152 let community = blocking(context.pool(), move |conn| {
153 Community::read(conn, community_id)
156 UndoFollowCommunity::send(&local_user_view.person, &community, context).await?;
158 let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
159 if blocking(context.pool(), unblock).await?.is_err() {
160 return Err(ApiError::err("community_block_already_exists").into());
164 let community_view = blocking(context.pool(), move |conn| {
165 CommunityView::read(conn, community_id, Some(person_id))
169 Ok(BlockCommunityResponse {
176 #[async_trait::async_trait(?Send)]
177 impl Perform for BanFromCommunity {
178 type Response = BanFromCommunityResponse;
182 context: &Data<LemmyContext>,
183 websocket_id: Option<ConnectionId>,
184 ) -> Result<BanFromCommunityResponse, LemmyError> {
185 let data: &BanFromCommunity = self;
186 let local_user_view =
187 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
189 let community_id = data.community_id;
190 let banned_person_id = data.person_id;
192 // Verify that only mods or admins can ban
193 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
195 let community_user_ban_form = CommunityPersonBanForm {
196 community_id: data.community_id,
197 person_id: data.person_id,
200 let community = blocking(context.pool(), move |conn: &'_ _| {
201 Community::read(conn, community_id)
204 let banned_person = blocking(context.pool(), move |conn: &'_ _| {
205 Person::read(conn, banned_person_id)
210 let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
211 if blocking(context.pool(), ban).await?.is_err() {
212 return Err(ApiError::err("community_user_already_banned").into());
215 // Also unsubscribe them from the community, if they are subscribed
216 let community_follower_form = CommunityFollowerForm {
217 community_id: data.community_id,
218 person_id: banned_person_id,
221 blocking(context.pool(), move |conn: &'_ _| {
222 CommunityFollower::unfollow(conn, &community_follower_form)
227 BlockUserFromCommunity::send(&community, &banned_person, &local_user_view.person, context)
230 let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
231 if blocking(context.pool(), unban).await?.is_err() {
232 return Err(ApiError::err("community_user_already_banned").into());
234 UndoBlockUserFromCommunity::send(
237 &local_user_view.person,
243 // Remove/Restore their data if that's desired
244 if data.remove_data.unwrap_or(false) {
246 blocking(context.pool(), move |conn: &'_ _| {
247 Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true)
252 // TODO Diesel doesn't allow updates with joins, so this has to be a loop
253 let comments = blocking(context.pool(), move |conn| {
254 CommentQueryBuilder::create(conn)
255 .creator_id(banned_person_id)
256 .community_id(community_id)
257 .limit(std::i64::MAX)
262 for comment_view in &comments {
263 let comment_id = comment_view.comment.id;
264 blocking(context.pool(), move |conn: &'_ _| {
265 Comment::update_removed(conn, comment_id, true)
272 // TODO eventually do correct expires
273 let expires = data.expires.map(naive_from_unix);
275 let form = ModBanFromCommunityForm {
276 mod_person_id: local_user_view.person.id,
277 other_person_id: data.person_id,
278 community_id: data.community_id,
279 reason: data.reason.to_owned(),
280 banned: Some(data.ban),
283 blocking(context.pool(), move |conn| {
284 ModBanFromCommunity::create(conn, &form)
288 let person_id = data.person_id;
289 let person_view = blocking(context.pool(), move |conn| {
290 PersonViewSafe::read(conn, person_id)
294 let res = BanFromCommunityResponse {
299 context.chat_server().do_send(SendCommunityRoomMessage {
300 op: UserOperation::BanFromCommunity,
301 response: res.clone(),
310 #[async_trait::async_trait(?Send)]
311 impl Perform for AddModToCommunity {
312 type Response = AddModToCommunityResponse;
316 context: &Data<LemmyContext>,
317 websocket_id: Option<ConnectionId>,
318 ) -> Result<AddModToCommunityResponse, LemmyError> {
319 let data: &AddModToCommunity = self;
320 let local_user_view =
321 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
323 let community_id = data.community_id;
325 // Verify that only mods or admins can add mod
326 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
328 // Update in local database
329 let community_moderator_form = CommunityModeratorForm {
330 community_id: data.community_id,
331 person_id: data.person_id,
334 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
335 if blocking(context.pool(), join).await?.is_err() {
336 return Err(ApiError::err("community_moderator_already_exists").into());
339 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
340 if blocking(context.pool(), leave).await?.is_err() {
341 return Err(ApiError::err("community_moderator_already_exists").into());
346 let form = ModAddCommunityForm {
347 mod_person_id: local_user_view.person.id,
348 other_person_id: data.person_id,
349 community_id: data.community_id,
350 removed: Some(!data.added),
352 blocking(context.pool(), move |conn| {
353 ModAddCommunity::create(conn, &form)
357 // Send to federated instances
358 let updated_mod_id = data.person_id;
359 let updated_mod = blocking(context.pool(), move |conn| {
360 Person::read(conn, updated_mod_id)
363 let community = blocking(context.pool(), move |conn| {
364 Community::read(conn, community_id)
368 AddMod::send(&community, &updated_mod, &local_user_view.person, context).await?;
370 RemoveMod::send(&community, &updated_mod, &local_user_view.person, context).await?;
373 // Note: in case a remote mod is added, this returns the old moderators list, it will only get
374 // updated once we receive an activity from the community (like `Announce/Add/Moderator`)
375 let community_id = data.community_id;
376 let moderators = blocking(context.pool(), move |conn| {
377 CommunityModeratorView::for_community(conn, community_id)
381 let res = AddModToCommunityResponse { moderators };
382 context.chat_server().do_send(SendCommunityRoomMessage {
383 op: UserOperation::AddModToCommunity,
384 response: res.clone(),
392 // TODO: we dont do anything for federation here, it should be updated the next time the community
393 // gets fetched. i hope we can get rid of the community creator role soon.
394 #[async_trait::async_trait(?Send)]
395 impl Perform for TransferCommunity {
396 type Response = GetCommunityResponse;
400 context: &Data<LemmyContext>,
401 _websocket_id: Option<ConnectionId>,
402 ) -> Result<GetCommunityResponse, LemmyError> {
403 let data: &TransferCommunity = self;
404 let local_user_view =
405 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
407 let site_creator_id = blocking(context.pool(), move |conn| {
408 Site::read(conn, 1).map(|s| s.creator_id)
412 let mut admins = blocking(context.pool(), PersonViewSafe::admins).await??;
414 // Making sure the site creator, if an admin, is at the top
415 let creator_index = admins
417 .position(|r| r.person.id == site_creator_id)
418 .context(location_info!())?;
419 let creator_person = admins.remove(creator_index);
420 admins.insert(0, creator_person);
422 // Fetch the community mods
423 let community_id = data.community_id;
424 let mut community_mods = blocking(context.pool(), move |conn| {
425 CommunityModeratorView::for_community(conn, community_id)
429 // Make sure transferrer is either the top community mod, or an admin
430 if local_user_view.person.id != community_mods[0].moderator.id
433 .map(|a| a.person.id)
434 .any(|x| x == local_user_view.person.id)
436 return Err(ApiError::err("not_an_admin").into());
439 // You have to re-do the community_moderator table, reordering it.
440 // Add the transferee to the top
441 let creator_index = community_mods
443 .position(|r| r.moderator.id == data.person_id)
444 .context(location_info!())?;
445 let creator_person = community_mods.remove(creator_index);
446 community_mods.insert(0, creator_person);
448 // Delete all the mods
449 let community_id = data.community_id;
450 blocking(context.pool(), move |conn| {
451 CommunityModerator::delete_for_community(conn, community_id)
455 // TODO: this should probably be a bulk operation
456 // Re-add the mods, in the new order
457 for cmod in &community_mods {
458 let community_moderator_form = CommunityModeratorForm {
459 community_id: cmod.community.id,
460 person_id: cmod.moderator.id,
463 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
464 if blocking(context.pool(), join).await?.is_err() {
465 return Err(ApiError::err("community_moderator_already_exists").into());
470 let form = ModTransferCommunityForm {
471 mod_person_id: local_user_view.person.id,
472 other_person_id: data.person_id,
473 community_id: data.community_id,
474 removed: Some(false),
476 blocking(context.pool(), move |conn| {
477 ModTransferCommunity::create(conn, &form)
481 let community_id = data.community_id;
482 let person_id = local_user_view.person.id;
483 let community_view = blocking(context.pool(), move |conn| {
484 CommunityView::read(conn, community_id, Some(person_id))
487 .map_err(|_| ApiError::err("couldnt_find_community"))?;
489 let community_id = data.community_id;
490 let moderators = blocking(context.pool(), move |conn| {
491 CommunityModeratorView::for_community(conn, community_id)
494 .map_err(|_| ApiError::err("couldnt_find_community"))?;
497 Ok(GetCommunityResponse {