main
use ::session::AuthedUser;
use api::http::{Json, Path, State};
use api::rest::{AppResponseData, NoData};
use uuid::Uuid;
use crate::errors::ApiError;
use crate::services::reactions::{ReactionAggDto, ReactionCmd, ReactionUserDto};
use crate::services::service::ChatService;
#[utoipa::path(
put,
path = "/chat/rooms/{room_id}/messages/{message_id}/reactions/@me",
tag = "chat",
description = "为消息添加当前用户的表情回应,并返回聚合结果。",
params(
("room_id" = Uuid, Path, description = "room id"),
("message_id" = Uuid, Path, description = "message id")
),
request_body = ReactionCmd,
responses(
(status = 200, description = "reaction added", body = AppResponseData<Vec<ReactionAggDto>>),
(status = 404, description = "not found", body = AppResponseData<NoData>),
(status = 400, description = "request failed validation", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn add_reaction(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path((room_id, message_id)): Path<(Uuid, Uuid)>,
Json(cmd): Json<ReactionCmd>,
) -> Result<AppResponseData<Vec<ReactionAggDto>>, ApiError> {
let agg = service.add_reaction(uid, room_id, message_id, cmd).await?;
Ok(AppResponseData::ok(agg))
}
#[utoipa::path(
delete,
path = "/chat/rooms/{room_id}/messages/{message_id}/reactions/@me",
tag = "chat",
description = "撤回当前用户在消息上指定的表情回应。",
params(
("room_id" = Uuid, Path, description = "room id"),
("message_id" = Uuid, Path, description = "message id")
),
request_body = ReactionCmd,
responses(
(status = 200, description = "reaction removed", body = AppResponseData<Vec<ReactionAggDto>>),
(status = 404, description = "not found", body = AppResponseData<NoData>),
(status = 400, description = "request failed validation", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn remove_reaction(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path((room_id, message_id)): Path<(Uuid, Uuid)>,
Json(cmd): Json<ReactionCmd>,
) -> Result<AppResponseData<Vec<ReactionAggDto>>, ApiError> {
let agg = service
.remove_reaction(uid, room_id, message_id, cmd)
.await?;
Ok(AppResponseData::ok(agg))
}
#[utoipa::path(
get,
path = "/chat/rooms/{room_id}/messages/{message_id}/reactions",
tag = "chat",
description = "列出指定消息上全部用户的表情回应明细。",
params(
("room_id" = Uuid, Path, description = "room id"),
("message_id" = Uuid, Path, description = "message id")
),
responses(
(status = 200, description = "reaction users", body = AppResponseData<Vec<ReactionUserDto>>),
(status = 404, description = "not found", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn list_reactions(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path((room_id, message_id)): Path<(Uuid, Uuid)>,
) -> Result<AppResponseData<Vec<ReactionUserDto>>, ApiError> {
let rows = service
.list_message_reactions(uid, room_id, message_id)
.await?;
Ok(AppResponseData::ok(rows))
}