use crate::{ApiResponse, error::ApiError}; use actix_web::{HttpResponse, Result, web}; use room::ws_context::WsUserContext; use service::AppService; use session::Session; use utoipa::IntoParams; use uuid::Uuid; #[derive(Debug, serde::Deserialize, IntoParams, utoipa::ToSchema)] pub struct ReactionRequest { pub emoji: String, } #[derive(Debug, serde::Deserialize, IntoParams)] pub struct MessageSearchQuery { pub q: String, pub limit: Option, pub offset: Option, } #[utoipa::path( post, path = "/api/rooms/{room_id}/messages/{message_id}/reactions", params( ("room_id" = Uuid, Path), ("message_id" = Uuid, Path), ), request_body = ReactionRequest, responses( (status = 200, description = "Add reaction", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "Room" )] pub async fn reaction_add( service: web::Data, session: Session, path: web::Path<(Uuid, Uuid)>, body: web::Json, ) -> Result { let (_room_id, message_id) = path.into_inner(); let user_id = session .user() .ok_or_else(|| ApiError::from(service::error::AppError::Unauthorized))?; let ctx = WsUserContext::new(user_id); let resp = service .room .message_reaction_add(message_id, body.into_inner().emoji, &ctx) .await .map_err(ApiError::from)?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( delete, path = "/api/rooms/{room_id}/messages/{message_id}/reactions/{emoji}", params( ("room_id" = Uuid, Path), ("message_id" = Uuid, Path), ("emoji" = String, Path), ), responses( (status = 200, description = "Remove reaction", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "Room" )] pub async fn reaction_remove( service: web::Data, session: Session, path: web::Path<(Uuid, Uuid, String)>, ) -> Result { let (_room_id, message_id, emoji) = path.into_inner(); let user_id = session .user() .ok_or_else(|| ApiError::from(service::error::AppError::Unauthorized))?; let ctx = WsUserContext::new(user_id); let resp = service .room .message_reaction_remove(message_id, emoji, &ctx) .await .map_err(ApiError::from)?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( get, path = "/api/rooms/{room_id}/messages/{message_id}/reactions", params( ("room_id" = Uuid, Path), ("message_id" = Uuid, Path), ), responses( (status = 200, description = "Get reactions", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "Room" )] pub async fn reaction_get( service: web::Data, session: Session, path: web::Path<(Uuid, Uuid)>, ) -> Result { let (_room_id, message_id) = path.into_inner(); let user_id = session .user() .ok_or_else(|| ApiError::from(service::error::AppError::Unauthorized))?; let ctx = WsUserContext::new(user_id); let resp = service .room .message_reactions_get(message_id, &ctx) .await .map_err(ApiError::from)?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( get, path = "/api/rooms/{room_id}/messages/search", params( ("room_id" = Uuid, Path), ("q" = String, Query), ("limit" = Option, Query), ("offset" = Option, Query), ), responses( (status = 200, description = "Search messages", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "Room" )] pub async fn message_search( service: web::Data, session: Session, path: web::Path, query: web::Query, ) -> Result { let room_id = path.into_inner(); let user_id = session .user() .ok_or_else(|| ApiError::from(service::error::AppError::Unauthorized))?; let ctx = WsUserContext::new(user_id); let resp = service .room .message_search(room_id, &query.q, query.limit, query.offset, &ctx) .await .map_err(ApiError::from)?; Ok(ApiResponse::ok(resp).to_response()) }