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;
56 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
58 let community_id = data.community_id;
59 let community = blocking(context.pool(), move |conn| {
60 Community::read(conn, community_id)
63 let community_follower_form = CommunityFollowerForm {
64 community_id: data.community_id,
65 person_id: local_user_view.person.id,
71 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
73 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
74 if blocking(context.pool(), follow).await?.is_err() {
75 return Err(ApiError::err("community_follower_already_exists").into());
79 move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
80 if blocking(context.pool(), unfollow).await?.is_err() {
81 return Err(ApiError::err("community_follower_already_exists").into());
84 } else if data.follow {
85 // Dont actually add to the community followers here, because you need
86 // to wait for the accept
87 FollowCommunityApub::send(&local_user_view.person, &community, context).await?;
89 UndoFollowCommunity::send(&local_user_view.person, &community, context).await?;
90 let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
91 if blocking(context.pool(), unfollow).await?.is_err() {
92 return Err(ApiError::err("community_follower_already_exists").into());
96 let community_id = data.community_id;
97 let person_id = local_user_view.person.id;
98 let mut community_view = blocking(context.pool(), move |conn| {
99 CommunityView::read(conn, community_id, Some(person_id))
103 // TODO: this needs to return a "pending" state, until Accept is received from the remote server
104 // For now, just assume that remote follows are accepted.
105 // Otherwise, the subscribed will be null
106 if !community.local {
107 community_view.subscribed = data.follow;
110 Ok(CommunityResponse { community_view })
114 #[async_trait::async_trait(?Send)]
115 impl Perform for BlockCommunity {
116 type Response = BlockCommunityResponse;
120 context: &Data<LemmyContext>,
121 _websocket_id: Option<ConnectionId>,
122 ) -> Result<BlockCommunityResponse, LemmyError> {
123 let data: &BlockCommunity = self;
124 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
126 let community_id = data.community_id;
127 let person_id = local_user_view.person.id;
128 let community_block_form = CommunityBlockForm {
134 let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
135 if blocking(context.pool(), block).await?.is_err() {
136 return Err(ApiError::err("community_block_already_exists").into());
139 // Also, unfollow the community, and send a federated unfollow
140 let community_follower_form = CommunityFollowerForm {
141 community_id: data.community_id,
145 blocking(context.pool(), move |conn: &'_ _| {
146 CommunityFollower::unfollow(conn, &community_follower_form)
150 let community = blocking(context.pool(), move |conn| {
151 Community::read(conn, community_id)
154 UndoFollowCommunity::send(&local_user_view.person, &community, context).await?;
156 let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
157 if blocking(context.pool(), unblock).await?.is_err() {
158 return Err(ApiError::err("community_block_already_exists").into());
162 let community_view = blocking(context.pool(), move |conn| {
163 CommunityView::read(conn, community_id, Some(person_id))
167 Ok(BlockCommunityResponse {
174 #[async_trait::async_trait(?Send)]
175 impl Perform for BanFromCommunity {
176 type Response = BanFromCommunityResponse;
180 context: &Data<LemmyContext>,
181 websocket_id: Option<ConnectionId>,
182 ) -> Result<BanFromCommunityResponse, LemmyError> {
183 let data: &BanFromCommunity = self;
184 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
186 let community_id = data.community_id;
187 let banned_person_id = data.person_id;
189 // Verify that only mods or admins can ban
190 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
192 let community_user_ban_form = CommunityPersonBanForm {
193 community_id: data.community_id,
194 person_id: data.person_id,
197 let community = blocking(context.pool(), move |conn: &'_ _| {
198 Community::read(conn, community_id)
201 let banned_person = blocking(context.pool(), move |conn: &'_ _| {
202 Person::read(conn, banned_person_id)
207 let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
208 if blocking(context.pool(), ban).await?.is_err() {
209 return Err(ApiError::err("community_user_already_banned").into());
212 // Also unsubscribe them from the community, if they are subscribed
213 let community_follower_form = CommunityFollowerForm {
214 community_id: data.community_id,
215 person_id: banned_person_id,
218 blocking(context.pool(), move |conn: &'_ _| {
219 CommunityFollower::unfollow(conn, &community_follower_form)
224 BlockUserFromCommunity::send(&community, &banned_person, &local_user_view.person, context)
227 let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
228 if blocking(context.pool(), unban).await?.is_err() {
229 return Err(ApiError::err("community_user_already_banned").into());
231 UndoBlockUserFromCommunity::send(
234 &local_user_view.person,
240 // Remove/Restore their data if that's desired
241 if data.remove_data.unwrap_or(false) {
243 blocking(context.pool(), move |conn: &'_ _| {
244 Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true)
249 // TODO Diesel doesn't allow updates with joins, so this has to be a loop
250 let comments = blocking(context.pool(), move |conn| {
251 CommentQueryBuilder::create(conn)
252 .creator_id(banned_person_id)
253 .community_id(community_id)
254 .limit(std::i64::MAX)
259 for comment_view in &comments {
260 let comment_id = comment_view.comment.id;
261 blocking(context.pool(), move |conn: &'_ _| {
262 Comment::update_removed(conn, comment_id, true)
269 // TODO eventually do correct expires
270 let expires = data.expires.map(naive_from_unix);
272 let form = ModBanFromCommunityForm {
273 mod_person_id: local_user_view.person.id,
274 other_person_id: data.person_id,
275 community_id: data.community_id,
276 reason: data.reason.to_owned(),
277 banned: Some(data.ban),
280 blocking(context.pool(), move |conn| {
281 ModBanFromCommunity::create(conn, &form)
285 let person_id = data.person_id;
286 let person_view = blocking(context.pool(), move |conn| {
287 PersonViewSafe::read(conn, person_id)
291 let res = BanFromCommunityResponse {
296 context.chat_server().do_send(SendCommunityRoomMessage {
297 op: UserOperation::BanFromCommunity,
298 response: res.clone(),
307 #[async_trait::async_trait(?Send)]
308 impl Perform for AddModToCommunity {
309 type Response = AddModToCommunityResponse;
313 context: &Data<LemmyContext>,
314 websocket_id: Option<ConnectionId>,
315 ) -> Result<AddModToCommunityResponse, LemmyError> {
316 let data: &AddModToCommunity = self;
317 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
319 let community_id = data.community_id;
321 // Verify that only mods or admins can add mod
322 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
324 // Update in local database
325 let community_moderator_form = CommunityModeratorForm {
326 community_id: data.community_id,
327 person_id: data.person_id,
330 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
331 if blocking(context.pool(), join).await?.is_err() {
332 return Err(ApiError::err("community_moderator_already_exists").into());
335 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
336 if blocking(context.pool(), leave).await?.is_err() {
337 return Err(ApiError::err("community_moderator_already_exists").into());
342 let form = ModAddCommunityForm {
343 mod_person_id: local_user_view.person.id,
344 other_person_id: data.person_id,
345 community_id: data.community_id,
346 removed: Some(!data.added),
348 blocking(context.pool(), move |conn| {
349 ModAddCommunity::create(conn, &form)
353 // Send to federated instances
354 let updated_mod_id = data.person_id;
355 let updated_mod = blocking(context.pool(), move |conn| {
356 Person::read(conn, updated_mod_id)
359 let community = blocking(context.pool(), move |conn| {
360 Community::read(conn, community_id)
364 AddMod::send(&community, &updated_mod, &local_user_view.person, context).await?;
366 RemoveMod::send(&community, &updated_mod, &local_user_view.person, context).await?;
369 // Note: in case a remote mod is added, this returns the old moderators list, it will only get
370 // updated once we receive an activity from the community (like `Announce/Add/Moderator`)
371 let community_id = data.community_id;
372 let moderators = blocking(context.pool(), move |conn| {
373 CommunityModeratorView::for_community(conn, community_id)
377 let res = AddModToCommunityResponse { moderators };
378 context.chat_server().do_send(SendCommunityRoomMessage {
379 op: UserOperation::AddModToCommunity,
380 response: res.clone(),
388 // TODO: we dont do anything for federation here, it should be updated the next time the community
389 // gets fetched. i hope we can get rid of the community creator role soon.
390 #[async_trait::async_trait(?Send)]
391 impl Perform for TransferCommunity {
392 type Response = GetCommunityResponse;
396 context: &Data<LemmyContext>,
397 _websocket_id: Option<ConnectionId>,
398 ) -> Result<GetCommunityResponse, LemmyError> {
399 let data: &TransferCommunity = self;
400 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
402 let site_creator_id = blocking(context.pool(), move |conn| {
403 Site::read(conn, 1).map(|s| s.creator_id)
407 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
409 // Making sure the site creator, if an admin, is at the top
410 let creator_index = admins
412 .position(|r| r.person.id == site_creator_id)
413 .context(location_info!())?;
414 let creator_person = admins.remove(creator_index);
415 admins.insert(0, creator_person);
417 // Fetch the community mods
418 let community_id = data.community_id;
419 let mut community_mods = blocking(context.pool(), move |conn| {
420 CommunityModeratorView::for_community(conn, community_id)
424 // Make sure transferrer is either the top community mod, or an admin
425 if local_user_view.person.id != community_mods[0].moderator.id
428 .map(|a| a.person.id)
429 .any(|x| x == local_user_view.person.id)
431 return Err(ApiError::err("not_an_admin").into());
434 // You have to re-do the community_moderator table, reordering it.
435 // Add the transferee to the top
436 let creator_index = community_mods
438 .position(|r| r.moderator.id == data.person_id)
439 .context(location_info!())?;
440 let creator_person = community_mods.remove(creator_index);
441 community_mods.insert(0, creator_person);
443 // Delete all the mods
444 let community_id = data.community_id;
445 blocking(context.pool(), move |conn| {
446 CommunityModerator::delete_for_community(conn, community_id)
450 // TODO: this should probably be a bulk operation
451 // Re-add the mods, in the new order
452 for cmod in &community_mods {
453 let community_moderator_form = CommunityModeratorForm {
454 community_id: cmod.community.id,
455 person_id: cmod.moderator.id,
458 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
459 if blocking(context.pool(), join).await?.is_err() {
460 return Err(ApiError::err("community_moderator_already_exists").into());
465 let form = ModTransferCommunityForm {
466 mod_person_id: local_user_view.person.id,
467 other_person_id: data.person_id,
468 community_id: data.community_id,
469 removed: Some(false),
471 blocking(context.pool(), move |conn| {
472 ModTransferCommunity::create(conn, &form)
476 let community_id = data.community_id;
477 let person_id = local_user_view.person.id;
478 let community_view = blocking(context.pool(), move |conn| {
479 CommunityView::read(conn, community_id, Some(person_id))
482 .map_err(|_| ApiError::err("couldnt_find_community"))?;
484 let community_id = data.community_id;
485 let moderators = blocking(context.pool(), move |conn| {
486 CommunityModeratorView::for_community(conn, community_id)
489 .map_err(|_| ApiError::err("couldnt_find_community"))?;
492 Ok(GetCommunityResponse {