|
| 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 | +}; |
0 commit comments