2020-05-22 04:52:10 +00:00
|
|
|
import {
|
|
|
|
Map as ImmutableMap,
|
|
|
|
List as ImmutableList,
|
|
|
|
} from 'immutable';
|
2020-05-20 16:46:49 +00:00
|
|
|
|
|
|
|
// https://emojipedia.org/facebook/
|
|
|
|
export const ALLOWED_EMOJI = [
|
|
|
|
'👍',
|
2020-05-20 20:52:28 +00:00
|
|
|
'❤',
|
2020-05-20 16:46:49 +00:00
|
|
|
'😂',
|
|
|
|
'😯',
|
|
|
|
'😢',
|
|
|
|
'😡',
|
|
|
|
];
|
|
|
|
|
|
|
|
export const sortEmoji = emojiReacts => (
|
|
|
|
emojiReacts.sortBy(emojiReact => -emojiReact.get('count'))
|
|
|
|
);
|
|
|
|
|
|
|
|
export const mergeEmoji = emojiReacts => (
|
|
|
|
emojiReacts // TODO: Merge similar emoji
|
|
|
|
);
|
|
|
|
|
|
|
|
export const mergeEmojiFavourites = (emojiReacts, favouritesCount) => {
|
2020-05-20 16:55:30 +00:00
|
|
|
if (!favouritesCount) return emojiReacts;
|
2020-05-20 16:46:49 +00:00
|
|
|
const likeIndex = emojiReacts.findIndex(emojiReact =>
|
|
|
|
emojiReact.get('name') === '👍');
|
|
|
|
if (likeIndex > -1) {
|
|
|
|
const likeCount = emojiReacts.getIn([likeIndex, 'count']);
|
|
|
|
return emojiReacts.setIn([likeIndex, 'count'], likeCount + favouritesCount);
|
|
|
|
} else {
|
|
|
|
return emojiReacts.push(ImmutableMap({ count: favouritesCount, me: false, name: '👍' }));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-22 19:08:38 +00:00
|
|
|
const hasMultiReactions = (emojiReacts, account) => (
|
|
|
|
emojiReacts.filter(
|
|
|
|
e => e.get('accounts').filter(
|
|
|
|
a => a.get('id') === account.get('id')
|
|
|
|
).count() > 0
|
|
|
|
).count() > 1
|
|
|
|
);
|
|
|
|
|
|
|
|
const inAccounts = (accounts, id) => (
|
|
|
|
accounts.filter(a => a.get('id') === id).count() > 0
|
|
|
|
);
|
|
|
|
|
|
|
|
export const oneEmojiPerAccount = (emojiReacts, me) => {
|
2020-05-22 04:52:10 +00:00
|
|
|
emojiReacts = emojiReacts.reverse();
|
2020-05-22 19:08:38 +00:00
|
|
|
|
|
|
|
return emojiReacts.reduce((acc, cur, idx) => {
|
|
|
|
const accounts = cur.get('accounts', ImmutableList())
|
|
|
|
.filter(a => !hasMultiReactions(acc, a));
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-05-20 16:46:49 +00:00
|
|
|
export const filterEmoji = emojiReacts => (
|
|
|
|
emojiReacts.filter(emojiReact => (
|
|
|
|
ALLOWED_EMOJI.includes(emojiReact.get('name'))
|
|
|
|
)));
|
|
|
|
|
2020-05-22 20:42:57 +00:00
|
|
|
export const reduceEmoji = (emojiReacts, favouritesCount, me) => (
|
2020-05-22 22:02:56 +00:00
|
|
|
filterEmoji(sortEmoji(mergeEmoji(mergeEmojiFavourites(
|
|
|
|
emojiReacts, favouritesCount
|
|
|
|
)))));
|
2020-05-22 04:17:11 +00:00
|
|
|
|
|
|
|
export const getReactForStatus = status => {
|
2020-05-22 04:52:10 +00:00
|
|
|
const emojiReacts = status.getIn(['pleroma', 'emoji_reactions'], ImmutableList());
|
|
|
|
const emojiReact = emojiReacts.reduce((acc, cur) => {
|
|
|
|
if (acc) return acc;
|
|
|
|
if (cur.get('me') === true) return cur.get('name');
|
|
|
|
return acc;
|
2020-05-22 20:42:57 +00:00
|
|
|
}, undefined);
|
2020-05-22 04:52:10 +00:00
|
|
|
return emojiReact ? emojiReact : status.get('favourited') && '👍';
|
2020-05-22 04:17:11 +00:00
|
|
|
};
|