Rewrite some API handlers to remove Perform trait (#3735)

* Rewrite some API handlers to remove Perform trait

* Convert CreateComment

* ci
This commit is contained in:
Nutomic 2023-07-28 16:39:38 +02:00 committed by GitHub
parent db76c5b7ff
commit 37998b3398
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 858 additions and 963 deletions

1
Cargo.lock generated
View file

@ -2573,6 +2573,7 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
name = "lemmy_api"
version = "0.18.1"
dependencies = [
"activitypub_federation",
"actix-web",
"anyhow",
"async-trait",

View file

@ -20,6 +20,7 @@ lemmy_db_views = { workspace = true, features = ["full"] }
lemmy_db_views_moderator = { workspace = true, features = ["full"] }
lemmy_db_views_actor = { workspace = true, features = ["full"] }
lemmy_api_common = { workspace = true, features = ["full"] }
activitypub_federation = { workspace = true }
bcrypt = { workspace = true }
serde = { workspace = true }
actix-web = { workspace = true }

View file

@ -1,5 +1,4 @@
use crate::Perform;
use actix_web::web::Data;
use actix_web::web::{Data, Json};
use lemmy_api_common::{
comment::{CommentResponse, DistinguishComment},
context::LemmyContext,
@ -12,14 +11,12 @@ use lemmy_db_schema::{
use lemmy_db_views::structs::CommentView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[async_trait::async_trait(?Send)]
impl Perform for DistinguishComment {
type Response = CommentResponse;
#[tracing::instrument(skip(context))]
async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
let data: &DistinguishComment = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
#[tracing::instrument(skip(context))]
pub async fn distinguish_comment(
data: Json<DistinguishComment>,
context: Data<LemmyContext>,
) -> Result<Json<CommentResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let comment_id = data.comment_id;
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
@ -52,10 +49,9 @@ impl Perform for DistinguishComment {
let person_id = local_user_view.person.id;
let comment_view = CommentView::read(&mut context.pool(), comment_id, Some(person_id)).await?;
Ok(CommentResponse {
Ok(Json(CommentResponse {
comment_view,
recipient_ids: Vec::new(),
form_id: None,
})
}
}))
}

View file

@ -1,3 +1,3 @@
mod distinguish;
mod like;
mod save;
pub mod distinguish;
pub mod like;
pub mod save;

View file

@ -1,5 +1,4 @@
use crate::Perform;
use actix_web::web::Data;
use actix_web::web::{Data, Json};
use lemmy_api_common::{
comment::{CommentResponse, SaveComment},
context::LemmyContext,
@ -12,14 +11,12 @@ use lemmy_db_schema::{
use lemmy_db_views::structs::CommentView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[async_trait::async_trait(?Send)]
impl Perform for SaveComment {
type Response = CommentResponse;
#[tracing::instrument(skip(context))]
async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
let data: &SaveComment = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
#[tracing::instrument(skip(context))]
pub async fn save_comment(
data: Json<SaveComment>,
context: Data<LemmyContext>,
) -> Result<Json<CommentResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let comment_saved_form = CommentSavedForm {
comment_id: data.comment_id,
@ -40,10 +37,9 @@ impl Perform for SaveComment {
let person_id = local_user_view.person.id;
let comment_view = CommentView::read(&mut context.pool(), comment_id, Some(person_id)).await?;
Ok(CommentResponse {
Ok(Json(CommentResponse {
comment_view,
recipient_ids: Vec::new(),
form_id: None,
})
}
}))
}

View file

@ -1,5 +1,4 @@
use crate::Perform;
use actix_web::web::Data;
use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{
comment::{ListCommentReports, ListCommentReportsResponse},
context::LemmyContext,
@ -10,17 +9,12 @@ use lemmy_utils::error::LemmyError;
/// Lists comment reports for a community if an id is supplied
/// or returns all comment reports for communities a user moderates
#[async_trait::async_trait(?Send)]
impl Perform for ListCommentReports {
type Response = ListCommentReportsResponse;
#[tracing::instrument(skip(context))]
async fn perform(
&self,
context: &Data<LemmyContext>,
) -> Result<ListCommentReportsResponse, LemmyError> {
let data: &ListCommentReports = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
#[tracing::instrument(skip(context))]
pub async fn list_comment_reports(
data: Query<ListCommentReports>,
context: Data<LemmyContext>,
) -> Result<Json<ListCommentReportsResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let community_id = data.community_id;
let unresolved_only = data.unresolved_only;
@ -36,6 +30,5 @@ impl Perform for ListCommentReports {
.list(&mut context.pool(), &local_user_view.person)
.await?;
Ok(ListCommentReportsResponse { comment_reports })
}
Ok(Json(ListCommentReportsResponse { comment_reports }))
}

View file

@ -1,3 +1,3 @@
mod create;
mod list;
mod resolve;
pub mod create;
pub mod list;
pub mod resolve;

View file

@ -1,5 +1,4 @@
use crate::Perform;
use actix_web::web::Data;
use actix_web::web::{Data, Json};
use lemmy_api_common::{
comment::{CommentReportResponse, ResolveCommentReport},
context::LemmyContext,
@ -10,17 +9,12 @@ use lemmy_db_views::structs::CommentReportView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
/// Resolves or unresolves a comment report and notifies the moderators of the community
#[async_trait::async_trait(?Send)]
impl Perform for ResolveCommentReport {
type Response = CommentReportResponse;
#[tracing::instrument(skip(context))]
async fn perform(
&self,
context: &Data<LemmyContext>,
) -> Result<CommentReportResponse, LemmyError> {
let data: &ResolveCommentReport = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
#[tracing::instrument(skip(context))]
pub async fn resolve_comment_report(
data: Json<ResolveCommentReport>,
context: Data<LemmyContext>,
) -> Result<Json<CommentReportResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let report_id = data.report_id;
let person_id = local_user_view.person.id;
@ -43,8 +37,7 @@ impl Perform for ResolveCommentReport {
let comment_report_view =
CommentReportView::read(&mut context.pool(), report_id, person_id).await?;
Ok(CommentReportResponse {
Ok(Json(CommentReportResponse {
comment_report_view,
})
}
}))
}

View file

@ -9,15 +9,15 @@ use lemmy_utils::{
};
use std::io::Cursor;
mod comment;
mod comment_report;
mod community;
mod local_user;
mod post;
mod post_report;
mod private_message;
mod private_message_report;
mod site;
pub mod comment;
pub mod comment_report;
pub mod community;
pub mod local_user;
pub mod post;
pub mod post_report;
pub mod private_message;
pub mod private_message_report;
pub mod site;
#[async_trait::async_trait(?Send)]
pub trait Perform {

View file

@ -1,13 +1,13 @@
mod add_admin;
mod ban_person;
mod block;
mod change_password;
mod change_password_after_reset;
mod get_captcha;
mod list_banned;
mod login;
mod notifications;
mod report_count;
mod reset_password;
mod save_settings;
mod verify_email;
pub mod add_admin;
pub mod ban_person;
pub mod block;
pub mod change_password;
pub mod change_password_after_reset;
pub mod get_captcha;
pub mod list_banned;
pub mod login;
pub mod notifications;
pub mod report_count;
pub mod reset_password;
pub mod save_settings;
pub mod verify_email;

View file

@ -1,5 +1,4 @@
use crate::Perform;
use actix_web::web::Data;
use actix_web::web::{Data, Json};
use lemmy_api_common::{
context::LemmyContext,
person::{CommentReplyResponse, MarkCommentReplyAsRead},
@ -12,17 +11,12 @@ use lemmy_db_schema::{
use lemmy_db_views_actor::structs::CommentReplyView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[async_trait::async_trait(?Send)]
impl Perform for MarkCommentReplyAsRead {
type Response = CommentReplyResponse;
#[tracing::instrument(skip(context))]
async fn perform(
&self,
context: &Data<LemmyContext>,
) -> Result<CommentReplyResponse, LemmyError> {
let data = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
#[tracing::instrument(skip(context))]
pub async fn mark_reply_as_read(
data: Json<MarkCommentReplyAsRead>,
context: Data<LemmyContext>,
) -> Result<Json<CommentReplyResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let comment_reply_id = data.comment_reply_id;
let read_comment_reply = CommentReply::read(&mut context.pool(), comment_reply_id).await?;
@ -47,6 +41,5 @@ impl Perform for MarkCommentReplyAsRead {
let comment_reply_view =
CommentReplyView::read(&mut context.pool(), comment_reply_id, Some(person_id)).await?;
Ok(CommentReplyResponse { comment_reply_view })
}
Ok(Json(CommentReplyResponse { comment_reply_view }))
}

View file

@ -1,6 +1,6 @@
mod list_mentions;
mod list_replies;
mod mark_all_read;
mod mark_mention_read;
mod mark_reply_read;
mod unread_count;
pub mod list_mentions;
pub mod list_replies;
pub mod mark_all_read;
pub mod mark_mention_read;
pub mod mark_reply_read;
pub mod unread_count;

View file

@ -23,7 +23,7 @@ use lemmy_db_views_actor::structs::CommunityView;
use lemmy_utils::{error::LemmyError, utils::mention::MentionData};
pub async fn build_comment_response(
context: &Data<LemmyContext>,
context: &LemmyContext,
comment_id: CommentId,
local_user_view: Option<LocalUserView>,
form_id: Option<String>,

View file

@ -1,7 +1,7 @@
use crate::context::LemmyContext;
use activitypub_federation::config::Data;
use futures::future::BoxFuture;
use lemmy_db_schema::source::post::Post;
use lemmy_db_schema::source::{comment::Comment, post::Post};
use lemmy_utils::{error::LemmyResult, SYNCHRONOUS_FEDERATION};
use once_cell::sync::{Lazy, OnceCell};
use tokio::{
@ -22,6 +22,7 @@ pub static MATCH_OUTGOING_ACTIVITIES: OnceCell<MatchOutgoingActivitiesBoxed> = O
#[derive(Debug)]
pub enum SendActivityData {
CreatePost(Post),
CreateComment(Comment),
}
// TODO: instead of static, move this into LemmyContext. make sure that stopping the process with

View file

@ -1,9 +1,10 @@
use crate::PerformCrud;
use actix_web::web::Data;
use activitypub_federation::config::Data;
use actix_web::web::Json;
use lemmy_api_common::{
build_response::{build_comment_response, send_local_notifs},
comment::{CommentResponse, CreateComment},
context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData},
utils::{
check_community_ban,
check_community_deleted_or_removed,
@ -35,17 +36,16 @@ use lemmy_utils::{
validation::is_valid_body_field,
},
};
use std::ops::Deref;
const MAX_COMMENT_DEPTH_LIMIT: usize = 100;
#[async_trait::async_trait(?Send)]
impl PerformCrud for CreateComment {
type Response = CommentResponse;
#[tracing::instrument(skip(context))]
async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
let data: &CreateComment = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
#[tracing::instrument(skip(context))]
pub async fn create_comment(
data: Json<CreateComment>,
context: Data<LemmyContext>,
) -> Result<Json<CommentResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?;
let content = remove_slurs(
@ -114,8 +114,7 @@ impl PerformCrud for CreateComment {
// Create the comment
let parent_path = parent_opt.clone().map(|t| t.path);
let inserted_comment =
Comment::create(&mut context.pool(), &comment_form, parent_path.as_ref())
let inserted_comment = Comment::create(&mut context.pool(), &comment_form, parent_path.as_ref())
.await
.with_lemmy_type(LemmyErrorType::CouldntCreateComment)?;
@ -144,7 +143,7 @@ impl PerformCrud for CreateComment {
&local_user_view.person,
&post,
true,
context,
&context,
)
.await?;
@ -160,6 +159,12 @@ impl PerformCrud for CreateComment {
.await
.with_lemmy_type(LemmyErrorType::CouldntLikeComment)?;
ActivityChannel::submit_activity(
SendActivityData::CreateComment(updated_comment.clone()),
&context,
)
.await?;
// If its a reply, mark the parent as read
if let Some(parent) = parent_opt {
let parent_id = parent.id;
@ -189,15 +194,16 @@ impl PerformCrud for CreateComment {
}
}
Ok(Json(
build_comment_response(
context,
context.deref(),
inserted_comment.id,
Some(local_user_view),
self.form_id.clone(),
data.form_id.clone(),
recipient_ids,
)
.await
}
.await?,
))
}
pub fn check_comment_depth(comment: &Comment) -> Result<(), LemmyError> {

View file

@ -15,6 +15,7 @@ use lemmy_db_schema::{
};
use lemmy_db_views::structs::CommentView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
use std::ops::Deref;
#[async_trait::async_trait(?Send)]
impl PerformCrud for DeleteComment {
@ -68,7 +69,7 @@ impl PerformCrud for DeleteComment {
.await?;
build_comment_response(
context,
context.deref(),
updated_comment.id,
Some(local_user_view),
None,

View file

@ -1,5 +1,5 @@
mod create;
mod delete;
mod read;
mod remove;
mod update;
pub mod create;
pub mod delete;
pub mod read;
pub mod remove;
pub mod update;

View file

@ -1,5 +1,4 @@
use crate::PerformCrud;
use actix_web::web::Data;
use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{
build_response::build_comment_response,
comment::{CommentResponse, GetComment},
@ -8,19 +7,19 @@ use lemmy_api_common::{
};
use lemmy_db_schema::source::local_site::LocalSite;
use lemmy_utils::error::LemmyError;
use std::ops::Deref;
#[async_trait::async_trait(?Send)]
impl PerformCrud for GetComment {
type Response = CommentResponse;
#[tracing::instrument(skip(context))]
async fn perform(&self, context: &Data<LemmyContext>) -> Result<Self::Response, LemmyError> {
let data = self;
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
#[tracing::instrument(skip(context))]
pub async fn get_comment(
data: Query<GetComment>,
context: Data<LemmyContext>,
) -> Result<Json<CommentResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
let local_site = LocalSite::read(&mut context.pool()).await?;
check_private_instance(&local_user_view, &local_site)?;
build_comment_response(context, data.id, local_user_view, None, vec![]).await
}
Ok(Json(
build_comment_response(context.deref(), data.id, local_user_view, None, vec![]).await?,
))
}

View file

@ -16,6 +16,7 @@ use lemmy_db_schema::{
};
use lemmy_db_views::structs::CommentView;
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
use std::ops::Deref;
#[async_trait::async_trait(?Send)]
impl PerformCrud for RemoveComment {
@ -76,7 +77,7 @@ impl PerformCrud for RemoveComment {
.await?;
build_comment_response(
context,
context.deref(),
updated_comment.id,
Some(local_user_view),
None,

View file

@ -29,6 +29,7 @@ use lemmy_utils::{
validation::is_valid_body_field,
},
};
use std::ops::Deref;
#[async_trait::async_trait(?Send)]
impl PerformCrud for EditComment {
@ -95,7 +96,7 @@ impl PerformCrud for EditComment {
.await?;
build_comment_response(
context,
context.deref(),
updated_comment.id,
Some(local_user_view),
self.form_id.clone(),

View file

@ -1,5 +1,4 @@
use crate::PerformCrud;
use actix_web::web::Data;
use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{
community::{ListCommunities, ListCommunitiesResponse},
context::LemmyContext,
@ -9,17 +8,12 @@ use lemmy_db_schema::source::local_site::LocalSite;
use lemmy_db_views_actor::community_view::CommunityQuery;
use lemmy_utils::error::LemmyError;
#[async_trait::async_trait(?Send)]
impl PerformCrud for ListCommunities {
type Response = ListCommunitiesResponse;
#[tracing::instrument(skip(context))]
async fn perform(
&self,
context: &Data<LemmyContext>,
) -> Result<ListCommunitiesResponse, LemmyError> {
let data: &ListCommunities = self;
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
#[tracing::instrument(skip(context))]
pub async fn list_communities(
data: Query<ListCommunities>,
context: Data<LemmyContext>,
) -> Result<Json<ListCommunitiesResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
let local_site = LocalSite::read(&mut context.pool()).await?;
let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok());
@ -45,6 +39,5 @@ impl PerformCrud for ListCommunities {
.await?;
// Return the jwt
Ok(ListCommunitiesResponse { communities })
}
Ok(Json(ListCommunitiesResponse { communities }))
}

View file

@ -1,5 +1,5 @@
mod create;
mod delete;
mod list;
mod remove;
mod update;
pub mod create;
pub mod delete;
pub mod list;
pub mod remove;
pub mod update;

View file

@ -2,13 +2,13 @@ use actix_web::web::Data;
use lemmy_api_common::context::LemmyContext;
use lemmy_utils::error::LemmyError;
mod comment;
mod community;
mod custom_emoji;
pub mod comment;
pub mod community;
pub mod custom_emoji;
pub mod post;
mod private_message;
mod site;
mod user;
pub mod private_message;
pub mod site;
pub mod user;
#[async_trait::async_trait(?Send)]
pub trait PerformCrud {

View file

@ -1,5 +1,5 @@
pub mod create;
mod delete;
mod read;
mod remove;
mod update;
pub mod delete;
pub mod read;
pub mod remove;
pub mod update;

View file

@ -1,5 +1,4 @@
use crate::PerformCrud;
use actix_web::web::Data;
use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{
context::LemmyContext,
post::{GetPost, GetPostResponse},
@ -19,14 +18,12 @@ use lemmy_db_views::{post_view::PostQuery, structs::PostView};
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[async_trait::async_trait(?Send)]
impl PerformCrud for GetPost {
type Response = GetPostResponse;
#[tracing::instrument(skip(context))]
async fn perform(&self, context: &Data<LemmyContext>) -> Result<GetPostResponse, LemmyError> {
let data: &GetPost = self;
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
#[tracing::instrument(skip(context))]
pub async fn get_post(
data: Query<GetPost>,
context: Data<LemmyContext>,
) -> Result<Json<GetPostResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
let local_site = LocalSite::read(&mut context.pool()).await?;
check_private_instance(&local_user_view, &local_site)?;
@ -95,8 +92,7 @@ impl PerformCrud for GetPost {
.with_lemmy_type(LemmyErrorType::CouldntFindPost)?;
}
let moderators =
CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
// Fetch the cross_posts
let cross_posts = if let Some(url) = &post_view.post.url {
@ -115,11 +111,10 @@ impl PerformCrud for GetPost {
};
// Return the jwt
Ok(GetPostResponse {
Ok(Json(GetPostResponse {
post_view,
community_view,
moderators,
cross_posts,
})
}
}))
}

View file

@ -1,4 +1,4 @@
mod create;
mod delete;
mod read;
mod update;
pub mod create;
pub mod delete;
pub mod read;
pub mod update;

View file

@ -1,5 +1,4 @@
use crate::PerformCrud;
use actix_web::web::Data;
use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{
context::LemmyContext,
private_message::{GetPrivateMessages, PrivateMessagesResponse},
@ -8,17 +7,12 @@ use lemmy_api_common::{
use lemmy_db_views::private_message_view::PrivateMessageQuery;
use lemmy_utils::error::LemmyError;
#[async_trait::async_trait(?Send)]
impl PerformCrud for GetPrivateMessages {
type Response = PrivateMessagesResponse;
#[tracing::instrument(skip(self, context))]
async fn perform(
&self,
context: &Data<LemmyContext>,
) -> Result<PrivateMessagesResponse, LemmyError> {
let data: &GetPrivateMessages = self;
let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), context).await?;
#[tracing::instrument(skip(context))]
pub async fn get_private_message(
data: Query<GetPrivateMessages>,
context: Data<LemmyContext>,
) -> Result<Json<PrivateMessagesResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), &context).await?;
let person_id = local_user_view.person.id;
let page = data.page;
@ -40,8 +34,7 @@ impl PerformCrud for GetPrivateMessages {
}
});
Ok(PrivateMessagesResponse {
Ok(Json(PrivateMessagesResponse {
private_messages: messages,
})
}
}))
}

View file

@ -1,9 +1,6 @@
use crate::{
site::{application_question_check, site_default_post_listing_type_check},
PerformCrud,
};
use crate::site::{application_question_check, site_default_post_listing_type_check};
use activitypub_federation::http_signatures::generate_actor_keypair;
use actix_web::web::Data;
use actix_web::web::{Data, Json};
use lemmy_api_common::{
context::LemmyContext,
site::{CreateSite, SiteResponse},
@ -43,20 +40,18 @@ use lemmy_utils::{
};
use url::Url;
#[async_trait::async_trait(?Send)]
impl PerformCrud for CreateSite {
type Response = SiteResponse;
#[tracing::instrument(skip(context))]
async fn perform(&self, context: &Data<LemmyContext>) -> Result<SiteResponse, LemmyError> {
let data: &CreateSite = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
#[tracing::instrument(skip(context))]
pub async fn create_site(
data: Json<CreateSite>,
context: Data<LemmyContext>,
) -> Result<Json<SiteResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let local_site = LocalSite::read(&mut context.pool()).await?;
// Make sure user is an admin; other types of users should not create site data...
is_admin(&local_user_view)?;
validate_create_payload(&local_site, data)?;
validate_create_payload(&local_site, &data)?;
let actor_id: DbUrl = Url::parse(&context.settings().get_protocol_and_hostname())?.into();
let inbox_url = Some(generate_site_inbox_url(&actor_id)?);
@ -140,11 +135,10 @@ impl PerformCrud for CreateSite {
.send(rate_limit_config)
.await?;
Ok(SiteResponse {
Ok(Json(SiteResponse {
site_view,
taglines,
})
}
}))
}
fn validate_create_payload(local_site: &LocalSite, create_site: &CreateSite) -> LemmyResult<()> {

View file

@ -1,9 +1,9 @@
use lemmy_db_schema::{ListingType, RegistrationMode};
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
mod create;
mod read;
mod update;
pub mod create;
pub mod read;
pub mod update;
/// Checks whether the default post listing type is valid for a site.
pub fn site_default_post_listing_type_check(

View file

@ -1,5 +1,4 @@
use crate::PerformCrud;
use actix_web::web::Data;
use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{
context::LemmyContext,
sensitive::Sensitive,
@ -28,21 +27,18 @@ use lemmy_utils::{
version,
};
#[async_trait::async_trait(?Send)]
impl PerformCrud for GetSite {
type Response = GetSiteResponse;
#[tracing::instrument(skip(context))]
async fn perform(&self, context: &Data<LemmyContext>) -> Result<GetSiteResponse, LemmyError> {
let data: &GetSite = self;
#[tracing::instrument(skip(context))]
pub async fn get_site(
data: Query<GetSite>,
context: Data<LemmyContext>,
) -> Result<Json<GetSiteResponse>, LemmyError> {
let site_view = SiteView::read_local(&mut context.pool()).await?;
let admins = PersonView::admins(&mut context.pool()).await?;
// Build the local user
let my_user = if let Some(local_user_view) =
local_user_settings_view_from_jwt_opt(data.auth.as_ref(), context).await
local_user_settings_view_from_jwt_opt(data.auth.as_ref(), &context).await
{
let person_id = local_user_view.person.id;
let local_user_id = local_user_view.local_user.id;
@ -87,7 +83,7 @@ impl PerformCrud for GetSite {
let custom_emojis =
CustomEmojiView::get_all(&mut context.pool(), site_view.local_site.id).await?;
Ok(GetSiteResponse {
Ok(Json(GetSiteResponse {
site_view,
admins,
version: version::VERSION.to_string(),
@ -96,8 +92,7 @@ impl PerformCrud for GetSite {
discussion_languages,
taglines,
custom_emojis,
})
}
}))
}
#[tracing::instrument(skip_all)]

View file

@ -1,8 +1,5 @@
use crate::{
site::{application_question_check, site_default_post_listing_type_check},
PerformCrud,
};
use actix_web::web::Data;
use crate::site::{application_question_check, site_default_post_listing_type_check};
use actix_web::web::{Data, Json};
use lemmy_api_common::{
context::LemmyContext,
site::{EditSite, SiteResponse},
@ -43,14 +40,12 @@ use lemmy_utils::{
},
};
#[async_trait::async_trait(?Send)]
impl PerformCrud for EditSite {
type Response = SiteResponse;
#[tracing::instrument(skip(context))]
async fn perform(&self, context: &Data<LemmyContext>) -> Result<SiteResponse, LemmyError> {
let data: &EditSite = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
#[tracing::instrument(skip(context))]
pub async fn update_site(
data: Json<EditSite>,
context: Data<LemmyContext>,
) -> Result<Json<SiteResponse>, LemmyError> {
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
let site_view = SiteView::read_local(&mut context.pool()).await?;
let local_site = site_view.local_site;
let site = site_view.site;
@ -58,7 +53,7 @@ impl PerformCrud for EditSite {
// Make sure user is an admin; other types of users should not update site data...
is_admin(&local_user_view)?;
validate_update_payload(&local_site, data)?;
validate_update_payload(&local_site, &data)?;
if let Some(discussion_languages) = data.discussion_languages.clone() {
SiteLanguage::update(&mut context.pool(), discussion_languages.clone(), &site).await?;
@ -177,13 +172,10 @@ impl PerformCrud for EditSite {
.send(rate_limit_config)
.await?;
let res = SiteResponse {
Ok(Json(SiteResponse {
site_view,
taglines,
};
Ok(res)
}
}))
}
fn validate_update_payload(local_site: &LocalSite, edit_site: &EditSite) -> LemmyResult<()> {

View file

@ -25,7 +25,7 @@ use activitypub_federation::{
};
use lemmy_api_common::{
build_response::send_local_notifs,
comment::{CommentResponse, CreateComment, EditComment},
comment::{CommentResponse, EditComment},
context::LemmyContext,
utils::{check_post_deleted_or_removed, is_mod_or_admin},
};
@ -43,25 +43,6 @@ use lemmy_db_schema::{
use lemmy_utils::{error::LemmyError, utils::mention::scrape_text_for_mentions};
use url::Url;
#[async_trait::async_trait]
impl SendActivity for CreateComment {
type Response = CommentResponse;
async fn send_activity(
_request: &Self,
response: &Self::Response,
context: &Data<LemmyContext>,
) -> Result<(), LemmyError> {
CreateOrUpdateNote::send(
&response.comment_view.comment,
response.comment_view.creator.id,
CreateOrUpdateType::Create,
context,
)
.await
}
}
#[async_trait::async_trait]
impl SendActivity for EditComment {
type Response = CommentResponse;
@ -72,10 +53,10 @@ impl SendActivity for EditComment {
context: &Data<LemmyContext>,
) -> Result<(), LemmyError> {
CreateOrUpdateNote::send(
&response.comment_view.comment,
response.comment_view.comment.clone(),
response.comment_view.creator.id,
CreateOrUpdateType::Update,
context,
context.reset_request_count(),
)
.await
}
@ -83,11 +64,11 @@ impl SendActivity for EditComment {
impl CreateOrUpdateNote {
#[tracing::instrument(skip(comment, person_id, kind, context))]
async fn send(
comment: &Comment,
pub(crate) async fn send(
comment: Comment,
person_id: PersonId,
kind: CreateOrUpdateType,
context: &Data<LemmyContext>,
context: Data<LemmyContext>,
) -> Result<(), LemmyError> {
// TODO: might be helpful to add a comment method to retrieve community directly
let post_id = comment.post_id;
@ -102,7 +83,7 @@ impl CreateOrUpdateNote {
kind.clone(),
&context.settings().get_protocol_and_hostname(),
)?;
let note = ApubComment(comment.clone()).into_json(context).await?;
let note = ApubComment(comment).into_json(&context).await?;
let create_or_update = CreateOrUpdateNote {
actor: person.id().into(),
@ -130,12 +111,12 @@ impl CreateOrUpdateNote {
.collect();
let mut inboxes = vec![];
for t in tagged_users {
let person = t.dereference(context).await?;
let person = t.dereference(&context).await?;
inboxes.push(person.shared_inbox_or_inbox());
}
let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
send_activity_in_community(activity, &person, &community, inboxes, false, context).await
send_activity_in_community(activity, &person, &community, inboxes, false, &context).await
}
}

View file

@ -1,6 +1,9 @@
use crate::{
objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
protocol::activities::{
create_or_update::{note::CreateOrUpdateNote, page::CreateOrUpdatePage},
CreateOrUpdateType,
},
CONTEXT,
};
use activitypub_federation::{
@ -217,15 +220,17 @@ pub async fn match_outgoing_activities(
data: SendActivityData,
context: &Data<LemmyContext>,
) -> LemmyResult<()> {
let fed_task = match data {
let context = context.reset_request_count();
let fed_task = async {
match data {
SendActivityData::CreatePost(post) => {
let creator_id = post.creator_id;
CreateOrUpdatePage::send(
post,
creator_id,
CreateOrUpdateType::Create,
context.reset_request_count(),
)
CreateOrUpdatePage::send(post, creator_id, CreateOrUpdateType::Create, context).await
}
SendActivityData::CreateComment(comment) => {
let creator_id = comment.creator_id;
CreateOrUpdateNote::send(comment, creator_id, CreateOrUpdateType::Create, context).await
}
}
};
if *SYNCHRONOUS_FEDERATION {

View file

@ -16,7 +16,7 @@ use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorExt2, LemmyErrorType};
#[tracing::instrument(skip(context))]
pub async fn read_community(
pub async fn get_community(
data: Query<GetCommunity>,
context: Data<LemmyContext>,
) -> Result<Json<GetCommunityResponse>, LemmyError> {

@ -1 +1 @@
Subproject commit 713ceed9c7ef84deaa222e68361e670e0763cd83
Subproject commit 1c42c579460871de7b4ea18e58dc25543b80d289

View file

@ -1,19 +1,12 @@
use actix_web::{guard, web, Error, HttpResponse, Result};
use lemmy_api::Perform;
use lemmy_api::{
comment::{distinguish::distinguish_comment, save::save_comment},
comment_report::{list::list_comment_reports, resolve::resolve_comment_report},
local_user::notifications::mark_reply_read::mark_reply_as_read,
Perform,
};
use lemmy_api_common::{
comment::{
CreateComment,
CreateCommentLike,
CreateCommentReport,
DeleteComment,
DistinguishComment,
EditComment,
GetComment,
ListCommentReports,
RemoveComment,
ResolveCommentReport,
SaveComment,
},
comment::{CreateCommentLike, CreateCommentReport, DeleteComment, EditComment, RemoveComment},
community::{
AddModToCommunity,
BanFromCommunity,
@ -23,7 +16,6 @@ use lemmy_api_common::{
EditCommunity,
FollowCommunity,
HideCommunity,
ListCommunities,
RemoveCommunity,
TransferCommunity,
},
@ -43,7 +35,6 @@ use lemmy_api_common::{
GetUnreadCount,
Login,
MarkAllAsRead,
MarkCommentReplyAsRead,
MarkPersonMentionAsRead,
PasswordChangeAfterReset,
PasswordReset,
@ -57,7 +48,6 @@ use lemmy_api_common::{
DeletePost,
EditPost,
FeaturePost,
GetPost,
GetSiteMetadata,
ListPostReports,
LockPost,
@ -71,18 +61,14 @@ use lemmy_api_common::{
CreatePrivateMessageReport,
DeletePrivateMessage,
EditPrivateMessage,
GetPrivateMessages,
ListPrivateMessageReports,
MarkPrivateMessageAsRead,
ResolvePrivateMessageReport,
},
site::{
ApproveRegistrationApplication,
CreateSite,
EditSite,
GetFederatedInstances,
GetModlog,
GetSite,
GetUnreadRegistrationApplicationCount,
LeaveAdmin,
ListRegistrationApplications,
@ -92,12 +78,19 @@ use lemmy_api_common::{
PurgePost,
},
};
use lemmy_api_crud::{post::create::create_post, PerformCrud};
use lemmy_api_crud::{
comment::{create::create_comment, read::get_comment},
community::list::list_communities,
post::{create::create_post, read::get_post},
private_message::read::get_private_message,
site::{create::create_site, read::get_site, update::update_site},
PerformCrud,
};
use lemmy_apub::{
api::{
list_comments::list_comments,
list_posts::list_posts,
read_community::read_community,
read_community::get_community,
read_person::read_person,
resolve_object::resolve_object,
search::search,
@ -114,10 +107,10 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
.service(
web::scope("/site")
.wrap(rate_limit.message())
.route("", web::get().to(route_get_crud::<GetSite>))
.route("", web::get().to(get_site))
// Admin Actions
.route("", web::post().to(route_post_crud::<CreateSite>))
.route("", web::put().to(route_post_crud::<EditSite>)),
.route("", web::post().to(create_site))
.route("", web::put().to(update_site)),
)
.service(
web::resource("/modlog")
@ -144,10 +137,10 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
.service(
web::scope("/community")
.wrap(rate_limit.message())
.route("", web::get().to(read_community))
.route("", web::get().to(get_community))
.route("", web::put().to(route_post_crud::<EditCommunity>))
.route("/hide", web::put().to(route_post::<HideCommunity>))
.route("/list", web::get().to(route_get_crud::<ListCommunities>))
.route("/list", web::get().to(list_communities))
.route("/follow", web::post().to(route_post::<FollowCommunity>))
.route("/block", web::post().to(route_post::<BlockCommunity>))
.route(
@ -179,7 +172,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
.service(
web::scope("/post")
.wrap(rate_limit.message())
.route("", web::get().to(route_get_crud::<GetPost>))
.route("", web::get().to(get_post))
.route("", web::put().to(route_post_crud::<EditPost>))
.route("/delete", web::post().to(route_post_crud::<DeletePost>))
.route("/remove", web::post().to(route_post_crud::<RemovePost>))
@ -209,41 +202,29 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
web::resource("/comment")
.guard(guard::Post())
.wrap(rate_limit.comment())
.route(web::post().to(route_post_crud::<CreateComment>)),
.route(web::post().to(create_comment)),
)
.service(
web::scope("/comment")
.wrap(rate_limit.message())
.route("", web::get().to(route_get_crud::<GetComment>))
.route("", web::get().to(get_comment))
.route("", web::put().to(route_post_crud::<EditComment>))
.route("/delete", web::post().to(route_post_crud::<DeleteComment>))
.route("/remove", web::post().to(route_post_crud::<RemoveComment>))
.route(
"/mark_as_read",
web::post().to(route_post::<MarkCommentReplyAsRead>),
)
.route(
"/distinguish",
web::post().to(route_post::<DistinguishComment>),
)
.route("/mark_as_read", web::post().to(mark_reply_as_read))
.route("/distinguish", web::post().to(distinguish_comment))
.route("/like", web::post().to(route_post::<CreateCommentLike>))
.route("/save", web::put().to(route_post::<SaveComment>))
.route("/save", web::put().to(save_comment))
.route("/list", web::get().to(list_comments))
.route("/report", web::post().to(route_post::<CreateCommentReport>))
.route(
"/report/resolve",
web::put().to(route_post::<ResolveCommentReport>),
)
.route(
"/report/list",
web::get().to(route_get::<ListCommentReports>),
),
.route("/report/resolve", web::put().to(resolve_comment_report))
.route("/report/list", web::get().to(list_comment_reports)),
)
// Private Message
.service(
web::scope("/private_message")
.wrap(rate_limit.message())
.route("/list", web::get().to(route_get_crud::<GetPrivateMessages>))
.route("/list", web::get().to(get_private_message))
.route("", web::post().to(route_post_crud::<CreatePrivateMessage>))
.route("", web::put().to(route_post_crud::<EditPrivateMessage>))
.route(
@ -447,22 +428,6 @@ where
Ok(HttpResponse::Ok().json(&res))
}
async fn route_get_crud<'a, Data>(
data: web::Query<Data>,
context: web::Data<LemmyContext>,
apub_data: activitypub_federation::config::Data<LemmyContext>,
) -> Result<HttpResponse, Error>
where
Data: PerformCrud
+ SendActivity<Response = <Data as PerformCrud>::Response>
+ Clone
+ Deserialize<'a>
+ Send
+ 'static,
{
perform_crud::<Data>(data.0, context, apub_data).await
}
async fn route_post_crud<'a, Data>(
data: web::Json<Data>,
context: web::Data<LemmyContext>,