Skip to content
Clone
main
74 lines 2.8 KB Rust
use ::session::AuthedUser;
use api::http::{Json, Path, Query, State};
use api::rest::{AppResponseData, NoData};
use uuid::Uuid;
use crate::errors::ApiError;
use crate::services::dms::{DmDto, OpenDmCmd};
use crate::services::service::ChatService;
#[utoipa::path(
post,
path = "/chat/dms",
tag = "chat",
description = "打开与指定用户的私聊会话;已有会话则恢复,新会话则创建。",
request_body = OpenDmCmd,
responses(
(status = 200, description = "dm opened", body = AppResponseData<DmDto>),
(status = 429, description = "creation quota exceeded", body = AppResponseData<NoData>),
(status = 404, description = "not found", body = AppResponseData<NoData>),
(status = 409, description = "resource already exists", body = AppResponseData<NoData>),
(status = 400, description = "request failed validation", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn open_dm(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Json(cmd): Json<OpenDmCmd>,
) -> Result<AppResponseData<DmDto>, ApiError> {
let dto = service.open_dm(uid, cmd).await?;
Ok(AppResponseData::ok(dto))
}
#[utoipa::path(
get,
path = "/chat/dms",
tag = "chat",
description = "列出当前用户仍有效的私聊会话及其未读信息。",
params(api::pagination::PageQuery),
responses(
(status = 200, description = "my dms", body = AppResponseData<api::pagination::Page<DmDto>>),
(status = 401, description = "unauthorized", body = AppResponseData<NoData>),
(status = 404, description = "resource not found", body = AppResponseData<NoData>)
)
)]
pub async fn list_dms(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Query(page): Query<api::pagination::PageQuery>,
) -> Result<AppResponseData<api::pagination::Page<DmDto>>, ApiError> {
let dms = service.list_dms(uid).await?;
Ok(AppResponseData::ok(api::pagination::paginate(dms, &page)?))
}
#[utoipa::path(
delete,
path = "/chat/dms/{room_id}",
tag = "chat",
description = "将当前用户从指定私聊会话中移除,不再接收该会话消息。",
params(("room_id" = Uuid, Path, description = "dm room id")),
responses(
(status = 200, description = "dm closed", body = AppResponseData<NoData>),
(status = 404, description = "not found", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn close_dm(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path(room_id): Path<Uuid>,
) -> Result<AppResponseData<NoData>, ApiError> {
service.close_dm(uid, room_id).await?;
Ok(AppResponseData::empty())
}