use actix_web::{HttpResponse, web}; use serde::Deserialize; use utoipa::IntoParams; use crate::api::response::{ApiErrorResponse, ApiResponse}; use crate::error::AppError; use crate::models::base_info; use crate::models::notifications::NotificationDetail; use crate::service::AppService; use crate::session::Session; #[derive(Debug, Deserialize, IntoParams)] pub struct QueryParams { pub unread_only: Option, pub limit: Option, pub offset: Option, } /// List notifications for the current user #[utoipa::path( get, path = "/api/v1/notifications", tag = "Notifications", operation_id = "notificationList", params(QueryParams), responses( (status = 200, description = "Notifications listed successfully", body = ApiResponse>), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn list_notifications( service: web::Data, session: Session, query: web::Query, ) -> Result { let notifications = service .notify .list_notifications( &session, query.unread_only.unwrap_or(false), query.limit.unwrap_or(50), query.offset.unwrap_or(0), ) .await?; let actor_ids: Vec<_> = notifications.iter().filter_map(|n| n.actor_id).collect(); let workspace_ids: Vec<_> = notifications .iter() .filter_map(|n| n.workspace_id) .collect(); let repo_ids: Vec<_> = notifications.iter().filter_map(|n| n.repo_id).collect(); let actors = base_info::resolve_users(&service.ctx.db, &actor_ids).await?; let workspaces = base_info::resolve_workspaces(&service.ctx.db, &workspace_ids).await?; let repos = base_info::resolve_repos(&service.ctx.db, &repo_ids).await?; let details: Vec = notifications .into_iter() .map(|n| { let actor = n.actor_id.and_then(|id| actors.get(&id).cloned()); let workspace = n.workspace_id.and_then(|id| workspaces.get(&id).cloned()); let repo = n.repo_id.and_then(|id| repos.get(&id).cloned()); n.into_detail(actor, workspace, repo) }) .collect(); Ok(HttpResponse::Ok().json(ApiResponse::new(details))) }