apply nyaification to Mastodon API response
This commit is contained in:
parent
c936e694de
commit
e6d5720634
1 changed files with 41 additions and 11 deletions
|
@ -20,8 +20,9 @@ import {
|
||||||
Notes,
|
Notes,
|
||||||
NoteThreadMutings,
|
NoteThreadMutings,
|
||||||
UserNotePinings,
|
UserNotePinings,
|
||||||
|
Users,
|
||||||
} from "@/models/index.js";
|
} from "@/models/index.js";
|
||||||
import { decodeReaction, isQuote } from "backend-rs";
|
import { decodeReaction, isQuote, nyaify } from "backend-rs";
|
||||||
import { MentionConverter } from "@/server/api/mastodon/converters/mention.js";
|
import { MentionConverter } from "@/server/api/mastodon/converters/mention.js";
|
||||||
import { PollConverter } from "@/server/api/mastodon/converters/poll.js";
|
import { PollConverter } from "@/server/api/mastodon/converters/poll.js";
|
||||||
import { populatePoll } from "@/models/repositories/note.js";
|
import { populatePoll } from "@/models/repositories/note.js";
|
||||||
|
@ -50,6 +51,27 @@ export class NoteConverter {
|
||||||
"note:card",
|
"note:card",
|
||||||
60 * 60,
|
60 * 60,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
private static applyNyaification(text: string, lang?: string) {
|
||||||
|
if (text === "") return "";
|
||||||
|
|
||||||
|
function nyaifyNode(node: mfm.MfmNode) {
|
||||||
|
if (node.type === "quote") return;
|
||||||
|
if (node.type === "text") node.props.text = nyaify(node.props.text, lang);
|
||||||
|
|
||||||
|
if (node.children) {
|
||||||
|
for (const child of node.children) {
|
||||||
|
nyaifyNode(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const tokens = mfm.parse(text);
|
||||||
|
for (const node of tokens) nyaifyNode(node);
|
||||||
|
|
||||||
|
return mfm.toString(tokens);
|
||||||
|
}
|
||||||
|
|
||||||
public static async encode(
|
public static async encode(
|
||||||
note: Note,
|
note: Note,
|
||||||
ctx: MastoContext,
|
ctx: MastoContext,
|
||||||
|
@ -163,7 +185,8 @@ export class NoteConverter {
|
||||||
|
|
||||||
const identifier = `${note.id}:${(note.updatedAt ?? note.createdAt).getTime()}`;
|
const identifier = `${note.id}:${(note.updatedAt ?? note.createdAt).getTime()}`;
|
||||||
|
|
||||||
const text = quoteUri.then((quoteUri) =>
|
const text = quoteUri
|
||||||
|
.then((quoteUri) =>
|
||||||
note.text !== null
|
note.text !== null
|
||||||
? quoteUri !== null
|
? quoteUri !== null
|
||||||
? note.text
|
? note.text
|
||||||
|
@ -172,7 +195,14 @@ export class NoteConverter {
|
||||||
.trimEnd()
|
.trimEnd()
|
||||||
: note.text
|
: note.text
|
||||||
: null,
|
: null,
|
||||||
);
|
)
|
||||||
|
.then(async (text) => {
|
||||||
|
const user = await Users.findOneBy({ id: note.userId });
|
||||||
|
if (user?.isCat && user?.speakAsCat) {
|
||||||
|
return this.applyNyaification(text, note.lang);
|
||||||
|
}
|
||||||
|
return text;
|
||||||
|
});
|
||||||
|
|
||||||
const content = this.noteContentHtmlCache
|
const content = this.noteContentHtmlCache
|
||||||
.fetch(
|
.fetch(
|
||||||
|
|
Loading…
Reference in a new issue