Removing unnecessary oper struct. Fixes #660 (#1077)

This commit is contained in:
Dessalines 2020-08-12 07:31:45 -04:00 committed by GitHub
parent d28e5245d2
commit 8daeba450c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 227 additions and 200 deletions

View file

@ -5,7 +5,6 @@ use crate::{
get_user_from_jwt_opt, get_user_from_jwt_opt,
is_mod_or_admin, is_mod_or_admin,
APIError, APIError,
Oper,
Perform, Perform,
}, },
apub::{ApubLikeableType, ApubObjectType}, apub::{ApubLikeableType, ApubObjectType},
@ -18,6 +17,7 @@ use crate::{
DbPool, DbPool,
LemmyError, LemmyError,
}; };
use actix_web::client::Client;
use lemmy_db::{ use lemmy_db::{
comment::*, comment::*,
comment_view::*, comment_view::*,
@ -121,15 +121,16 @@ pub struct GetCommentsResponse {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<CreateComment> { impl Perform for CreateComment {
type Response = CommentResponse; type Response = CommentResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<CommentResponse, LemmyError> { ) -> Result<CommentResponse, LemmyError> {
let data: &CreateComment = &self.data; let data: &CreateComment = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let content_slurs_removed = remove_slurs(&data.content.to_owned()); let content_slurs_removed = remove_slurs(&data.content.to_owned());
@ -180,9 +181,7 @@ impl Perform for Oper<CreateComment> {
Err(_e) => return Err(APIError::err("couldnt_create_comment").into()), Err(_e) => return Err(APIError::err("couldnt_create_comment").into()),
}; };
updated_comment updated_comment.send_create(&user, &client, pool).await?;
.send_create(&user, &self.client, pool)
.await?;
// Scan the comment for user mentions, add those rows // Scan the comment for user mentions, add those rows
let mentions = scrape_text_for_mentions(&comment_form.content); let mentions = scrape_text_for_mentions(&comment_form.content);
@ -202,7 +201,7 @@ impl Perform for Oper<CreateComment> {
return Err(APIError::err("couldnt_like_comment").into()); return Err(APIError::err("couldnt_like_comment").into());
} }
updated_comment.send_like(&user, &self.client, pool).await?; updated_comment.send_like(&user, &client, pool).await?;
let user_id = user.id; let user_id = user.id;
let comment_view = blocking(pool, move |conn| { let comment_view = blocking(pool, move |conn| {
@ -233,15 +232,16 @@ impl Perform for Oper<CreateComment> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<EditComment> { impl Perform for EditComment {
type Response = CommentResponse; type Response = CommentResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<CommentResponse, LemmyError> { ) -> Result<CommentResponse, LemmyError> {
let data: &EditComment = &self.data; let data: &EditComment = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let edit_id = data.edit_id; let edit_id = data.edit_id;
@ -268,9 +268,7 @@ impl Perform for Oper<EditComment> {
}; };
// Send the apub update // Send the apub update
updated_comment updated_comment.send_update(&user, &client, pool).await?;
.send_update(&user, &self.client, pool)
.await?;
// Do the mentions / recipients // Do the mentions / recipients
let post_id = orig_comment.post_id; let post_id = orig_comment.post_id;
@ -311,15 +309,16 @@ impl Perform for Oper<EditComment> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<DeleteComment> { impl Perform for DeleteComment {
type Response = CommentResponse; type Response = CommentResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<CommentResponse, LemmyError> { ) -> Result<CommentResponse, LemmyError> {
let data: &DeleteComment = &self.data; let data: &DeleteComment = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let edit_id = data.edit_id; let edit_id = data.edit_id;
@ -346,12 +345,10 @@ impl Perform for Oper<DeleteComment> {
// Send the apub message // Send the apub message
if deleted { if deleted {
updated_comment updated_comment.send_delete(&user, &client, pool).await?;
.send_delete(&user, &self.client, pool)
.await?;
} else { } else {
updated_comment updated_comment
.send_undo_delete(&user, &self.client, pool) .send_undo_delete(&user, &client, pool)
.await?; .await?;
} }
@ -393,15 +390,16 @@ impl Perform for Oper<DeleteComment> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<RemoveComment> { impl Perform for RemoveComment {
type Response = CommentResponse; type Response = CommentResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<CommentResponse, LemmyError> { ) -> Result<CommentResponse, LemmyError> {
let data: &RemoveComment = &self.data; let data: &RemoveComment = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let edit_id = data.edit_id; let edit_id = data.edit_id;
@ -435,12 +433,10 @@ impl Perform for Oper<RemoveComment> {
// Send the apub message // Send the apub message
if removed { if removed {
updated_comment updated_comment.send_remove(&user, &client, pool).await?;
.send_remove(&user, &self.client, pool)
.await?;
} else { } else {
updated_comment updated_comment
.send_undo_remove(&user, &self.client, pool) .send_undo_remove(&user, &client, pool)
.await?; .await?;
} }
@ -482,15 +478,16 @@ impl Perform for Oper<RemoveComment> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<MarkCommentAsRead> { impl Perform for MarkCommentAsRead {
type Response = CommentResponse; type Response = CommentResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<CommentResponse, LemmyError> { ) -> Result<CommentResponse, LemmyError> {
let data: &MarkCommentAsRead = &self.data; let data: &MarkCommentAsRead = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let edit_id = data.edit_id; let edit_id = data.edit_id;
@ -545,15 +542,16 @@ impl Perform for Oper<MarkCommentAsRead> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<SaveComment> { impl Perform for SaveComment {
type Response = CommentResponse; type Response = CommentResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<CommentResponse, LemmyError> { ) -> Result<CommentResponse, LemmyError> {
let data: &SaveComment = &self.data; let data: &SaveComment = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let comment_saved_form = CommentSavedForm { let comment_saved_form = CommentSavedForm {
@ -589,15 +587,16 @@ impl Perform for Oper<SaveComment> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<CreateCommentLike> { impl Perform for CreateCommentLike {
type Response = CommentResponse; type Response = CommentResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<CommentResponse, LemmyError> { ) -> Result<CommentResponse, LemmyError> {
let data: &CreateCommentLike = &self.data; let data: &CreateCommentLike = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let mut recipient_ids = Vec::new(); let mut recipient_ids = Vec::new();
@ -659,12 +658,12 @@ impl Perform for Oper<CreateCommentLike> {
} }
if like_form.score == 1 { if like_form.score == 1 {
comment.send_like(&user, &self.client, pool).await?; comment.send_like(&user, &client, pool).await?;
} else if like_form.score == -1 { } else if like_form.score == -1 {
comment.send_dislike(&user, &self.client, pool).await?; comment.send_dislike(&user, &client, pool).await?;
} }
} else { } else {
comment.send_undo_like(&user, &self.client, pool).await?; comment.send_undo_like(&user, &client, pool).await?;
} }
// Have to refetch the comment to get the current state // Have to refetch the comment to get the current state
@ -698,15 +697,16 @@ impl Perform for Oper<CreateCommentLike> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetComments> { impl Perform for GetComments {
type Response = GetCommentsResponse; type Response = GetCommentsResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetCommentsResponse, LemmyError> { ) -> Result<GetCommentsResponse, LemmyError> {
let data: &GetComments = &self.data; let data: &GetComments = &self;
let user = get_user_from_jwt_opt(&data.auth, pool).await?; let user = get_user_from_jwt_opt(&data.auth, pool).await?;
let user_id = user.map(|u| u.id); let user_id = user.map(|u| u.id);

View file

@ -1,6 +1,6 @@
use super::*; use super::*;
use crate::{ use crate::{
api::{is_admin, is_mod_or_admin, APIError, Oper, Perform}, api::{is_admin, is_mod_or_admin, APIError, Perform},
apub::ActorType, apub::ActorType,
blocking, blocking,
websocket::{ websocket::{
@ -10,6 +10,7 @@ use crate::{
}, },
DbPool, DbPool,
}; };
use actix_web::client::Client;
use lemmy_db::{ use lemmy_db::{
diesel_option_overwrite, diesel_option_overwrite,
naive_now, naive_now,
@ -155,15 +156,16 @@ pub struct TransferCommunity {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetCommunity> { impl Perform for GetCommunity {
type Response = GetCommunityResponse; type Response = GetCommunityResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetCommunityResponse, LemmyError> { ) -> Result<GetCommunityResponse, LemmyError> {
let data: &GetCommunity = &self.data; let data: &GetCommunity = &self;
let user = get_user_from_jwt_opt(&data.auth, pool).await?; let user = get_user_from_jwt_opt(&data.auth, pool).await?;
let user_id = user.map(|u| u.id); let user_id = user.map(|u| u.id);
@ -226,15 +228,16 @@ impl Perform for Oper<GetCommunity> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<CreateCommunity> { impl Perform for CreateCommunity {
type Response = CommunityResponse; type Response = CommunityResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<CommunityResponse, LemmyError> { ) -> Result<CommunityResponse, LemmyError> {
let data: &CreateCommunity = &self.data; let data: &CreateCommunity = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
check_slurs(&data.name)?; check_slurs(&data.name)?;
@ -318,15 +321,16 @@ impl Perform for Oper<CreateCommunity> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<EditCommunity> { impl Perform for EditCommunity {
type Response = CommunityResponse; type Response = CommunityResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<CommunityResponse, LemmyError> { ) -> Result<CommunityResponse, LemmyError> {
let data: &EditCommunity = &self.data; let data: &EditCommunity = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
check_slurs(&data.title)?; check_slurs(&data.title)?;
@ -400,15 +404,16 @@ impl Perform for Oper<EditCommunity> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<DeleteCommunity> { impl Perform for DeleteCommunity {
type Response = CommunityResponse; type Response = CommunityResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<CommunityResponse, LemmyError> { ) -> Result<CommunityResponse, LemmyError> {
let data: &DeleteCommunity = &self.data; let data: &DeleteCommunity = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
// Verify its the creator (only a creator can delete the community) // Verify its the creator (only a creator can delete the community)
@ -432,12 +437,10 @@ impl Perform for Oper<DeleteCommunity> {
// Send apub messages // Send apub messages
if deleted { if deleted {
updated_community updated_community.send_delete(&user, &client, pool).await?;
.send_delete(&user, &self.client, pool)
.await?;
} else { } else {
updated_community updated_community
.send_undo_delete(&user, &self.client, pool) .send_undo_delete(&user, &client, pool)
.await?; .await?;
} }
@ -459,15 +462,16 @@ impl Perform for Oper<DeleteCommunity> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<RemoveCommunity> { impl Perform for RemoveCommunity {
type Response = CommunityResponse; type Response = CommunityResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<CommunityResponse, LemmyError> { ) -> Result<CommunityResponse, LemmyError> {
let data: &RemoveCommunity = &self.data; let data: &RemoveCommunity = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
// Verify its an admin (only an admin can remove a community) // Verify its an admin (only an admin can remove a community)
@ -501,12 +505,10 @@ impl Perform for Oper<RemoveCommunity> {
// Apub messages // Apub messages
if removed { if removed {
updated_community updated_community.send_remove(&user, &client, pool).await?;
.send_remove(&user, &self.client, pool)
.await?;
} else { } else {
updated_community updated_community
.send_undo_remove(&user, &self.client, pool) .send_undo_remove(&user, &client, pool)
.await?; .await?;
} }
@ -528,15 +530,16 @@ impl Perform for Oper<RemoveCommunity> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<ListCommunities> { impl Perform for ListCommunities {
type Response = ListCommunitiesResponse; type Response = ListCommunitiesResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<ListCommunitiesResponse, LemmyError> { ) -> Result<ListCommunitiesResponse, LemmyError> {
let data: &ListCommunities = &self.data; let data: &ListCommunities = &self;
let user = get_user_from_jwt_opt(&data.auth, pool).await?; let user = get_user_from_jwt_opt(&data.auth, pool).await?;
let user_id = match &user { let user_id = match &user {
@ -570,15 +573,16 @@ impl Perform for Oper<ListCommunities> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<FollowCommunity> { impl Perform for FollowCommunity {
type Response = CommunityResponse; type Response = CommunityResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<CommunityResponse, LemmyError> { ) -> Result<CommunityResponse, LemmyError> {
let data: &FollowCommunity = &self.data; let data: &FollowCommunity = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let community_id = data.community_id; let community_id = data.community_id;
@ -605,11 +609,11 @@ impl Perform for Oper<FollowCommunity> {
// Dont actually add to the community followers here, because you need // Dont actually add to the community followers here, because you need
// to wait for the accept // to wait for the accept
user user
.send_follow(&community.actor_id()?, &self.client, pool) .send_follow(&community.actor_id()?, &client, pool)
.await?; .await?;
} else { } else {
user user
.send_unfollow(&community.actor_id()?, &self.client, pool) .send_unfollow(&community.actor_id()?, &client, pool)
.await?; .await?;
let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form); let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
if blocking(pool, unfollow).await?.is_err() { if blocking(pool, unfollow).await?.is_err() {
@ -632,15 +636,16 @@ impl Perform for Oper<FollowCommunity> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetFollowedCommunities> { impl Perform for GetFollowedCommunities {
type Response = GetFollowedCommunitiesResponse; type Response = GetFollowedCommunitiesResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetFollowedCommunitiesResponse, LemmyError> { ) -> Result<GetFollowedCommunitiesResponse, LemmyError> {
let data: &GetFollowedCommunities = &self.data; let data: &GetFollowedCommunities = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let user_id = user.id; let user_id = user.id;
@ -659,15 +664,16 @@ impl Perform for Oper<GetFollowedCommunities> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<BanFromCommunity> { impl Perform for BanFromCommunity {
type Response = BanFromCommunityResponse; type Response = BanFromCommunityResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<BanFromCommunityResponse, LemmyError> { ) -> Result<BanFromCommunityResponse, LemmyError> {
let data: &BanFromCommunity = &self.data; let data: &BanFromCommunity = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let community_id = data.community_id; let community_id = data.community_id;
@ -731,15 +737,16 @@ impl Perform for Oper<BanFromCommunity> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<AddModToCommunity> { impl Perform for AddModToCommunity {
type Response = AddModToCommunityResponse; type Response = AddModToCommunityResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<AddModToCommunityResponse, LemmyError> { ) -> Result<AddModToCommunityResponse, LemmyError> {
let data: &AddModToCommunity = &self.data; let data: &AddModToCommunity = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let community_moderator_form = CommunityModeratorForm { let community_moderator_form = CommunityModeratorForm {
@ -795,15 +802,16 @@ impl Perform for Oper<AddModToCommunity> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<TransferCommunity> { impl Perform for TransferCommunity {
type Response = GetCommunityResponse; type Response = GetCommunityResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetCommunityResponse, LemmyError> { ) -> Result<GetCommunityResponse, LemmyError> {
let data: &TransferCommunity = &self.data; let data: &TransferCommunity = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let community_id = data.community_id; let community_id = data.community_id;

View file

@ -33,17 +33,6 @@ impl APIError {
} }
} }
pub struct Oper<T> {
data: T,
client: Client,
}
impl<Data> Oper<Data> {
pub fn new(data: Data, client: Client) -> Oper<Data> {
Oper { data, client }
}
}
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
pub trait Perform { pub trait Perform {
type Response: serde::ser::Serialize + Send; type Response: serde::ser::Serialize + Send;
@ -52,6 +41,7 @@ pub trait Perform {
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<Self::Response, LemmyError>; ) -> Result<Self::Response, LemmyError>;
} }

View file

@ -7,7 +7,6 @@ use crate::{
get_user_from_jwt_opt, get_user_from_jwt_opt,
is_mod_or_admin, is_mod_or_admin,
APIError, APIError,
Oper,
Perform, Perform,
}, },
apub::{ApubLikeableType, ApubObjectType}, apub::{ApubLikeableType, ApubObjectType},
@ -21,6 +20,7 @@ use crate::{
DbPool, DbPool,
LemmyError, LemmyError,
}; };
use actix_web::client::Client;
use lemmy_db::{ use lemmy_db::{
comment_view::*, comment_view::*,
community_view::*, community_view::*,
@ -140,15 +140,16 @@ pub struct SavePost {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<CreatePost> { impl Perform for CreatePost {
type Response = PostResponse; type Response = PostResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<PostResponse, LemmyError> { ) -> Result<PostResponse, LemmyError> {
let data: &CreatePost = &self.data; let data: &CreatePost = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
check_slurs(&data.name)?; check_slurs(&data.name)?;
@ -169,7 +170,7 @@ impl Perform for Oper<CreatePost> {
// Fetch Iframely and pictrs cached image // Fetch Iframely and pictrs cached image
let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) = let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
fetch_iframely_and_pictrs_data(&self.client, data.url.to_owned()).await; fetch_iframely_and_pictrs_data(&client, data.url.to_owned()).await;
let post_form = PostForm { let post_form = PostForm {
name: data.name.trim().to_owned(), name: data.name.trim().to_owned(),
@ -217,7 +218,7 @@ impl Perform for Oper<CreatePost> {
Err(_e) => return Err(APIError::err("couldnt_create_post").into()), Err(_e) => return Err(APIError::err("couldnt_create_post").into()),
}; };
updated_post.send_create(&user, &self.client, pool).await?; updated_post.send_create(&user, &client, pool).await?;
// They like their own post by default // They like their own post by default
let like_form = PostLikeForm { let like_form = PostLikeForm {
@ -231,7 +232,7 @@ impl Perform for Oper<CreatePost> {
return Err(APIError::err("couldnt_like_post").into()); return Err(APIError::err("couldnt_like_post").into());
} }
updated_post.send_like(&user, &self.client, pool).await?; updated_post.send_like(&user, &client, pool).await?;
// Refetch the view // Refetch the view
let inserted_post_id = inserted_post.id; let inserted_post_id = inserted_post.id;
@ -259,15 +260,16 @@ impl Perform for Oper<CreatePost> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetPost> { impl Perform for GetPost {
type Response = GetPostResponse; type Response = GetPostResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetPostResponse, LemmyError> { ) -> Result<GetPostResponse, LemmyError> {
let data: &GetPost = &self.data; let data: &GetPost = &self;
let user = get_user_from_jwt_opt(&data.auth, pool).await?; let user = get_user_from_jwt_opt(&data.auth, pool).await?;
let user_id = user.map(|u| u.id); let user_id = user.map(|u| u.id);
@ -329,15 +331,16 @@ impl Perform for Oper<GetPost> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetPosts> { impl Perform for GetPosts {
type Response = GetPostsResponse; type Response = GetPostsResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetPostsResponse, LemmyError> { ) -> Result<GetPostsResponse, LemmyError> {
let data: &GetPosts = &self.data; let data: &GetPosts = &self;
let user = get_user_from_jwt_opt(&data.auth, pool).await?; let user = get_user_from_jwt_opt(&data.auth, pool).await?;
let user_id = match &user { let user_id = match &user {
@ -394,15 +397,16 @@ impl Perform for Oper<GetPosts> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<CreatePostLike> { impl Perform for CreatePostLike {
type Response = PostResponse; type Response = PostResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<PostResponse, LemmyError> { ) -> Result<PostResponse, LemmyError> {
let data: &CreatePostLike = &self.data; let data: &CreatePostLike = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
// Don't do a downvote if site has downvotes disabled // Don't do a downvote if site has downvotes disabled
@ -439,12 +443,12 @@ impl Perform for Oper<CreatePostLike> {
} }
if like_form.score == 1 { if like_form.score == 1 {
post.send_like(&user, &self.client, pool).await?; post.send_like(&user, &client, pool).await?;
} else if like_form.score == -1 { } else if like_form.score == -1 {
post.send_dislike(&user, &self.client, pool).await?; post.send_dislike(&user, &client, pool).await?;
} }
} else { } else {
post.send_undo_like(&user, &self.client, pool).await?; post.send_undo_like(&user, &client, pool).await?;
} }
let post_id = data.post_id; let post_id = data.post_id;
@ -473,15 +477,16 @@ impl Perform for Oper<CreatePostLike> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<EditPost> { impl Perform for EditPost {
type Response = PostResponse; type Response = PostResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<PostResponse, LemmyError> { ) -> Result<PostResponse, LemmyError> {
let data: &EditPost = &self.data; let data: &EditPost = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
check_slurs(&data.name)?; check_slurs(&data.name)?;
@ -503,7 +508,7 @@ impl Perform for Oper<EditPost> {
// Fetch Iframely and Pictrs cached image // Fetch Iframely and Pictrs cached image
let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) = let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
fetch_iframely_and_pictrs_data(&self.client, data.url.to_owned()).await; fetch_iframely_and_pictrs_data(&client, data.url.to_owned()).await;
let post_form = PostForm { let post_form = PostForm {
name: data.name.trim().to_owned(), name: data.name.trim().to_owned(),
@ -542,7 +547,7 @@ impl Perform for Oper<EditPost> {
}; };
// Send apub update // Send apub update
updated_post.send_update(&user, &self.client, pool).await?; updated_post.send_update(&user, &client, pool).await?;
let edit_id = data.edit_id; let edit_id = data.edit_id;
let post_view = blocking(pool, move |conn| { let post_view = blocking(pool, move |conn| {
@ -565,15 +570,16 @@ impl Perform for Oper<EditPost> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<DeletePost> { impl Perform for DeletePost {
type Response = PostResponse; type Response = PostResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<PostResponse, LemmyError> { ) -> Result<PostResponse, LemmyError> {
let data: &DeletePost = &self.data; let data: &DeletePost = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let edit_id = data.edit_id; let edit_id = data.edit_id;
@ -596,11 +602,9 @@ impl Perform for Oper<DeletePost> {
// apub updates // apub updates
if deleted { if deleted {
updated_post.send_delete(&user, &self.client, pool).await?; updated_post.send_delete(&user, &client, pool).await?;
} else { } else {
updated_post updated_post.send_undo_delete(&user, &client, pool).await?;
.send_undo_delete(&user, &self.client, pool)
.await?;
} }
// Refetch the post // Refetch the post
@ -625,15 +629,16 @@ impl Perform for Oper<DeletePost> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<RemovePost> { impl Perform for RemovePost {
type Response = PostResponse; type Response = PostResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<PostResponse, LemmyError> { ) -> Result<PostResponse, LemmyError> {
let data: &RemovePost = &self.data; let data: &RemovePost = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let edit_id = data.edit_id; let edit_id = data.edit_id;
@ -663,11 +668,9 @@ impl Perform for Oper<RemovePost> {
// apub updates // apub updates
if removed { if removed {
updated_post.send_remove(&user, &self.client, pool).await?; updated_post.send_remove(&user, &client, pool).await?;
} else { } else {
updated_post updated_post.send_undo_remove(&user, &client, pool).await?;
.send_undo_remove(&user, &self.client, pool)
.await?;
} }
// Refetch the post // Refetch the post
@ -693,15 +696,16 @@ impl Perform for Oper<RemovePost> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<LockPost> { impl Perform for LockPost {
type Response = PostResponse; type Response = PostResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<PostResponse, LemmyError> { ) -> Result<PostResponse, LemmyError> {
let data: &LockPost = &self.data; let data: &LockPost = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let edit_id = data.edit_id; let edit_id = data.edit_id;
@ -727,7 +731,7 @@ impl Perform for Oper<LockPost> {
blocking(pool, move |conn| ModLockPost::create(conn, &form)).await??; blocking(pool, move |conn| ModLockPost::create(conn, &form)).await??;
// apub updates // apub updates
updated_post.send_update(&user, &self.client, pool).await?; updated_post.send_update(&user, &client, pool).await?;
// Refetch the post // Refetch the post
let edit_id = data.edit_id; let edit_id = data.edit_id;
@ -751,15 +755,16 @@ impl Perform for Oper<LockPost> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<StickyPost> { impl Perform for StickyPost {
type Response = PostResponse; type Response = PostResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<PostResponse, LemmyError> { ) -> Result<PostResponse, LemmyError> {
let data: &StickyPost = &self.data; let data: &StickyPost = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let edit_id = data.edit_id; let edit_id = data.edit_id;
@ -788,7 +793,7 @@ impl Perform for Oper<StickyPost> {
// Apub updates // Apub updates
// TODO stickied should pry work like locked for ease of use // TODO stickied should pry work like locked for ease of use
updated_post.send_update(&user, &self.client, pool).await?; updated_post.send_update(&user, &client, pool).await?;
// Refetch the post // Refetch the post
let edit_id = data.edit_id; let edit_id = data.edit_id;
@ -812,15 +817,16 @@ impl Perform for Oper<StickyPost> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<SavePost> { impl Perform for SavePost {
type Response = PostResponse; type Response = PostResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<PostResponse, LemmyError> { ) -> Result<PostResponse, LemmyError> {
let data: &SavePost = &self.data; let data: &SavePost = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let post_saved_form = PostSavedForm { let post_saved_form = PostSavedForm {

View file

@ -7,7 +7,6 @@ use crate::{
get_user_from_jwt_opt, get_user_from_jwt_opt,
is_admin, is_admin,
APIError, APIError,
Oper,
Perform, Perform,
}, },
apub::fetcher::search_by_apub_id, apub::fetcher::search_by_apub_id,
@ -17,6 +16,7 @@ use crate::{
DbPool, DbPool,
LemmyError, LemmyError,
}; };
use actix_web::client::Client;
use lemmy_db::{ use lemmy_db::{
category::*, category::*,
comment_view::*, comment_view::*,
@ -156,15 +156,16 @@ pub struct SaveSiteConfig {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<ListCategories> { impl Perform for ListCategories {
type Response = ListCategoriesResponse; type Response = ListCategoriesResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<ListCategoriesResponse, LemmyError> { ) -> Result<ListCategoriesResponse, LemmyError> {
let _data: &ListCategories = &self.data; let _data: &ListCategories = &self;
let categories = blocking(pool, move |conn| Category::list_all(conn)).await??; let categories = blocking(pool, move |conn| Category::list_all(conn)).await??;
@ -174,15 +175,16 @@ impl Perform for Oper<ListCategories> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetModlog> { impl Perform for GetModlog {
type Response = GetModlogResponse; type Response = GetModlogResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetModlogResponse, LemmyError> { ) -> Result<GetModlogResponse, LemmyError> {
let data: &GetModlog = &self.data; let data: &GetModlog = &self;
let community_id = data.community_id; let community_id = data.community_id;
let mod_user_id = data.mod_user_id; let mod_user_id = data.mod_user_id;
@ -248,15 +250,16 @@ impl Perform for Oper<GetModlog> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<CreateSite> { impl Perform for CreateSite {
type Response = SiteResponse; type Response = SiteResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<SiteResponse, LemmyError> { ) -> Result<SiteResponse, LemmyError> {
let data: &CreateSite = &self.data; let data: &CreateSite = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
@ -290,14 +293,15 @@ impl Perform for Oper<CreateSite> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<EditSite> { impl Perform for EditSite {
type Response = SiteResponse; type Response = SiteResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<SiteResponse, LemmyError> { ) -> Result<SiteResponse, LemmyError> {
let data: &EditSite = &self.data; let data: &EditSite = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
check_slurs(&data.name)?; check_slurs(&data.name)?;
@ -345,15 +349,16 @@ impl Perform for Oper<EditSite> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetSite> { impl Perform for GetSite {
type Response = GetSiteResponse; type Response = GetSiteResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<GetSiteResponse, LemmyError> { ) -> Result<GetSiteResponse, LemmyError> {
let data: &GetSite = &self.data; let data: &GetSite = &self;
// TODO refactor this a little // TODO refactor this a little
let res = blocking(pool, move |conn| Site::read(conn, 1)).await?; let res = blocking(pool, move |conn| Site::read(conn, 1)).await?;
@ -370,8 +375,8 @@ impl Perform for Oper<GetSite> {
captcha_uuid: None, captcha_uuid: None,
captcha_answer: None, captcha_answer: None,
}; };
let login_response = Oper::new(register, self.client.clone()) let login_response = register
.perform(pool, websocket_info.clone()) .perform(pool, websocket_info.clone(), client.clone())
.await?; .await?;
info!("Admin {} created", setup.admin_username); info!("Admin {} created", setup.admin_username);
@ -385,8 +390,8 @@ impl Perform for Oper<GetSite> {
enable_nsfw: true, enable_nsfw: true,
auth: login_response.jwt, auth: login_response.jwt,
}; };
Oper::new(create_site, self.client.clone()) create_site
.perform(pool, websocket_info.clone()) .perform(pool, websocket_info.clone(), client.clone())
.await?; .await?;
info!("Site {} created", setup.site_name); info!("Site {} created", setup.site_name);
Some(blocking(pool, move |conn| SiteView::read(conn)).await??) Some(blocking(pool, move |conn| SiteView::read(conn)).await??)
@ -440,19 +445,20 @@ impl Perform for Oper<GetSite> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<Search> { impl Perform for Search {
type Response = SearchResponse; type Response = SearchResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<SearchResponse, LemmyError> { ) -> Result<SearchResponse, LemmyError> {
let data: &Search = &self.data; let data: &Search = &self;
dbg!(&data); dbg!(&data);
match search_by_apub_id(&data.q, &self.client, pool).await { match search_by_apub_id(&data.q, &client, pool).await {
Ok(r) => return Ok(r), Ok(r) => return Ok(r),
Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e), Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
} }
@ -604,15 +610,16 @@ impl Perform for Oper<Search> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<TransferSite> { impl Perform for TransferSite {
type Response = GetSiteResponse; type Response = GetSiteResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetSiteResponse, LemmyError> { ) -> Result<GetSiteResponse, LemmyError> {
let data: &TransferSite = &self.data; let data: &TransferSite = &self;
let mut user = get_user_from_jwt(&data.auth, pool).await?; let mut user = get_user_from_jwt(&data.auth, pool).await?;
// TODO add a User_::read_safe() for this. // TODO add a User_::read_safe() for this.
@ -667,15 +674,16 @@ impl Perform for Oper<TransferSite> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetSiteConfig> { impl Perform for GetSiteConfig {
type Response = GetSiteConfigResponse; type Response = GetSiteConfigResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetSiteConfigResponse, LemmyError> { ) -> Result<GetSiteConfigResponse, LemmyError> {
let data: &GetSiteConfig = &self.data; let data: &GetSiteConfig = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
// Only let admins read this // Only let admins read this
@ -688,15 +696,16 @@ impl Perform for Oper<GetSiteConfig> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<SaveSiteConfig> { impl Perform for SaveSiteConfig {
type Response = GetSiteConfigResponse; type Response = GetSiteConfigResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetSiteConfigResponse, LemmyError> { ) -> Result<GetSiteConfigResponse, LemmyError> {
let data: &SaveSiteConfig = &self.data; let data: &SaveSiteConfig = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
// Only let admins read this // Only let admins read this

View file

@ -6,7 +6,6 @@ use crate::{
get_user_from_jwt_opt, get_user_from_jwt_opt,
is_admin, is_admin,
APIError, APIError,
Oper,
Perform, Perform,
}, },
apub::ApubObjectType, apub::ApubObjectType,
@ -20,6 +19,7 @@ use crate::{
DbPool, DbPool,
LemmyError, LemmyError,
}; };
use actix_web::client::Client;
use bcrypt::verify; use bcrypt::verify;
use captcha::{gen, Difficulty}; use captcha::{gen, Difficulty};
use chrono::Duration; use chrono::Duration;
@ -294,15 +294,16 @@ pub struct UserJoinResponse {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<Login> { impl Perform for Login {
type Response = LoginResponse; type Response = LoginResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<LoginResponse, LemmyError> { ) -> Result<LoginResponse, LemmyError> {
let data: &Login = &self.data; let data: &Login = &self;
// Fetch that username / email // Fetch that username / email
let username_or_email = data.username_or_email.clone(); let username_or_email = data.username_or_email.clone();
@ -329,15 +330,16 @@ impl Perform for Oper<Login> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<Register> { impl Perform for Register {
type Response = LoginResponse; type Response = LoginResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<LoginResponse, LemmyError> { ) -> Result<LoginResponse, LemmyError> {
let data: &Register = &self.data; let data: &Register = &self;
// Make sure site has open registration // Make sure site has open registration
if let Ok(site) = blocking(pool, move |conn| SiteView::read(conn)).await? { if let Ok(site) = blocking(pool, move |conn| SiteView::read(conn)).await? {
@ -498,13 +500,14 @@ impl Perform for Oper<Register> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetCaptcha> { impl Perform for GetCaptcha {
type Response = GetCaptchaResponse; type Response = GetCaptchaResponse;
async fn perform( async fn perform(
&self, &self,
_pool: &DbPool, _pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<Self::Response, LemmyError> { ) -> Result<Self::Response, LemmyError> {
let captcha_settings = Settings::get().captcha; let captcha_settings = Settings::get().captcha;
@ -546,15 +549,16 @@ impl Perform for Oper<GetCaptcha> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<SaveUserSettings> { impl Perform for SaveUserSettings {
type Response = LoginResponse; type Response = LoginResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<LoginResponse, LemmyError> { ) -> Result<LoginResponse, LemmyError> {
let data: &SaveUserSettings = &self.data; let data: &SaveUserSettings = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let user_id = user.id; let user_id = user.id;
@ -669,15 +673,16 @@ impl Perform for Oper<SaveUserSettings> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetUserDetails> { impl Perform for GetUserDetails {
type Response = GetUserDetailsResponse; type Response = GetUserDetailsResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetUserDetailsResponse, LemmyError> { ) -> Result<GetUserDetailsResponse, LemmyError> {
let data: &GetUserDetails = &self.data; let data: &GetUserDetails = &self;
let user = get_user_from_jwt_opt(&data.auth, pool).await?; let user = get_user_from_jwt_opt(&data.auth, pool).await?;
let show_nsfw = match &user { let show_nsfw = match &user {
@ -764,15 +769,16 @@ impl Perform for Oper<GetUserDetails> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<AddAdmin> { impl Perform for AddAdmin {
type Response = AddAdminResponse; type Response = AddAdminResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<AddAdminResponse, LemmyError> { ) -> Result<AddAdminResponse, LemmyError> {
let data: &AddAdmin = &self.data; let data: &AddAdmin = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
// Make sure user is an admin // Make sure user is an admin
@ -817,15 +823,16 @@ impl Perform for Oper<AddAdmin> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<BanUser> { impl Perform for BanUser {
type Response = BanUserResponse; type Response = BanUserResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<BanUserResponse, LemmyError> { ) -> Result<BanUserResponse, LemmyError> {
let data: &BanUser = &self.data; let data: &BanUser = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
// Make sure user is an admin // Make sure user is an admin
@ -875,15 +882,16 @@ impl Perform for Oper<BanUser> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetReplies> { impl Perform for GetReplies {
type Response = GetRepliesResponse; type Response = GetRepliesResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetRepliesResponse, LemmyError> { ) -> Result<GetRepliesResponse, LemmyError> {
let data: &GetReplies = &self.data; let data: &GetReplies = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let sort = SortType::from_str(&data.sort)?; let sort = SortType::from_str(&data.sort)?;
@ -907,15 +915,16 @@ impl Perform for Oper<GetReplies> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetUserMentions> { impl Perform for GetUserMentions {
type Response = GetUserMentionsResponse; type Response = GetUserMentionsResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetUserMentionsResponse, LemmyError> { ) -> Result<GetUserMentionsResponse, LemmyError> {
let data: &GetUserMentions = &self.data; let data: &GetUserMentions = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let sort = SortType::from_str(&data.sort)?; let sort = SortType::from_str(&data.sort)?;
@ -939,15 +948,16 @@ impl Perform for Oper<GetUserMentions> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<MarkUserMentionAsRead> { impl Perform for MarkUserMentionAsRead {
type Response = UserMentionResponse; type Response = UserMentionResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<UserMentionResponse, LemmyError> { ) -> Result<UserMentionResponse, LemmyError> {
let data: &MarkUserMentionAsRead = &self.data; let data: &MarkUserMentionAsRead = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let user_mention_id = data.user_mention_id; let user_mention_id = data.user_mention_id;
@ -979,15 +989,16 @@ impl Perform for Oper<MarkUserMentionAsRead> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<MarkAllAsRead> { impl Perform for MarkAllAsRead {
type Response = GetRepliesResponse; type Response = GetRepliesResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<GetRepliesResponse, LemmyError> { ) -> Result<GetRepliesResponse, LemmyError> {
let data: &MarkAllAsRead = &self.data; let data: &MarkAllAsRead = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let user_id = user.id; let user_id = user.id;
@ -1028,15 +1039,16 @@ impl Perform for Oper<MarkAllAsRead> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<DeleteAccount> { impl Perform for DeleteAccount {
type Response = LoginResponse; type Response = LoginResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<LoginResponse, LemmyError> { ) -> Result<LoginResponse, LemmyError> {
let data: &DeleteAccount = &self.data; let data: &DeleteAccount = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
// Verify the password // Verify the password
@ -1090,15 +1102,16 @@ impl Perform for Oper<DeleteAccount> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<PasswordReset> { impl Perform for PasswordReset {
type Response = PasswordResetResponse; type Response = PasswordResetResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<PasswordResetResponse, LemmyError> { ) -> Result<PasswordResetResponse, LemmyError> {
let data: &PasswordReset = &self.data; let data: &PasswordReset = &self;
// Fetch that email // Fetch that email
let email = data.email.clone(); let email = data.email.clone();
@ -1134,15 +1147,16 @@ impl Perform for Oper<PasswordReset> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<PasswordChange> { impl Perform for PasswordChange {
type Response = LoginResponse; type Response = LoginResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<LoginResponse, LemmyError> { ) -> Result<LoginResponse, LemmyError> {
let data: &PasswordChange = &self.data; let data: &PasswordChange = &self;
// Fetch the user_id from the token // Fetch the user_id from the token
let token = data.token.clone(); let token = data.token.clone();
@ -1175,15 +1189,16 @@ impl Perform for Oper<PasswordChange> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<CreatePrivateMessage> { impl Perform for CreatePrivateMessage {
type Response = PrivateMessageResponse; type Response = PrivateMessageResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<PrivateMessageResponse, LemmyError> { ) -> Result<PrivateMessageResponse, LemmyError> {
let data: &CreatePrivateMessage = &self.data; let data: &CreatePrivateMessage = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let hostname = &format!("https://{}", Settings::get().hostname); let hostname = &format!("https://{}", Settings::get().hostname);
@ -1229,7 +1244,7 @@ impl Perform for Oper<CreatePrivateMessage> {
}; };
updated_private_message updated_private_message
.send_create(&user, &self.client, pool) .send_create(&user, &client, pool)
.await?; .await?;
// Send notifications to the recipient // Send notifications to the recipient
@ -1274,15 +1289,16 @@ impl Perform for Oper<CreatePrivateMessage> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<EditPrivateMessage> { impl Perform for EditPrivateMessage {
type Response = PrivateMessageResponse; type Response = PrivateMessageResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<PrivateMessageResponse, LemmyError> { ) -> Result<PrivateMessageResponse, LemmyError> {
let data: &EditPrivateMessage = &self.data; let data: &EditPrivateMessage = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
// Checking permissions // Checking permissions
@ -1307,7 +1323,7 @@ impl Perform for Oper<EditPrivateMessage> {
// Send the apub update // Send the apub update
updated_private_message updated_private_message
.send_update(&user, &self.client, pool) .send_update(&user, &client, pool)
.await?; .await?;
let edit_id = data.edit_id; let edit_id = data.edit_id;
@ -1330,15 +1346,16 @@ impl Perform for Oper<EditPrivateMessage> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<DeletePrivateMessage> { impl Perform for DeletePrivateMessage {
type Response = PrivateMessageResponse; type Response = PrivateMessageResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
client: Client,
) -> Result<PrivateMessageResponse, LemmyError> { ) -> Result<PrivateMessageResponse, LemmyError> {
let data: &DeletePrivateMessage = &self.data; let data: &DeletePrivateMessage = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
// Checking permissions // Checking permissions
@ -1364,11 +1381,11 @@ impl Perform for Oper<DeletePrivateMessage> {
// Send the apub update // Send the apub update
if data.deleted { if data.deleted {
updated_private_message updated_private_message
.send_delete(&user, &self.client, pool) .send_delete(&user, &client, pool)
.await?; .await?;
} else { } else {
updated_private_message updated_private_message
.send_undo_delete(&user, &self.client, pool) .send_undo_delete(&user, &client, pool)
.await?; .await?;
} }
@ -1392,15 +1409,16 @@ impl Perform for Oper<DeletePrivateMessage> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<MarkPrivateMessageAsRead> { impl Perform for MarkPrivateMessageAsRead {
type Response = PrivateMessageResponse; type Response = PrivateMessageResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<PrivateMessageResponse, LemmyError> { ) -> Result<PrivateMessageResponse, LemmyError> {
let data: &MarkPrivateMessageAsRead = &self.data; let data: &MarkPrivateMessageAsRead = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
// Checking permissions // Checking permissions
@ -1445,15 +1463,16 @@ impl Perform for Oper<MarkPrivateMessageAsRead> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<GetPrivateMessages> { impl Perform for GetPrivateMessages {
type Response = PrivateMessagesResponse; type Response = PrivateMessagesResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
_websocket_info: Option<WebsocketInfo>, _websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<PrivateMessagesResponse, LemmyError> { ) -> Result<PrivateMessagesResponse, LemmyError> {
let data: &GetPrivateMessages = &self.data; let data: &GetPrivateMessages = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
let user_id = user.id; let user_id = user.id;
@ -1474,15 +1493,16 @@ impl Perform for Oper<GetPrivateMessages> {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for Oper<UserJoin> { impl Perform for UserJoin {
type Response = UserJoinResponse; type Response = UserJoinResponse;
async fn perform( async fn perform(
&self, &self,
pool: &DbPool, pool: &DbPool,
websocket_info: Option<WebsocketInfo>, websocket_info: Option<WebsocketInfo>,
_client: Client,
) -> Result<UserJoinResponse, LemmyError> { ) -> Result<UserJoinResponse, LemmyError> {
let data: &UserJoin = &self.data; let data: &UserJoin = &self;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
if let Some(ws) = websocket_info { if let Some(ws) = websocket_info {

View file

@ -1,5 +1,5 @@
use crate::{ use crate::{
api::{comment::*, community::*, post::*, site::*, user::*, Oper, Perform}, api::{comment::*, community::*, post::*, site::*, user::*, Perform},
rate_limit::RateLimit, rate_limit::RateLimit,
routes::{ChatServerParam, DbPoolParam}, routes::{ChatServerParam, DbPoolParam},
websocket::WebsocketInfo, websocket::WebsocketInfo,
@ -179,7 +179,7 @@ async fn perform<Request>(
chat_server: ChatServerParam, chat_server: ChatServerParam,
) -> Result<HttpResponse, Error> ) -> Result<HttpResponse, Error>
where where
Oper<Request>: Perform, Request: Perform,
Request: Send + 'static, Request: Send + 'static,
{ {
let ws_info = WebsocketInfo { let ws_info = WebsocketInfo {
@ -187,10 +187,8 @@ where
id: None, id: None,
}; };
let oper: Oper<Request> = Oper::new(data, client.clone()); let res = data
.perform(&db, Some(ws_info), client.clone())
let res = oper
.perform(&db, Some(ws_info))
.await .await
.map(|json| HttpResponse::Ok().json(json)) .map(|json| HttpResponse::Ok().json(json))
.map_err(ErrorBadRequest)?; .map_err(ErrorBadRequest)?;
@ -204,8 +202,7 @@ async fn route_get<Data>(
chat_server: ChatServerParam, chat_server: ChatServerParam,
) -> Result<HttpResponse, Error> ) -> Result<HttpResponse, Error>
where where
Data: Serialize + Send + 'static, Data: Serialize + Send + 'static + Perform,
Oper<Data>: Perform,
{ {
perform::<Data>(data.0, &client, db, chat_server).await perform::<Data>(data.0, &client, db, chat_server).await
} }
@ -217,8 +214,7 @@ async fn route_post<Data>(
chat_server: ChatServerParam, chat_server: ChatServerParam,
) -> Result<HttpResponse, Error> ) -> Result<HttpResponse, Error>
where where
Data: Serialize + Send + 'static, Data: Serialize + Send + 'static + Perform,
Oper<Data>: Perform,
{ {
perform::<Data>(data.0, &client, db, chat_server).await perform::<Data>(data.0, &client, db, chat_server).await
} }

View file

@ -556,7 +556,7 @@ struct Args<'a> {
async fn do_user_operation<'a, 'b, Data>(args: Args<'b>) -> Result<String, LemmyError> async fn do_user_operation<'a, 'b, Data>(args: Args<'b>) -> Result<String, LemmyError>
where where
for<'de> Data: Deserialize<'de> + 'a, for<'de> Data: Deserialize<'de> + 'a,
Oper<Data>: Perform, Data: Perform,
{ {
let Args { let Args {
client, client,
@ -581,9 +581,7 @@ where
let fut = async move { let fut = async move {
let pool = pool.clone(); let pool = pool.clone();
let parsed_data: Data = serde_json::from_str(&data)?; let parsed_data: Data = serde_json::from_str(&data)?;
let res = Oper::new(parsed_data, client) let res = parsed_data.perform(&pool, Some(ws_info), client).await?;
.perform(&pool, Some(ws_info))
.await?;
to_json_string(&op, &res) to_json_string(&op, &res)
}; };