| 1 | import type { Locale } from '@yikart/common' |
| 2 | import { AccountType } from '@yikart/common' |
| 3 | import { z } from 'zod' |
| 4 | import { BilibiliOptionSchema } from './bilibili/bilibili.schema' |
| 5 | import { DouyinOptionSchema } from './douyin/douyin.schema' |
| 6 | import { FacebookOptionSchema } from './facebook/facebook.schema' |
| 7 | import { InstagramOptionSchema } from './instagram/instagram.schema' |
| 8 | import { KwaiOptionSchema } from './kwai/kwai.schema' |
| 9 | import { LinkedInOptionSchema } from './linkedin/linkedin.schema' |
| 10 | import { PinterestOptionSchema } from './pinterest/pinterest.schema' |
| 11 | import { PublishContentMode } from './platforms.interface' |
| 12 | import { RedNoteOptionSchema } from './rednote/rednote.schema' |
| 13 | import { ThreadsOptionSchema } from './threads/threads.schema' |
| 14 | import { TiktokOptionSchema } from './tiktok/tiktok.schema' |
| 15 | import { TwitterOptionSchema } from './twitter/twitter.schema' |
| 16 | import { WeChatChannelsOptionSchema } from './wechat/wechat-channels/wechat-channels.schema' |
| 17 | import { WeChatOfficialOptionSchema } from './wechat/wechat-official/wechat-official.schema' |
| 18 | import { YoutubeOptionSchema } from './youtube/youtube.schema' |
| 19 | |
| 20 | export enum PublishValidationIssueCode { |
| 21 | Required = 'required', |
| 22 | TooBig = 'too_big', |
| 23 | TooSmall = 'too_small', |
| 24 | InvalidCombination = 'invalid_combination', |
| 25 | UnsupportedFormat = 'unsupported_format', |
| 26 | UnsupportedContentMode = 'unsupported_content_mode', |
| 27 | InvalidDuration = 'invalid_duration', |
| 28 | InvalidUrl = 'invalid_url', |
| 29 | MediaUnreachable = 'media_unreachable', |
| 30 | InvalidOption = 'invalid_option', |
| 31 | } |
| 32 | |
| 33 | export enum PublishValidationField { |
| 34 | Post = 'post', |
| 35 | Title = 'title', |
| 36 | Body = 'body', |
| 37 | Text = 'text', |
| 38 | Media = 'media', |
| 39 | Image = 'image', |
| 40 | Video = 'video', |
| 41 | Cover = 'cover', |
| 42 | Topic = 'topic', |
| 43 | Url = 'url', |
| 44 | Option = 'option', |
| 45 | } |
| 46 | |
| 47 | export enum PublishValidationCombination { |
| 48 | ImageVideo = 'image_video', |
| 49 | ReelImage = 'reel_image', |
| 50 | } |
| 51 | |
| 52 | export interface PublishValidationIssue { |
| 53 | code: PublishValidationIssueCode |
| 54 | path: Array<string | number> |
| 55 | message?: string |
| 56 | params?: Record<string, unknown> |
| 57 | } |
| 58 | |
| 59 | export function formatPublishValidationIssue( |
| 60 | issue: PublishValidationIssue, |
| 61 | locale: Locale, |
| 62 | ): PublishValidationIssue { |
| 63 | return { |
| 64 | ...issue, |
| 65 | message: getPublishValidationIssueMessage(issue, locale), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | function getPublishValidationIssueMessage(issue: PublishValidationIssue, locale: Locale): string { |
| 70 | const label = getFieldLabel(getIssueField(issue), locale) |
| 71 | |
| 72 | switch (issue.code) { |
| 73 | case PublishValidationIssueCode.Required: |
| 74 | return locale === 'zh-CN' |
| 75 | ? `${label}为必填项` |
| 76 | : `${label} is required` |
| 77 | |
| 78 | case PublishValidationIssueCode.TooBig: |
| 79 | return getLimitMessage(issue, label, 'maximum', locale) |
| 80 | |
| 81 | case PublishValidationIssueCode.TooSmall: |
| 82 | return getLimitMessage(issue, label, 'minimum', locale) |
| 83 | |
| 84 | case PublishValidationIssueCode.InvalidCombination: |
| 85 | return getCombinationMessage(issue, locale) |
| 86 | |
| 87 | case PublishValidationIssueCode.UnsupportedFormat: |
| 88 | return locale === 'zh-CN' |
| 89 | ? `${label}格式不支持,支持格式:${getFormatsParam(issue)}` |
| 90 | : `${label} format is not supported. Supported formats: ${getFormatsParam(issue)}` |
| 91 | |
| 92 | case PublishValidationIssueCode.UnsupportedContentMode: |
| 93 | return locale === 'zh-CN' |
| 94 | ? `平台不支持${getContentModeLabel(issue, locale)}发布` |
| 95 | : `Platform does not support ${getContentModeLabel(issue, locale)} publishing` |
| 96 | |
| 97 | case PublishValidationIssueCode.InvalidDuration: |
| 98 | return getDurationMessage(issue, label, locale) |
| 99 | |
| 100 | case PublishValidationIssueCode.InvalidUrl: |
| 101 | return locale === 'zh-CN' |
| 102 | ? `${label}必须使用 HTTPS` |
| 103 | : `${label} must use HTTPS` |
| 104 | |
| 105 | case PublishValidationIssueCode.MediaUnreachable: |
| 106 | return locale === 'zh-CN' |
| 107 | ? `${label}无法从服务端拉取或解析:${String(issue.params?.['reason'] ?? '')}` |
| 108 | : `${label} could not be fetched or probed by the server: ${String(issue.params?.['reason'] ?? '')}` |
| 109 | |
| 110 | case PublishValidationIssueCode.InvalidOption: |
| 111 | return locale === 'zh-CN' |
| 112 | ? `${label}无效` |
| 113 | : `${label} is invalid` |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | function getFieldLabel(field: PublishValidationField, locale: Locale): string { |
| 118 | switch (field) { |
| 119 | case PublishValidationField.Title: |
| 120 | return locale === 'zh-CN' ? '标题' : 'Title' |
| 121 | |
| 122 | case PublishValidationField.Body: |
| 123 | return locale === 'zh-CN' ? '正文' : 'Body' |
| 124 | |
| 125 | case PublishValidationField.Text: |
| 126 | return locale === 'zh-CN' ? '文本' : 'Text' |
| 127 | |
| 128 | case PublishValidationField.Media: |
| 129 | return locale === 'zh-CN' ? '媒体' : 'Media' |
| 130 | |
| 131 | case PublishValidationField.Image: |
| 132 | return locale === 'zh-CN' ? '图片' : 'Image' |
| 133 | |
| 134 | case PublishValidationField.Video: |
| 135 | return locale === 'zh-CN' ? '视频' : 'Video' |
| 136 | |
| 137 | case PublishValidationField.Cover: |
| 138 | return locale === 'zh-CN' ? '封面' : 'Cover' |
| 139 | |
| 140 | case PublishValidationField.Topic: |
| 141 | return locale === 'zh-CN' ? '话题' : 'Topic' |
| 142 | |
| 143 | case PublishValidationField.Url: |
| 144 | return locale === 'zh-CN' ? '链接' : 'URL' |
| 145 | |
| 146 | case PublishValidationField.Option: |
| 147 | return locale === 'zh-CN' ? '发布选项' : 'Publish option' |
| 148 | |
| 149 | case PublishValidationField.Post: |
| 150 | return locale === 'zh-CN' ? '发布内容' : 'Post content' |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | function getIssueField(issue: PublishValidationIssue): PublishValidationField { |
| 155 | const field = issue.params?.['field'] |
| 156 | if (isPublishValidationField(field)) { |
| 157 | return field |
| 158 | } |
| 159 | |
| 160 | const lastPath = issue.path[issue.path.length - 1] |
| 161 | if (isPublishValidationField(lastPath)) { |
| 162 | return lastPath |
| 163 | } |
| 164 | |
| 165 | return PublishValidationField.Post |
| 166 | } |
| 167 | |
| 168 | function isPublishValidationField(value: unknown): value is PublishValidationField { |
| 169 | return typeof value === 'string' |
| 170 | && Object.values(PublishValidationField).includes(value as PublishValidationField) |
| 171 | } |
| 172 | |
| 173 | function getCombinationMessage(issue: PublishValidationIssue, locale: Locale): string { |
| 174 | if (issue.params?.['combination'] === PublishValidationCombination.ReelImage) { |
| 175 | return locale === 'zh-CN' |
| 176 | ? 'Reels 不支持图片媒体' |
| 177 | : 'Reels do not support image media' |
| 178 | } |
| 179 | |
| 180 | return locale === 'zh-CN' |
| 181 | ? '不能在同一篇发布中混合图片和视频' |
| 182 | : 'Images and videos cannot be mixed in one post' |
| 183 | } |
| 184 | |
| 185 | function getNumberParam(issue: PublishValidationIssue, key: string): number { |
| 186 | const value = issue.params?.[key] |
| 187 | return Number(value ?? 0) |
| 188 | } |
| 189 | |
| 190 | function getOptionalNumberParam(issue: PublishValidationIssue, key: string): number | undefined { |
| 191 | const value = issue.params?.[key] |
| 192 | return value === undefined ? undefined : Number(value) |
| 193 | } |
| 194 | |
| 195 | function getDurationMessage(issue: PublishValidationIssue, label: string, locale: Locale): string { |
| 196 | const minimum = getOptionalNumberParam(issue, 'minimum') |
| 197 | const maximum = getOptionalNumberParam(issue, 'maximum') |
| 198 | if (minimum !== undefined && maximum !== undefined) { |
| 199 | return locale === 'zh-CN' |
| 200 | ? `${label}时长必须在 ${minimum} 到 ${maximum} 秒之间` |
| 201 | : `${label} duration must be between ${minimum} and ${maximum} seconds` |
| 202 | } |
| 203 | if (minimum !== undefined) { |
| 204 | return locale === 'zh-CN' |
| 205 | ? `${label}时长不能少于 ${minimum} 秒` |
| 206 | : `${label} duration must be at least ${minimum} seconds` |
| 207 | } |
| 208 | if (maximum !== undefined) { |
| 209 | return locale === 'zh-CN' |
| 210 | ? `${label}时长不能超过 ${maximum} 秒` |
| 211 | : `${label} duration must be at most ${maximum} seconds` |
| 212 | } |
| 213 | return locale === 'zh-CN' |
| 214 | ? `${label}时长无效` |
| 215 | : `${label} duration is invalid` |
| 216 | } |
| 217 | |
| 218 | function getLimitMessage(issue: PublishValidationIssue, label: string, boundKey: 'minimum' | 'maximum', locale: Locale): string { |
| 219 | const bound = getNumberParam(issue, boundKey) |
| 220 | if (issue.params?.['dimension'] === 'aspectRatio') { |
| 221 | const current = getOptionalNumberParam(issue, 'current') |
| 222 | const currentText = current === undefined |
| 223 | ? '' |
| 224 | : locale === 'zh-CN' |
| 225 | ? `,当前为 ${current}` |
| 226 | : `, current is ${current}` |
| 227 | |
| 228 | if (boundKey === 'minimum') { |
| 229 | return locale === 'zh-CN' |
| 230 | ? `${label}宽高比不能少于 ${bound}${currentText}` |
| 231 | : `${label} aspect ratio must be at least ${bound}${currentText}` |
| 232 | } |
| 233 | |
| 234 | return locale === 'zh-CN' |
| 235 | ? `${label}宽高比不能超过 ${bound}${currentText}` |
| 236 | : `${label} aspect ratio must be at most ${bound}${currentText}` |
| 237 | } |
| 238 | |
| 239 | const unitLabel = getUnitLabel(issue, locale) |
| 240 | if (boundKey === 'minimum') { |
| 241 | return locale === 'zh-CN' |
| 242 | ? `${label}不能少于 ${bound} ${unitLabel}` |
| 243 | : `${label} must be at least ${bound} ${unitLabel}` |
| 244 | } |
| 245 | return locale === 'zh-CN' |
| 246 | ? `${label}不能超过 ${bound} ${unitLabel}` |
| 247 | : `${label} must be at most ${bound} ${unitLabel}` |
| 248 | } |
| 249 | |
| 250 | function getFormatsParam(issue: PublishValidationIssue): string { |
| 251 | const formats = issue.params?.['formats'] ?? issue.params?.['allowed'] |
| 252 | if (Array.isArray(formats)) { |
| 253 | return formats.filter((format): format is string => typeof format === 'string').join(', ') |
| 254 | } |
| 255 | if (typeof formats === 'string') { |
| 256 | return formats |
| 257 | } |
| 258 | return '' |
| 259 | } |
| 260 | |
| 261 | function getContentModeLabel(issue: PublishValidationIssue, locale: Locale): string { |
| 262 | const mode = issue.params?.['mode'] |
| 263 | if (mode === 'text' || mode === PublishContentMode.Text) { |
| 264 | return locale === 'zh-CN' ? '纯文本' : 'text' |
| 265 | } |
| 266 | if (mode === 'image_text' || mode === PublishContentMode.ImageText) { |
| 267 | return locale === 'zh-CN' ? '图文' : 'image-text' |
| 268 | } |
| 269 | if (mode === 'video' || mode === PublishContentMode.Video) { |
| 270 | return locale === 'zh-CN' ? '视频' : 'video' |
| 271 | } |
| 272 | return locale === 'zh-CN' ? '该内容模式' : 'this content mode' |
| 273 | } |
| 274 | |
| 275 | function getUnitLabel(issue: PublishValidationIssue, locale: Locale): string { |
| 276 | const unit = issue.params?.['unit'] |
| 277 | if (unit === 'characters') { |
| 278 | return locale === 'zh-CN' ? '个字符' : 'characters' |
| 279 | } |
| 280 | if (unit === 'items') { |
| 281 | return locale === 'zh-CN' ? '项' : 'items' |
| 282 | } |
| 283 | if (unit === 'bytes') { |
| 284 | return locale === 'zh-CN' ? '字节' : 'bytes' |
| 285 | } |
| 286 | if (unit === 'pixels') { |
| 287 | return locale === 'zh-CN' ? '像素' : 'pixels' |
| 288 | } |
| 289 | if (unit === 'seconds') { |
| 290 | return locale === 'zh-CN' ? '秒' : 'seconds' |
| 291 | } |
| 292 | return '' |
| 293 | } |
| 294 | |
| 295 | export function createPlatformPublishOptionItemSchema<TShape extends z.ZodRawShape>(extraShape: TShape) { |
| 296 | return z.discriminatedUnion('platform', [ |
| 297 | z.object({ |
| 298 | platform: z.literal(AccountType.Bilibili).describe('平台'), |
| 299 | option: BilibiliOptionSchema.describe('哔哩哔哩发布选项'), |
| 300 | ...extraShape, |
| 301 | }), |
| 302 | z.object({ |
| 303 | platform: z.literal(AccountType.YouTube).describe('平台'), |
| 304 | option: YoutubeOptionSchema.optional().describe('YouTube 发布选项'), |
| 305 | ...extraShape, |
| 306 | }), |
| 307 | z.object({ |
| 308 | platform: z.literal(AccountType.WeChatOfficial).describe('平台'), |
| 309 | option: WeChatOfficialOptionSchema.optional().describe('微信公众号发布选项'), |
| 310 | ...extraShape, |
| 311 | }), |
| 312 | z.object({ |
| 313 | platform: z.literal(AccountType.Facebook).describe('平台'), |
| 314 | option: FacebookOptionSchema.optional().describe('Facebook 发布选项'), |
| 315 | ...extraShape, |
| 316 | }), |
| 317 | z.object({ |
| 318 | platform: z.literal(AccountType.Instagram).describe('平台'), |
| 319 | option: InstagramOptionSchema.optional().describe('Instagram 发布选项'), |
| 320 | ...extraShape, |
| 321 | }), |
| 322 | z.object({ |
| 323 | platform: z.literal(AccountType.Threads).describe('平台'), |
| 324 | option: ThreadsOptionSchema.optional().describe('Threads 发布选项'), |
| 325 | ...extraShape, |
| 326 | }), |
| 327 | z.object({ |
| 328 | platform: z.literal(AccountType.Pinterest).describe('平台'), |
| 329 | option: PinterestOptionSchema.describe('Pinterest 发布选项'), |
| 330 | ...extraShape, |
| 331 | }), |
| 332 | z.object({ |
| 333 | platform: z.literal(AccountType.TikTok).describe('平台'), |
| 334 | option: TiktokOptionSchema.optional().describe('TikTok 发布选项'), |
| 335 | ...extraShape, |
| 336 | }), |
| 337 | z.object({ |
| 338 | platform: z.literal(AccountType.Douyin).describe('平台'), |
| 339 | option: DouyinOptionSchema.optional().describe('抖音发布选项'), |
| 340 | ...extraShape, |
| 341 | }), |
| 342 | z.object({ |
| 343 | platform: z.literal(AccountType.Twitter).describe('平台'), |
| 344 | option: TwitterOptionSchema.optional().describe('Twitter / X 发布选项'), |
| 345 | ...extraShape, |
| 346 | }), |
| 347 | z.object({ |
| 348 | platform: z.literal(AccountType.Kwai).describe('平台'), |
| 349 | option: KwaiOptionSchema.optional().describe('快手发布选项'), |
| 350 | ...extraShape, |
| 351 | }), |
| 352 | z.object({ |
| 353 | platform: z.literal(AccountType.LinkedIn).describe('平台'), |
| 354 | option: LinkedInOptionSchema.optional().describe('LinkedIn 发布选项'), |
| 355 | ...extraShape, |
| 356 | }), |
| 357 | z.object({ |
| 358 | platform: z.literal(AccountType.RedNote).describe('平台'), |
| 359 | option: RedNoteOptionSchema.describe('小红书发布选项'), |
| 360 | ...extraShape, |
| 361 | }), |
| 362 | z.object({ |
| 363 | platform: z.literal(AccountType.WeChatChannels).describe('平台'), |
| 364 | option: WeChatChannelsOptionSchema.optional().describe('微信视频号发布选项'), |
| 365 | ...extraShape, |
| 366 | }), |
| 367 | ]) |
| 368 | } |
| 369 | |
| 370 | export const PlatformPublishOptionItemSchema = createPlatformPublishOptionItemSchema({}) |
| 371 | |
| 372 | const topicPattern = /#([\w\p{Script=Han}]+)/gu |
| 373 | |
| 374 | export function parseTopicsFromBody(body?: string): string[] { |
| 375 | if (!body) |
| 376 | return [] |
| 377 | const topics: string[] = [] |
| 378 | for (const match of body.matchAll(topicPattern)) { |
| 379 | const topic = match[1] |
| 380 | if (topic && !topics.includes(topic)) { |
| 381 | topics.push(topic) |
| 382 | } |
| 383 | } |
| 384 | return topics |
| 385 | } |
| 386 | |
| 387 | export function stripTopicsFromBody(body?: string): string | undefined { |
| 388 | if (body === undefined) |
| 389 | return undefined |
| 390 | return body |
| 391 | .replace(topicPattern, '') |
| 392 | .replace(/[ \t]+/g, ' ') |
| 393 | .replace(/[ \t]+\n/g, '\n') |
| 394 | .replace(/\n[ \t]+/g, '\n') |
| 395 | .replace(/\n{3,}/g, '\n\n') |
| 396 | .trim() |
| 397 | } |
| 398 | |
| 399 | export function parseTopicInsertionsFromBody(body?: string): Array<{ name: string, start: number }> { |
| 400 | if (!body) |
| 401 | return [] |
| 402 | |
| 403 | const insertions: Array<{ name: string, start: number }> = [] |
| 404 | const seen = new Set<string>() |
| 405 | for (const match of body.matchAll(topicPattern)) { |
| 406 | const name = match[1] |
| 407 | if (!name || seen.has(name)) { |
| 408 | continue |
| 409 | } |
| 410 | seen.add(name) |
| 411 | insertions.push({ |
| 412 | name, |
| 413 | start: stripTopicsFromBody(body.slice(0, match.index))?.length ?? 0, |
| 414 | }) |
| 415 | } |
| 416 | return insertions |
| 417 | } |
| 418 | |
| 419 | export type PlatformPublishOptionItem = z.infer<typeof PlatformPublishOptionItemSchema> |
| 420 |