bigbuffet-rw/app/soapbox/utils/emoji_reacts.js

121 lines
3.4 KiB
JavaScript
Raw Normal View History

import {
Map as ImmutableMap,
List as ImmutableList,
} from 'immutable';
2020-05-20 09:46:49 -07:00
2020-05-22 21:29:30 -07:00
// https://emojipedia.org/facebook
// I've customized them.
2020-05-20 09:46:49 -07:00
export const ALLOWED_EMOJI = [
'👍',
'❤️',
2020-05-22 22:04:25 -07:00
'😆',
2020-05-22 21:29:30 -07:00
'😮',
2020-05-20 09:46:49 -07:00
'😢',
2020-05-22 21:29:30 -07:00
'😩',
2020-05-20 09:46:49 -07: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, favourited) => {
if (!favouritesCount) return emojiReacts;
const likeIndex = emojiReacts.findIndex(emojiReact => emojiReact.get('name') === '👍');
2020-05-20 09:46:49 -07:00
if (likeIndex > -1) {
const likeCount = emojiReacts.getIn([likeIndex, 'count']);
favourited = favourited || emojiReacts.getIn([likeIndex, 'me'], false);
return emojiReacts
.setIn([likeIndex, 'count'], likeCount + favouritesCount)
.setIn([likeIndex, 'me'], favourited);
2020-05-20 09:46:49 -07:00
} else {
return emojiReacts.push(ImmutableMap({ count: favouritesCount, me: favourited, name: '👍' }));
2020-05-20 09:46:49 -07:00
}
};
2020-05-22 12:08:38 -07:00
const hasMultiReactions = (emojiReacts, account) => (
emojiReacts.filter(
e => e.get('accounts').filter(
a => a.get('id') === account.get('id'),
).count() > 0,
2020-05-22 12:08:38 -07:00
).count() > 1
);
const inAccounts = (accounts, id) => (
accounts.filter(a => a.get('id') === id).count() > 0
);
export const oneEmojiPerAccount = (emojiReacts, me) => {
emojiReacts = emojiReacts.reverse();
2020-05-22 12:08:38 -07: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 13:42:57 -07:00
me: me ? inAccounts(accounts, me) : false,
2020-05-22 12:08:38 -07:00
}));
}, emojiReacts)
.filter(e => e.get('count') > 0)
.reverse();
2020-05-21 21:17:11 -07:00
};
export const filterEmoji = (emojiReacts, allowedEmoji=ALLOWED_EMOJI) => (
2020-05-20 09:46:49 -07:00
emojiReacts.filter(emojiReact => (
allowedEmoji.includes(emojiReact.get('name'))
2020-05-20 09:46:49 -07:00
)));
export const reduceEmoji = (emojiReacts, favouritesCount, favourited, allowedEmoji=ALLOWED_EMOJI) => (
filterEmoji(sortEmoji(mergeEmoji(mergeEmojiFavourites(
emojiReacts, favouritesCount, favourited,
))), allowedEmoji));
2020-05-21 21:17:11 -07:00
2020-12-24 12:40:47 -08:00
export const getReactForStatus = (status, allowedEmoji=ALLOWED_EMOJI) => {
return reduceEmoji(
status.getIn(['pleroma', 'emoji_reactions'], ImmutableList()),
2020-12-24 12:40:47 -08:00
status.get('favourites_count', 0),
status.get('favourited'),
2020-12-24 12:40:47 -08:00
allowedEmoji,
2020-05-22 16:44:24 -07:00
).filter(e => e.get('me') === true)
.getIn([0, 'name']);
2020-05-21 21:17:11 -07:00
};
2020-05-23 18:29:25 -07:00
export const simulateEmojiReact = (emojiReacts, emoji) => {
const idx = emojiReacts.findIndex(e => e.get('name') === emoji);
if (idx > -1) {
const emojiReact = emojiReacts.get(idx);
return emojiReacts.set(idx, emojiReact.merge({
count: emojiReact.get('count') + 1,
me: true,
}));
} else {
return emojiReacts.push(ImmutableMap({
count: 1,
me: true,
name: emoji,
}));
}
};
export const simulateUnEmojiReact = (emojiReacts, emoji) => {
const idx = emojiReacts.findIndex(e =>
e.get('name') === emoji && e.get('me') === true);
if (idx > -1) {
const emojiReact = emojiReacts.get(idx);
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;
}
};