Add accept invitation

This commit is contained in:
Adrian Woźniak 2020-04-21 13:27:54 +02:00
parent 9941f0704c
commit 2cb27bb889
4 changed files with 108 additions and 2 deletions

View File

@ -689,8 +689,10 @@ pub enum WsMsg {
// invitations
InvitationListRequest,
InvitationListLoaded(Vec<Invitation>),
//
InvitedUsersRequest,
InvitedUsersLoaded(Vec<User>),
//
InvitationSendRequest {
name: UsernameString,
email: EmailString,

View File

@ -3,12 +3,12 @@ use diesel::pg::Pg;
use diesel::prelude::*;
use jirs_data::{
EmailString, Invitation, InvitationId, InvitationState, ProjectId, UserId, UsernameString,
EmailString, Invitation, InvitationId, InvitationState, ProjectId, User, UserId, UsernameString,
};
use crate::db::DbExecutor;
use crate::errors::ServiceErrors;
use crate::models::InvitationForm;
use crate::models::{InvitationForm, UserForm};
pub struct ListInvitation {
pub user_id: UserId,
@ -129,3 +129,53 @@ impl Handler<RevokeInvitation> for DbExecutor {
Ok(())
}
}
pub struct AcceptInvitation {
pub id: InvitationId,
}
impl Message for AcceptInvitation {
type Result = Result<User, ServiceErrors>;
}
impl Handler<AcceptInvitation> for DbExecutor {
type Result = Result<User, ServiceErrors>;
fn handle(&mut self, msg: AcceptInvitation, _ctx: &mut Self::Context) -> Self::Result {
use crate::schema::invitations::dsl::*;
use crate::schema::users::dsl::users;
let conn = &self
.pool
.get()
.map_err(|_| ServiceErrors::DatabaseConnectionLost)?;
let query = invitations.find(msg.id);
debug!("{}", diesel::debug_query::<Pg, _>(&query).to_string());
let invitation: Invitation = query
.first(conn)
.map_err(|_| ServiceErrors::DatabaseConnectionLost)?;
let query = diesel::update(invitations)
.set(state.eq(InvitationState::Accepted))
.filter(id.eq(invitation.id));
debug!("{}", diesel::debug_query::<Pg, _>(&query).to_string());
query
.execute(conn)
.map_err(|_| ServiceErrors::DatabaseConnectionLost)?;
let form = UserForm {
name: invitation.name,
email: invitation.email,
avatar_url: None,
project_id: invitation.project_id,
};
let query = diesel::insert_into(users).values(form);
debug!("{}", diesel::debug_query::<Pg, _>(&query).to_string());
let user: User = query
.get_result(conn)
.map_err(|_| ServiceErrors::DatabaseConnectionLost)?;
Ok(user)
}
}

View File

@ -102,3 +102,25 @@ impl Handler<RevokeInvitation> for WebSocketActor {
Ok(res)
}
}
pub struct AcceptInvitation {
pub id: InvitationId,
}
impl Message for AcceptInvitation {
type Result = WsResult;
}
impl Handler<AcceptInvitation> for WebSocketActor {
type Result = WsResult;
fn handle(&mut self, msg: AcceptInvitation, _ctx: &mut Self::Context) -> Self::Result {
self.require_user()?;
let AcceptInvitation { id } = msg;
let res = match block_on(self.db.send(invitations::AcceptInvitation { id })) {
Ok(Ok(_)) => Some(WsMsg::InvitationAcceptSuccess(id)),
_ => None,
};
Ok(res)
}
}

View File

@ -266,6 +266,38 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WebSocketActor {
}
}
trait MessageHandler {
fn try_message<M>(&self) -> Option<M>
where
M: Message<Result = WsResult>;
}
impl WebSocketActor {
fn try_handle_message(
&mut self,
msg: WsMsg,
ctx: <WebSocketActor as Actor>::Context,
) -> WsResult {
use invitations::*;
Ok(None)
// match msg {
// WsMsg::InvitationSendRequest { name, email } => self.handle(
// CreateInvitation {
// name: name.clone(),
// email: email.clone(),
// },
// ctx,
// ),
// WsMsg::InvitationListRequest => self.handle(ListInvitation, ctx),
// WsMsg::InvitationAcceptRequest(id) => Ok(None),
// WsMsg::InvitationRevokeRequest(id) => self.handle(RevokeInvitation { id: *id }, ctx),
// WsMsg::InvitedUsersRequest => Ok(None),
// _ => Ok(None),
// }
}
}
#[derive(Message, Debug)]
#[rtype(result = "()")]
pub enum InnerMsg {