use ::session::{AuthedUser, OptionalUser}; use api::http::{Json, Path, Query, State}; use api::rest::AppResponseData; use models::common::watch_mode::WatchMode; use serde::Deserialize; use utoipa::IntoParams; use crate::errors::{ApiError, GitApiError}; use crate::handler::{RepoPath, resolve_reader}; use crate::service::GitService; use crate::service::watches::{RepoWatchDto, UpsertWatchCmd, WatcherDto}; #[derive(Debug, Clone, Deserialize, IntoParams)] #[serde(deny_unknown_fields)] pub struct WatchQuery { pub limit: Option, pub offset: Option, pub mode: Option, } impl WatchQuery { fn bounds(&self) -> Result<(u64, u64), api::pagination::PageError> { api::pagination::page_bounds(self.limit, self.offset, 50, 100) } } #[utoipa::path( put, path = "/repos/{space_name}/{repo_name}/watch", tag = "repos", description = "设置或更新当前用户对仓库的关注、不关注或忽略模式。", params(RepoPath), request_body = UpsertWatchCmd, responses( (status = 200, description = "upsert watch", body = AppResponseData) ) )] pub async fn upsert_watch( State(service): State, AuthedUser(uid): AuthedUser, Path(path): Path, Json(cmd): Json, ) -> Result, ApiError> { let (ctx, _) = resolve_reader( &service, Some(uid), path.space_name.clone(), path.repo_name.clone(), ) .await?; let out = service .upsert_watch(&ctx, uid, cmd) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) } #[utoipa::path( get, path = "/repos/{space_name}/{repo_name}/watchers", tag = "repos", description = "分页列出仓库关注者,并可按关注模式过滤。", params(RepoPath, WatchQuery), responses( (status = 200, description = "list watchers", body = AppResponseData>) ) )] pub async fn list_watchers( State(service): State, OptionalUser(viewer): OptionalUser, Path(path): Path, Query(q): Query, ) -> Result>, ApiError> { let ctx = resolve_reader( &service, viewer, path.space_name.clone(), path.repo_name.clone(), ) .await? .0; let (limit, offset) = q.bounds()?; let out = service .list_watchers(&ctx, q.mode, limit, offset) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) }