234 lines
6.2 KiB
Rust
234 lines
6.2 KiB
Rust
use room::ws_context::WsUserContext;
|
|
use uuid::Uuid;
|
|
|
|
use crate::error::AppTransportError;
|
|
use crate::event::{category, rooms};
|
|
use crate::handler::session::TransportSession;
|
|
use crate::handler::types::WsOutEvent;
|
|
|
|
pub(crate) async fn room_get(
|
|
session: &TransportSession,
|
|
room: models::RoomId,
|
|
) -> Result<Option<WsOutEvent>, AppTransportError> {
|
|
let ctx = WsUserContext::new(session.user.user_id);
|
|
session
|
|
.service
|
|
.room
|
|
.require_room_access(room, ctx.user_id)
|
|
.await
|
|
.map_err(|e| {
|
|
tracing::warn!(error = %e, "require_room_access failed");
|
|
AppTransportError::Internal
|
|
})?;
|
|
let rm = session
|
|
.service
|
|
.room
|
|
.find_room_or_404(room)
|
|
.await
|
|
.map_err(|e| {
|
|
tracing::warn!(error = %e, "find_room failed");
|
|
AppTransportError::Internal
|
|
})?;
|
|
Ok(Some(WsOutEvent::RoomCreated {
|
|
room_id: rm.id,
|
|
data: rooms::RoomCreatedService {
|
|
id: rm.id,
|
|
project: rm.project,
|
|
room_name: rm.room_name,
|
|
public: rm.public,
|
|
category: rm.category,
|
|
created_by: rm.created_by,
|
|
created_at: rm.created_at,
|
|
},
|
|
}))
|
|
}
|
|
|
|
pub(crate) async fn room_create(
|
|
session: &TransportSession,
|
|
project: models::ProjectId,
|
|
room_name: String,
|
|
public: bool,
|
|
category: Option<Uuid>,
|
|
) -> Result<Option<WsOutEvent>, AppTransportError> {
|
|
let ctx = WsUserContext::new(session.user.user_id);
|
|
let rm = session
|
|
.service
|
|
.room
|
|
.room_create(
|
|
project.to_string(),
|
|
room::RoomCreateRequest {
|
|
room_name,
|
|
public,
|
|
category,
|
|
},
|
|
&ctx,
|
|
)
|
|
.await
|
|
.map_err(|e| {
|
|
tracing::warn!(error = %e, "room_create failed");
|
|
AppTransportError::Internal
|
|
})?;
|
|
Ok(Some(WsOutEvent::RoomCreated {
|
|
room_id: rm.id,
|
|
data: rooms::RoomCreatedService {
|
|
id: rm.id,
|
|
project,
|
|
room_name: rm.room_name,
|
|
public: rm.public,
|
|
category,
|
|
created_by: session.user.user_id,
|
|
created_at: rm.created_at,
|
|
},
|
|
}))
|
|
}
|
|
|
|
pub(crate) async fn room_update(
|
|
session: &TransportSession,
|
|
room: models::RoomId,
|
|
room_name: Option<String>,
|
|
public: Option<bool>,
|
|
category: Option<Uuid>,
|
|
) -> Result<Option<WsOutEvent>, AppTransportError> {
|
|
let ctx = WsUserContext::new(session.user.user_id);
|
|
session
|
|
.service
|
|
.room
|
|
.room_update(
|
|
room,
|
|
room::RoomUpdateRequest {
|
|
room_name,
|
|
public,
|
|
category,
|
|
},
|
|
&ctx,
|
|
)
|
|
.await
|
|
.map_err(|e| {
|
|
tracing::warn!(error = %e, "room_update failed");
|
|
AppTransportError::Internal
|
|
})?;
|
|
Ok(None)
|
|
}
|
|
|
|
pub(crate) async fn room_delete(
|
|
session: &TransportSession,
|
|
room: models::RoomId,
|
|
) -> Result<Option<WsOutEvent>, AppTransportError> {
|
|
let ctx = WsUserContext::new(session.user.user_id);
|
|
session
|
|
.service
|
|
.room
|
|
.room_delete(room, &ctx)
|
|
.await
|
|
.map_err(|e| {
|
|
tracing::warn!(error = %e, "room_delete failed");
|
|
AppTransportError::Internal
|
|
})?;
|
|
Ok(None)
|
|
}
|
|
|
|
pub(crate) async fn category_create(
|
|
session: &TransportSession,
|
|
project: models::ProjectId,
|
|
name: String,
|
|
position: Option<i32>,
|
|
) -> Result<Option<WsOutEvent>, AppTransportError> {
|
|
let ctx = WsUserContext::new(session.user.user_id);
|
|
let cat = session
|
|
.service
|
|
.room
|
|
.room_category_create(
|
|
project.to_string(),
|
|
room::RoomCategoryCreateRequest { name, position },
|
|
&ctx,
|
|
)
|
|
.await
|
|
.map_err(|e| {
|
|
tracing::warn!(error = %e, "room_category_create failed");
|
|
AppTransportError::Internal
|
|
})?;
|
|
Ok(Some(WsOutEvent::CategoryCreated {
|
|
project,
|
|
data: category::CategoryCreatedService {
|
|
id: cat.id,
|
|
project,
|
|
name: cat.name,
|
|
position: cat.position,
|
|
created_by: session.user.user_id,
|
|
created_at: cat.created_at,
|
|
},
|
|
}))
|
|
}
|
|
|
|
pub(crate) async fn category_update(
|
|
session: &TransportSession,
|
|
id: Uuid,
|
|
name: Option<String>,
|
|
position: Option<i32>,
|
|
) -> Result<Option<WsOutEvent>, AppTransportError> {
|
|
let ctx = WsUserContext::new(session.user.user_id);
|
|
session
|
|
.service
|
|
.room
|
|
.room_category_update(id, room::RoomCategoryUpdateRequest { name, position }, &ctx)
|
|
.await
|
|
.map_err(|e| {
|
|
tracing::warn!(error = %e, "room_category_update failed");
|
|
AppTransportError::Internal
|
|
})?;
|
|
Ok(None)
|
|
}
|
|
|
|
pub(crate) async fn category_delete(
|
|
session: &TransportSession,
|
|
id: Uuid,
|
|
) -> Result<Option<WsOutEvent>, AppTransportError> {
|
|
let ctx = WsUserContext::new(session.user.user_id);
|
|
session
|
|
.service
|
|
.room
|
|
.room_category_delete(id, &ctx)
|
|
.await
|
|
.map_err(|e| {
|
|
tracing::warn!(error = %e, "room_category_delete failed");
|
|
AppTransportError::Internal
|
|
})?;
|
|
Ok(None)
|
|
}
|
|
|
|
pub(crate) async fn access_grant(
|
|
session: &TransportSession,
|
|
room: models::RoomId,
|
|
user: models::UserId,
|
|
) -> Result<Option<WsOutEvent>, AppTransportError> {
|
|
let ctx = WsUserContext::new(session.user.user_id);
|
|
session
|
|
.service
|
|
.room
|
|
.room_access_grant(room, user, &ctx)
|
|
.await
|
|
.map_err(|e| {
|
|
tracing::warn!(error = %e, "room_access_grant failed");
|
|
AppTransportError::Internal
|
|
})?;
|
|
Ok(None)
|
|
}
|
|
|
|
pub(crate) async fn access_revoke(
|
|
session: &TransportSession,
|
|
room: models::RoomId,
|
|
user: models::UserId,
|
|
) -> Result<Option<WsOutEvent>, AppTransportError> {
|
|
let ctx = WsUserContext::new(session.user.user_id);
|
|
session
|
|
.service
|
|
.room
|
|
.room_access_revoke(room, user, &ctx)
|
|
.await
|
|
.map_err(|e| {
|
|
tracing::warn!(error = %e, "room_access_revoke failed");
|
|
AppTransportError::Internal
|
|
})?;
|
|
Ok(None)
|
|
}
|