diff --git a/src/api/mod.rs b/src/api/mod.rs index 11d9aa5d6..d3bb6956a 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,4 +1,4 @@ -use crate::{api::claims::Claims, websocket::handlers::Args, DbPool, LemmyContext}; +use crate::{api::claims::Claims, DbPool, LemmyContext}; use actix_web::{web, web::Data}; use lemmy_db::{ community::Community, @@ -106,26 +106,168 @@ pub(in crate::api) async fn check_community_ban( } } -pub(super) async fn do_user_operation<'a, 'b, Data>(args: Args<'b>) -> Result -where - for<'de> Data: Deserialize<'de> + 'a, - Data: Perform, -{ - let Args { - context, - id, - op, - data, - } = args; - - do_websocket_operation::(context, data, id, op).await -} - -pub async fn do_websocket_operation<'a, 'b, Data>( +pub async fn match_websocket_operation( context: LemmyContext, - data: &'a str, id: ConnectionId, op: UserOperation, + data: &str, +) -> Result { + match op { + // User ops + UserOperation::Login => do_websocket_operation::(context, id, op, data).await, + UserOperation::Register => do_websocket_operation::(context, id, op, data).await, + UserOperation::GetCaptcha => do_websocket_operation::(context, id, op, data).await, + UserOperation::GetUserDetails => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::GetReplies => do_websocket_operation::(context, id, op, data).await, + UserOperation::AddAdmin => do_websocket_operation::(context, id, op, data).await, + UserOperation::BanUser => do_websocket_operation::(context, id, op, data).await, + UserOperation::GetUserMentions => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::MarkUserMentionAsRead => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::MarkAllAsRead => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::DeleteAccount => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::PasswordReset => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::PasswordChange => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::UserJoin => do_websocket_operation::(context, id, op, data).await, + UserOperation::PostJoin => do_websocket_operation::(context, id, op, data).await, + UserOperation::CommunityJoin => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::SaveUserSettings => { + do_websocket_operation::(context, id, op, data).await + } + + // Private Message ops + UserOperation::CreatePrivateMessage => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::EditPrivateMessage => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::DeletePrivateMessage => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::MarkPrivateMessageAsRead => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::GetPrivateMessages => { + do_websocket_operation::(context, id, op, data).await + } + + // Site ops + UserOperation::GetModlog => do_websocket_operation::(context, id, op, data).await, + UserOperation::CreateSite => do_websocket_operation::(context, id, op, data).await, + UserOperation::EditSite => do_websocket_operation::(context, id, op, data).await, + UserOperation::GetSite => do_websocket_operation::(context, id, op, data).await, + UserOperation::GetSiteConfig => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::SaveSiteConfig => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::Search => do_websocket_operation::(context, id, op, data).await, + UserOperation::TransferCommunity => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::TransferSite => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::ListCategories => { + do_websocket_operation::(context, id, op, data).await + } + + // Community ops + UserOperation::GetCommunity => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::ListCommunities => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::CreateCommunity => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::EditCommunity => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::DeleteCommunity => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::RemoveCommunity => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::FollowCommunity => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::GetFollowedCommunities => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::BanFromCommunity => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::AddModToCommunity => { + do_websocket_operation::(context, id, op, data).await + } + + // Post ops + UserOperation::CreatePost => do_websocket_operation::(context, id, op, data).await, + UserOperation::GetPost => do_websocket_operation::(context, id, op, data).await, + UserOperation::GetPosts => do_websocket_operation::(context, id, op, data).await, + UserOperation::EditPost => do_websocket_operation::(context, id, op, data).await, + UserOperation::DeletePost => do_websocket_operation::(context, id, op, data).await, + UserOperation::RemovePost => do_websocket_operation::(context, id, op, data).await, + UserOperation::LockPost => do_websocket_operation::(context, id, op, data).await, + UserOperation::StickyPost => do_websocket_operation::(context, id, op, data).await, + UserOperation::CreatePostLike => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::SavePost => do_websocket_operation::(context, id, op, data).await, + + // Comment ops + UserOperation::CreateComment => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::EditComment => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::DeleteComment => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::RemoveComment => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::MarkCommentAsRead => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::SaveComment => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::GetComments => { + do_websocket_operation::(context, id, op, data).await + } + UserOperation::CreateCommentLike => { + do_websocket_operation::(context, id, op, data).await + } + } +} + +async fn do_websocket_operation<'a, 'b, Data>( + context: LemmyContext, + id: ConnectionId, + op: UserOperation, + data: &str, ) -> Result where for<'de> Data: Deserialize<'de> + 'a, @@ -137,83 +279,3 @@ where .await?; serialize_websocket_message(&op, &res) } - -pub async fn xxx(args: Args<'_>) -> Result { - match args.op { - // User ops - UserOperation::Login => do_user_operation::(args).await, - UserOperation::Register => do_user_operation::(args).await, - UserOperation::GetCaptcha => do_user_operation::(args).await, - UserOperation::GetUserDetails => do_user_operation::(args).await, - UserOperation::GetReplies => do_user_operation::(args).await, - UserOperation::AddAdmin => do_user_operation::(args).await, - UserOperation::BanUser => do_user_operation::(args).await, - UserOperation::GetUserMentions => do_user_operation::(args).await, - UserOperation::MarkUserMentionAsRead => do_user_operation::(args).await, - UserOperation::MarkAllAsRead => do_user_operation::(args).await, - UserOperation::DeleteAccount => do_user_operation::(args).await, - UserOperation::PasswordReset => do_user_operation::(args).await, - UserOperation::PasswordChange => do_user_operation::(args).await, - UserOperation::UserJoin => do_user_operation::(args).await, - UserOperation::PostJoin => do_user_operation::(args).await, - UserOperation::CommunityJoin => do_user_operation::(args).await, - UserOperation::SaveUserSettings => do_user_operation::(args).await, - - // Private Message ops - UserOperation::CreatePrivateMessage => do_user_operation::(args).await, - UserOperation::EditPrivateMessage => do_user_operation::(args).await, - UserOperation::DeletePrivateMessage => do_user_operation::(args).await, - UserOperation::MarkPrivateMessageAsRead => { - do_user_operation::(args).await - } - UserOperation::GetPrivateMessages => do_user_operation::(args).await, - - // Site ops - UserOperation::GetModlog => do_user_operation::(args).await, - UserOperation::CreateSite => do_user_operation::(args).await, - UserOperation::EditSite => do_user_operation::(args).await, - UserOperation::GetSite => do_user_operation::(args).await, - UserOperation::GetSiteConfig => do_user_operation::(args).await, - UserOperation::SaveSiteConfig => do_user_operation::(args).await, - UserOperation::Search => do_user_operation::(args).await, - UserOperation::TransferCommunity => do_user_operation::(args).await, - UserOperation::TransferSite => do_user_operation::(args).await, - UserOperation::ListCategories => do_user_operation::(args).await, - - // Community ops - UserOperation::GetCommunity => do_user_operation::(args).await, - UserOperation::ListCommunities => do_user_operation::(args).await, - UserOperation::CreateCommunity => do_user_operation::(args).await, - UserOperation::EditCommunity => do_user_operation::(args).await, - UserOperation::DeleteCommunity => do_user_operation::(args).await, - UserOperation::RemoveCommunity => do_user_operation::(args).await, - UserOperation::FollowCommunity => do_user_operation::(args).await, - UserOperation::GetFollowedCommunities => { - do_user_operation::(args).await - } - UserOperation::BanFromCommunity => do_user_operation::(args).await, - UserOperation::AddModToCommunity => do_user_operation::(args).await, - - // Post ops - UserOperation::CreatePost => do_user_operation::(args).await, - UserOperation::GetPost => do_user_operation::(args).await, - UserOperation::GetPosts => do_user_operation::(args).await, - UserOperation::EditPost => do_user_operation::(args).await, - UserOperation::DeletePost => do_user_operation::(args).await, - UserOperation::RemovePost => do_user_operation::(args).await, - UserOperation::LockPost => do_user_operation::(args).await, - UserOperation::StickyPost => do_user_operation::(args).await, - UserOperation::CreatePostLike => do_user_operation::(args).await, - UserOperation::SavePost => do_user_operation::(args).await, - - // Comment ops - UserOperation::CreateComment => do_user_operation::(args).await, - UserOperation::EditComment => do_user_operation::(args).await, - UserOperation::DeleteComment => do_user_operation::(args).await, - UserOperation::RemoveComment => do_user_operation::(args).await, - UserOperation::MarkCommentAsRead => do_user_operation::(args).await, - UserOperation::SaveComment => do_user_operation::(args).await, - UserOperation::GetComments => do_user_operation::(args).await, - UserOperation::CreateCommentLike => do_user_operation::(args).await, - } -} diff --git a/src/main.rs b/src/main.rs index 331a7490a..17c9a3f50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use lazy_static::lazy_static; use lemmy_db::get_database_url_from_env; use lemmy_rate_limit::{rate_limiter::RateLimiter, RateLimit}; use lemmy_server::{ - api::xxx, + api::match_websocket_operation, apub::activity_queue::create_activity_queue, code_migrations::run_advanced_migrations, routes::*, @@ -78,7 +78,7 @@ async fn main() -> Result<(), LemmyError> { let chat_server = ChatServer::startup( pool.clone(), rate_limiter.clone(), - |args| Box::pin(xxx(args)), + |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)), Client::default(), activity_queue.clone(), ) diff --git a/src/websocket/chat_server.rs b/src/websocket/chat_server.rs index ccc8155ae..648047e0f 100644 --- a/src/websocket/chat_server.rs +++ b/src/websocket/chat_server.rs @@ -1,4 +1,4 @@ -use crate::{websocket::handlers::Args, LemmyContext}; +use crate::LemmyContext; use actix::prelude::*; use anyhow::Context as acontext; use background_jobs::QueueHandle; @@ -28,8 +28,12 @@ use std::{ }; use tokio::macros::support::Pin; -type MessageHandlerType = - fn(args: Args) -> Pin> + '_>>; +type MessageHandlerType = fn( + context: LemmyContext, + id: ConnectionId, + op: UserOperation, + data: &str, +) -> Pin> + '_>>; /// `ChatServer` manages chat rooms and responsible for coordinating chat /// session. @@ -373,14 +377,8 @@ impl ChatServer { client, activity_queue, }; - let args = Args { - context, - id: msg.id, - op: user_operation.clone(), - data, - }; - let fut = (message_handler)(args); + let fut = (message_handler)(context, msg.id, user_operation.clone(), data); match user_operation { UserOperation::Register => rate_limiter.register().wrap(ip, fut).await, UserOperation::CreatePost => rate_limiter.post().wrap(ip, fut).await, diff --git a/src/websocket/handlers.rs b/src/websocket/handlers.rs index 601fd0896..b255c1b92 100644 --- a/src/websocket/handlers.rs +++ b/src/websocket/handlers.rs @@ -1,22 +1,11 @@ -use crate::{ - websocket::chat_server::{ChatServer, SessionInfo}, - LemmyContext, -}; +use crate::websocket::chat_server::{ChatServer, SessionInfo}; use actix::{Actor, Context, Handler, ResponseFuture}; use lemmy_db::naive_now; use lemmy_structs::websocket::*; -use lemmy_utils::ConnectionId; use log::{error, info}; use rand::Rng; use serde::Serialize; -pub struct Args<'a> { - pub context: LemmyContext, - pub id: ConnectionId, - pub op: UserOperation, - pub data: &'a str, -} - /// Make actor from `ChatServer` impl Actor for ChatServer { /// We are going to use simple Context, we just need ability to communicate