summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/ReactionService.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/backend/src/core/ReactionService.ts')
-rw-r--r--packages/backend/src/core/ReactionService.ts49
1 files changed, 25 insertions, 24 deletions
diff --git a/packages/backend/src/core/ReactionService.ts b/packages/backend/src/core/ReactionService.ts
index 5014156a5c..cb0b079df0 100644
--- a/packages/backend/src/core/ReactionService.ts
+++ b/packages/backend/src/core/ReactionService.ts
@@ -322,35 +322,36 @@ export class ReactionService {
//#endregion
}
+ /**
+ * 文字列タイプのレガシーな形式のリアクションを現在の形式に変換しつつ、
+ * データベース上には存在する「0個のリアクションがついている」という情報を削除する。
+ */
@bindThis
- public convertLegacyReactions(reactions: Record<string, number>) {
- const _reactions = {} as Record<string, number>;
+ public convertLegacyReactions(reactions: MiNote['reactions']): MiNote['reactions'] {
+ return Object.entries(reactions)
+ .filter(([, count]) => {
+ // `ReactionService.prototype.delete`ではリアクション削除時に、
+ // `MiNote['reactions']`のエントリの値をデクリメントしているが、
+ // デクリメントしているだけなのでエントリ自体は0を値として持つ形で残り続ける。
+ // そのため、この処理がなければ、「0個のリアクションがついている」ということになってしまう。
+ return count > 0;
+ })
+ .map(([reaction, count]) => {
+ // unchecked indexed access
+ const convertedReaction = legacies[reaction] as string | undefined;
- for (const reaction of Object.keys(reactions)) {
- if (reactions[reaction] <= 0) continue;
+ const key = this.decodeReaction(convertedReaction ?? reaction).reaction;
- if (Object.keys(legacies).includes(reaction)) {
- if (_reactions[legacies[reaction]]) {
- _reactions[legacies[reaction]] += reactions[reaction];
- } else {
- _reactions[legacies[reaction]] = reactions[reaction];
- }
- } else {
- if (_reactions[reaction]) {
- _reactions[reaction] += reactions[reaction];
- } else {
- _reactions[reaction] = reactions[reaction];
- }
- }
- }
-
- const _reactions2 = {} as Record<string, number>;
+ return [key, count] as const;
+ })
+ .reduce<MiNote['reactions']>((acc, [key, count]) => {
+ // unchecked indexed access
+ const prevCount = acc[key] as number | undefined;
- for (const reaction of Object.keys(_reactions)) {
- _reactions2[this.decodeReaction(reaction).reaction] = _reactions[reaction];
- }
+ acc[key] = (prevCount ?? 0) + count;
- return _reactions2;
+ return acc;
+ }, {});
}
@bindThis