From e826ecc4efa3a7202c31bee4f4702b4baf83bc7a Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 2 Sep 2020 13:45:23 +0200 Subject: [PATCH] Fix some errors by moving LemmyContext and Perform into lemmy_websocket --- server/Cargo.lock | 2 + server/lemmy_api_structs/src/lib.rs | 13 ----- server/lemmy_db/src/lib.rs | 2 + server/lemmy_websocket/Cargo.toml | 2 + server/lemmy_websocket/src/handlers.rs | 14 ++---- server/lemmy_websocket/src/lib.rs | 66 ++++++++++++++++++++++++++ server/src/api/handle_websocket.rs | 0 server/src/lib.rs | 48 ------------------- 8 files changed, 77 insertions(+), 70 deletions(-) create mode 100644 server/src/api/handle_websocket.rs diff --git a/server/Cargo.lock b/server/Cargo.lock index 33e2d4367..20cb1ac1c 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -1918,6 +1918,8 @@ dependencies = [ "actix", "actix-web", "anyhow", + "async-trait", + "background-jobs", "chrono", "diesel", "lemmy_api_structs", diff --git a/server/lemmy_api_structs/src/lib.rs b/server/lemmy_api_structs/src/lib.rs index 1d1aaf0a2..05b02581d 100644 --- a/server/lemmy_api_structs/src/lib.rs +++ b/server/lemmy_api_structs/src/lib.rs @@ -9,8 +9,6 @@ pub mod site; pub mod user; use thiserror::Error; -use actix_web::web::Data; -use lemmy_utils::{ConnectionId, LemmyError}; #[derive(Debug, Error)] #[error("{{\"error\":\"{message}\"}}")] @@ -24,15 +22,4 @@ impl APIError { message: msg.to_string(), } } -} - -#[async_trait::async_trait(?Send)] -pub trait Perform { - type Response: serde::ser::Serialize + Send; - - async fn perform( - &self, - context: &Data, - websocket_id: Option, - ) -> Result; } \ No newline at end of file diff --git a/server/lemmy_db/src/lib.rs b/server/lemmy_db/src/lib.rs index bf7d00cab..d2e1862cf 100644 --- a/server/lemmy_db/src/lib.rs +++ b/server/lemmy_db/src/lib.rs @@ -40,6 +40,8 @@ pub mod user_mention; pub mod user_mention_view; pub mod user_view; +pub type DbPool = diesel::r2d2::Pool>; + pub trait Crud { fn create(conn: &PgConnection, form: &T) -> Result where diff --git a/server/lemmy_websocket/Cargo.toml b/server/lemmy_websocket/Cargo.toml index ab5ea349c..49dd3760f 100644 --- a/server/lemmy_websocket/Cargo.toml +++ b/server/lemmy_websocket/Cargo.toml @@ -21,3 +21,5 @@ chrono = { version = "0.4.7", features = ["serde"] } reqwest = { version = "0.10", features = ["json"] } strum = "0.18.0" strum_macros = "0.18.0" +async-trait = "0.1.36" +background-jobs = " 0.8.0-alpha.2" diff --git a/server/lemmy_websocket/src/handlers.rs b/server/lemmy_websocket/src/handlers.rs index 13c5e276e..b31e150f6 100644 --- a/server/lemmy_websocket/src/handlers.rs +++ b/server/lemmy_websocket/src/handlers.rs @@ -1,12 +1,8 @@ -use crate::{ - api::Perform, - { - chat_server::{ChatServer, SessionInfo}, - messages::*, - UserOperation, - }, - LemmyContext, -}; +use crate::{ { + chat_server::{ChatServer, SessionInfo}, + messages::*, + UserOperation, +}, LemmyContext, Perform}; use actix::{Actor, Context, Handler, ResponseFuture}; use actix_web::web; use lemmy_db::naive_now; diff --git a/server/lemmy_websocket/src/lib.rs b/server/lemmy_websocket/src/lib.rs index 5e4d56998..28de6ee0e 100644 --- a/server/lemmy_websocket/src/lib.rs +++ b/server/lemmy_websocket/src/lib.rs @@ -10,6 +10,15 @@ pub extern crate reqwest; pub extern crate log; #[macro_use] pub extern crate strum_macros; +pub extern crate background_jobs; + +use actix::Addr; +use crate::chat_server::ChatServer; +use reqwest::Client; +use lemmy_db::DbPool; +use background_jobs::QueueHandle; +use actix_web::web::Data; +use lemmy_utils::{ConnectionId, LemmyError}; pub mod chat_server; pub mod handlers; @@ -76,3 +85,60 @@ pub enum UserOperation { GetSiteConfig, SaveSiteConfig, } + +pub struct LemmyContext { + pub pool: DbPool, + pub chat_server: Addr, + pub client: Client, + pub activity_queue: QueueHandle, +} + +impl LemmyContext { + pub fn create( + pool: DbPool, + chat_server: Addr, + client: Client, + activity_queue: QueueHandle, + ) -> LemmyContext { + LemmyContext { + pool, + chat_server, + client, + activity_queue, + } + } + pub fn pool(&self) -> &DbPool { + &self.pool + } + pub fn chat_server(&self) -> &Addr { + &self.chat_server + } + pub fn client(&self) -> &Client { + &self.client + } + pub fn activity_queue(&self) -> &QueueHandle { + &self.activity_queue + } +} + +impl Clone for LemmyContext { + fn clone(&self) -> Self { + LemmyContext { + pool: self.pool.clone(), + chat_server: self.chat_server.clone(), + client: self.client.clone(), + activity_queue: self.activity_queue.clone(), + } + } +} + +#[async_trait::async_trait(?Send)] +pub trait Perform { + type Response: serde::ser::Serialize + Send; + + async fn perform( + &self, + context: &Data, + websocket_id: Option, + ) -> Result; +} \ No newline at end of file diff --git a/server/src/api/handle_websocket.rs b/server/src/api/handle_websocket.rs new file mode 100644 index 000000000..e69de29bb diff --git a/server/src/lib.rs b/server/src/lib.rs index 380dc4454..8a817c3b7 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -43,54 +43,6 @@ use reqwest::Client; use serde::Deserialize; use std::process::Command; -pub type DbPool = diesel::r2d2::Pool>; - -pub struct LemmyContext { - pub pool: DbPool, - pub chat_server: Addr, - pub client: Client, - pub activity_queue: QueueHandle, -} - -impl LemmyContext { - pub fn create( - pool: DbPool, - chat_server: Addr, - client: Client, - activity_queue: QueueHandle, - ) -> LemmyContext { - LemmyContext { - pool, - chat_server, - client, - activity_queue, - } - } - pub fn pool(&self) -> &DbPool { - &self.pool - } - pub fn chat_server(&self) -> &Addr { - &self.chat_server - } - pub fn client(&self) -> &Client { - &self.client - } - pub fn activity_queue(&self) -> &QueueHandle { - &self.activity_queue - } -} - -impl Clone for LemmyContext { - fn clone(&self) -> Self { - LemmyContext { - pool: self.pool.clone(), - chat_server: self.chat_server.clone(), - client: self.client.clone(), - activity_queue: self.activity_queue.clone(), - } - } -} - #[derive(Deserialize, Debug)] pub struct IframelyResponse { title: Option,