Rewrite to handlers

This commit is contained in:
Adrian Wozniak 2020-04-21 20:04:19 +02:00
parent 80fb8f5466
commit 9badbad71f
3 changed files with 13 additions and 21 deletions

View File

@ -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<LoadIssueComments> 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<LoadIssueComments> for WebSocketActor {
}
impl WsHandler<CreateCommentPayload> 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<CreateCommentPayload> for WebSocketActor {
Ok(Ok(_)) => (),
_ => return Ok(None),
};
self.handle_msg(LoadIssueComments { issue_id })
self.handle_msg(LoadIssueComments { issue_id }, ctx)
}
}
impl WsHandler<UpdateCommentPayload> 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<UpdateCommentPayload> 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<DeleteComment> 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;

View File

@ -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<Option<WsMsg>, WsMsg>;
pub fn current_user(current_user: &Option<jirs_data::User>) -> 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)
}
}

View File

@ -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<LoadProjectUsers> 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(),