Fix some errors by moving LemmyContext and Perform into lemmy_websocket

This commit is contained in:
Felix Ableitner 2020-09-02 13:45:23 +02:00
parent 02e0363fd2
commit e826ecc4ef
8 changed files with 77 additions and 70 deletions

2
server/Cargo.lock generated vendored
View file

@ -1918,6 +1918,8 @@ dependencies = [
"actix", "actix",
"actix-web", "actix-web",
"anyhow", "anyhow",
"async-trait",
"background-jobs",
"chrono", "chrono",
"diesel", "diesel",
"lemmy_api_structs", "lemmy_api_structs",

View file

@ -9,8 +9,6 @@ pub mod site;
pub mod user; pub mod user;
use thiserror::Error; use thiserror::Error;
use actix_web::web::Data;
use lemmy_utils::{ConnectionId, LemmyError};
#[derive(Debug, Error)] #[derive(Debug, Error)]
#[error("{{\"error\":\"{message}\"}}")] #[error("{{\"error\":\"{message}\"}}")]
@ -24,15 +22,4 @@ impl APIError {
message: msg.to_string(), message: msg.to_string(),
} }
} }
}
#[async_trait::async_trait(?Send)]
pub trait Perform {
type Response: serde::ser::Serialize + Send;
async fn perform(
&self,
context: &Data<LemmyContext>,
websocket_id: Option<ConnectionId>,
) -> Result<Self::Response, LemmyError>;
} }

View file

@ -40,6 +40,8 @@ pub mod user_mention;
pub mod user_mention_view; pub mod user_mention_view;
pub mod user_view; pub mod user_view;
pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
pub trait Crud<T> { pub trait Crud<T> {
fn create(conn: &PgConnection, form: &T) -> Result<Self, Error> fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
where where

View file

@ -21,3 +21,5 @@ chrono = { version = "0.4.7", features = ["serde"] }
reqwest = { version = "0.10", features = ["json"] } reqwest = { version = "0.10", features = ["json"] }
strum = "0.18.0" strum = "0.18.0"
strum_macros = "0.18.0" strum_macros = "0.18.0"
async-trait = "0.1.36"
background-jobs = " 0.8.0-alpha.2"

View file

@ -1,12 +1,8 @@
use crate::{ use crate::{ {
api::Perform, chat_server::{ChatServer, SessionInfo},
{ messages::*,
chat_server::{ChatServer, SessionInfo}, UserOperation,
messages::*, }, LemmyContext, Perform};
UserOperation,
},
LemmyContext,
};
use actix::{Actor, Context, Handler, ResponseFuture}; use actix::{Actor, Context, Handler, ResponseFuture};
use actix_web::web; use actix_web::web;
use lemmy_db::naive_now; use lemmy_db::naive_now;

View file

@ -10,6 +10,15 @@ pub extern crate reqwest;
pub extern crate log; pub extern crate log;
#[macro_use] #[macro_use]
pub extern crate strum_macros; 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 chat_server;
pub mod handlers; pub mod handlers;
@ -76,3 +85,60 @@ pub enum UserOperation {
GetSiteConfig, GetSiteConfig,
SaveSiteConfig, SaveSiteConfig,
} }
pub struct LemmyContext {
pub pool: DbPool,
pub chat_server: Addr<ChatServer>,
pub client: Client,
pub activity_queue: QueueHandle,
}
impl LemmyContext {
pub fn create(
pool: DbPool,
chat_server: Addr<ChatServer>,
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<ChatServer> {
&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<LemmyContext>,
websocket_id: Option<ConnectionId>,
) -> Result<Self::Response, LemmyError>;
}

View file

View file

@ -43,54 +43,6 @@ use reqwest::Client;
use serde::Deserialize; use serde::Deserialize;
use std::process::Command; use std::process::Command;
pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
pub struct LemmyContext {
pub pool: DbPool,
pub chat_server: Addr<ChatServer>,
pub client: Client,
pub activity_queue: QueueHandle,
}
impl LemmyContext {
pub fn create(
pool: DbPool,
chat_server: Addr<ChatServer>,
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<ChatServer> {
&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)] #[derive(Deserialize, Debug)]
pub struct IframelyResponse { pub struct IframelyResponse {
title: Option<String>, title: Option<String>,