Skip to content

Commit 04a1cf5

Browse files
authored
Merge pull request #114 from rebase-network/dev
feat: extract publisher
2 parents 9da0298 + 6ec11ef commit 04a1cf5

16 files changed

Lines changed: 464 additions & 355 deletions

‎.github/workflows/ci.yml‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,33 @@ jobs:
3737

3838
- name: Build
3939
run: pnpm build
40+
41+
backend:
42+
runs-on: ubuntu-latest
43+
steps:
44+
- name: Checkout
45+
uses: actions/checkout@v4
46+
47+
- name: Setup pnpm
48+
uses: pnpm/action-setup@v4
49+
50+
- name: Setup Node.js
51+
uses: actions/setup-node@v4
52+
with:
53+
node-version: 22
54+
cache: pnpm
55+
56+
- name: Install dependencies
57+
run: pnpm install --frozen-lockfile
58+
59+
- name: Typecheck
60+
run: pnpm typecheck:backend
61+
62+
- name: Test
63+
run: pnpm test
64+
65+
- name: Build API
66+
run: pnpm build:api
67+
68+
- name: Build admin
69+
run: pnpm build:admin

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ test-results
1616
.local/x-rebase-profile/
1717
wrangler.toml.bak
1818
geekdaily.csv
19+
.playwright-mcp/

‎apps/api/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"build": "pnpm --filter @rebase/shared build && pnpm --filter @rebase/db build && tsc -p tsconfig.json",
99
"start": "node --env-file=../../.env dist/index.js",
1010
"typecheck": "pnpm --filter @rebase/shared build && pnpm --filter @rebase/db build && tsc -p tsconfig.json --noEmit",
11+
"test": "pnpm --filter @rebase/shared build && pnpm --filter @rebase/db build && node --import tsx --test 'src/**/*.test.ts'",
1112
"bootstrap-admin": "node --env-file=../../.env --import tsx src/scripts/bootstrap-admin.ts"
1213
},
1314
"dependencies": {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import assert from 'node:assert/strict';
2+
3+
import { externalChannels, shouldAutoPublish } from './external-publishing.js';
4+
5+
const { infoq, learnblockchain, x } = externalChannels;
6+
7+
assert.equal(shouldAutoPublish(infoq, { status: 'published', infoqArticleUuid: null }), true);
8+
assert.equal(shouldAutoPublish(infoq, { status: 'published', infoqArticleUuid: 'existing' }), false);
9+
assert.equal(shouldAutoPublish(infoq, { status: 'draft', infoqArticleUuid: null }), false);
10+
assert.equal(shouldAutoPublish(learnblockchain, { status: 'published', learnBlockchainArticleId: null }), true);
11+
assert.equal(shouldAutoPublish(learnblockchain, { status: 'published', learnBlockchainArticleId: '123' }), false);
12+
assert.equal(shouldAutoPublish(x, { status: 'published', xPostId: null }), true);
13+
assert.equal(shouldAutoPublish(x, { status: 'published', xPostId: '2093' }), false);
14+
assert.equal(shouldAutoPublish(x, { status: 'draft', xPostId: null }), false);
15+
assert.equal(shouldAutoPublish(x, { status: 'published', infoqArticleUuid: 'other-channel', xPostId: null }), true);
16+
console.log('External publishing self-check passed');
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
import { eq } from 'drizzle-orm';
2+
3+
import { articles, events, geekdailyEpisodes } from '@rebase/db';
4+
import type { AdminArticleRecord, AdminEventRecord, AdminGeekDailyRecord } from '@rebase/shared';
5+
6+
import { getAdminArticle } from './articles.js';
7+
import { createAuditEntry, type AuditActor } from './audit.js';
8+
import { getDb } from './db.js';
9+
import { ApiError, badRequest, serviceUnavailable } from './errors.js';
10+
import { getAdminEvent } from './events.js';
11+
import { getAdminGeekDailyEpisode } from './geekdaily.js';
12+
import { infoqChannel } from './infoq.js';
13+
import { learnBlockchainChannel } from './learnblockchain.js';
14+
import { getPublicSiteConfig } from './site.js';
15+
import { xChannel } from './x.js';
16+
17+
export type ExternalIdField = 'infoqArticleUuid' | 'learnBlockchainArticleId' | 'xPostId';
18+
19+
export type ExternalPublishInput = {
20+
title: string;
21+
summary: string;
22+
bodyMarkdown: string;
23+
tags: string[];
24+
sourceUrl: string;
25+
};
26+
27+
export type ExternalChannel = {
28+
name: string;
29+
auditKey: string;
30+
idField: ExternalIdField;
31+
isConfigured: () => boolean | Promise<boolean>;
32+
publish: (input: ExternalPublishInput) => Promise<string>;
33+
};
34+
35+
type ContentSource<T> = {
36+
label: string;
37+
auditPrefix: string;
38+
targetType: string;
39+
get: (id: string) => Promise<T | null>;
40+
saveExternalId: (id: string, field: ExternalIdField, value: string) => Promise<void>;
41+
describe: (record: T) => string;
42+
toPublishInput: (record: T) => Omit<ExternalPublishInput, 'sourceUrl'> & { sourcePath: string };
43+
};
44+
45+
type RecordByKind = {
46+
article: AdminArticleRecord;
47+
event: AdminEventRecord;
48+
geekdaily: AdminGeekDailyRecord;
49+
};
50+
51+
export type ExternalContentKind = keyof RecordByKind;
52+
53+
const contentSources: { [K in ExternalContentKind]: ContentSource<RecordByKind[K]> } = {
54+
article: {
55+
label: 'article',
56+
auditPrefix: 'article',
57+
targetType: 'article',
58+
get: getAdminArticle,
59+
saveExternalId: async (id, field, value) => {
60+
await getDb().update(articles).set({ [field]: value, updatedAt: new Date() }).where(eq(articles.id, id));
61+
},
62+
describe: (record) => `article ${record.title}`,
63+
toPublishInput: (record) => ({
64+
title: record.title,
65+
summary: record.summary,
66+
bodyMarkdown: record.bodyMarkdown,
67+
tags: record.tags,
68+
sourcePath: `/articles/${record.publicNumber}-${record.slug}`,
69+
}),
70+
},
71+
event: {
72+
label: 'event',
73+
auditPrefix: 'event',
74+
targetType: 'event',
75+
get: getAdminEvent,
76+
saveExternalId: async (id, field, value) => {
77+
await getDb().update(events).set({ [field]: value, updatedAt: new Date() }).where(eq(events.id, id));
78+
},
79+
describe: (record) => `event ${record.title}`,
80+
toPublishInput: (record) => {
81+
const details = [`活动时间:${record.startAt ?? ''} 至 ${record.endAt ?? ''}`, `活动地点:${record.city} ${record.location} ${record.venue}`];
82+
if (record.registrationUrl) details.push(`报名链接:${record.registrationUrl}`);
83+
return {
84+
title: `活动|${record.title}`,
85+
summary: record.summary,
86+
bodyMarkdown: `${details.join('\n')}\n\n${record.bodyMarkdown}`,
87+
tags: record.tags,
88+
sourcePath: `/events/${record.publicNumber}-${record.slug}`,
89+
};
90+
},
91+
},
92+
geekdaily: {
93+
label: 'GeekDaily episode',
94+
auditPrefix: 'geekdaily',
95+
targetType: 'geekdaily_episode',
96+
get: getAdminGeekDailyEpisode,
97+
saveExternalId: async (id, field, value) => {
98+
await getDb().update(geekdailyEpisodes).set({ [field]: value, updatedAt: new Date() }).where(eq(geekdailyEpisodes.id, id));
99+
},
100+
describe: (record) => `GeekDaily ${record.episodeNumber}`,
101+
toPublishInput: (record) => ({
102+
title: `极客日报|${record.title}`,
103+
summary: record.summary,
104+
bodyMarkdown: record.bodyMarkdown,
105+
tags: record.tags,
106+
sourcePath: `/geekdaily/${record.slug}`,
107+
}),
108+
},
109+
};
110+
111+
export const externalChannels = {
112+
infoq: infoqChannel,
113+
learnblockchain: learnBlockchainChannel,
114+
x: xChannel,
115+
} satisfies Record<string, ExternalChannel>;
116+
117+
export type ExternalChannelKey = keyof typeof externalChannels;
118+
119+
export const shouldAutoPublish = (channel: ExternalChannel, record: { status: string } & Partial<Record<ExternalIdField, string | null>>) =>
120+
record.status === 'published' && !record[channel.idField];
121+
122+
// ponytail: one process-wide publish queue per channel; use a durable job worker if volume requires parallelism.
123+
const publishQueues = new Map<string, Promise<void>>();
124+
const queuePublish = <T>(channel: ExternalChannel, task: () => Promise<T>) => {
125+
const next = (publishQueues.get(channel.name) ?? Promise.resolve()).then(task);
126+
publishQueues.set(channel.name, next.then(() => undefined, () => undefined));
127+
return next;
128+
};
129+
130+
const getSourceUrl = async (path: string) => new URL(path, `${(await getPublicSiteConfig()).primaryDomain}/`).toString();
131+
132+
const describeError = (error: unknown) => {
133+
if (error instanceof ApiError) return { message: error.message, status: error.status, code: error.code, details: error.details ?? null };
134+
if (error instanceof Error) return { message: error.message, name: error.name, stack: error.stack };
135+
return { message: String(error) };
136+
};
137+
138+
// One JSON line per publish attempt so `manage.sh logs api` can be grepped by channel or target.
139+
const logPublishEvent = (level: 'info' | 'error', event: string, fields: Record<string, unknown>) => {
140+
const line = JSON.stringify({ time: new Date().toISOString(), level, event, ...fields });
141+
if (level === 'error') console.error(line);
142+
else console.log(line);
143+
};
144+
145+
const recordPublishFailure = async <T>(
146+
source: ContentSource<T>,
147+
channel: ExternalChannel,
148+
id: string,
149+
actor: AuditActor,
150+
record: T,
151+
error: unknown,
152+
context: Record<string, unknown>,
153+
) => {
154+
const failure = describeError(error);
155+
logPublishEvent('error', 'external_publish_failed', { ...context, error: failure });
156+
try {
157+
await createAuditEntry({
158+
...actor,
159+
action: `${source.auditPrefix}.${channel.auditKey}_publish_failed`,
160+
targetType: source.targetType,
161+
targetId: id,
162+
summary: `Failed to publish ${source.describe(record)} to ${channel.name}: ${failure.message}`.slice(0, 500),
163+
payloadJson: { ...context, error: { ...failure, stack: undefined } },
164+
});
165+
} catch (auditError) {
166+
logPublishEvent('error', 'external_publish_failure_audit_failed', { ...context, error: describeError(auditError) });
167+
}
168+
};
169+
170+
export const publishToExternal = async <K extends ExternalContentKind>(
171+
kind: K,
172+
channelKey: ExternalChannelKey,
173+
id: string,
174+
actor: AuditActor,
175+
): Promise<RecordByKind[K]> => {
176+
const source = contentSources[kind] as ContentSource<RecordByKind[K]>;
177+
const channel: ExternalChannel = externalChannels[channelKey];
178+
const record = await source.get(id);
179+
if (!record) throw badRequest(`${source.label} not found`);
180+
if (record.status !== 'published') throw badRequest(`publish the Rebase ${source.label} before sending it to ${channel.name}`);
181+
if (record[channel.idField]) return record;
182+
const startedAt = Date.now();
183+
const context = { channel: channel.name, kind, targetId: id, title: source.describe(record) };
184+
let externalId: string | null;
185+
try {
186+
externalId = await queuePublish(channel, async () => {
187+
const latest = await source.get(id);
188+
if (!latest) throw badRequest(`${source.label} not found`);
189+
if (latest[channel.idField]) return null;
190+
const { sourcePath, ...input } = source.toPublishInput(latest);
191+
return channel.publish({ ...input, sourceUrl: await getSourceUrl(sourcePath) });
192+
});
193+
} catch (error) {
194+
await recordPublishFailure(source, channel, id, actor, record, error, { ...context, durationMs: Date.now() - startedAt });
195+
throw error;
196+
}
197+
if (!externalId) return (await source.get(id)) as RecordByKind[K];
198+
logPublishEvent('info', 'external_publish_succeeded', { ...context, externalId, durationMs: Date.now() - startedAt });
199+
await source.saveExternalId(id, channel.idField, externalId);
200+
await createAuditEntry({
201+
...actor,
202+
action: `${source.auditPrefix}.${channel.auditKey}_publish`,
203+
targetType: source.targetType,
204+
targetId: id,
205+
summary: `Published ${source.describe(record)} to ${channel.name}`,
206+
});
207+
return (await source.get(id)) as RecordByKind[K];
208+
};
209+
210+
export const autoPublishToExternal = async <K extends ExternalContentKind>(kind: K, record: RecordByKind[K] | null, actor: AuditActor) => {
211+
if (!record) return record;
212+
let next = record;
213+
let firstError: unknown = null;
214+
const failures: string[] = [];
215+
216+
for (const [channelKey, channel] of Object.entries(externalChannels) as Array<[ExternalChannelKey, ExternalChannel]>) {
217+
if (!shouldAutoPublish(channel, next) || !(await channel.isConfigured())) continue;
218+
try {
219+
next = await publishToExternal(kind, channelKey, next.id, actor);
220+
} catch (error) {
221+
firstError ??= error;
222+
failures.push(`${channel.name}:${error instanceof Error ? error.message : String(error)}`);
223+
}
224+
}
225+
226+
if (firstError) {
227+
const message = `站内内容已保存,但外部发布失败:${failures.join(';')}`;
228+
if (firstError instanceof ApiError) {
229+
throw new ApiError(firstError.status, firstError.code, message, {
230+
...firstError.details,
231+
failures,
232+
});
233+
}
234+
throw serviceUnavailable(message, { failures });
235+
}
236+
return next;
237+
};

‎apps/api/src/lib/infoq.test.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import assert from 'node:assert/strict';
22

3-
import { infoqWordCount, markdownToDoc, shouldAutoPublishToInfoq } from './infoq.js';
3+
import { infoqWordCount, markdownToDoc } from './infoq.js';
44

55
const doc = markdownToDoc('# 标题\n\n正文 **加粗** 和 [链接](https://rebase.network)。\n\n- 一\n- 二');
66
assert.equal(doc.content?.[0]?.type, 'heading');
77
assert.equal(doc.content?.[1]?.content?.[1]?.marks?.[0]?.type, 'strong');
88
assert.equal(doc.content?.[1]?.content?.[3]?.type, 'link');
99
assert.equal(doc.content?.[2]?.type, 'bulletedlist');
1010
assert.equal(infoqWordCount('abc中文'), 4);
11-
assert.equal(shouldAutoPublishToInfoq({ status: 'published', infoqArticleUuid: null }), true);
12-
assert.equal(shouldAutoPublishToInfoq({ status: 'published', infoqArticleUuid: 'existing' }), false);
13-
assert.equal(shouldAutoPublishToInfoq({ status: 'draft', infoqArticleUuid: null }), false);
1411
assert.doesNotMatch(JSON.stringify(markdownToDoc('before <script>alert(1)</script> after')), /[<>]/);
1512
assert.doesNotMatch(JSON.stringify(markdownToDoc('<script>alert(1)</script>')), /[<>]/);
1613
console.log('InfoQ markdown conversion self-check passed');

0 commit comments

Comments
 (0)