2020-04-21 19:35:39 +02:00
|
|
|
use futures::executor::block_on;
|
2020-04-11 11:18:41 +02:00
|
|
|
|
2020-04-13 10:56:42 +02:00
|
|
|
use jirs_data::{CommentId, CreateCommentPayload, IssueId, UpdateCommentPayload, WsMsg};
|
2020-04-11 11:18:41 +02:00
|
|
|
|
2020-04-21 20:04:19 +02:00
|
|
|
use crate::ws::{WebSocketActor, WsHandler, WsResult};
|
2020-04-21 19:35:39 +02:00
|
|
|
|
|
|
|
pub struct LoadIssueComments {
|
|
|
|
pub issue_id: IssueId,
|
2020-04-11 11:18:41 +02:00
|
|
|
}
|
2020-04-13 10:56:42 +02:00
|
|
|
|
2020-04-21 19:35:39 +02:00
|
|
|
impl WsHandler<LoadIssueComments> for WebSocketActor {
|
2020-04-21 20:04:19 +02:00
|
|
|
fn handle_msg(&mut self, msg: LoadIssueComments, _ctx: &mut Self::Context) -> WsResult {
|
2020-04-21 19:35:39 +02:00
|
|
|
self.require_user()?;
|
|
|
|
|
|
|
|
let comments = match block_on(self.db.send(crate::db::comments::LoadIssueComments {
|
|
|
|
issue_id: msg.issue_id,
|
|
|
|
})) {
|
2020-04-22 14:12:12 +02:00
|
|
|
Ok(Ok(comments)) => comments,
|
2020-04-21 19:35:39 +02:00
|
|
|
_ => return Ok(None),
|
|
|
|
};
|
2020-04-13 10:56:42 +02:00
|
|
|
|
2020-04-21 19:35:39 +02:00
|
|
|
Ok(Some(WsMsg::IssueCommentsLoaded(comments)))
|
2020-04-13 10:56:42 +02:00
|
|
|
}
|
2020-04-21 19:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WsHandler<CreateCommentPayload> for WebSocketActor {
|
2020-04-21 20:04:19 +02:00
|
|
|
fn handle_msg(&mut self, mut msg: CreateCommentPayload, ctx: &mut Self::Context) -> WsResult {
|
2020-04-21 19:35:39 +02:00
|
|
|
use crate::db::comments::CreateComment;
|
|
|
|
|
|
|
|
let user_id = self.require_user()?.id;
|
|
|
|
if msg.user_id.is_none() {
|
|
|
|
msg.user_id = Some(user_id);
|
|
|
|
}
|
|
|
|
let issue_id = msg.issue_id;
|
|
|
|
match block_on(self.db.send(CreateComment {
|
2020-04-13 10:56:42 +02:00
|
|
|
user_id,
|
|
|
|
issue_id,
|
2020-04-21 19:35:39 +02:00
|
|
|
body: msg.body,
|
|
|
|
})) {
|
|
|
|
Ok(Ok(_)) => (),
|
|
|
|
_ => return Ok(None),
|
|
|
|
};
|
2020-04-21 20:04:19 +02:00
|
|
|
self.handle_msg(LoadIssueComments { issue_id }, ctx)
|
2020-04-21 19:35:39 +02:00
|
|
|
}
|
2020-04-13 10:56:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 19:35:39 +02:00
|
|
|
impl WsHandler<UpdateCommentPayload> for WebSocketActor {
|
2020-04-21 20:04:19 +02:00
|
|
|
fn handle_msg(&mut self, msg: UpdateCommentPayload, ctx: &mut Self::Context) -> WsResult {
|
2020-04-21 19:35:39 +02:00
|
|
|
use crate::db::comments::UpdateComment;
|
2020-04-13 10:56:42 +02:00
|
|
|
|
2020-04-21 19:35:39 +02:00
|
|
|
info!("{:?}", msg);
|
|
|
|
let user_id = self.require_user()?.id;
|
2020-04-13 10:56:42 +02:00
|
|
|
|
2020-04-21 19:35:39 +02:00
|
|
|
let UpdateCommentPayload {
|
|
|
|
id: comment_id,
|
|
|
|
body,
|
|
|
|
} = msg;
|
2020-04-13 10:56:42 +02:00
|
|
|
|
2020-04-21 19:35:39 +02:00
|
|
|
let issue_id = match block_on(self.db.send(UpdateComment {
|
2020-04-13 10:56:42 +02:00
|
|
|
comment_id,
|
|
|
|
user_id,
|
|
|
|
body,
|
2020-04-21 19:35:39 +02:00
|
|
|
})) {
|
|
|
|
Ok(Ok(comment)) => comment.issue_id,
|
|
|
|
_ => return Ok(None),
|
|
|
|
};
|
2020-04-21 20:04:19 +02:00
|
|
|
if let Some(v) = self.handle_msg(LoadIssueComments { issue_id }, ctx)? {
|
2020-04-21 19:35:39 +02:00
|
|
|
self.broadcast(&v);
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
2020-04-13 10:56:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 19:35:39 +02:00
|
|
|
pub struct DeleteComment {
|
|
|
|
pub comment_id: CommentId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WsHandler<DeleteComment> for WebSocketActor {
|
2020-04-21 20:04:19 +02:00
|
|
|
fn handle_msg(&mut self, msg: DeleteComment, _ctx: &mut Self::Context) -> WsResult {
|
2020-04-21 19:35:39 +02:00
|
|
|
use crate::db::comments::DeleteComment;
|
|
|
|
|
|
|
|
let user_id = self.require_user()?.id;
|
|
|
|
|
|
|
|
let m = DeleteComment {
|
|
|
|
comment_id: msg.comment_id,
|
|
|
|
user_id,
|
|
|
|
};
|
|
|
|
match block_on(self.db.send(m)) {
|
|
|
|
Ok(Ok(_)) => (),
|
|
|
|
_ => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Some(WsMsg::CommentDeleted(msg.comment_id)))
|
|
|
|
}
|
2020-04-13 10:56:42 +02:00
|
|
|
}
|