nomadro/src/modules/work/lib/store.ts
Eric abdcafb1eb Ship Work access gate, pads/boards upgrades, and locale hydration fix on .com.
Gate /social/work behind join/login/profile; harden Work UX and LanguageSwitcher SSR; deploy portal, admin, and work API.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-08 00:14:39 +08:00

306 lines
8.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { randomUUID } from "crypto";
import { promises as fs } from "fs";
import path from "path";
// Legacy offline JSON schema is intentionally distinct from the current API model.
type WorkCard = { id: string; title: string; description: string; listId: string; position: number; updatedAt: string };
type WorkBoard = { id: string; title: string; description: string; createdAt: string; updatedAt: string; lists: { id: string; title: string; position: number }[]; cards: WorkCard[] };
type WorkPad = { id: string; title: string; text: string; authors: string[]; revisions: { id: string; author: string; text: string; at: string }[]; createdAt: string; updatedAt: string };
type WorkStoreFile = { boards: WorkBoard[]; pads: WorkPad[] };
const DATA_DIR = path.join(process.cwd(), "data", "work");
const STORE_PATH = path.join(DATA_DIR, "store.json");
function now() {
return new Date().toISOString();
}
function seed(): WorkStoreFile {
const t = now();
const listTodo = "list-todo";
const listDoing = "list-doing";
const listDone = "list-done";
return {
boards: [
{
id: "board-demo",
title: "Nomadro remote sprint",
description: "原生看板种子 · 可改可删",
createdAt: t,
updatedAt: t,
lists: [
{ id: listTodo, title: "Backlog", position: 0 },
{ id: listDoing, title: "Doing", position: 1 },
{ id: listDone, title: "Done", position: 2 },
],
cards: [
{
id: "card-1",
title: "Ship /social/work native UI",
description: "No subdomain redirects",
listId: listDoing,
position: 0,
updatedAt: t,
},
{
id: "card-2",
title: "Wire public API + SDK",
description: "",
listId: listDone,
position: 0,
updatedAt: t,
},
{
id: "card-3",
title: "Invite remote collaborators",
description: "From /social/team",
listId: listTodo,
position: 0,
updatedAt: t,
},
],
},
],
pads: [
{
id: "pad-welcome",
title: "Welcome pad",
text:
"# Nomadro Live Pad\n\n多人共写草稿(Etherpad 模型二次开发)。\n改这里会写入修订历史。\n",
authors: ["system"],
revisions: [
{
id: "rev-0",
author: "system",
text:
"# Nomadro Live Pad\n\n多人共写草稿(Etherpad 模型二次开发)。\n改这里会写入修订历史。\n",
at: t,
},
],
createdAt: t,
updatedAt: t,
},
],
};
}
async function ensure(): Promise<WorkStoreFile> {
await fs.mkdir(DATA_DIR, { recursive: true });
try {
const raw = await fs.readFile(STORE_PATH, "utf8");
const parsed = JSON.parse(raw) as WorkStoreFile;
if (!parsed.boards || !parsed.pads) return seed();
return parsed;
} catch {
const s = seed();
await fs.writeFile(STORE_PATH, JSON.stringify(s, null, 2), "utf8");
return s;
}
}
async function save(data: WorkStoreFile) {
await fs.mkdir(DATA_DIR, { recursive: true });
await fs.writeFile(STORE_PATH, JSON.stringify(data, null, 2), "utf8");
}
export async function listBoards(): Promise<
Pick<WorkBoard, "id" | "title" | "description" | "updatedAt" | "createdAt">[]
> {
const data = await ensure();
return data.boards.map(({ id, title, description, updatedAt, createdAt }) => ({
id,
title,
description,
updatedAt,
createdAt,
}));
}
export async function getBoard(id: string): Promise<WorkBoard | null> {
const data = await ensure();
return data.boards.find((b) => b.id === id) ?? null;
}
export async function createBoard(input: {
title: string;
description?: string;
}): Promise<WorkBoard> {
const data = await ensure();
const t = now();
const board: WorkBoard = {
id: `board-${randomUUID().slice(0, 8)}`,
title: input.title.trim() || "Untitled board",
description: (input.description || "").trim(),
createdAt: t,
updatedAt: t,
lists: [
{ id: `list-${randomUUID().slice(0, 8)}`, title: "Backlog", position: 0 },
{ id: `list-${randomUUID().slice(0, 8)}`, title: "Doing", position: 1 },
{ id: `list-${randomUUID().slice(0, 8)}`, title: "Done", position: 2 },
],
cards: [],
};
data.boards.unshift(board);
await save(data);
return board;
}
export async function addCard(
boardId: string,
input: { listId: string; title: string; description?: string },
): Promise<WorkCard | null> {
const data = await ensure();
const board = data.boards.find((b) => b.id === boardId);
if (!board) return null;
const list = board.lists.find((l) => l.id === input.listId);
if (!list) return null;
const t = now();
const siblings = board.cards.filter((c) => c.listId === input.listId);
const card: WorkCard = {
id: `card-${randomUUID().slice(0, 8)}`,
title: input.title.trim() || "Untitled",
description: (input.description || "").trim(),
listId: input.listId,
position: siblings.length,
updatedAt: t,
};
board.cards.push(card);
board.updatedAt = t;
await save(data);
return card;
}
export async function moveCard(
boardId: string,
cardId: string,
input: { listId: string; position: number },
): Promise<WorkBoard | null> {
const data = await ensure();
const board = data.boards.find((b) => b.id === boardId);
if (!board) return null;
const card = board.cards.find((c) => c.id === cardId);
if (!card) return null;
if (!board.lists.some((l) => l.id === input.listId)) return null;
const t = now();
const fromList = card.listId;
const others = board.cards
.filter((c) => c.listId === input.listId && c.id !== cardId)
.sort((a, b) => a.position - b.position);
const pos = Math.max(0, Math.min(input.position, others.length));
others.splice(pos, 0, card);
card.listId = input.listId;
card.updatedAt = t;
others.forEach((c, i) => {
c.position = i;
});
if (fromList !== input.listId) {
board.cards
.filter((c) => c.listId === fromList)
.sort((a, b) => a.position - b.position)
.forEach((c, i) => {
c.position = i;
});
}
board.updatedAt = t;
await save(data);
return board;
}
export async function addList(
boardId: string,
title: string,
): Promise<WorkBoard | null> {
const data = await ensure();
const board = data.boards.find((b) => b.id === boardId);
if (!board) return null;
const t = now();
board.lists.push({
id: `list-${randomUUID().slice(0, 8)}`,
title: title.trim() || "List",
position: board.lists.length,
});
board.updatedAt = t;
await save(data);
return board;
}
export async function listPads(): Promise<
Pick<WorkPad, "id" | "title" | "updatedAt" | "createdAt" | "authors">[]
> {
const data = await ensure();
return data.pads.map(({ id, title, updatedAt, createdAt, authors }) => ({
id,
title,
updatedAt,
createdAt,
authors,
}));
}
export async function getPad(id: string): Promise<WorkPad | null> {
const data = await ensure();
return data.pads.find((p) => p.id === id) ?? null;
}
export async function createPad(input: {
title: string;
author?: string;
}): Promise<WorkPad> {
const data = await ensure();
const t = now();
const author = (input.author || "guest").trim() || "guest";
const text = "";
const pad: WorkPad = {
id: `pad-${randomUUID().slice(0, 8)}`,
title: input.title.trim() || "Untitled pad",
text,
authors: [author],
revisions: [{ id: `rev-${randomUUID().slice(0, 8)}`, author, text, at: t }],
createdAt: t,
updatedAt: t,
};
data.pads.unshift(pad);
await save(data);
return pad;
}
export async function updatePad(
id: string,
input: { text: string; author?: string; title?: string },
): Promise<WorkPad | null> {
const data = await ensure();
const pad = data.pads.find((p) => p.id === id);
if (!pad) return null;
const t = now();
const author = (input.author || "guest").trim() || "guest";
if (typeof input.title === "string" && input.title.trim()) {
pad.title = input.title.trim();
}
pad.text = input.text;
if (!pad.authors.includes(author)) pad.authors.push(author);
const last = pad.revisions[pad.revisions.length - 1];
if (!last || last.text !== input.text || last.author !== author) {
pad.revisions.push({
id: `rev-${randomUUID().slice(0, 8)}`,
author,
text: input.text,
at: t,
});
}
// keep last 40 revisions (oldest → newest in file)
if (pad.revisions.length > 40) {
pad.revisions = pad.revisions.slice(-40);
}
pad.updatedAt = t;
await save(data);
return pad;
}
export async function deletePad(id: string): Promise<boolean> {
const data = await ensure();
const before = data.pads.length;
data.pads = data.pads.filter((p) => p.id !== id);
if (data.pads.length === before) return false;
await save(data);
return true;
}