soapbox/app/soapbox/utils/emoji-reacts.ts

130 wiersze
4.1 KiB
TypeScript
Czysty Zwykły widok Historia

import {
Map as ImmutableMap,
List as ImmutableList,
} from 'immutable';
2020-05-20 16:46:49 +00:00
2022-04-01 23:39:27 +00:00
import type { Me } from 'soapbox/types/soapbox';
2020-05-23 04:29:30 +00:00
// https://emojipedia.org/facebook
// I've customized them.
2022-04-01 23:39:27 +00:00
export const ALLOWED_EMOJI = ImmutableList([
2020-05-20 16:46:49 +00:00
'👍',
'❤️',
2020-05-23 05:04:25 +00:00
'😆',
2020-05-23 04:29:30 +00:00
'😮',
2020-05-20 16:46:49 +00:00
'😢',
2020-05-23 04:29:30 +00:00
'😩',
2022-04-01 23:39:27 +00:00
]);
type Account = ImmutableMap<string, any>;
type EmojiReact = ImmutableMap<string, any>;
2020-05-20 16:46:49 +00:00
2022-04-01 23:39:27 +00:00
export const sortEmoji = (emojiReacts: ImmutableList<EmojiReact>): ImmutableList<EmojiReact> => (
2020-05-20 16:46:49 +00:00
emojiReacts.sortBy(emojiReact => -emojiReact.get('count'))
);
2022-04-01 23:39:27 +00:00
export const mergeEmoji = (emojiReacts: ImmutableList<EmojiReact>): ImmutableList<EmojiReact> => (
2020-05-20 16:46:49 +00:00
emojiReacts // TODO: Merge similar emoji
);
2022-04-01 23:39:27 +00:00
export const mergeEmojiFavourites = (emojiReacts = ImmutableList<EmojiReact>(), favouritesCount: number, favourited: boolean) => {
if (!favouritesCount) return emojiReacts;
const likeIndex = emojiReacts.findIndex(emojiReact => emojiReact.get('name') === '👍');
2020-05-20 16:46:49 +00:00
if (likeIndex > -1) {
2022-04-01 23:39:27 +00:00
const likeCount = Number(emojiReacts.getIn([likeIndex, 'count']));
favourited = favourited || Boolean(emojiReacts.getIn([likeIndex, 'me'], false));
return emojiReacts
.setIn([likeIndex, 'count'], likeCount + favouritesCount)
.setIn([likeIndex, 'me'], favourited);
2020-05-20 16:46:49 +00:00
} else {
return emojiReacts.push(ImmutableMap({ count: favouritesCount, me: favourited, name: '👍' }));
2020-05-20 16:46:49 +00:00
}
};
2022-04-01 23:39:27 +00:00
const hasMultiReactions = (emojiReacts: ImmutableList<EmojiReact>, account: Account): boolean => (
2020-05-22 19:08:38 +00:00
emojiReacts.filter(
e => e.get('accounts').filter(
2022-04-01 23:39:27 +00:00
(a: Account) => a.get('id') === account.get('id'),
).count() > 0,
2020-05-22 19:08:38 +00:00
).count() > 1
);
2022-04-01 23:39:27 +00:00
const inAccounts = (accounts: ImmutableList<Account>, id: string): boolean => (
2020-05-22 19:08:38 +00:00
accounts.filter(a => a.get('id') === id).count() > 0
);
2022-04-01 23:39:27 +00:00
export const oneEmojiPerAccount = (emojiReacts: ImmutableList<EmojiReact>, me: Me) => {
emojiReacts = emojiReacts.reverse();
2020-05-22 19:08:38 +00:00
return emojiReacts.reduce((acc, cur, idx) => {
const accounts = cur.get('accounts', ImmutableList())
2022-04-01 23:39:27 +00:00
.filter((a: Account) => !hasMultiReactions(acc, a));
2020-05-22 19:08:38 +00:00
return acc.set(idx, cur.merge({
accounts: accounts,
count: accounts.count(),
2020-05-22 20:42:57 +00:00
me: me ? inAccounts(accounts, me) : false,
2020-05-22 19:08:38 +00:00
}));
}, emojiReacts)
.filter(e => e.get('count') > 0)
.reverse();
2020-05-22 04:17:11 +00:00
};
export const filterEmoji = (emojiReacts: ImmutableList<EmojiReact>, allowedEmoji = ALLOWED_EMOJI): ImmutableList<EmojiReact> => (
2020-05-20 16:46:49 +00:00
emojiReacts.filter(emojiReact => (
allowedEmoji.includes(emojiReact.get('name'))
2020-05-20 16:46:49 +00:00
)));
export const reduceEmoji = (emojiReacts: ImmutableList<EmojiReact>, favouritesCount: number, favourited: boolean, allowedEmoji = ALLOWED_EMOJI): ImmutableList<EmojiReact> => (
filterEmoji(sortEmoji(mergeEmoji(mergeEmojiFavourites(
emojiReacts, favouritesCount, favourited,
))), allowedEmoji));
2020-05-22 04:17:11 +00:00
export const getReactForStatus = (status: any, allowedEmoji = ALLOWED_EMOJI): string | undefined => {
2022-04-03 02:40:47 +00:00
const result = reduceEmoji(
status.pleroma.get('emoji_reactions', ImmutableList()),
status.favourites_count || 0,
status.favourited,
2020-12-24 20:40:47 +00:00
allowedEmoji,
2020-05-22 23:44:24 +00:00
).filter(e => e.get('me') === true)
2022-04-03 02:40:47 +00:00
.getIn([0, 'name']);
return typeof result === 'string' ? result : undefined;
2020-05-22 04:17:11 +00:00
};
2020-05-24 01:29:25 +00:00
2022-04-01 23:39:27 +00:00
export const simulateEmojiReact = (emojiReacts: ImmutableList<EmojiReact>, emoji: string) => {
2020-05-24 01:29:25 +00:00
const idx = emojiReacts.findIndex(e => e.get('name') === emoji);
2022-04-01 23:39:27 +00:00
const emojiReact = emojiReacts.get(idx);
2022-04-03 02:40:47 +00:00
if (idx > -1 && emojiReact) {
2020-05-24 01:29:25 +00:00
return emojiReacts.set(idx, emojiReact.merge({
count: emojiReact.get('count') + 1,
me: true,
}));
} else {
return emojiReacts.push(ImmutableMap({
count: 1,
me: true,
name: emoji,
}));
}
};
2022-04-01 23:39:27 +00:00
export const simulateUnEmojiReact = (emojiReacts: ImmutableList<EmojiReact>, emoji: string) => {
const idx = emojiReacts.findIndex(e =>
e.get('name') === emoji && e.get('me') === true);
2022-04-01 23:39:27 +00:00
const emojiReact = emojiReacts.get(idx);
if (emojiReact) {
const newCount = emojiReact.get('count') - 1;
if (newCount < 1) return emojiReacts.delete(idx);
return emojiReacts.set(idx, emojiReact.merge({
count: emojiReact.get('count') - 1,
me: false,
}));
} else {
return emojiReacts;
}
};