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

View File

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

View File

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

View File

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

View File

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

View File

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