2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt_opt};
|
2021-11-05 00:24:10 +00:00
|
|
|
use lemmy_apub::{get_actor_id_from_name, objects::community::ApubCommunity};
|
|
|
|
use lemmy_apub_lib::{object_id::ObjectId, webfinger::WebfingerType};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
from_opt_str_to_opt_enum,
|
|
|
|
traits::DeleteableOrRemoveable,
|
|
|
|
ListingType,
|
|
|
|
SortType,
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_db_views_actor::{
|
|
|
|
community_moderator_view::CommunityModeratorView,
|
|
|
|
community_view::{CommunityQueryBuilder, CommunityView},
|
|
|
|
};
|
|
|
|
use lemmy_utils::{ApiError, ConnectionId, LemmyError};
|
|
|
|
use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for GetCommunity {
|
|
|
|
type Response = GetCommunityResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<GetCommunityResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &GetCommunity = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
let person_id = local_user_view.map(|u| u.person.id);
|
|
|
|
|
|
|
|
let community_id = match data.id {
|
|
|
|
Some(id) => id,
|
|
|
|
None => {
|
|
|
|
let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
|
2021-09-22 15:57:09 +00:00
|
|
|
let community_actor_id =
|
2021-10-25 16:09:21 +00:00
|
|
|
get_actor_id_from_name(WebfingerType::Group, &name, context).await?;
|
2021-07-20 04:29:50 +00:00
|
|
|
|
2021-10-18 21:36:44 +00:00
|
|
|
ObjectId::<ApubCommunity>::new(community_actor_id)
|
2021-10-06 20:20:05 +00:00
|
|
|
.dereference(context, &mut 0)
|
|
|
|
.await
|
2021-10-13 19:50:21 +00:00
|
|
|
.map_err(|e| ApiError::err("couldnt_find_community", e))?
|
2021-10-06 20:20:05 +00:00
|
|
|
.id
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-30 18:44:15 +00:00
|
|
|
let mut community_view = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
CommunityView::read(conn, community_id, person_id)
|
|
|
|
})
|
|
|
|
.await?
|
2021-10-13 19:50:21 +00:00
|
|
|
.map_err(|e| ApiError::err("couldnt_find_community", e))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-10-27 13:34:18 +00:00
|
|
|
// Blank out deleted or removed info for non-logged in users
|
|
|
|
if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
|
|
|
|
{
|
2021-07-30 18:44:15 +00:00
|
|
|
community_view.community = community_view.community.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
|
|
|
|
2021-04-16 13:10:43 +00:00
|
|
|
let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
CommunityModeratorView::for_community(conn, community_id)
|
|
|
|
})
|
|
|
|
.await?
|
2021-10-13 19:50:21 +00:00
|
|
|
.map_err(|e| ApiError::err("couldnt_find_community", e))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
let online = context
|
|
|
|
.chat_server()
|
|
|
|
.send(GetCommunityUsersOnline { community_id })
|
|
|
|
.await
|
|
|
|
.unwrap_or(1);
|
|
|
|
|
|
|
|
let res = GetCommunityResponse {
|
|
|
|
community_view,
|
|
|
|
moderators,
|
|
|
|
online,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Return the jwt
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for ListCommunities {
|
|
|
|
type Response = ListCommunitiesResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<ListCommunitiesResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &ListCommunities = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
let person_id = local_user_view.to_owned().map(|l| l.person.id);
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Don't show NSFW by default
|
|
|
|
let show_nsfw = match &local_user_view {
|
|
|
|
Some(uv) => uv.local_user.show_nsfw,
|
|
|
|
None => false,
|
|
|
|
};
|
|
|
|
|
2021-04-15 03:37:51 +00:00
|
|
|
let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
|
|
|
|
let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
2021-07-30 18:44:15 +00:00
|
|
|
let mut communities = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
CommunityQueryBuilder::create(conn)
|
2021-04-15 03:37:51 +00:00
|
|
|
.listing_type(listing_type)
|
|
|
|
.sort(sort)
|
2021-03-25 19:19:40 +00:00
|
|
|
.show_nsfw(show_nsfw)
|
|
|
|
.my_person_id(person_id)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-10-27 13:34:18 +00:00
|
|
|
// Blank out deleted or removed info for non-logged in users
|
|
|
|
if person_id.is_none() {
|
|
|
|
for cv in communities
|
|
|
|
.iter_mut()
|
|
|
|
.filter(|cv| cv.community.deleted || cv.community.removed)
|
|
|
|
{
|
|
|
|
cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
2021-07-30 18:44:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
// Return the jwt
|
|
|
|
Ok(ListCommunitiesResponse { communities })
|
|
|
|
}
|
|
|
|
}
|