]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/read.rs
2ec4054b25e30ad4cd49bbaca9a85489cf3663c9
[lemmy.git] / crates / api_crud / src / community / read.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   check_private_instance,
6   community::*,
7   get_local_user_view_from_jwt_opt,
8 };
9 use lemmy_apub::{
10   fetcher::webfinger::webfinger_resolve,
11   objects::community::ApubCommunity,
12   EndpointType,
13 };
14 use lemmy_apub_lib::object_id::ObjectId;
15 use lemmy_db_schema::{
16   from_opt_str_to_opt_enum,
17   traits::DeleteableOrRemoveable,
18   ListingType,
19   SortType,
20 };
21 use lemmy_db_views_actor::{
22   community_moderator_view::CommunityModeratorView,
23   community_view::{CommunityQueryBuilder, CommunityView},
24 };
25 use lemmy_utils::{ConnectionId, LemmyError};
26 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
27
28 #[async_trait::async_trait(?Send)]
29 impl PerformCrud for GetCommunity {
30   type Response = GetCommunityResponse;
31
32   #[tracing::instrument(skip(context, _websocket_id))]
33   async fn perform(
34     &self,
35     context: &Data<LemmyContext>,
36     _websocket_id: Option<ConnectionId>,
37   ) -> Result<GetCommunityResponse, LemmyError> {
38     let data: &GetCommunity = self;
39     let local_user_view =
40       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
41         .await?;
42
43     check_private_instance(&local_user_view, context.pool()).await?;
44
45     let person_id = local_user_view.map(|u| u.person.id);
46
47     let community_id = match data.id {
48       Some(id) => id,
49       None => {
50         let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
51         let community_actor_id =
52           webfinger_resolve::<ApubCommunity>(&name, EndpointType::Community, context, &mut 0)
53             .await?;
54
55         ObjectId::<ApubCommunity>::new(community_actor_id)
56           .dereference(context, context.client(), &mut 0)
57           .await
58           .map_err(LemmyError::from)
59           .map_err(|e| e.with_message("couldnt_find_community"))?
60           .id
61       }
62     };
63
64     let mut community_view = blocking(context.pool(), move |conn| {
65       CommunityView::read(conn, community_id, person_id)
66     })
67     .await?
68     .map_err(LemmyError::from)
69     .map_err(|e| e.with_message("couldnt_find_community"))?;
70
71     // Blank out deleted or removed info for non-logged in users
72     if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
73     {
74       community_view.community = community_view.community.blank_out_deleted_or_removed_info();
75     }
76
77     let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
78       CommunityModeratorView::for_community(conn, community_id)
79     })
80     .await?
81     .map_err(LemmyError::from)
82     .map_err(|e| e.with_message("couldnt_find_community"))?;
83
84     let online = context
85       .chat_server()
86       .send(GetCommunityUsersOnline { community_id })
87       .await
88       .unwrap_or(1);
89
90     let res = GetCommunityResponse {
91       community_view,
92       moderators,
93       online,
94     };
95
96     // Return the jwt
97     Ok(res)
98   }
99 }
100
101 #[async_trait::async_trait(?Send)]
102 impl PerformCrud for ListCommunities {
103   type Response = ListCommunitiesResponse;
104
105   #[tracing::instrument(skip(context, _websocket_id))]
106   async fn perform(
107     &self,
108     context: &Data<LemmyContext>,
109     _websocket_id: Option<ConnectionId>,
110   ) -> Result<ListCommunitiesResponse, LemmyError> {
111     let data: &ListCommunities = self;
112     let local_user_view =
113       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
114         .await?;
115
116     check_private_instance(&local_user_view, context.pool()).await?;
117
118     let person_id = local_user_view.to_owned().map(|l| l.person.id);
119
120     // Don't show NSFW by default
121     let show_nsfw = match &local_user_view {
122       Some(uv) => uv.local_user.show_nsfw,
123       None => false,
124     };
125
126     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
127     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
128
129     let page = data.page;
130     let limit = data.limit;
131     let mut communities = blocking(context.pool(), move |conn| {
132       CommunityQueryBuilder::create(conn)
133         .listing_type(listing_type)
134         .sort(sort)
135         .show_nsfw(show_nsfw)
136         .my_person_id(person_id)
137         .page(page)
138         .limit(limit)
139         .list()
140     })
141     .await??;
142
143     // Blank out deleted or removed info for non-logged in users
144     if person_id.is_none() {
145       for cv in communities
146         .iter_mut()
147         .filter(|cv| cv.community.deleted || cv.community.removed)
148       {
149         cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
150       }
151     }
152
153     // Return the jwt
154     Ok(ListCommunitiesResponse { communities })
155   }
156 }