cec6dce955
- Add git operation endpoints: archive, compare branches, diff, tree, repository extras - Add repo endpoints: contributors, delete fork, get branch/commit status/deploy key/invitation/member/release/tag/webhook, topics, release assets, webhook deliveries/retry - Add PR endpoints: review requests, templates - Add user endpoints: block/unblock, follow/unfollow, presence, personal access tokens, account restore - Add workspace endpoints: billing history, approvals, domains, integrations, invitations, members, webhooks, restore - Add internal API, notification API, IM API modules - Update route configuration and OpenAPI spec
73 lines
2.5 KiB
Rust
73 lines
2.5 KiB
Rust
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<bool>,
|
|
pub limit: Option<i64>,
|
|
pub offset: Option<i64>,
|
|
}
|
|
|
|
/// 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<Vec<NotificationDetail>>),
|
|
(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<AppService>,
|
|
session: Session,
|
|
query: web::Query<QueryParams>,
|
|
) -> Result<HttpResponse, AppError> {
|
|
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<NotificationDetail> = 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)))
|
|
}
|