From 9badbad71f0693a8607a12fda24e37228cbb9308 Mon Sep 17 00:00:00 2001 From: Adrian Wozniak Date: Tue, 21 Apr 2020 20:04:19 +0200 Subject: [PATCH] Rewrite to handlers --- jirs-server/src/ws/comments.rs | 17 +++++++---------- jirs-server/src/ws/mod.rs | 13 ++++--------- jirs-server/src/ws/users.rs | 4 ++-- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/jirs-server/src/ws/comments.rs b/jirs-server/src/ws/comments.rs index 93a3c473..242a4a6a 100644 --- a/jirs-server/src/ws/comments.rs +++ b/jirs-server/src/ws/comments.rs @@ -1,18 +1,15 @@ -use actix::Addr; -use actix_web::web::Data; use futures::executor::block_on; use jirs_data::{CommentId, CreateCommentPayload, IssueId, UpdateCommentPayload, WsMsg}; -use crate::db::DbExecutor; -use crate::ws::{current_user, WebSocketActor, WsHandler, WsResult}; +use crate::ws::{WebSocketActor, WsHandler, WsResult}; pub struct LoadIssueComments { pub issue_id: IssueId, } impl WsHandler for WebSocketActor { - fn handle_msg(&mut self, msg: LoadIssueComments, _ctx: Self::Context) -> WsResult { + fn handle_msg(&mut self, msg: LoadIssueComments, _ctx: &mut Self::Context) -> WsResult { self.require_user()?; let comments = match block_on(self.db.send(crate::db::comments::LoadIssueComments { @@ -27,7 +24,7 @@ impl WsHandler for WebSocketActor { } impl WsHandler for WebSocketActor { - fn handle_msg(&mut self, mut msg: CreateCommentPayload, _ctx: Self::Context) -> WsResult { + fn handle_msg(&mut self, mut msg: CreateCommentPayload, ctx: &mut Self::Context) -> WsResult { use crate::db::comments::CreateComment; let user_id = self.require_user()?.id; @@ -43,12 +40,12 @@ impl WsHandler for WebSocketActor { Ok(Ok(_)) => (), _ => return Ok(None), }; - self.handle_msg(LoadIssueComments { issue_id }) + self.handle_msg(LoadIssueComments { issue_id }, ctx) } } impl WsHandler for WebSocketActor { - fn handle_msg(&mut self, msg: UpdateCommentPayload, _ctx: Self::Context) -> WsResult { + fn handle_msg(&mut self, msg: UpdateCommentPayload, ctx: &mut Self::Context) -> WsResult { use crate::db::comments::UpdateComment; info!("{:?}", msg); @@ -67,7 +64,7 @@ impl WsHandler for WebSocketActor { Ok(Ok(comment)) => comment.issue_id, _ => return Ok(None), }; - if let Some(v) = self.handle_msg(LoadIssueComments { issue_id })? { + if let Some(v) = self.handle_msg(LoadIssueComments { issue_id }, ctx)? { self.broadcast(&v); } Ok(None) @@ -79,7 +76,7 @@ pub struct DeleteComment { } impl WsHandler for WebSocketActor { - fn handle_msg(&mut self, msg: DeleteComment, _ctx: Self::Context) -> WsResult { + fn handle_msg(&mut self, msg: DeleteComment, _ctx: &mut Self::Context) -> WsResult { use crate::db::comments::DeleteComment; let user_id = self.require_user()?.id; diff --git a/jirs-server/src/ws/mod.rs b/jirs-server/src/ws/mod.rs index 0fa5490c..39a4ebf1 100644 --- a/jirs-server/src/ws/mod.rs +++ b/jirs-server/src/ws/mod.rs @@ -4,7 +4,6 @@ use actix::{Actor, ActorContext, Addr, Context, Handler, Message, Recipient, Str use actix_web::web::Data; use actix_web::{get, web, Error, HttpRequest, HttpResponse}; use actix_web_actors::ws; -use futures::executor::block_on; use jirs_data::{ProjectId, UserId, WsMsg}; @@ -23,13 +22,6 @@ pub mod users; pub type WsResult = std::result::Result, WsMsg>; -pub fn current_user(current_user: &Option) -> Result<&jirs_data::User, WsMsg> { - current_user - .as_ref() - .map(|u| u) - .ok_or_else(|| WsMsg::AuthorizeExpired) -} - trait WsMessageSender { fn send_msg(&mut self, msg: &jirs_data::WsMsg); } @@ -180,7 +172,10 @@ impl WebSocketActor { } fn require_user(&self) -> Result<&jirs_data::User, WsMsg> { - current_user(&self.current_user) + self.current_user + .as_ref() + .map(|u| u) + .ok_or_else(|| WsMsg::AuthorizeExpired) } } diff --git a/jirs-server/src/ws/users.rs b/jirs-server/src/ws/users.rs index 927ba4bb..1d412714 100644 --- a/jirs-server/src/ws/users.rs +++ b/jirs-server/src/ws/users.rs @@ -4,7 +4,7 @@ use jirs_data::WsMsg; use crate::db::users::Register as DbRegister; use crate::ws::auth::Authenticate; -use crate::ws::{current_user, WebSocketActor, WsHandler, WsResult}; +use crate::ws::{WebSocketActor, WsHandler, WsResult}; pub struct LoadProjectUsers; @@ -12,7 +12,7 @@ impl WsHandler for WebSocketActor { fn handle_msg(&mut self, _msg: LoadProjectUsers, _ctx: &mut Self::Context) -> WsResult { use crate::db::users::LoadProjectUsers as Msg; - let project_id = current_user(&self.current_user).map(|u| u.project_id)?; + let project_id = self.require_user()?.project_id; let m = match block_on(self.db.send(Msg { project_id })) { Ok(Ok(v)) => Some(WsMsg::ProjectUsersLoaded( v.into_iter().map(|i| i.into()).collect(),