main
use ::session::AuthedUser;
use api::http::{Json, Path, State};
use api::rest::{AppResponseData, NoData};
use crate::errors::ApiError;
use crate::services::presence::{PresenceDto, SetPresenceCmd};
use crate::services::service::ChatService;
#[utoipa::path(
put,
path = "/chat/presence",
tag = "chat",
description = "更新当前用户的在线状态、离开状态或隐身状态。",
request_body = SetPresenceCmd,
responses(
(status = 200, description = "presence updated", body = AppResponseData<PresenceDto>),
(status = 401, description = "unauthorized", body = AppResponseData<NoData>),
(status = 400, description = "request failed validation", body = AppResponseData<NoData>)
)
)]
pub async fn set_presence(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Json(cmd): Json<SetPresenceCmd>,
) -> Result<AppResponseData<PresenceDto>, ApiError> {
let dto = service.participants.set_presence(uid, cmd).await?;
Ok(AppResponseData::ok(dto))
}
#[utoipa::path(
get,
path = "/chat/users/{user_id}/presence",
tag = "chat",
description = "读取指定用户的在线状态;仅在可见关系成立时返回。",
params(("user_id" = i64, Path, description = "user id")),
responses(
(status = 200, description = "presence", body = AppResponseData<PresenceDto>),
(status = 401, description = "unauthorized", body = AppResponseData<NoData>)
)
)]
pub async fn get_presence(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path(user_id): Path<i64>,
) -> Result<AppResponseData<PresenceDto>, ApiError> {
let dto = service.participants.get_presence_for(uid, user_id).await?;
Ok(AppResponseData::ok(dto))
}
#[utoipa::path(
get,
path = "/chat/spaces/{space_name}/presences",
tag = "chat",
description = "返回指定空间活跃成员及其在线状态。",
params(("space_name" = String, Path, description = "space name")),
responses(
(status = 200, description = "space presences", body = AppResponseData<Vec<PresenceDto>>),
(status = 404, description = "not found", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn list_space_presences(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path(space_name): Path<String>,
) -> Result<AppResponseData<Vec<PresenceDto>>, ApiError> {
let presences = service
.participants
.list_space_presences(uid, &space_name)
.await?;
Ok(AppResponseData::ok(presences))
}