use actix_web::{HttpResponse, web}; use serde::Deserialize; use utoipa::IntoParams; use crate::api::response::{ApiErrorResponse, ApiResponse}; use crate::error::AppError; use crate::models::common::{DeviceType, PresenceStatus}; use crate::models::users::UserPresence; use crate::service::AppService; use crate::service::user::social::UpdatePresenceParams; use crate::session::Session; #[derive(Debug, Deserialize, IntoParams, utoipa::ToSchema)] pub struct UpdatePresenceBody { /// New presence status pub status: PresenceStatus, /// Optional custom status text (e.g., "In a meeting") pub custom_status_text: Option, /// Optional custom status emoji (e.g., ":palm_tree:") pub custom_status_emoji: Option, /// Device type the user is currently using pub device_type: Option, /// IP address of the current session pub ip_address: Option, } /// Update user presence /// /// Updates the presence status for the authenticated user. /// Supports custom status text, emoji, device type, and IP address. /// Creates a new presence record if one does not exist. /// Requires authentication. #[utoipa::path( put, path = "/api/v1/user/presence", tag = "User", operation_id = "userUpdatePresence", params(UpdatePresenceBody), responses( (status = 200, description = "Presence updated successfully.", body = ApiResponse), (status = 400, description = "Invalid request body", body = ApiErrorResponse), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn update_presence( service: web::Data, session: Session, body: web::Json, ) -> Result { let params = UpdatePresenceParams { status: body.status, custom_status_text: body.custom_status_text.clone(), custom_status_emoji: body.custom_status_emoji.clone(), device_type: body.device_type, ip_address: body.ip_address.clone(), }; let presence = service.user.user_presence_update(&session, params).await?; Ok(HttpResponse::Ok().json(ApiResponse::new(presence))) }