]> Untitled Git - lemmy.git/blobdiff - crates/api_crud/src/post/read.rs
First pass at invite-only migration. (#1949)
[lemmy.git] / crates / api_crud / src / post / read.rs
index d2a46f53e5f5dd5a8554996dfcc50098087f5ee5..10ecefc38793a34d24bd87c6befd53f6de7aae08 100644 (file)
@@ -1,8 +1,17 @@
 use crate::PerformCrud;
 use actix_web::web::Data;
-use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, mark_post_as_read, post::*};
-use lemmy_apub::get_actor_id_from_name;
-use lemmy_apub_lib::webfinger::WebfingerType;
+use lemmy_api_common::{
+  blocking,
+  check_private_instance,
+  get_local_user_view_from_jwt_opt,
+  mark_post_as_read,
+  post::*,
+};
+use lemmy_apub::{
+  fetcher::webfinger::webfinger_resolve,
+  objects::community::ApubCommunity,
+  EndpointType,
+};
 use lemmy_db_schema::{
   from_opt_str_to_opt_enum,
   traits::DeleteableOrRemoveable,
@@ -17,13 +26,14 @@ use lemmy_db_views_actor::{
   community_moderator_view::CommunityModeratorView,
   community_view::CommunityView,
 };
-use lemmy_utils::{ApiError, ConnectionId, LemmyError};
+use lemmy_utils::{ConnectionId, LemmyError};
 use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
 
 #[async_trait::async_trait(?Send)]
 impl PerformCrud for GetPost {
   type Response = GetPostResponse;
 
+  #[tracing::instrument(skip(context, _websocket_id))]
   async fn perform(
     &self,
     context: &Data<LemmyContext>,
@@ -31,7 +41,10 @@ impl PerformCrud for GetPost {
   ) -> Result<GetPostResponse, LemmyError> {
     let data: &GetPost = self;
     let local_user_view =
-      get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
+      get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
+        .await?;
+
+    check_private_instance(&local_user_view, context.pool()).await?;
 
     let show_bot_accounts = local_user_view
       .as_ref()
@@ -43,7 +56,8 @@ impl PerformCrud for GetPost {
       PostView::read(conn, id, person_id)
     })
     .await?
-    .map_err(|e| ApiError::err("couldnt_find_post", e))?;
+    .map_err(LemmyError::from)
+    .map_err(|e| e.with_message("couldnt_find_post"))?;
 
     // Mark the post as read
     if let Some(person_id) = person_id {
@@ -67,7 +81,8 @@ impl PerformCrud for GetPost {
       CommunityView::read(conn, community_id, person_id)
     })
     .await?
-    .map_err(|e| ApiError::err("couldnt_find_community", e))?;
+    .map_err(LemmyError::from)
+    .map_err(|e| e.with_message("couldnt_find_community"))?;
 
     // Blank out deleted or removed info for non-logged in users
     if person_id.is_none() {
@@ -112,6 +127,7 @@ impl PerformCrud for GetPost {
 impl PerformCrud for GetPosts {
   type Response = GetPostsResponse;
 
+  #[tracing::instrument(skip(context, _websocket_id))]
   async fn perform(
     &self,
     context: &Data<LemmyContext>,
@@ -119,7 +135,10 @@ impl PerformCrud for GetPosts {
   ) -> Result<GetPostsResponse, LemmyError> {
     let data: &GetPosts = self;
     let local_user_view =
-      get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
+      get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
+        .await?;
+
+    check_private_instance(&local_user_view, context.pool()).await?;
 
     let person_id = local_user_view.to_owned().map(|l| l.person.id);
 
@@ -138,7 +157,7 @@ impl PerformCrud for GetPosts {
     let limit = data.limit;
     let community_id = data.community_id;
     let community_actor_id = if let Some(name) = &data.community_name {
-      get_actor_id_from_name(WebfingerType::Group, name, context)
+      webfinger_resolve::<ApubCommunity>(name, EndpointType::Community, context, &mut 0)
         .await
         .ok()
     } else {
@@ -162,7 +181,8 @@ impl PerformCrud for GetPosts {
         .list()
     })
     .await?
-    .map_err(|e| ApiError::err("couldnt_get_posts", e))?;
+    .map_err(LemmyError::from)
+    .map_err(|e| e.with_message("couldnt_get_posts"))?;
 
     // Blank out deleted or removed info for non-logged in users
     if person_id.is_none() {