soapbox/app/gabsocial/utils/emoji_reacts.js

65 wiersze
1.9 KiB
JavaScript
Czysty Zwykły widok Historia

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) => {
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 04:17:11 +00:00
export const oneEmojiPerAccount = emojiReacts => {
emojiReacts = emojiReacts.reverse();
return emojiReacts.reduce((acc, cur) => {
if (acc.filter(
e => e.get('me') && e.get('name') !== cur.get('name')
).length > 0) return acc.delete(cur); // FIXME: Incomplete
return acc;
}, emojiReacts).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'))
)));
export const reduceEmoji = (emojiReacts, favouritesCount) => (
sortEmoji(filterEmoji(mergeEmoji(mergeEmojiFavourites(
emojiReacts, favouritesCount
)))));
2020-05-22 04:17:11 +00:00
export const getReactForStatus = status => {
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;
}, false);
return emojiReact ? emojiReact : status.get('favourited') && '👍';
2020-05-22 04:17:11 +00:00
};