From abcaed23d4dabf8bb7ed1275732cb492a9cc9547 Mon Sep 17 00:00:00 2001 From: naskya Date: Fri, 29 Dec 2023 00:28:45 +0900 Subject: [PATCH] feat: randomly add miao at end of the post in Chinese cat mode --- README.md | 1 + packages/backend/src/misc/nyaize.ts | 7 +++++- .../backend/src/models/repositories/note.ts | 23 +++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 17855aeb..13d77c89 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ ## 細かい変更点 +- 中国語の猫モードでは 0.1 の確率で投稿の末尾に「喵」を追加するように - 設定のバックアップファイルに `misskeyVersion` の値が含まれていなくても警告しないように変更 - マージされていない本家版へのプルリクエストを独断でマージ - RTL Layout Support ([!10452](https://git.joinfirefish.org/firefish/firefish/-/merge_requests/10452)) diff --git a/packages/backend/src/misc/nyaize.ts b/packages/backend/src/misc/nyaize.ts index 32c54fbd..d9d5c346 100644 --- a/packages/backend/src/misc/nyaize.ts +++ b/packages/backend/src/misc/nyaize.ts @@ -1,4 +1,8 @@ -export function nyaize(text: string, lang?: string): string { +export function nyaize( + text: string, + lang: string | undefined, + appendMiao: boolean, +): string { text = text // ja-JP .replaceAll("な", "にゃ") @@ -26,6 +30,7 @@ export function nyaize(text: string, lang?: string): string { // zh-CN, zh-TW if (lang === "zh") text = text.replace(/(妙|庙|描|渺|瞄|秒|苗|藐|廟)/g, "喵"); + if (appendMiao) text += "喵"; return text; } diff --git a/packages/backend/src/models/repositories/note.ts b/packages/backend/src/models/repositories/note.ts index 60e5a8d5..0a61eae8 100644 --- a/packages/backend/src/models/repositories/note.ts +++ b/packages/backend/src/models/repositories/note.ts @@ -23,6 +23,7 @@ import { } from "@/misc/populate-emojis.js"; import { db } from "@/db/postgre.js"; import { IdentifiableError } from "@/misc/identifiable-error.js"; +import rng from "random-seed"; export async function populatePoll(note: Note, meId: User["id"] | null) { const poll = await Polls.findOneByOrFail({ noteId: note.id }); @@ -260,12 +261,30 @@ export const NoteRepository = db.getRepository(Note).extend({ lang: note.lang, }); - if (packed.user.isCat && packed.user.speakAsCat && packed.text) { + const CHINESE_APPEND_MIAO_PROBABILITY = 0.1; + const shouldAppendMiao = (() => { + if (packed.lang?.startsWith("zh")) { + let seed: string | null = null; + if (packed.url != null) seed = packed.url.split("/").pop() ?? null; + if (packed.uri != null) seed = packed.uri.split("/").pop() ?? null; + if (seed == null) seed = packed.id; + + const rand = rng.create(seed); + return rand.random() <= CHINESE_APPEND_MIAO_PROBABILITY; + } + return false; + })(); + + if (packed.user.isCat && packed.user.speakAsCat && packed.text != null) { const tokens = packed.text ? mfm.parse(packed.text) : []; function nyaizeNode(node: mfm.MfmNode) { if (node.type === "quote") return; if (node.type === "text") - node.props.text = nyaize(node.props.text, packed.lang); + node.props.text = nyaize( + node.props.text, + packed.lang, + shouldAppendMiao, + ); if (node.children) { for (const child of node.children) {