Remove PerformApub trait (#3423)

* Remove PerformApub trait

This is completely useless now that websocket is gone. In the future
I also plan to remove Perform and PerformCrud traits, but it will be
difficult to do that while still compiling crates in parallel.

* params need to use query
pull/3441/head
Nutomic 2023-07-03 11:01:41 +02:00 committed by GitHub
parent 682ca55e0c
commit 3578dab67f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 458 additions and 553 deletions

View File

@ -5,8 +5,6 @@ use lemmy_api_common::{
CommentResponse, CommentResponse,
DistinguishComment, DistinguishComment,
GetComment, GetComment,
GetComments,
GetCommentsResponse,
ListCommentReports, ListCommentReports,
ListCommentReportsResponse, ListCommentReportsResponse,
ResolveCommentReport, ResolveCommentReport,
@ -15,7 +13,6 @@ use lemmy_api_common::{
community::{ community::{
CommunityResponse, CommunityResponse,
CreateCommunity, CreateCommunity,
GetCommunity,
GetCommunityResponse, GetCommunityResponse,
ListCommunities, ListCommunities,
ListCommunitiesResponse, ListCommunitiesResponse,
@ -39,8 +36,6 @@ use lemmy_api_common::{
GetBannedPersons, GetBannedPersons,
GetCaptcha, GetCaptcha,
GetCaptchaResponse, GetCaptchaResponse,
GetPersonDetails,
GetPersonDetailsResponse,
GetPersonMentions, GetPersonMentions,
GetPersonMentionsResponse, GetPersonMentionsResponse,
GetReplies, GetReplies,
@ -66,8 +61,6 @@ use lemmy_api_common::{
post::{ post::{
GetPost, GetPost,
GetPostResponse, GetPostResponse,
GetPosts,
GetPostsResponse,
GetSiteMetadata, GetSiteMetadata,
GetSiteMetadataResponse, GetSiteMetadataResponse,
ListPostReports, ListPostReports,
@ -110,10 +103,6 @@ use lemmy_api_common::{
PurgePerson, PurgePerson,
PurgePost, PurgePost,
RegistrationApplicationResponse, RegistrationApplicationResponse,
ResolveObject,
ResolveObjectResponse,
Search,
SearchResponse,
SiteResponse, SiteResponse,
}, },
}; };
@ -122,10 +111,6 @@ impl SendActivity for Register {
type Response = LoginResponse; type Response = LoginResponse;
} }
impl SendActivity for GetPersonDetails {
type Response = GetPersonDetailsResponse;
}
impl SendActivity for GetPrivateMessages { impl SendActivity for GetPrivateMessages {
type Response = PrivateMessagesResponse; type Response = PrivateMessagesResponse;
} }
@ -142,10 +127,6 @@ impl SendActivity for GetSite {
type Response = GetSiteResponse; type Response = GetSiteResponse;
} }
impl SendActivity for GetCommunity {
type Response = GetCommunityResponse;
}
impl SendActivity for ListCommunities { impl SendActivity for ListCommunities {
type Response = ListCommunitiesResponse; type Response = ListCommunitiesResponse;
} }
@ -158,18 +139,10 @@ impl SendActivity for GetPost {
type Response = GetPostResponse; type Response = GetPostResponse;
} }
impl SendActivity for GetPosts {
type Response = GetPostsResponse;
}
impl SendActivity for GetComment { impl SendActivity for GetComment {
type Response = CommentResponse; type Response = CommentResponse;
} }
impl SendActivity for GetComments {
type Response = GetCommentsResponse;
}
impl SendActivity for Login { impl SendActivity for Login {
type Response = LoginResponse; type Response = LoginResponse;
} }
@ -286,14 +259,6 @@ impl SendActivity for PurgeComment {
type Response = PurgeItemResponse; type Response = PurgeItemResponse;
} }
impl SendActivity for Search {
type Response = SearchResponse;
}
impl SendActivity for ResolveObject {
type Response = ResolveObjectResponse;
}
impl SendActivity for TransferCommunity { impl SendActivity for TransferCommunity {
type Response = GetCommunityResponse; type Response = GetCommunityResponse;
} }

View File

@ -1,9 +1,10 @@
use crate::{ use crate::{
api::{listing_type_with_default, PerformApub}, api::listing_type_with_default,
fetcher::resolve_actor_identifier, fetcher::resolve_actor_identifier,
objects::community::ApubCommunity, objects::community::ApubCommunity,
}; };
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use actix_web::web::{Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
comment::{GetComments, GetCommentsResponse}, comment::{GetComments, GetCommentsResponse},
context::LemmyContext, context::LemmyContext,
@ -16,19 +17,17 @@ use lemmy_db_schema::{
use lemmy_db_views::comment_view::CommentQuery; use lemmy_db_views::comment_view::CommentQuery;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[async_trait::async_trait] #[tracing::instrument(skip(context))]
impl PerformApub for GetComments { pub async fn list_comments(
type Response = GetCommentsResponse; data: Query<GetComments>,
context: Data<LemmyContext>,
#[tracing::instrument(skip(context))] ) -> Result<Json<GetCommentsResponse>, LemmyError> {
async fn perform(&self, context: &Data<LemmyContext>) -> Result<GetCommentsResponse, LemmyError> { let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
let data: &GetComments = self;
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
let local_site = LocalSite::read(context.pool()).await?; let local_site = LocalSite::read(context.pool()).await?;
check_private_instance(&local_user_view, &local_site)?; check_private_instance(&local_user_view, &local_site)?;
let community_id = if let Some(name) = &data.community_name { let community_id = if let Some(name) = &data.community_name {
resolve_actor_identifier::<ApubCommunity, Community>(name, context, &None, true) resolve_actor_identifier::<ApubCommunity, Community>(name, &context, &None, true)
.await .await
.ok() .ok()
.map(|c| c.id) .map(|c| c.id)
@ -71,6 +70,5 @@ impl PerformApub for GetComments {
.await .await
.map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?; .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
Ok(GetCommentsResponse { comments }) Ok(Json(GetCommentsResponse { comments }))
}
} }

View File

@ -1,9 +1,10 @@
use crate::{ use crate::{
api::{listing_type_with_default, PerformApub}, api::listing_type_with_default,
fetcher::resolve_actor_identifier, fetcher::resolve_actor_identifier,
objects::community::ApubCommunity, objects::community::ApubCommunity,
}; };
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use actix_web::web::{Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{GetPosts, GetPostsResponse}, post::{GetPosts, GetPostsResponse},
@ -13,14 +14,12 @@ use lemmy_db_schema::source::{community::Community, local_site::LocalSite};
use lemmy_db_views::post_view::PostQuery; use lemmy_db_views::post_view::PostQuery;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[async_trait::async_trait] #[tracing::instrument(skip(context))]
impl PerformApub for GetPosts { pub async fn list_posts(
type Response = GetPostsResponse; data: Query<GetPosts>,
context: Data<LemmyContext>,
#[tracing::instrument(skip(context))] ) -> Result<Json<GetPostsResponse>, LemmyError> {
async fn perform(&self, context: &Data<LemmyContext>) -> Result<GetPostsResponse, LemmyError> { let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
let data: &GetPosts = self;
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
let local_site = LocalSite::read(context.pool()).await?; let local_site = LocalSite::read(context.pool()).await?;
check_private_instance(&local_user_view, &local_site)?; check_private_instance(&local_user_view, &local_site)?;
@ -30,7 +29,7 @@ impl PerformApub for GetPosts {
let page = data.page; let page = data.page;
let limit = data.limit; let limit = data.limit;
let community_id = if let Some(name) = &data.community_name { let community_id = if let Some(name) = &data.community_name {
resolve_actor_identifier::<ApubCommunity, Community>(name, context, &None, true) resolve_actor_identifier::<ApubCommunity, Community>(name, &context, &None, true)
.await .await
.ok() .ok()
.map(|c| c.id) .map(|c| c.id)
@ -41,8 +40,7 @@ impl PerformApub for GetPosts {
let listing_type = listing_type_with_default(data.type_, &local_site, community_id)?; let listing_type = listing_type_with_default(data.type_, &local_site, community_id)?;
let is_mod_or_admin = let is_mod_or_admin = is_mod_or_admin_opt(context.pool(), local_user_view.as_ref(), community_id)
is_mod_or_admin_opt(context.pool(), local_user_view.as_ref(), community_id)
.await .await
.is_ok(); .is_ok();
@ -61,6 +59,5 @@ impl PerformApub for GetPosts {
.await .await
.map_err(|e| LemmyError::from_error_message(e, "couldnt_get_posts"))?; .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_posts"))?;
Ok(GetPostsResponse { posts }) Ok(Json(GetPostsResponse { posts }))
}
} }

View File

@ -1,21 +1,12 @@
use activitypub_federation::config::Data;
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{newtypes::CommunityId, source::local_site::LocalSite, ListingType}; use lemmy_db_schema::{newtypes::CommunityId, source::local_site::LocalSite, ListingType};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
mod list_comments; pub mod list_comments;
mod list_posts; pub mod list_posts;
mod read_community; pub mod read_community;
mod read_person; pub mod read_person;
mod resolve_object; pub mod resolve_object;
mod search; pub mod search;
#[async_trait::async_trait]
pub trait PerformApub {
type Response: serde::ser::Serialize + Send;
async fn perform(&self, context: &Data<LemmyContext>) -> Result<Self::Response, LemmyError>;
}
/// Returns default listing type, depending if the query is for frontpage or community. /// Returns default listing type, depending if the query is for frontpage or community.
fn listing_type_with_default( fn listing_type_with_default(

View File

@ -1,9 +1,6 @@
use crate::{ use crate::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
api::PerformApub,
fetcher::resolve_actor_identifier,
objects::community::ApubCommunity,
};
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use actix_web::web::{Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
community::{GetCommunity, GetCommunityResponse}, community::{GetCommunity, GetCommunityResponse},
context::LemmyContext, context::LemmyContext,
@ -18,17 +15,12 @@ use lemmy_db_schema::source::{
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView}; use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[async_trait::async_trait] #[tracing::instrument(skip(context))]
impl PerformApub for GetCommunity { pub async fn read_community(
type Response = GetCommunityResponse; data: Query<GetCommunity>,
context: Data<LemmyContext>,
#[tracing::instrument(skip(context))] ) -> Result<Json<GetCommunityResponse>, LemmyError> {
async fn perform( let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
&self,
context: &Data<LemmyContext>,
) -> Result<GetCommunityResponse, LemmyError> {
let data: &GetCommunity = self;
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
let local_site = LocalSite::read(context.pool()).await?; let local_site = LocalSite::read(context.pool()).await?;
if data.name.is_none() && data.id.is_none() { if data.name.is_none() && data.id.is_none() {
@ -43,7 +35,7 @@ impl PerformApub for GetCommunity {
Some(id) => id, Some(id) => id,
None => { None => {
let name = data.name.clone().unwrap_or_else(|| "main".to_string()); let name = data.name.clone().unwrap_or_else(|| "main".to_string());
resolve_actor_identifier::<ApubCommunity, Community>(&name, context, &local_user_view, true) resolve_actor_identifier::<ApubCommunity, Community>(&name, &context, &local_user_view, true)
.await .await
.map_err(|e| e.with_message("couldnt_find_community"))? .map_err(|e| e.with_message("couldnt_find_community"))?
.id .id
@ -68,8 +60,7 @@ impl PerformApub for GetCommunity {
.await .await
.map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
let site_id = let site_id = Site::instance_actor_id_from_url(community_view.community.actor_id.clone().into());
Site::instance_actor_id_from_url(community_view.community.actor_id.clone().into());
let mut site = Site::read_from_apub_id(context.pool(), &site_id.into()).await?; let mut site = Site::read_from_apub_id(context.pool(), &site_id.into()).await?;
// no need to include metadata for local site (its already available through other endpoints). // no need to include metadata for local site (its already available through other endpoints).
// this also prevents us from leaking the federation private key. // this also prevents us from leaking the federation private key.
@ -82,14 +73,10 @@ impl PerformApub for GetCommunity {
let community_id = community_view.community.id; let community_id = community_view.community.id;
let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?; let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?;
let res = GetCommunityResponse { Ok(Json(GetCommunityResponse {
community_view, community_view,
site, site,
moderators, moderators,
discussion_languages, discussion_languages,
}; }))
// Return the jwt
Ok(res)
}
} }

View File

@ -1,5 +1,6 @@
use crate::{api::PerformApub, fetcher::resolve_actor_identifier, objects::person::ApubPerson}; use crate::{fetcher::resolve_actor_identifier, objects::person::ApubPerson};
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use actix_web::web::{Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{GetPersonDetails, GetPersonDetailsResponse}, person::{GetPersonDetails, GetPersonDetailsResponse},
@ -13,23 +14,17 @@ use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery};
use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonView}; use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonView};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[async_trait::async_trait] #[tracing::instrument(skip(context))]
impl PerformApub for GetPersonDetails { pub async fn read_person(
type Response = GetPersonDetailsResponse; data: Query<GetPersonDetails>,
context: Data<LemmyContext>,
#[tracing::instrument(skip(self, context))] ) -> Result<Json<GetPersonDetailsResponse>, LemmyError> {
async fn perform(
&self,
context: &Data<LemmyContext>,
) -> Result<GetPersonDetailsResponse, LemmyError> {
let data: &GetPersonDetails = self;
// Check to make sure a person name or an id is given // Check to make sure a person name or an id is given
if data.username.is_none() && data.person_id.is_none() { if data.username.is_none() && data.person_id.is_none() {
return Err(LemmyError::from_message("no_id_given")); return Err(LemmyError::from_message("no_id_given"));
} }
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
let local_site = LocalSite::read(context.pool()).await?; let local_site = LocalSite::read(context.pool()).await?;
let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok()); let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok());
@ -39,7 +34,7 @@ impl PerformApub for GetPersonDetails {
Some(id) => id, Some(id) => id,
None => { None => {
if let Some(username) = &data.username { if let Some(username) = &data.username {
resolve_actor_identifier::<ApubPerson, Person>(username, context, &local_user_view, true) resolve_actor_identifier::<ApubPerson, Person>(username, &context, &local_user_view, true)
.await .await
.map_err(|e| e.with_message("couldnt_find_that_username_or_email"))? .map_err(|e| e.with_message("couldnt_find_that_username_or_email"))?
.id .id
@ -110,11 +105,10 @@ impl PerformApub for GetPersonDetails {
let moderates = CommunityModeratorView::for_person(context.pool(), person_details_id).await?; let moderates = CommunityModeratorView::for_person(context.pool(), person_details_id).await?;
// Return the jwt // Return the jwt
Ok(GetPersonDetailsResponse { Ok(Json(GetPersonDetailsResponse {
person_view, person_view,
moderates, moderates,
comments, comments,
posts, posts,
}) }))
}
} }

View File

@ -1,8 +1,6 @@
use crate::{ use crate::fetcher::search::{search_query_to_object_id, SearchableObjects};
api::PerformApub,
fetcher::search::{search_query_to_object_id, SearchableObjects},
};
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use actix_web::web::{Json, Query};
use diesel::NotFound; use diesel::NotFound;
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
@ -14,34 +12,29 @@ use lemmy_db_views::structs::{CommentView, PostView};
use lemmy_db_views_actor::structs::{CommunityView, PersonView}; use lemmy_db_views_actor::structs::{CommunityView, PersonView};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[async_trait::async_trait] #[tracing::instrument(skip(context))]
impl PerformApub for ResolveObject { pub async fn resolve_object(
type Response = ResolveObjectResponse; data: Query<ResolveObject>,
context: Data<LemmyContext>,
#[tracing::instrument(skip(context))] ) -> Result<Json<ResolveObjectResponse>, LemmyError> {
async fn perform( let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
&self,
context: &Data<LemmyContext>,
) -> Result<ResolveObjectResponse, LemmyError> {
let local_user_view = local_user_view_from_jwt(&self.auth, context).await?;
let local_site = LocalSite::read(context.pool()).await?; let local_site = LocalSite::read(context.pool()).await?;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
check_private_instance(&Some(local_user_view), &local_site)?; check_private_instance(&Some(local_user_view), &local_site)?;
let res = search_query_to_object_id(&self.q, context) let res = search_query_to_object_id(&data.q, &context)
.await .await
.map_err(|e| e.with_message("couldnt_find_object"))?; .map_err(|e| e.with_message("couldnt_find_object"))?;
convert_response(res, person_id, context.pool()) convert_response(res, person_id, context.pool())
.await .await
.map_err(|e| e.with_message("couldnt_find_object")) .map_err(|e| e.with_message("couldnt_find_object"))
}
} }
async fn convert_response( async fn convert_response(
object: SearchableObjects, object: SearchableObjects,
user_id: PersonId, user_id: PersonId,
pool: &DbPool, pool: &DbPool,
) -> Result<ResolveObjectResponse, LemmyError> { ) -> Result<Json<ResolveObjectResponse>, LemmyError> {
use SearchableObjects::*; use SearchableObjects::*;
let removed_or_deleted; let removed_or_deleted;
let mut res = ResolveObjectResponse::default(); let mut res = ResolveObjectResponse::default();
@ -67,5 +60,5 @@ async fn convert_response(
if removed_or_deleted { if removed_or_deleted {
return Err(NotFound {}.into()); return Err(NotFound {}.into());
} }
Ok(res) Ok(Json(res))
} }

View File

@ -1,9 +1,6 @@
use crate::{ use crate::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
api::PerformApub,
fetcher::resolve_actor_identifier,
objects::community::ApubCommunity,
};
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use actix_web::web::{Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
site::{Search, SearchResponse}, site::{Search, SearchResponse},
@ -18,15 +15,12 @@ use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery};
use lemmy_db_views_actor::{community_view::CommunityQuery, person_view::PersonQuery}; use lemmy_db_views_actor::{community_view::CommunityQuery, person_view::PersonQuery};
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
#[async_trait::async_trait] #[tracing::instrument(skip(context))]
impl PerformApub for Search { pub async fn search(
type Response = SearchResponse; data: Query<Search>,
context: Data<LemmyContext>,
#[tracing::instrument(skip(context))] ) -> Result<Json<SearchResponse>, LemmyError> {
async fn perform(&self, context: &Data<LemmyContext>) -> Result<SearchResponse, LemmyError> { let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
let data: &Search = self;
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
let local_site = LocalSite::read(context.pool()).await?; let local_site = LocalSite::read(context.pool()).await?;
check_private_instance(&local_user_view, &local_site)?; check_private_instance(&local_user_view, &local_site)?;
@ -47,7 +41,7 @@ impl PerformApub for Search {
let listing_type = data.listing_type; let listing_type = data.listing_type;
let search_type = data.type_.unwrap_or(SearchType::All); let search_type = data.type_.unwrap_or(SearchType::All);
let community_id = if let Some(name) = &data.community_name { let community_id = if let Some(name) = &data.community_name {
resolve_actor_identifier::<ApubCommunity, Community>(name, context, &local_user_view, false) resolve_actor_identifier::<ApubCommunity, Community>(name, &context, &local_user_view, false)
.await .await
.ok() .ok()
.map(|c| c.id) .map(|c| c.id)
@ -204,12 +198,11 @@ impl PerformApub for Search {
}; };
// Return the jwt // Return the jwt
Ok(SearchResponse { Ok(Json(SearchResponse {
type_: search_type, type_: search_type,
comments, comments,
posts, posts,
communities, communities,
users, users,
}) }))
}
} }

View File

@ -9,7 +9,6 @@ use lemmy_api_common::{
DistinguishComment, DistinguishComment,
EditComment, EditComment,
GetComment, GetComment,
GetComments,
ListCommentReports, ListCommentReports,
RemoveComment, RemoveComment,
ResolveCommentReport, ResolveCommentReport,
@ -23,7 +22,6 @@ use lemmy_api_common::{
DeleteCommunity, DeleteCommunity,
EditCommunity, EditCommunity,
FollowCommunity, FollowCommunity,
GetCommunity,
HideCommunity, HideCommunity,
ListCommunities, ListCommunities,
RemoveCommunity, RemoveCommunity,
@ -39,7 +37,6 @@ use lemmy_api_common::{
DeleteAccount, DeleteAccount,
GetBannedPersons, GetBannedPersons,
GetCaptcha, GetCaptcha,
GetPersonDetails,
GetPersonMentions, GetPersonMentions,
GetReplies, GetReplies,
GetReportCount, GetReportCount,
@ -62,7 +59,6 @@ use lemmy_api_common::{
EditPost, EditPost,
FeaturePost, FeaturePost,
GetPost, GetPost,
GetPosts,
GetSiteMetadata, GetSiteMetadata,
ListPostReports, ListPostReports,
LockPost, LockPost,
@ -95,12 +91,20 @@ use lemmy_api_common::{
PurgeCommunity, PurgeCommunity,
PurgePerson, PurgePerson,
PurgePost, PurgePost,
ResolveObject,
Search,
}, },
}; };
use lemmy_api_crud::PerformCrud; use lemmy_api_crud::PerformCrud;
use lemmy_apub::{api::PerformApub, SendActivity}; use lemmy_apub::{
api::{
list_comments::list_comments,
list_posts::list_posts,
read_community::read_community,
read_person::read_person,
resolve_object::resolve_object,
search::search,
},
SendActivity,
};
use lemmy_utils::rate_limit::RateLimitCell; use lemmy_utils::rate_limit::RateLimitCell;
use serde::Deserialize; use serde::Deserialize;
@ -124,12 +128,12 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
.service( .service(
web::resource("/search") web::resource("/search")
.wrap(rate_limit.search()) .wrap(rate_limit.search())
.route(web::get().to(route_get_apub::<Search>)), .route(web::get().to(search)),
) )
.service( .service(
web::resource("/resolve_object") web::resource("/resolve_object")
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route(web::get().to(route_get_apub::<ResolveObject>)), .route(web::get().to(resolve_object)),
) )
// Community // Community
.service( .service(
@ -141,7 +145,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
.service( .service(
web::scope("/community") web::scope("/community")
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route("", web::get().to(route_get_apub::<GetCommunity>)) .route("", web::get().to(read_community))
.route("", web::put().to(route_post_crud::<EditCommunity>)) .route("", web::put().to(route_post_crud::<EditCommunity>))
.route("/hide", web::put().to(route_post::<HideCommunity>)) .route("/hide", web::put().to(route_post::<HideCommunity>))
.route("/list", web::get().to(route_get_crud::<ListCommunities>)) .route("/list", web::get().to(route_get_crud::<ListCommunities>))
@ -186,7 +190,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
) )
.route("/lock", web::post().to(route_post::<LockPost>)) .route("/lock", web::post().to(route_post::<LockPost>))
.route("/feature", web::post().to(route_post::<FeaturePost>)) .route("/feature", web::post().to(route_post::<FeaturePost>))
.route("/list", web::get().to(route_get_apub::<GetPosts>)) .route("/list", web::get().to(list_posts))
.route("/like", web::post().to(route_post::<CreatePostLike>)) .route("/like", web::post().to(route_post::<CreatePostLike>))
.route("/save", web::put().to(route_post::<SavePost>)) .route("/save", web::put().to(route_post::<SavePost>))
.route("/report", web::post().to(route_post::<CreatePostReport>)) .route("/report", web::post().to(route_post::<CreatePostReport>))
@ -225,7 +229,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
) )
.route("/like", web::post().to(route_post::<CreateCommentLike>)) .route("/like", web::post().to(route_post::<CreateCommentLike>))
.route("/save", web::put().to(route_post::<SaveComment>)) .route("/save", web::put().to(route_post::<SaveComment>))
.route("/list", web::get().to(route_get_apub::<GetComments>)) .route("/list", web::get().to(list_comments))
.route("/report", web::post().to(route_post::<CreateCommentReport>)) .route("/report", web::post().to(route_post::<CreateCommentReport>))
.route( .route(
"/report/resolve", "/report/resolve",
@ -283,7 +287,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
.service( .service(
web::scope("/user") web::scope("/user")
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route("", web::get().to(route_get_apub::<GetPersonDetails>)) .route("", web::get().to(read_person))
.route("/mention", web::get().to(route_get::<GetPersonMentions>)) .route("/mention", web::get().to(route_get::<GetPersonMentions>))
.route( .route(
"/mention/mark_as_read", "/mention/mark_as_read",
@ -398,23 +402,6 @@ where
perform::<Data>(data.0, context, apub_data).await perform::<Data>(data.0, context, apub_data).await
} }
async fn route_get_apub<'a, Data>(
data: web::Query<Data>,
context: activitypub_federation::config::Data<LemmyContext>,
) -> Result<HttpResponse, Error>
where
Data: PerformApub
+ SendActivity<Response = <Data as PerformApub>::Response>
+ Clone
+ Deserialize<'a>
+ Send
+ 'static,
{
let res = data.perform(&context).await?;
SendActivity::send_activity(&data.0, &res, &context).await?;
Ok(HttpResponse::Ok().json(res))
}
async fn route_post<'a, Data>( async fn route_post<'a, Data>(
data: web::Json<Data>, data: web::Json<Data>,
context: web::Data<LemmyContext>, context: web::Data<LemmyContext>,