10 use actix_web::web::Data;
13 generate_apub_endpoint,
14 generate_followers_url,
16 generate_shared_inbox_url,
20 use lemmy_db_queries::{
21 diesel_option_overwrite,
24 community::{CommunityModerator_, Community_},
35 use lemmy_db_schema::{
37 source::{comment::Comment, community::*, moderator::*, post::Post, site::*},
39 use lemmy_db_views::comment_view::CommentQueryBuilder;
40 use lemmy_db_views_actor::{
41 community_follower_view::CommunityFollowerView,
42 community_moderator_view::CommunityModeratorView,
43 community_view::{CommunityQueryBuilder, CommunityView},
44 user_view::UserViewSafe,
46 use lemmy_structs::{blocking, community::*};
48 apub::generate_actor_keypair,
50 utils::{check_slurs, check_slurs_opt, is_valid_community_name, naive_from_unix},
55 use lemmy_websocket::{
56 messages::{GetCommunityUsersOnline, SendCommunityRoomMessage},
60 use std::str::FromStr;
62 #[async_trait::async_trait(?Send)]
63 impl Perform for GetCommunity {
64 type Response = GetCommunityResponse;
68 context: &Data<LemmyContext>,
69 _websocket_id: Option<ConnectionId>,
70 ) -> Result<GetCommunityResponse, LemmyError> {
71 let data: &GetCommunity = &self;
72 let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
73 let user_id = user.map(|u| u.id);
75 let community_id = match data.id {
78 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
79 match blocking(context.pool(), move |conn| {
80 Community::read_from_name(conn, &name)
84 Ok(community) => community,
85 Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
91 let community_view = match blocking(context.pool(), move |conn| {
92 CommunityView::read(conn, community_id, user_id)
96 Ok(community) => community,
97 Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
100 let moderators: Vec<CommunityModeratorView> = match blocking(context.pool(), move |conn| {
101 CommunityModeratorView::for_community(conn, community_id)
105 Ok(moderators) => moderators,
106 Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
111 .send(GetCommunityUsersOnline { community_id })
115 let res = GetCommunityResponse {
126 #[async_trait::async_trait(?Send)]
127 impl Perform for CreateCommunity {
128 type Response = CommunityResponse;
132 context: &Data<LemmyContext>,
133 _websocket_id: Option<ConnectionId>,
134 ) -> Result<CommunityResponse, LemmyError> {
135 let data: &CreateCommunity = &self;
136 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
138 check_slurs(&data.name)?;
139 check_slurs(&data.title)?;
140 check_slurs_opt(&data.description)?;
142 if !is_valid_community_name(&data.name) {
143 return Err(ApiError::err("invalid_community_name").into());
146 // Double check for duplicate community actor_ids
147 let community_actor_id = generate_apub_endpoint(EndpointType::Community, &data.name)?;
148 let actor_id_cloned = community_actor_id.to_owned();
149 let community_dupe = blocking(context.pool(), move |conn| {
150 Community::read_from_apub_id(conn, &actor_id_cloned)
153 if community_dupe.is_ok() {
154 return Err(ApiError::err("community_already_exists").into());
157 // Check to make sure the icon and banners are urls
158 let icon = diesel_option_overwrite(&data.icon);
159 let banner = diesel_option_overwrite(&data.banner);
161 check_optional_url(&icon)?;
162 check_optional_url(&banner)?;
164 // When you create a community, make sure the user becomes a moderator and a follower
165 let keypair = generate_actor_keypair()?;
167 let community_form = CommunityForm {
168 name: data.name.to_owned(),
169 title: data.title.to_owned(),
170 description: data.description.to_owned(),
178 actor_id: Some(community_actor_id.to_owned()),
180 private_key: Some(keypair.private_key),
181 public_key: Some(keypair.public_key),
182 last_refreshed_at: None,
184 followers_url: Some(generate_followers_url(&community_actor_id)?),
185 inbox_url: Some(generate_inbox_url(&community_actor_id)?),
186 shared_inbox_url: Some(Some(generate_shared_inbox_url(&community_actor_id)?)),
189 let inserted_community = match blocking(context.pool(), move |conn| {
190 Community::create(conn, &community_form)
194 Ok(community) => community,
195 Err(_e) => return Err(ApiError::err("community_already_exists").into()),
198 // The community creator becomes a moderator
199 let community_moderator_form = CommunityModeratorForm {
200 community_id: inserted_community.id,
204 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
205 if blocking(context.pool(), join).await?.is_err() {
206 return Err(ApiError::err("community_moderator_already_exists").into());
209 // Follow your own community
210 let community_follower_form = CommunityFollowerForm {
211 community_id: inserted_community.id,
216 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
217 if blocking(context.pool(), follow).await?.is_err() {
218 return Err(ApiError::err("community_follower_already_exists").into());
221 let user_id = user.id;
222 let community_view = blocking(context.pool(), move |conn| {
223 CommunityView::read(conn, inserted_community.id, Some(user_id))
227 Ok(CommunityResponse { community_view })
231 #[async_trait::async_trait(?Send)]
232 impl Perform for EditCommunity {
233 type Response = CommunityResponse;
237 context: &Data<LemmyContext>,
238 websocket_id: Option<ConnectionId>,
239 ) -> Result<CommunityResponse, LemmyError> {
240 let data: &EditCommunity = &self;
241 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
243 check_slurs(&data.title)?;
244 check_slurs_opt(&data.description)?;
246 // Verify its a mod (only mods can edit it)
247 let community_id = data.community_id;
248 let mods: Vec<i32> = blocking(context.pool(), move |conn| {
249 CommunityModeratorView::for_community(conn, community_id)
250 .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
253 if !mods.contains(&user.id) {
254 return Err(ApiError::err("not_a_moderator").into());
257 let community_id = data.community_id;
258 let read_community = blocking(context.pool(), move |conn| {
259 Community::read(conn, community_id)
263 let icon = diesel_option_overwrite(&data.icon);
264 let banner = diesel_option_overwrite(&data.banner);
266 check_optional_url(&icon)?;
267 check_optional_url(&banner)?;
269 let community_form = CommunityForm {
270 name: read_community.name,
271 title: data.title.to_owned(),
272 description: data.description.to_owned(),
275 creator_id: read_community.creator_id,
276 removed: Some(read_community.removed),
277 deleted: Some(read_community.deleted),
279 updated: Some(naive_now()),
280 actor_id: Some(read_community.actor_id),
281 local: read_community.local,
282 private_key: read_community.private_key,
283 public_key: read_community.public_key,
284 last_refreshed_at: None,
288 shared_inbox_url: None,
291 let community_id = data.community_id;
292 match blocking(context.pool(), move |conn| {
293 Community::update(conn, community_id, &community_form)
297 Ok(community) => community,
298 Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
301 // TODO there needs to be some kind of an apub update
302 // process for communities and users
304 let community_id = data.community_id;
305 let user_id = user.id;
306 let community_view = blocking(context.pool(), move |conn| {
307 CommunityView::read(conn, community_id, Some(user_id))
311 let res = CommunityResponse { community_view };
313 send_community_websocket(&res, context, websocket_id, UserOperation::EditCommunity);
319 #[async_trait::async_trait(?Send)]
320 impl Perform for DeleteCommunity {
321 type Response = CommunityResponse;
325 context: &Data<LemmyContext>,
326 websocket_id: Option<ConnectionId>,
327 ) -> Result<CommunityResponse, LemmyError> {
328 let data: &DeleteCommunity = &self;
329 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
331 // Verify its the creator (only a creator can delete the community)
332 let community_id = data.community_id;
333 let read_community = blocking(context.pool(), move |conn| {
334 Community::read(conn, community_id)
337 if read_community.creator_id != user.id {
338 return Err(ApiError::err("no_community_edit_allowed").into());
342 let community_id = data.community_id;
343 let deleted = data.deleted;
344 let updated_community = match blocking(context.pool(), move |conn| {
345 Community::update_deleted(conn, community_id, deleted)
349 Ok(community) => community,
350 Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
353 // Send apub messages
355 updated_community.send_delete(context).await?;
357 updated_community.send_undo_delete(context).await?;
360 let community_id = data.community_id;
361 let user_id = user.id;
362 let community_view = blocking(context.pool(), move |conn| {
363 CommunityView::read(conn, community_id, Some(user_id))
367 let res = CommunityResponse { community_view };
369 send_community_websocket(&res, context, websocket_id, UserOperation::DeleteCommunity);
375 #[async_trait::async_trait(?Send)]
376 impl Perform for RemoveCommunity {
377 type Response = CommunityResponse;
381 context: &Data<LemmyContext>,
382 websocket_id: Option<ConnectionId>,
383 ) -> Result<CommunityResponse, LemmyError> {
384 let data: &RemoveCommunity = &self;
385 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
387 // Verify its an admin (only an admin can remove a community)
388 is_admin(context.pool(), user.id).await?;
391 let community_id = data.community_id;
392 let removed = data.removed;
393 let updated_community = match blocking(context.pool(), move |conn| {
394 Community::update_removed(conn, community_id, removed)
398 Ok(community) => community,
399 Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
403 let expires = match data.expires {
404 Some(time) => Some(naive_from_unix(time)),
407 let form = ModRemoveCommunityForm {
408 mod_user_id: user.id,
409 community_id: data.community_id,
410 removed: Some(removed),
411 reason: data.reason.to_owned(),
414 blocking(context.pool(), move |conn| {
415 ModRemoveCommunity::create(conn, &form)
421 updated_community.send_remove(context).await?;
423 updated_community.send_undo_remove(context).await?;
426 let community_id = data.community_id;
427 let user_id = user.id;
428 let community_view = blocking(context.pool(), move |conn| {
429 CommunityView::read(conn, community_id, Some(user_id))
433 let res = CommunityResponse { community_view };
435 send_community_websocket(&res, context, websocket_id, UserOperation::RemoveCommunity);
441 #[async_trait::async_trait(?Send)]
442 impl Perform for ListCommunities {
443 type Response = ListCommunitiesResponse;
447 context: &Data<LemmyContext>,
448 _websocket_id: Option<ConnectionId>,
449 ) -> Result<ListCommunitiesResponse, LemmyError> {
450 let data: &ListCommunities = &self;
451 let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
453 let user_id = match &user {
454 Some(user) => Some(user.id),
458 let show_nsfw = match &user {
459 Some(user) => user.show_nsfw,
463 let type_ = ListingType::from_str(&data.type_)?;
464 let sort = SortType::from_str(&data.sort)?;
466 let page = data.page;
467 let limit = data.limit;
468 let communities = blocking(context.pool(), move |conn| {
469 CommunityQueryBuilder::create(conn)
470 .listing_type(&type_)
472 .show_nsfw(show_nsfw)
481 Ok(ListCommunitiesResponse { communities })
485 #[async_trait::async_trait(?Send)]
486 impl Perform for FollowCommunity {
487 type Response = CommunityResponse;
491 context: &Data<LemmyContext>,
492 _websocket_id: Option<ConnectionId>,
493 ) -> Result<CommunityResponse, LemmyError> {
494 let data: &FollowCommunity = &self;
495 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
497 let community_id = data.community_id;
498 let community = blocking(context.pool(), move |conn| {
499 Community::read(conn, community_id)
502 let community_follower_form = CommunityFollowerForm {
503 community_id: data.community_id,
510 check_community_ban(user.id, community_id, context.pool()).await?;
512 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
513 if blocking(context.pool(), follow).await?.is_err() {
514 return Err(ApiError::err("community_follower_already_exists").into());
518 move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
519 if blocking(context.pool(), unfollow).await?.is_err() {
520 return Err(ApiError::err("community_follower_already_exists").into());
523 } else if data.follow {
524 // Dont actually add to the community followers here, because you need
525 // to wait for the accept
526 user.send_follow(&community.actor_id(), context).await?;
528 user.send_unfollow(&community.actor_id(), context).await?;
529 let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
530 if blocking(context.pool(), unfollow).await?.is_err() {
531 return Err(ApiError::err("community_follower_already_exists").into());
535 let community_id = data.community_id;
536 let user_id = user.id;
537 let mut community_view = blocking(context.pool(), move |conn| {
538 CommunityView::read(conn, community_id, Some(user_id))
542 // TODO: this needs to return a "pending" state, until Accept is received from the remote server
543 // For now, just assume that remote follows are accepted.
544 // Otherwise, the subscribed will be null
545 if !community.local {
546 community_view.subscribed = data.follow;
549 Ok(CommunityResponse { community_view })
553 #[async_trait::async_trait(?Send)]
554 impl Perform for GetFollowedCommunities {
555 type Response = GetFollowedCommunitiesResponse;
559 context: &Data<LemmyContext>,
560 _websocket_id: Option<ConnectionId>,
561 ) -> Result<GetFollowedCommunitiesResponse, LemmyError> {
562 let data: &GetFollowedCommunities = &self;
563 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
565 let user_id = user.id;
566 let communities = match blocking(context.pool(), move |conn| {
567 CommunityFollowerView::for_user(conn, user_id)
571 Ok(communities) => communities,
572 _ => return Err(ApiError::err("system_err_login").into()),
576 Ok(GetFollowedCommunitiesResponse { communities })
580 #[async_trait::async_trait(?Send)]
581 impl Perform for BanFromCommunity {
582 type Response = BanFromCommunityResponse;
586 context: &Data<LemmyContext>,
587 websocket_id: Option<ConnectionId>,
588 ) -> Result<BanFromCommunityResponse, LemmyError> {
589 let data: &BanFromCommunity = &self;
590 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
592 let community_id = data.community_id;
593 let banned_user_id = data.user_id;
595 // Verify that only mods or admins can ban
596 is_mod_or_admin(context.pool(), user.id, community_id).await?;
598 let community_user_ban_form = CommunityUserBanForm {
599 community_id: data.community_id,
600 user_id: data.user_id,
604 let ban = move |conn: &'_ _| CommunityUserBan::ban(conn, &community_user_ban_form);
605 if blocking(context.pool(), ban).await?.is_err() {
606 return Err(ApiError::err("community_user_already_banned").into());
609 // Also unsubscribe them from the community, if they are subscribed
610 let community_follower_form = CommunityFollowerForm {
611 community_id: data.community_id,
612 user_id: banned_user_id,
615 blocking(context.pool(), move |conn: &'_ _| {
616 CommunityFollower::unfollow(conn, &community_follower_form)
621 let unban = move |conn: &'_ _| CommunityUserBan::unban(conn, &community_user_ban_form);
622 if blocking(context.pool(), unban).await?.is_err() {
623 return Err(ApiError::err("community_user_already_banned").into());
627 // Remove/Restore their data if that's desired
628 if data.remove_data {
630 blocking(context.pool(), move |conn: &'_ _| {
631 Post::update_removed_for_creator(conn, banned_user_id, Some(community_id), true)
636 // TODO Diesel doesn't allow updates with joins, so this has to be a loop
637 let comments = blocking(context.pool(), move |conn| {
638 CommentQueryBuilder::create(conn)
639 .creator_id(banned_user_id)
640 .community_id(community_id)
641 .limit(std::i64::MAX)
646 for comment_view in &comments {
647 let comment_id = comment_view.comment.id;
648 blocking(context.pool(), move |conn: &'_ _| {
649 Comment::update_removed(conn, comment_id, true)
656 // TODO eventually do correct expires
657 let expires = match data.expires {
658 Some(time) => Some(naive_from_unix(time)),
662 let form = ModBanFromCommunityForm {
663 mod_user_id: user.id,
664 other_user_id: data.user_id,
665 community_id: data.community_id,
666 reason: data.reason.to_owned(),
667 banned: Some(data.ban),
670 blocking(context.pool(), move |conn| {
671 ModBanFromCommunity::create(conn, &form)
675 let user_id = data.user_id;
676 let user_view = blocking(context.pool(), move |conn| {
677 UserViewSafe::read(conn, user_id)
681 let res = BanFromCommunityResponse {
686 context.chat_server().do_send(SendCommunityRoomMessage {
687 op: UserOperation::BanFromCommunity,
688 response: res.clone(),
697 #[async_trait::async_trait(?Send)]
698 impl Perform for AddModToCommunity {
699 type Response = AddModToCommunityResponse;
703 context: &Data<LemmyContext>,
704 websocket_id: Option<ConnectionId>,
705 ) -> Result<AddModToCommunityResponse, LemmyError> {
706 let data: &AddModToCommunity = &self;
707 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
709 let community_moderator_form = CommunityModeratorForm {
710 community_id: data.community_id,
711 user_id: data.user_id,
714 let community_id = data.community_id;
716 // Verify that only mods or admins can add mod
717 is_mod_or_admin(context.pool(), user.id, community_id).await?;
720 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
721 if blocking(context.pool(), join).await?.is_err() {
722 return Err(ApiError::err("community_moderator_already_exists").into());
725 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
726 if blocking(context.pool(), leave).await?.is_err() {
727 return Err(ApiError::err("community_moderator_already_exists").into());
732 let form = ModAddCommunityForm {
733 mod_user_id: user.id,
734 other_user_id: data.user_id,
735 community_id: data.community_id,
736 removed: Some(!data.added),
738 blocking(context.pool(), move |conn| {
739 ModAddCommunity::create(conn, &form)
743 let community_id = data.community_id;
744 let moderators = blocking(context.pool(), move |conn| {
745 CommunityModeratorView::for_community(conn, community_id)
749 let res = AddModToCommunityResponse { moderators };
751 context.chat_server().do_send(SendCommunityRoomMessage {
752 op: UserOperation::AddModToCommunity,
753 response: res.clone(),
762 #[async_trait::async_trait(?Send)]
763 impl Perform for TransferCommunity {
764 type Response = GetCommunityResponse;
768 context: &Data<LemmyContext>,
769 _websocket_id: Option<ConnectionId>,
770 ) -> Result<GetCommunityResponse, LemmyError> {
771 let data: &TransferCommunity = &self;
772 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
774 let community_id = data.community_id;
775 let read_community = blocking(context.pool(), move |conn| {
776 Community::read(conn, community_id)
780 let site_creator_id = blocking(context.pool(), move |conn| {
781 Site::read(conn, 1).map(|s| s.creator_id)
785 let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
787 // Making sure the creator, if an admin, is at the top
788 let creator_index = admins
790 .position(|r| r.user.id == site_creator_id)
791 .context(location_info!())?;
792 let creator_user = admins.remove(creator_index);
793 admins.insert(0, creator_user);
795 // Make sure user is the creator, or an admin
796 if user.id != read_community.creator_id
797 && !admins.iter().map(|a| a.user.id).any(|x| x == user.id)
799 return Err(ApiError::err("not_an_admin").into());
802 let community_id = data.community_id;
803 let new_creator = data.user_id;
804 let update = move |conn: &'_ _| Community::update_creator(conn, community_id, new_creator);
805 if blocking(context.pool(), update).await?.is_err() {
806 return Err(ApiError::err("couldnt_update_community").into());
809 // You also have to re-do the community_moderator table, reordering it.
810 let community_id = data.community_id;
811 let mut community_mods = blocking(context.pool(), move |conn| {
812 CommunityModeratorView::for_community(conn, community_id)
815 let creator_index = community_mods
817 .position(|r| r.moderator.id == data.user_id)
818 .context(location_info!())?;
819 let creator_user = community_mods.remove(creator_index);
820 community_mods.insert(0, creator_user);
822 let community_id = data.community_id;
823 blocking(context.pool(), move |conn| {
824 CommunityModerator::delete_for_community(conn, community_id)
828 // TODO: this should probably be a bulk operation
829 for cmod in &community_mods {
830 let community_moderator_form = CommunityModeratorForm {
831 community_id: cmod.community.id,
832 user_id: cmod.moderator.id,
835 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
836 if blocking(context.pool(), join).await?.is_err() {
837 return Err(ApiError::err("community_moderator_already_exists").into());
842 let form = ModAddCommunityForm {
843 mod_user_id: user.id,
844 other_user_id: data.user_id,
845 community_id: data.community_id,
846 removed: Some(false),
848 blocking(context.pool(), move |conn| {
849 ModAddCommunity::create(conn, &form)
853 let community_id = data.community_id;
854 let user_id = user.id;
855 let community_view = match blocking(context.pool(), move |conn| {
856 CommunityView::read(conn, community_id, Some(user_id))
860 Ok(community) => community,
861 Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
864 let community_id = data.community_id;
865 let moderators = match blocking(context.pool(), move |conn| {
866 CommunityModeratorView::for_community(conn, community_id)
870 Ok(moderators) => moderators,
871 Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
875 Ok(GetCommunityResponse {
883 fn send_community_websocket(
884 res: &CommunityResponse,
885 context: &Data<LemmyContext>,
886 websocket_id: Option<ConnectionId>,
889 // Strip out the user id and subscribed when sending to others
890 let mut res_sent = res.clone();
891 res_sent.community_view.subscribed = false;
893 context.chat_server().do_send(SendCommunityRoomMessage {
896 community_id: res.community_view.community.id,