|
| 1 | +/* |
| 2 | +Copyright 2019 Tulir Asokan <[email protected]> |
| 3 | +Copyright 2020 The Matrix.org Foundation C.I.C. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +import SettingsStore, {SettingLevel} from "../settings/SettingsStore"; |
| 19 | +import {sortBy} from "lodash"; |
| 20 | + |
| 21 | +interface ILegacyFormat { |
| 22 | + [emoji: string]: [number, number]; // [count, date] |
| 23 | +} |
| 24 | + |
| 25 | +// New format tries to be more space efficient for synchronization. Ordered by Date descending. |
| 26 | +type Format = [string, number][]; // [emoji, count] |
| 27 | + |
| 28 | +const SETTING_NAME = "recent_emoji"; |
| 29 | + |
| 30 | +// we store more recents than we typically query but this lets us sort by weighted usage |
| 31 | +// even if you haven't used your typically favourite emoji for a little while. |
| 32 | +const STORAGE_LIMIT = 100; |
| 33 | + |
| 34 | +// TODO remove this after some time |
| 35 | +function migrate() { |
| 36 | + const data: ILegacyFormat = JSON.parse(window.localStorage.mx_reaction_count || '{}'); |
| 37 | + const sorted = Object.entries(data).sort(([, [count1, date1]], [, [count2, date2]]) => date2 - date1); |
| 38 | + const newFormat = sorted.map(([emoji, [count, date]]) => [emoji, count]); |
| 39 | + SettingsStore.setValue(SETTING_NAME, null, SettingLevel.ACCOUNT, newFormat.slice(0, STORAGE_LIMIT)); |
| 40 | +} |
| 41 | + |
| 42 | +function getRecentEmoji(): Format { |
| 43 | + return SettingsStore.getValue(SETTING_NAME) || []; |
| 44 | +} |
| 45 | + |
| 46 | +export function add(emoji: string) { |
| 47 | + const recents = getRecentEmoji(); |
| 48 | + const i = recents.findIndex(([e]) => e === emoji); |
| 49 | + |
| 50 | + let newEntry; |
| 51 | + if (i >= 0) { |
| 52 | + // first remove the existing tuple so that we can increment it and push it to the front |
| 53 | + [newEntry] = recents.splice(i, 1); |
| 54 | + newEntry[1]++; // increment the usage count |
| 55 | + } else { |
| 56 | + newEntry = [emoji, 1]; |
| 57 | + } |
| 58 | + |
| 59 | + SettingsStore.setValue(SETTING_NAME, null, SettingLevel.ACCOUNT, [newEntry, ...recents].slice(0, STORAGE_LIMIT)); |
| 60 | +} |
| 61 | + |
| 62 | +export function get(limit = 24) { |
| 63 | + let recents = getRecentEmoji(); |
| 64 | + |
| 65 | + if (recents.length < 1) { |
| 66 | + migrate(); |
| 67 | + recents = getRecentEmoji(); |
| 68 | + } |
| 69 | + |
| 70 | + // perform a stable sort on `count` to keep the recent (date) order as a secondary sort factor |
| 71 | + const sorted = sortBy(recents, "1"); |
| 72 | + return sorted.slice(0, limit).map(([emoji]) => emoji); |
| 73 | +} |
0 commit comments