use actix_web::{HttpResponse, web}; use serde::Deserialize; use utoipa::IntoParams; use crate::api::response::{ApiErrorResponse, ApiResponse}; use crate::error::AppError; use crate::models::issues::IssueSubscriber; use crate::service::AppService; use crate::session::Session; #[derive(Debug, Deserialize, IntoParams)] pub struct PathParams { /// Workspace name (unique identifier) pub workspace_name: String, /// Issue number (unique within the workspace) pub number: i64, } #[derive(Debug, Deserialize, IntoParams)] pub struct QueryParams { /// Maximum number of subscribers to return (default: 50, max: 100) pub limit: Option, /// Number of subscribers to skip for pagination (default: 0) pub offset: Option, } /// List subscribers of an issue /// /// Returns a paginated list of all users subscribed to the given issue. /// Shows who receives notifications and their subscription reason (author, assignee, manual). /// Requires read access to the issue. #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/issues/{number}/subscribers", tag = "Issues", operation_id = "issueListSubscribers", params(PathParams, QueryParams), responses( (status = 200, description = "Subscribers listed successfully. Returns array of subscriber objects.", body = ApiResponse>), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions to view this issue", body = ApiErrorResponse), (status = 404, description = "Issue not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn list_subscribers( service: web::Data, session: Session, path: web::Path, query: web::Query, ) -> Result { let subscribers = service .issue .issue_subscribers( &session, &path.workspace_name, path.number, query.limit.unwrap_or(50), query.offset.unwrap_or(0), ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(subscribers))) } /// Subscribe to an issue /// /// Subscribes the authenticated user to the given issue to receive notifications. /// Requires read access to the issue. /// /// Effects: /// - User is added as a subscriber with "manual" reason /// - User receives notifications for all issue activity /// /// Returns the created subscription record. #[utoipa::path( post, path = "/api/v1/workspaces/{workspace_name}/issues/{number}/subscribe", tag = "Issues", operation_id = "issueSubscribe", params(PathParams), responses( (status = 200, description = "Subscribed successfully. Returns the subscription record.", body = ApiResponse), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions to view this issue", body = ApiErrorResponse), (status = 404, description = "Issue not found", body = ApiErrorResponse), (status = 409, description = "Already subscribed to this issue", 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 .issue .issue_subscribe(&session, &path.workspace_name, path.number) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(sub))) } /// Unsubscribe from an issue /// /// Removes the authenticated user's subscription to the given issue. /// Stops all notifications for this issue. /// /// Returns success message on completion. #[utoipa::path( delete, path = "/api/v1/workspaces/{workspace_name}/issues/{number}/subscribe", tag = "Issues", operation_id = "issueUnsubscribe", params(PathParams), responses( (status = 200, description = "Unsubscribed successfully.", body = ApiResponse), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 404, description = "Not currently subscribed to this issue", 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 .issue .issue_unsubscribe(&session, &path.workspace_name, path.number) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new("Unsubscribed".to_string()))) } #[derive(Debug, Deserialize, utoipa::ToSchema)] pub struct MuteIssueParams { /// Whether to mute (true) or unmute (false) notifications pub muted: bool, } /// Mute or unmute issue notifications /// /// Mutes or unmutes notifications for the given issue without unsubscribing. /// Requires an active subscription to the issue. /// /// Returns success message on completion. #[utoipa::path( put, path = "/api/v1/workspaces/{workspace_name}/issues/{number}/mute", tag = "Issues", operation_id = "issueMute", params(PathParams), request_body( content = MuteIssueParams, description = "Mute/unmute parameters", content_type = "application/json" ), responses( (status = 200, description = "Mute status updated successfully.", body = ApiResponse), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 404, description = "Not currently subscribed to this issue", 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 .issue .issue_mute(&session, &path.workspace_name, path.number, params.muted) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new("Mute status updated".to_string()))) }