From b23b3e4d2102c274e4e1dd4bdc4ca6cfec42c354 Mon Sep 17 00:00:00 2001
From: syuilo <Syuilotan@yahoo.co.jp>
Date: Wed, 19 Feb 2020 06:36:50 +0900
Subject: [PATCH] Fix #5984

---
 src/misc/reaction-lib.ts        | 22 ++++++++++++++++++++++
 src/models/repositories/note.ts |  4 ++--
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/src/misc/reaction-lib.ts b/src/misc/reaction-lib.ts
index eba57ea303..c525d89385 100644
--- a/src/misc/reaction-lib.ts
+++ b/src/misc/reaction-lib.ts
@@ -20,6 +20,28 @@ export async function getFallbackReaction(): Promise<string> {
 	return meta.useStarForReactionFallback ? '⭐' : '👍';
 }
 
+export function convertLegacyReactions(reactions: Record<string, number>) {
+	const _reactions = {} as Record<string, number>;
+
+	for (const reaction of Object.keys(reactions)) {
+		if (Object.keys(legacy10).includes(reaction)) {
+			if (_reactions[legacy10[reaction]]) {
+				_reactions[legacy10[reaction]] += reactions[reaction];
+			} else {
+				_reactions[legacy10[reaction]] = reactions[reaction];
+			}
+		} else {
+			if (_reactions[reaction]) {
+				_reactions[reaction] += reactions[reaction];
+			} else {
+				_reactions[reaction] = reactions[reaction];
+			}
+		}
+	}
+
+	return _reactions;
+}
+
 export async function toDbReaction(reaction?: string | null): Promise<string> {
 	if (reaction == null) return await getFallbackReaction();
 
diff --git a/src/models/repositories/note.ts b/src/models/repositories/note.ts
index 59ec63b45c..cc856f2ba1 100644
--- a/src/models/repositories/note.ts
+++ b/src/models/repositories/note.ts
@@ -6,7 +6,7 @@ import { Emojis, Users, PollVotes, DriveFiles, NoteReactions, Followings, Polls
 import { ensure } from '../../prelude/ensure';
 import { SchemaType } from '../../misc/schema';
 import { awaitAll } from '../../prelude/await-all';
-import { convertLegacyReaction } from '../../misc/reaction-lib';
+import { convertLegacyReaction, convertLegacyReactions } from '../../misc/reaction-lib';
 
 export type PackedNote = SchemaType<typeof packedNoteSchema>;
 
@@ -187,7 +187,7 @@ export class NoteRepository extends Repository<Note> {
 			viaMobile: note.viaMobile || undefined,
 			renoteCount: note.renoteCount,
 			repliesCount: note.repliesCount,
-			reactions: note.reactions, // v12 TODO: convert legacy reaction
+			reactions: convertLegacyReactions(note.reactions),
 			tags: note.tags.length > 0 ? note.tags : undefined,
 			emojis: populateEmojis(note.emojis, host, Object.keys(note.reactions)),
 			fileIds: note.fileIds,