main
use ::session::AuthedUser;
use api::http::{Json, Path, State};
use api::rest::{AppResponseData, NoData};
use uuid::Uuid;
use crate::errors::ApiError;
use space::services::dto::SpacePath;
use crate::services::rooms::{CreateRoomCmd, RoomBriefDto, RoomDto, UpdateRoomCmd};
use crate::services::service::ChatService;
#[utoipa::path(
get,
path = "/chat/spaces/{space_name}/rooms",
tag = "chat",
description = "列出空间内可见频道,并附带未读、成员和偏好信息。",
params(("space_name" = String, Path, description = "space name")),
responses(
(status = 200, description = "list rooms", body = AppResponseData<Vec<RoomBriefDto>>),
(status = 401, description = "unauthorized", body = AppResponseData<NoData>),
(status = 404, description = "not found", body = AppResponseData<NoData>)
)
)]
pub async fn list_space_rooms(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path(space_name): Path<String>,
) -> Result<AppResponseData<Vec<RoomBriefDto>>, ApiError> {
let rooms = service.list_space_rooms(uid, &space_name).await?;
Ok(AppResponseData::ok(rooms))
}
#[utoipa::path(
get,
path = "/chat/rooms/{room_id}",
tag = "chat",
description = "读取单个房间的详情及当前用户未读数。",
params(("room_id" = Uuid, Path, description = "room id")),
responses(
(status = 200, description = "room detail", body = AppResponseData<RoomBriefDto>),
(status = 404, description = "not found", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn get_room(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path(room_id): Path<Uuid>,
) -> Result<AppResponseData<RoomBriefDto>, ApiError> {
let room = service.get_room(uid, room_id).await?;
Ok(AppResponseData::ok(room))
}
#[utoipa::path(
patch,
path = "/chat/rooms/{room_id}",
tag = "chat",
description = "更新房间名称、主题、隐私、限速或排序等属性。",
params(("room_id" = Uuid, Path, description = "room id")),
request_body = UpdateRoomCmd,
responses(
(status = 200, description = "room updated", body = AppResponseData<RoomBriefDto>),
(status = 403, description = "forbidden", body = AppResponseData<NoData>),
(status = 400, description = "request failed validation", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn update_room(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path(room_id): Path<Uuid>,
Json(cmd): Json<UpdateRoomCmd>,
) -> Result<AppResponseData<RoomBriefDto>, ApiError> {
let room = service.update_room(uid, room_id, cmd).await?;
Ok(AppResponseData::ok(room))
}
#[utoipa::path(
post,
path = "/chat/spaces/{space_name}/rooms",
tag = "chat",
description = "在空间下创建聊天频道;公开频道会自动加入全部活跃成员。",
params(SpacePath),
request_body = CreateRoomCmd,
responses(
(status = 200, description = "room created", body = AppResponseData<RoomDto>),
(status = 429, description = "creation quota exceeded", body = AppResponseData<NoData>),
(status = 400, description = "bad request", body = AppResponseData<NoData>),
(status = 403, description = "forbidden", body = AppResponseData<NoData>),
(status = 404, description = "space not found", body = AppResponseData<NoData>),
(status = 409, description = "room name conflict", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn create_room(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path(path): Path<SpacePath>,
Json(cmd): Json<CreateRoomCmd>,
) -> Result<AppResponseData<RoomDto>, ApiError> {
let room = service.create_room(uid, &path.space_name, cmd).await?;
Ok(AppResponseData::ok(room))
}
#[utoipa::path(
delete,
path = "/chat/rooms/{room_id}",
tag = "chat",
description = "软删除聊天频道并停止其对外可见。",
params(
("room_id" = Uuid, Path, description = "room id")
),
responses(
(status = 200, description = "room deleted", body = AppResponseData<NoData>),
(status = 403, description = "forbidden", body = AppResponseData<NoData>),
(status = 404, description = "room or space not found", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn delete_room(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path(room_id): Path<Uuid>,
) -> Result<AppResponseData<NoData>, ApiError> {
service.delete_room(uid, room_id).await?;
Ok(AppResponseData::empty())
}