feat(ov): public read api on replicas with hot sorting
P2 Task 6(ov 公开读 API + viewer 切换):
- ov 装配 PublicOvModule(仅 app-ov.module):/public/albums/{token}、
/photos、/photos/{id} 三端点同契约读 ov 副本——translations/categories
自 JSON 列解析(防御性过滤),banner 经 ov 侧 STORAGE_PORT_OV 预签名
(同本地 MinIO;只读属约定,真实 OSS 只读凭证 P5),viewCount 取
album_stats 权威累计(无行默认 0;计数端点 Task 8 接线)
- 排序:latest=发布时间 keyset(shared/cursor.ts 同 cn);hot=likeTotal
desc + photoId desc 决胜,顺序按 `${albumId}|${category}` 短期快照
(内存 TTL 默认 30s,HOT_SORT_TTL_MS 惰性读取)——计数逐请求现读、
顺序冻结窗口内一致,偏移游标(base64url 整数)在快照内严格稳定;
非法 sort 400 VALIDATION_FAILED(cn 不感知该参数)
- 契约:viewCount 放宽为 integer|null(cn null、ov 权威值);
listPublicPhotos 增 sort 枚举(latest|hot,缺省 latest);
PhotoPublishedPayload 增 categoryId——补上 Tasks 3/4 管线缺口
(payloads/publish.service/sync-inbox 投影 + 关联测试随改)
- e2e(services/api/test/public-ov.e2e-spec.ts,7 用例):双入口同进程
起 cn+ov,cn API 种子→真实发送管线投递;断言相册/列表/单图与 cn
深等(仅签名 URL 剔除)、banner 真字节、参数矩阵 400/404、hot 排序
TTL 内快照(计数新/顺序旧)+TTL 过期翻转+偏移翻页、隐藏/下线/紧急
停用逐态撤下
- viewer 切 ov:API 基址 VITE_API_BASE(缺省 http://127.0.0.1:8081,
空串=同源网关;Vite 只读 apps/viewer/.env*,根 .env 对前端无效),
移除 /v1 dev 代理(CORS_ORIGINS 增 127.0.0.1:5175 直连两形);
playwright webServer 增 dev:ov;e2e 夹具 flushSync(albumId)=cn
bootstrap 快照 + deliverPendingOnce(sender 新导出)经真实管线投影
种子,deleteSeededPhoto 改软删让墓碑可同步;14 个 Playwright 全绿
(journey 管理/staffB 腿仍走 cn 8080)
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
// services/api/test/public.e2e-spec.ts 的种子形状)。公开端点匿名可达,无需登录态。
|
||||
// Task 7 起补 seedPhoto:直种「已发布完成态」照片(photos + thumb/browse 变体行
|
||||
// + MinIO 真实 JPEG 对象,行形状同 worker 发布事务落库形态)。
|
||||
// P2 Task 6 起 viewer 读 ov 副本(VITE_API_BASE 直连 8081)——直种只落 cn 库,
|
||||
// 故 seed* 后必须 flushSync(albumId):经 cn bootstrap(当前态快照写回 outbox)
|
||||
// + deliverPendingOnce(真实发送管线排空)把种子投影到 ov 副本,无旁路。
|
||||
import { randomBytes } from 'node:crypto';
|
||||
import { join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
@@ -10,6 +13,12 @@ import { config } from 'dotenv';
|
||||
import sharp from 'sharp';
|
||||
import { PrismaClient } from '@photo/db-cn';
|
||||
import { createStorageFromEnv, type MinioStorageAdapter } from '@photo/storage';
|
||||
import {
|
||||
SYNC_SIGNATURE_HEADER,
|
||||
SYNC_TIMESTAMP_HEADER,
|
||||
signSyncBody,
|
||||
} from '@photo/contracts';
|
||||
import { deliverPendingOnce } from '../../../services/sync/src/sender';
|
||||
|
||||
// 仓库根 .env 唯一来源(同 services/api test/load-env.ts 约定)。必须在
|
||||
// PrismaClient / createStorageFromEnv 构造前执行——两者均在首次查询/调用时读 env。
|
||||
@@ -198,11 +207,45 @@ export async function seedPhoto(
|
||||
return photoId;
|
||||
}
|
||||
|
||||
/** 直删照片域行(Task 8 e2e:模拟已分享深链的照片下线——变体行在前、主行在后;
|
||||
* MinIO 对象随 dev 前缀清理约定,不在此删)。 */
|
||||
/** cn→ov 种子同步(P2 Task 6):先打 cn bootstrap(当前态快照事件写回 outbox,
|
||||
* 优先级 normal),再 deliverPendingOnce 经真实发送管线(HMAC 签名
|
||||
* content-batches)排空至 ov 副本。幂等可重入:同版本新 eventId 在 ov 侧按
|
||||
* 当前态重断言应用;与常驻 dev:sync 发送器并发亦安全(eventId 幂等)。 */
|
||||
export async function flushSync(albumId: string): Promise<void> {
|
||||
const internalKey = process.env.SYNC_INTERNAL_KEY ?? 'dev-sync-secret-change-me';
|
||||
const rawBody = JSON.stringify({ albumId });
|
||||
const timestamp = Math.floor(Date.now() / 1000).toString();
|
||||
const cnBase = (process.env.CN_API_URL ?? 'http://127.0.0.1:8080').replace(/\/+$/, '');
|
||||
const bootstrap = await fetch(`${cnBase}/v1/internal/sync/bootstrap`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
[SYNC_TIMESTAMP_HEADER]: timestamp,
|
||||
[SYNC_SIGNATURE_HEADER]: signSyncBody(internalKey, rawBody),
|
||||
},
|
||||
body: rawBody,
|
||||
});
|
||||
if (!bootstrap.ok) {
|
||||
throw new Error(
|
||||
`flushSync bootstrap 失败(${bootstrap.status}):${(await bootstrap.text()).slice(0, 300)}`,
|
||||
);
|
||||
}
|
||||
await deliverPendingOnce({
|
||||
prisma,
|
||||
ovApiUrl: process.env.OV_API_URL ?? 'http://127.0.0.1:8081',
|
||||
internalKey,
|
||||
});
|
||||
}
|
||||
|
||||
/** 模拟已分享深链的照片下线(Task 8 e2e):软删(visibility=deleted +
|
||||
* entityVersion 自增)——硬删会让 cn 行缺席、bootstrap 快照无从发墓碑,ov 副本
|
||||
* 将永远留着旧发布行;软删 + flushSync 让 photo.deleted 事件走真实管线撤下。
|
||||
* 变体行/MinIO 对象保留(公开读只看 photos 行;行清理归 cleanupSeededAlbums)。 */
|
||||
export async function deleteSeededPhoto(photoId: string): Promise<void> {
|
||||
await prisma.photo_variants.deleteMany({ where: { photoId } });
|
||||
await prisma.photos.deleteMany({ where: { id: photoId } });
|
||||
await prisma.photos.update({
|
||||
where: { id: photoId },
|
||||
data: { visibility: 'deleted', entityVersion: { increment: 1 } },
|
||||
});
|
||||
}
|
||||
|
||||
/** 清理本文件种下的相册域行(扁平字符串外键、无 DB 级联——按子表在前手工删;
|
||||
@@ -223,6 +266,11 @@ export async function cleanupSeededAlbums(): Promise<void> {
|
||||
});
|
||||
}
|
||||
await prisma.photos.deleteMany({ where: { albumId: { in: seededAlbumIds } } });
|
||||
// flushSync 经 outbox 投递的快照事件行(已投递/未投递一并清——未投递
|
||||
// 残留会污染后续 deliverPendingOnce 的排空判定轮次)。
|
||||
await prisma.outbox_events.deleteMany({
|
||||
where: { albumId: { in: seededAlbumIds } },
|
||||
});
|
||||
const categories = await prisma.categories.findMany({
|
||||
where: { albumId: { in: seededAlbumIds } },
|
||||
select: { id: true },
|
||||
|
||||
Reference in New Issue
Block a user