refactor: 💥 properly deprecate legacy reactions
This commit is contained in:
parent
ecb0956d20
commit
bbb2396d89
3 changed files with 17 additions and 57 deletions
|
@ -4,58 +4,22 @@ import { Emojis } from "@/models/index.js";
|
||||||
import { toPunyNullable } from "./convert-host.js";
|
import { toPunyNullable } from "./convert-host.js";
|
||||||
import { IsNull } from "typeorm";
|
import { IsNull } from "typeorm";
|
||||||
|
|
||||||
const legacies = new Map([
|
|
||||||
["like", "👍"],
|
|
||||||
["love", "❤️"],
|
|
||||||
["laugh", "😆"],
|
|
||||||
["hmm", "🤔"],
|
|
||||||
["surprise", "😮"],
|
|
||||||
["congrats", "🎉"],
|
|
||||||
["angry", "💢"],
|
|
||||||
["confused", "😥"],
|
|
||||||
["rip", "😇"],
|
|
||||||
["pudding", "🍮"],
|
|
||||||
["star", "⭐"],
|
|
||||||
]);
|
|
||||||
|
|
||||||
export async function getFallbackReaction() {
|
export async function getFallbackReaction() {
|
||||||
const meta = await fetchMeta();
|
const meta = await fetchMeta();
|
||||||
return meta.defaultReaction;
|
return meta.defaultReaction;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function convertLegacyReactions(reactions: Record<string, number>) {
|
export function convertReactions(reactions: Record<string, number>) {
|
||||||
const _reactions = new Map();
|
const result = new Map();
|
||||||
const decodedReactions = new Map();
|
|
||||||
|
|
||||||
for (const reaction in reactions) {
|
for (const reaction in reactions) {
|
||||||
if (reactions[reaction] <= 0) continue;
|
if (reactions[reaction] <= 0) continue;
|
||||||
|
|
||||||
let decodedReaction;
|
const decoded = decodeReaction(reaction).reaction;
|
||||||
if (decodedReactions.has(reaction)) {
|
result.set(decoded, (result.get(decoded) || 0) + reactions[reaction]);
|
||||||
decodedReaction = decodedReactions.get(reaction);
|
|
||||||
} else {
|
|
||||||
decodedReaction = decodeReaction(reaction);
|
|
||||||
decodedReactions.set(reaction, decodedReaction);
|
|
||||||
}
|
|
||||||
|
|
||||||
let emoji = legacies.get(decodedReaction.reaction);
|
|
||||||
if (emoji) {
|
|
||||||
_reactions.set(emoji, (_reactions.get(emoji) || 0) + reactions[reaction]);
|
|
||||||
} else {
|
|
||||||
_reactions.set(
|
|
||||||
reaction,
|
|
||||||
(_reactions.get(reaction) || 0) + reactions[reaction],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const _reactions2 = new Map();
|
return Object.fromEntries(result);
|
||||||
for (const [reaction, count] of _reactions) {
|
|
||||||
const decodedReaction = decodedReactions.get(reaction);
|
|
||||||
_reactions2.set(decodedReaction.reaction, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Object.fromEntries(_reactions2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function toDbReaction(
|
export async function toDbReaction(
|
||||||
|
@ -66,9 +30,7 @@ export async function toDbReaction(
|
||||||
|
|
||||||
reacterHost = toPunyNullable(reacterHost);
|
reacterHost = toPunyNullable(reacterHost);
|
||||||
|
|
||||||
// Convert string-type reactions to unicode
|
if (reaction === "♥️") return "❤️";
|
||||||
const emoji = legacies.get(reaction) || (reaction === "♥️" ? "❤️" : null);
|
|
||||||
if (emoji) return emoji;
|
|
||||||
|
|
||||||
// Allow unicode reactions
|
// Allow unicode reactions
|
||||||
const match = emojiRegex.exec(reaction);
|
const match = emojiRegex.exec(reaction);
|
||||||
|
@ -129,8 +91,6 @@ export function decodeReaction(str: string): DecodedReaction {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function convertLegacyReaction(reaction: string): string {
|
export function convertToDecoded(reaction: string): string {
|
||||||
const decoded = decodeReaction(reaction).reaction;
|
return decodeReaction(reaction).reaction;
|
||||||
if (legacies.has(decoded)) return legacies.get(decoded)!;
|
|
||||||
return decoded;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { db } from "@/db/postgre.js";
|
||||||
import { NoteReaction } from "@/models/entities/note-reaction.js";
|
import { NoteReaction } from "@/models/entities/note-reaction.js";
|
||||||
import { Notes, Users } from "../index.js";
|
import { Notes, Users } from "../index.js";
|
||||||
import type { Packed } from "@/misc/schema.js";
|
import type { Packed } from "@/misc/schema.js";
|
||||||
import { convertLegacyReaction } from "@/misc/reaction-lib.js";
|
import { convertToDecoded } from "@/misc/reaction-lib.js";
|
||||||
import type { User } from "@/models/entities/user.js";
|
import type { User } from "@/models/entities/user.js";
|
||||||
|
|
||||||
export const NoteReactionRepository = db.getRepository(NoteReaction).extend({
|
export const NoteReactionRepository = db.getRepository(NoteReaction).extend({
|
||||||
|
@ -27,7 +27,7 @@ export const NoteReactionRepository = db.getRepository(NoteReaction).extend({
|
||||||
id: reaction.id,
|
id: reaction.id,
|
||||||
createdAt: reaction.createdAt.toISOString(),
|
createdAt: reaction.createdAt.toISOString(),
|
||||||
user: await Users.pack(reaction.user ?? reaction.userId, me),
|
user: await Users.pack(reaction.user ?? reaction.userId, me),
|
||||||
type: convertLegacyReaction(reaction.reaction),
|
type: convertToDecoded(reaction.reaction),
|
||||||
...(opts.withNote
|
...(opts.withNote
|
||||||
? {
|
? {
|
||||||
// may throw error
|
// may throw error
|
||||||
|
@ -41,7 +41,7 @@ export const NoteReactionRepository = db.getRepository(NoteReaction).extend({
|
||||||
src: NoteReaction[],
|
src: NoteReaction[],
|
||||||
me?: { id: User["id"] } | null | undefined,
|
me?: { id: User["id"] } | null | undefined,
|
||||||
options?: {
|
options?: {
|
||||||
withNote: booleam;
|
withNote: boolean;
|
||||||
},
|
},
|
||||||
): Promise<Packed<"NoteReaction">[]> {
|
): Promise<Packed<"NoteReaction">[]> {
|
||||||
const reactions = await Promise.allSettled(
|
const reactions = await Promise.allSettled(
|
||||||
|
|
|
@ -15,8 +15,8 @@ import type { Packed } from "@/misc/schema.js";
|
||||||
import { nyaize } from "@/misc/nyaize.js";
|
import { nyaize } from "@/misc/nyaize.js";
|
||||||
import { awaitAll } from "@/prelude/await-all.js";
|
import { awaitAll } from "@/prelude/await-all.js";
|
||||||
import {
|
import {
|
||||||
convertLegacyReaction,
|
convertToDecoded,
|
||||||
convertLegacyReactions,
|
convertReactions,
|
||||||
decodeReaction,
|
decodeReaction,
|
||||||
} from "@/misc/reaction-lib.js";
|
} from "@/misc/reaction-lib.js";
|
||||||
import type { NoteReaction } from "@/models/entities/note-reaction.js";
|
import type { NoteReaction } from "@/models/entities/note-reaction.js";
|
||||||
|
@ -77,7 +77,7 @@ async function populateMyReaction(
|
||||||
if (_hint_?.myReactions) {
|
if (_hint_?.myReactions) {
|
||||||
const reaction = _hint_.myReactions.get(note.id);
|
const reaction = _hint_.myReactions.get(note.id);
|
||||||
if (reaction) {
|
if (reaction) {
|
||||||
return convertLegacyReaction(reaction.reaction);
|
return convertToDecoded(reaction.reaction);
|
||||||
} else if (reaction === null) {
|
} else if (reaction === null) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ async function populateMyReaction(
|
||||||
});
|
});
|
||||||
|
|
||||||
if (reaction) {
|
if (reaction) {
|
||||||
return convertLegacyReaction(reaction.reaction);
|
return convertToDecoded(reaction.reaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -221,7 +221,7 @@ export const NoteRepository = db.getRepository(Note).extend({
|
||||||
note.visibility === "specified" ? note.visibleUserIds : undefined,
|
note.visibility === "specified" ? note.visibleUserIds : undefined,
|
||||||
renoteCount: note.renoteCount,
|
renoteCount: note.renoteCount,
|
||||||
repliesCount: note.repliesCount,
|
repliesCount: note.repliesCount,
|
||||||
reactions: convertLegacyReactions(note.reactions),
|
reactions: convertReactions(note.reactions),
|
||||||
reactionEmojis: reactionEmoji,
|
reactionEmojis: reactionEmoji,
|
||||||
emojis: noteEmoji,
|
emojis: noteEmoji,
|
||||||
tags: note.tags.length > 0 ? note.tags : undefined,
|
tags: note.tags.length > 0 ? note.tags : undefined,
|
||||||
|
|
Loading…
Reference in a new issue