10 use actix_web::web::Data;
12 use lemmy_apub::ActorType;
13 use lemmy_db_queries::{
14 diesel_option_overwrite,
17 community::{CommunityModerator_, Community_},
28 use lemmy_db_schema::{
30 source::{comment::Comment, community::*, moderator::*, post::Post, site::*},
32 use lemmy_db_views::comment_view::CommentQueryBuilder;
33 use lemmy_db_views_actor::{
34 community_follower_view::CommunityFollowerView,
35 community_moderator_view::CommunityModeratorView,
36 community_view::{CommunityQueryBuilder, CommunityView},
37 user_view::UserViewSafe,
39 use lemmy_structs::{blocking, community::*};
41 apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
43 utils::{check_slurs, check_slurs_opt, is_valid_community_name, naive_from_unix},
48 use lemmy_websocket::{
49 messages::{GetCommunityUsersOnline, SendCommunityRoomMessage},
53 use std::str::FromStr;
55 #[async_trait::async_trait(?Send)]
56 impl Perform for GetCommunity {
57 type Response = GetCommunityResponse;
61 context: &Data<LemmyContext>,
62 _websocket_id: Option<ConnectionId>,
63 ) -> Result<GetCommunityResponse, LemmyError> {
64 let data: &GetCommunity = &self;
65 let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
66 let user_id = user.map(|u| u.id);
68 let community_id = match data.id {
71 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
72 match blocking(context.pool(), move |conn| {
73 Community::read_from_name(conn, &name)
77 Ok(community) => community,
78 Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
84 let community_view = match blocking(context.pool(), move |conn| {
85 CommunityView::read(conn, community_id, user_id)
89 Ok(community) => community,
90 Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
93 let moderators: Vec<CommunityModeratorView> = match blocking(context.pool(), move |conn| {
94 CommunityModeratorView::for_community(conn, community_id)
98 Ok(moderators) => moderators,
99 Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
104 .send(GetCommunityUsersOnline { community_id })
108 let res = GetCommunityResponse {
119 #[async_trait::async_trait(?Send)]
120 impl Perform for CreateCommunity {
121 type Response = CommunityResponse;
125 context: &Data<LemmyContext>,
126 _websocket_id: Option<ConnectionId>,
127 ) -> Result<CommunityResponse, LemmyError> {
128 let data: &CreateCommunity = &self;
129 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
131 check_slurs(&data.name)?;
132 check_slurs(&data.title)?;
133 check_slurs_opt(&data.description)?;
135 if !is_valid_community_name(&data.name) {
136 return Err(APIError::err("invalid_community_name").into());
139 // Double check for duplicate community actor_ids
140 let actor_id = make_apub_endpoint(EndpointType::Community, &data.name);
141 let actor_id_cloned = actor_id.to_owned();
142 let community_dupe = blocking(context.pool(), move |conn| {
143 Community::read_from_apub_id(conn, &actor_id_cloned.into())
146 if community_dupe.is_ok() {
147 return Err(APIError::err("community_already_exists").into());
150 // Check to make sure the icon and banners are urls
151 let icon = diesel_option_overwrite(&data.icon);
152 let banner = diesel_option_overwrite(&data.banner);
154 check_optional_url(&icon)?;
155 check_optional_url(&banner)?;
157 // When you create a community, make sure the user becomes a moderator and a follower
158 let keypair = generate_actor_keypair()?;
160 let community_form = CommunityForm {
161 name: data.name.to_owned(),
162 title: data.title.to_owned(),
163 description: data.description.to_owned(),
166 category_id: data.category_id,
172 actor_id: Some(actor_id.into()),
174 private_key: Some(keypair.private_key),
175 public_key: Some(keypair.public_key),
176 last_refreshed_at: None,
180 let inserted_community = match blocking(context.pool(), move |conn| {
181 Community::create(conn, &community_form)
185 Ok(community) => community,
186 Err(_e) => return Err(APIError::err("community_already_exists").into()),
189 // The community creator becomes a moderator
190 let community_moderator_form = CommunityModeratorForm {
191 community_id: inserted_community.id,
195 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
196 if blocking(context.pool(), join).await?.is_err() {
197 return Err(APIError::err("community_moderator_already_exists").into());
200 // Follow your own community
201 let community_follower_form = CommunityFollowerForm {
202 community_id: inserted_community.id,
207 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
208 if blocking(context.pool(), follow).await?.is_err() {
209 return Err(APIError::err("community_follower_already_exists").into());
212 let user_id = user.id;
213 let community_view = blocking(context.pool(), move |conn| {
214 CommunityView::read(conn, inserted_community.id, Some(user_id))
218 Ok(CommunityResponse { community_view })
222 #[async_trait::async_trait(?Send)]
223 impl Perform for EditCommunity {
224 type Response = CommunityResponse;
228 context: &Data<LemmyContext>,
229 websocket_id: Option<ConnectionId>,
230 ) -> Result<CommunityResponse, LemmyError> {
231 let data: &EditCommunity = &self;
232 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
234 check_slurs(&data.title)?;
235 check_slurs_opt(&data.description)?;
237 // Verify its a mod (only mods can edit it)
238 let community_id = data.community_id;
239 let mods: Vec<i32> = blocking(context.pool(), move |conn| {
240 CommunityModeratorView::for_community(conn, community_id)
241 .map(|v| v.into_iter().map(|m| m.moderator.id).collect())
244 if !mods.contains(&user.id) {
245 return Err(APIError::err("not_a_moderator").into());
248 let community_id = data.community_id;
249 let read_community = blocking(context.pool(), move |conn| {
250 Community::read(conn, community_id)
254 let icon = diesel_option_overwrite(&data.icon);
255 let banner = diesel_option_overwrite(&data.banner);
257 check_optional_url(&icon)?;
258 check_optional_url(&banner)?;
260 let community_form = CommunityForm {
261 name: read_community.name,
262 title: data.title.to_owned(),
263 description: data.description.to_owned(),
266 category_id: data.category_id.to_owned(),
267 creator_id: read_community.creator_id,
268 removed: Some(read_community.removed),
269 deleted: Some(read_community.deleted),
271 updated: Some(naive_now()),
272 actor_id: Some(read_community.actor_id),
273 local: read_community.local,
274 private_key: read_community.private_key,
275 public_key: read_community.public_key,
276 last_refreshed_at: None,
280 let community_id = data.community_id;
281 match blocking(context.pool(), move |conn| {
282 Community::update(conn, community_id, &community_form)
286 Ok(community) => community,
287 Err(_e) => return Err(APIError::err("couldnt_update_community").into()),
290 // TODO there needs to be some kind of an apub update
291 // process for communities and users
293 let community_id = data.community_id;
294 let user_id = user.id;
295 let community_view = blocking(context.pool(), move |conn| {
296 CommunityView::read(conn, community_id, Some(user_id))
300 let res = CommunityResponse { community_view };
302 send_community_websocket(&res, context, websocket_id, UserOperation::EditCommunity);
308 #[async_trait::async_trait(?Send)]
309 impl Perform for DeleteCommunity {
310 type Response = CommunityResponse;
314 context: &Data<LemmyContext>,
315 websocket_id: Option<ConnectionId>,
316 ) -> Result<CommunityResponse, LemmyError> {
317 let data: &DeleteCommunity = &self;
318 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
320 // Verify its the creator (only a creator can delete the community)
321 let community_id = data.community_id;
322 let read_community = blocking(context.pool(), move |conn| {
323 Community::read(conn, community_id)
326 if read_community.creator_id != user.id {
327 return Err(APIError::err("no_community_edit_allowed").into());
331 let community_id = data.community_id;
332 let deleted = data.deleted;
333 let updated_community = match blocking(context.pool(), move |conn| {
334 Community::update_deleted(conn, community_id, deleted)
338 Ok(community) => community,
339 Err(_e) => return Err(APIError::err("couldnt_update_community").into()),
342 // Send apub messages
344 updated_community.send_delete(context).await?;
346 updated_community.send_undo_delete(context).await?;
349 let community_id = data.community_id;
350 let user_id = user.id;
351 let community_view = blocking(context.pool(), move |conn| {
352 CommunityView::read(conn, community_id, Some(user_id))
356 let res = CommunityResponse { community_view };
358 send_community_websocket(&res, context, websocket_id, UserOperation::DeleteCommunity);
364 #[async_trait::async_trait(?Send)]
365 impl Perform for RemoveCommunity {
366 type Response = CommunityResponse;
370 context: &Data<LemmyContext>,
371 websocket_id: Option<ConnectionId>,
372 ) -> Result<CommunityResponse, LemmyError> {
373 let data: &RemoveCommunity = &self;
374 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
376 // Verify its an admin (only an admin can remove a community)
377 is_admin(context.pool(), user.id).await?;
380 let community_id = data.community_id;
381 let removed = data.removed;
382 let updated_community = match blocking(context.pool(), move |conn| {
383 Community::update_removed(conn, community_id, removed)
387 Ok(community) => community,
388 Err(_e) => return Err(APIError::err("couldnt_update_community").into()),
392 let expires = match data.expires {
393 Some(time) => Some(naive_from_unix(time)),
396 let form = ModRemoveCommunityForm {
397 mod_user_id: user.id,
398 community_id: data.community_id,
399 removed: Some(removed),
400 reason: data.reason.to_owned(),
403 blocking(context.pool(), move |conn| {
404 ModRemoveCommunity::create(conn, &form)
410 updated_community.send_remove(context).await?;
412 updated_community.send_undo_remove(context).await?;
415 let community_id = data.community_id;
416 let user_id = user.id;
417 let community_view = blocking(context.pool(), move |conn| {
418 CommunityView::read(conn, community_id, Some(user_id))
422 let res = CommunityResponse { community_view };
424 send_community_websocket(&res, context, websocket_id, UserOperation::RemoveCommunity);
430 #[async_trait::async_trait(?Send)]
431 impl Perform for ListCommunities {
432 type Response = ListCommunitiesResponse;
436 context: &Data<LemmyContext>,
437 _websocket_id: Option<ConnectionId>,
438 ) -> Result<ListCommunitiesResponse, LemmyError> {
439 let data: &ListCommunities = &self;
440 let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
442 let user_id = match &user {
443 Some(user) => Some(user.id),
447 let show_nsfw = match &user {
448 Some(user) => user.show_nsfw,
452 let type_ = ListingType::from_str(&data.type_)?;
453 let sort = SortType::from_str(&data.sort)?;
455 let page = data.page;
456 let limit = data.limit;
457 let communities = blocking(context.pool(), move |conn| {
458 CommunityQueryBuilder::create(conn)
459 .listing_type(&type_)
461 .show_nsfw(show_nsfw)
470 Ok(ListCommunitiesResponse { communities })
474 #[async_trait::async_trait(?Send)]
475 impl Perform for FollowCommunity {
476 type Response = CommunityResponse;
480 context: &Data<LemmyContext>,
481 _websocket_id: Option<ConnectionId>,
482 ) -> Result<CommunityResponse, LemmyError> {
483 let data: &FollowCommunity = &self;
484 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
486 let community_id = data.community_id;
487 let community = blocking(context.pool(), move |conn| {
488 Community::read(conn, community_id)
491 let community_follower_form = CommunityFollowerForm {
492 community_id: data.community_id,
499 check_community_ban(user.id, community_id, context.pool()).await?;
501 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
502 if blocking(context.pool(), follow).await?.is_err() {
503 return Err(APIError::err("community_follower_already_exists").into());
507 move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
508 if blocking(context.pool(), unfollow).await?.is_err() {
509 return Err(APIError::err("community_follower_already_exists").into());
512 } else if data.follow {
513 // Dont actually add to the community followers here, because you need
514 // to wait for the accept
515 user.send_follow(&community.actor_id(), context).await?;
517 user.send_unfollow(&community.actor_id(), context).await?;
518 let unfollow = 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());
524 let community_id = data.community_id;
525 let user_id = user.id;
526 let mut community_view = blocking(context.pool(), move |conn| {
527 CommunityView::read(conn, community_id, Some(user_id))
531 // TODO: this needs to return a "pending" state, until Accept is received from the remote server
532 // For now, just assume that remote follows are accepted.
533 // Otherwise, the subscribed will be null
534 if !community.local {
535 community_view.subscribed = data.follow;
538 Ok(CommunityResponse { community_view })
542 #[async_trait::async_trait(?Send)]
543 impl Perform for GetFollowedCommunities {
544 type Response = GetFollowedCommunitiesResponse;
548 context: &Data<LemmyContext>,
549 _websocket_id: Option<ConnectionId>,
550 ) -> Result<GetFollowedCommunitiesResponse, LemmyError> {
551 let data: &GetFollowedCommunities = &self;
552 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
554 let user_id = user.id;
555 let communities = match blocking(context.pool(), move |conn| {
556 CommunityFollowerView::for_user(conn, user_id)
560 Ok(communities) => communities,
561 _ => return Err(APIError::err("system_err_login").into()),
565 Ok(GetFollowedCommunitiesResponse { communities })
569 #[async_trait::async_trait(?Send)]
570 impl Perform for BanFromCommunity {
571 type Response = BanFromCommunityResponse;
575 context: &Data<LemmyContext>,
576 websocket_id: Option<ConnectionId>,
577 ) -> Result<BanFromCommunityResponse, LemmyError> {
578 let data: &BanFromCommunity = &self;
579 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
581 let community_id = data.community_id;
582 let banned_user_id = data.user_id;
584 // Verify that only mods or admins can ban
585 is_mod_or_admin(context.pool(), user.id, community_id).await?;
587 let community_user_ban_form = CommunityUserBanForm {
588 community_id: data.community_id,
589 user_id: data.user_id,
593 let ban = move |conn: &'_ _| CommunityUserBan::ban(conn, &community_user_ban_form);
594 if blocking(context.pool(), ban).await?.is_err() {
595 return Err(APIError::err("community_user_already_banned").into());
598 // Also unsubscribe them from the community, if they are subscribed
599 let community_follower_form = CommunityFollowerForm {
600 community_id: data.community_id,
601 user_id: banned_user_id,
604 blocking(context.pool(), move |conn: &'_ _| {
605 CommunityFollower::unfollow(conn, &community_follower_form)
610 let unban = move |conn: &'_ _| CommunityUserBan::unban(conn, &community_user_ban_form);
611 if blocking(context.pool(), unban).await?.is_err() {
612 return Err(APIError::err("community_user_already_banned").into());
616 // Remove/Restore their data if that's desired
617 if data.remove_data {
619 blocking(context.pool(), move |conn: &'_ _| {
620 Post::update_removed_for_creator(conn, banned_user_id, Some(community_id), true)
625 // TODO Diesel doesn't allow updates with joins, so this has to be a loop
626 let comments = blocking(context.pool(), move |conn| {
627 CommentQueryBuilder::create(conn)
628 .creator_id(banned_user_id)
629 .community_id(community_id)
630 .limit(std::i64::MAX)
635 for comment_view in &comments {
636 let comment_id = comment_view.comment.id;
637 blocking(context.pool(), move |conn: &'_ _| {
638 Comment::update_removed(conn, comment_id, true)
645 // TODO eventually do correct expires
646 let expires = match data.expires {
647 Some(time) => Some(naive_from_unix(time)),
651 let form = ModBanFromCommunityForm {
652 mod_user_id: user.id,
653 other_user_id: data.user_id,
654 community_id: data.community_id,
655 reason: data.reason.to_owned(),
656 banned: Some(data.ban),
659 blocking(context.pool(), move |conn| {
660 ModBanFromCommunity::create(conn, &form)
664 let user_id = data.user_id;
665 let user_view = blocking(context.pool(), move |conn| {
666 UserViewSafe::read(conn, user_id)
670 let res = BanFromCommunityResponse {
675 context.chat_server().do_send(SendCommunityRoomMessage {
676 op: UserOperation::BanFromCommunity,
677 response: res.clone(),
686 #[async_trait::async_trait(?Send)]
687 impl Perform for AddModToCommunity {
688 type Response = AddModToCommunityResponse;
692 context: &Data<LemmyContext>,
693 websocket_id: Option<ConnectionId>,
694 ) -> Result<AddModToCommunityResponse, LemmyError> {
695 let data: &AddModToCommunity = &self;
696 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
698 let community_moderator_form = CommunityModeratorForm {
699 community_id: data.community_id,
700 user_id: data.user_id,
703 let community_id = data.community_id;
705 // Verify that only mods or admins can add mod
706 is_mod_or_admin(context.pool(), user.id, community_id).await?;
709 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
710 if blocking(context.pool(), join).await?.is_err() {
711 return Err(APIError::err("community_moderator_already_exists").into());
714 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
715 if blocking(context.pool(), leave).await?.is_err() {
716 return Err(APIError::err("community_moderator_already_exists").into());
721 let form = ModAddCommunityForm {
722 mod_user_id: user.id,
723 other_user_id: data.user_id,
724 community_id: data.community_id,
725 removed: Some(!data.added),
727 blocking(context.pool(), move |conn| {
728 ModAddCommunity::create(conn, &form)
732 let community_id = data.community_id;
733 let moderators = blocking(context.pool(), move |conn| {
734 CommunityModeratorView::for_community(conn, community_id)
738 let res = AddModToCommunityResponse { moderators };
740 context.chat_server().do_send(SendCommunityRoomMessage {
741 op: UserOperation::AddModToCommunity,
742 response: res.clone(),
751 #[async_trait::async_trait(?Send)]
752 impl Perform for TransferCommunity {
753 type Response = GetCommunityResponse;
757 context: &Data<LemmyContext>,
758 _websocket_id: Option<ConnectionId>,
759 ) -> Result<GetCommunityResponse, LemmyError> {
760 let data: &TransferCommunity = &self;
761 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
763 let community_id = data.community_id;
764 let read_community = blocking(context.pool(), move |conn| {
765 Community::read(conn, community_id)
769 let site_creator_id = blocking(context.pool(), move |conn| {
770 Site::read(conn, 1).map(|s| s.creator_id)
774 let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
776 // Making sure the creator, if an admin, is at the top
777 let creator_index = admins
779 .position(|r| r.user.id == site_creator_id)
780 .context(location_info!())?;
781 let creator_user = admins.remove(creator_index);
782 admins.insert(0, creator_user);
784 // Make sure user is the creator, or an admin
785 if user.id != read_community.creator_id
786 && !admins.iter().map(|a| a.user.id).any(|x| x == user.id)
788 return Err(APIError::err("not_an_admin").into());
791 let community_id = data.community_id;
792 let new_creator = data.user_id;
793 let update = move |conn: &'_ _| Community::update_creator(conn, community_id, new_creator);
794 if blocking(context.pool(), update).await?.is_err() {
795 return Err(APIError::err("couldnt_update_community").into());
798 // You also have to re-do the community_moderator table, reordering it.
799 let community_id = data.community_id;
800 let mut community_mods = blocking(context.pool(), move |conn| {
801 CommunityModeratorView::for_community(conn, community_id)
804 let creator_index = community_mods
806 .position(|r| r.moderator.id == data.user_id)
807 .context(location_info!())?;
808 let creator_user = community_mods.remove(creator_index);
809 community_mods.insert(0, creator_user);
811 let community_id = data.community_id;
812 blocking(context.pool(), move |conn| {
813 CommunityModerator::delete_for_community(conn, community_id)
817 // TODO: this should probably be a bulk operation
818 for cmod in &community_mods {
819 let community_moderator_form = CommunityModeratorForm {
820 community_id: cmod.community.id,
821 user_id: cmod.moderator.id,
824 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
825 if blocking(context.pool(), join).await?.is_err() {
826 return Err(APIError::err("community_moderator_already_exists").into());
831 let form = ModAddCommunityForm {
832 mod_user_id: user.id,
833 other_user_id: data.user_id,
834 community_id: data.community_id,
835 removed: Some(false),
837 blocking(context.pool(), move |conn| {
838 ModAddCommunity::create(conn, &form)
842 let community_id = data.community_id;
843 let user_id = user.id;
844 let community_view = match blocking(context.pool(), move |conn| {
845 CommunityView::read(conn, community_id, Some(user_id))
849 Ok(community) => community,
850 Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
853 let community_id = data.community_id;
854 let moderators = match blocking(context.pool(), move |conn| {
855 CommunityModeratorView::for_community(conn, community_id)
859 Ok(moderators) => moderators,
860 Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
864 Ok(GetCommunityResponse {
872 fn send_community_websocket(
873 res: &CommunityResponse,
874 context: &Data<LemmyContext>,
875 websocket_id: Option<ConnectionId>,
878 // Strip out the user id and subscribed when sending to others
879 let mut res_sent = res.clone();
880 res_sent.community_view.subscribed = false;
882 context.chat_server().do_send(SendCommunityRoomMessage {
885 community_id: res.community_view.community.id,