refactor: 💥 properly deprecate legacy reactions

This commit is contained in:
ThatOneCalculator 2023-09-20 22:48:50 -07:00
parent ecb0956d20
commit bbb2396d89
No known key found for this signature in database
GPG Key ID: 8703CACD01000000
3 changed files with 17 additions and 57 deletions

View File

@ -4,58 +4,22 @@ import { Emojis } from "@/models/index.js";
import { toPunyNullable } from "./convert-host.js";
import { IsNull } from "typeorm";
const legacies = new Map([
["like", "👍"],
["love", "❤️"],
["laugh", "😆"],
["hmm", "🤔"],
["surprise", "😮"],
["congrats", "🎉"],
["angry", "💢"],
["confused", "😥"],
["rip", "😇"],
["pudding", "🍮"],
["star", "⭐"],
]);
export async function getFallbackReaction() {
const meta = await fetchMeta();
return meta.defaultReaction;
}
export function convertLegacyReactions(reactions: Record<string, number>) {
const _reactions = new Map();
const decodedReactions = new Map();
export function convertReactions(reactions: Record<string, number>) {
const result = new Map();
for (const reaction in reactions) {
if (reactions[reaction] <= 0) continue;
let decodedReaction;
if (decodedReactions.has(reaction)) {
decodedReaction = decodedReactions.get(reaction);
} else {
decodedReaction = decodeReaction(reaction);
decodedReactions.set(reaction, decodedReaction);
const decoded = decodeReaction(reaction).reaction;
result.set(decoded, (result.get(decoded) || 0) + reactions[reaction]);
}
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();
for (const [reaction, count] of _reactions) {
const decodedReaction = decodedReactions.get(reaction);
_reactions2.set(decodedReaction.reaction, count);
}
return Object.fromEntries(_reactions2);
return Object.fromEntries(result);
}
export async function toDbReaction(
@ -66,9 +30,7 @@ export async function toDbReaction(
reacterHost = toPunyNullable(reacterHost);
// Convert string-type reactions to unicode
const emoji = legacies.get(reaction) || (reaction === "♥️" ? "❤️" : null);
if (emoji) return emoji;
if (reaction === "♥️") return "❤️";
// Allow unicode reactions
const match = emojiRegex.exec(reaction);
@ -129,8 +91,6 @@ export function decodeReaction(str: string): DecodedReaction {
};
}
export function convertLegacyReaction(reaction: string): string {
const decoded = decodeReaction(reaction).reaction;
if (legacies.has(decoded)) return legacies.get(decoded)!;
return decoded;
export function convertToDecoded(reaction: string): string {
return decodeReaction(reaction).reaction;
}

View File

@ -2,7 +2,7 @@ import { db } from "@/db/postgre.js";
import { NoteReaction } from "@/models/entities/note-reaction.js";
import { Notes, Users } from "../index.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";
export const NoteReactionRepository = db.getRepository(NoteReaction).extend({
@ -27,7 +27,7 @@ export const NoteReactionRepository = db.getRepository(NoteReaction).extend({
id: reaction.id,
createdAt: reaction.createdAt.toISOString(),
user: await Users.pack(reaction.user ?? reaction.userId, me),
type: convertLegacyReaction(reaction.reaction),
type: convertToDecoded(reaction.reaction),
...(opts.withNote
? {
// may throw error
@ -41,7 +41,7 @@ export const NoteReactionRepository = db.getRepository(NoteReaction).extend({
src: NoteReaction[],
me?: { id: User["id"] } | null | undefined,
options?: {
withNote: booleam;
withNote: boolean;
},
): Promise<Packed<"NoteReaction">[]> {
const reactions = await Promise.allSettled(

View File

@ -15,8 +15,8 @@ import type { Packed } from "@/misc/schema.js";
import { nyaize } from "@/misc/nyaize.js";
import { awaitAll } from "@/prelude/await-all.js";
import {
convertLegacyReaction,
convertLegacyReactions,
convertToDecoded,
convertReactions,
decodeReaction,
} from "@/misc/reaction-lib.js";
import type { NoteReaction } from "@/models/entities/note-reaction.js";
@ -77,7 +77,7 @@ async function populateMyReaction(
if (_hint_?.myReactions) {
const reaction = _hint_.myReactions.get(note.id);
if (reaction) {
return convertLegacyReaction(reaction.reaction);
return convertToDecoded(reaction.reaction);
} else if (reaction === null) {
return undefined;
}
@ -90,7 +90,7 @@ async function populateMyReaction(
});
if (reaction) {
return convertLegacyReaction(reaction.reaction);
return convertToDecoded(reaction.reaction);
}
return undefined;
@ -221,7 +221,7 @@ export const NoteRepository = db.getRepository(Note).extend({
note.visibility === "specified" ? note.visibleUserIds : undefined,
renoteCount: note.renoteCount,
repliesCount: note.repliesCount,
reactions: convertLegacyReactions(note.reactions),
reactions: convertReactions(note.reactions),
reactionEmojis: reactionEmoji,
emojis: noteEmoji,
tags: note.tags.length > 0 ? note.tags : undefined,