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>), (status = 401, description = "unauthorized", body = AppResponseData), (status = 404, description = "not found", body = AppResponseData) ) )] pub async fn list_space_rooms( State(service): State, AuthedUser(uid): AuthedUser, Path(space_name): Path, ) -> Result>, 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), (status = 404, description = "not found", body = AppResponseData), (status = 401, description = "authentication required", body = AppResponseData) ) )] pub async fn get_room( State(service): State, AuthedUser(uid): AuthedUser, Path(room_id): Path, ) -> Result, 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), (status = 403, description = "forbidden", body = AppResponseData), (status = 400, description = "request failed validation", body = AppResponseData), (status = 401, description = "authentication required", body = AppResponseData) ) )] pub async fn update_room( State(service): State, AuthedUser(uid): AuthedUser, Path(room_id): Path, Json(cmd): Json, ) -> Result, 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), (status = 429, description = "creation quota exceeded", body = AppResponseData), (status = 400, description = "bad request", body = AppResponseData), (status = 403, description = "forbidden", body = AppResponseData), (status = 404, description = "space not found", body = AppResponseData), (status = 409, description = "room name conflict", body = AppResponseData), (status = 401, description = "authentication required", body = AppResponseData) ) )] pub async fn create_room( State(service): State, AuthedUser(uid): AuthedUser, Path(path): Path, Json(cmd): Json, ) -> Result, 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), (status = 403, description = "forbidden", body = AppResponseData), (status = 404, description = "room or space not found", body = AppResponseData), (status = 401, description = "authentication required", body = AppResponseData) ) )] pub async fn delete_room( State(service): State, AuthedUser(uid): AuthedUser, Path(room_id): Path, ) -> Result, ApiError> { service.delete_room(uid, room_id).await?; Ok(AppResponseData::empty()) }