use actix_web::{HttpResponse, web}; use serde::Deserialize; use utoipa::IntoParams; use crate::api::response::{ApiErrorResponse, ApiResponse}; use crate::error::AppError; use crate::models::prs::PrSubscription; use crate::service::AppService; use crate::session::Session; #[derive(Debug, Deserialize, IntoParams)] pub struct PrPath { /// Workspace name (unique identifier) pub workspace_name: String, /// Repository name (unique within the workspace) pub repo_name: String, /// PR number (unique within the repository) pub number: i64, } #[derive(Debug, Deserialize, IntoParams)] pub struct QP { pub limit: Option, pub offset: Option, } /// List subscriptions on a PR #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/subscriptions", tag = "Pull Requests", operation_id = "prListSubscriptions", params(PrPath, QP), responses( (status = 200, description = "Subscriptions listed.", body = ApiResponse>), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 404, description = "PR not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn list_subscriptions( service: web::Data, session: Session, path: web::Path, query: web::Query, ) -> Result { let subs = service .pr .pr_subscriptions( &session, &path.workspace_name, &path.repo_name, path.number, query.limit.unwrap_or(50), query.offset.unwrap_or(0), ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(subs))) } /// Subscribe to a PR to receive notifications #[utoipa::path( post, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/subscribe", tag = "Pull Requests", operation_id = "prSubscribe", params(PrPath), responses( (status = 200, description = "Subscribed.", body = ApiResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 404, description = "PR not found", body = ApiErrorResponse), (status = 409, description = "Already subscribed", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn subscribe( service: web::Data, session: Session, path: web::Path, ) -> Result { let sub = service .pr .pr_subscribe(&session, &path.workspace_name, &path.repo_name, path.number) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(sub))) } /// Unsubscribe from a PR #[utoipa::path( delete, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/subscribe", tag = "Pull Requests", operation_id = "prUnsubscribe", params(PrPath), responses( (status = 200, description = "Unsubscribed.", body = ApiResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 404, description = "Not subscribed", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn unsubscribe( service: web::Data, session: Session, path: web::Path, ) -> Result { service .pr .pr_unsubscribe(&session, &path.workspace_name, &path.repo_name, path.number) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new("Unsubscribed".to_string()))) } #[derive(Debug, Deserialize, utoipa::ToSchema)] pub struct MutePrParams { /// Whether to mute (true) or unmute (false) notifications pub muted: bool, } /// Mute or unmute notifications for a PR #[utoipa::path( put, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/mute", tag = "Pull Requests", operation_id = "prMute", params(PrPath), request_body(content = MutePrParams, description = "Mute/unmute parameters", content_type = "application/json"), responses( (status = 200, description = "Mute status updated.", body = ApiResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 404, description = "Not subscribed", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn mute( service: web::Data, session: Session, path: web::Path, params: web::Json, ) -> Result { service .pr .pr_mute( &session, &path.workspace_name, &path.repo_name, path.number, params.muted, ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new("Mute status updated".to_string()))) }