diff --git a/app/gabsocial/reducers/statuses.js b/app/gabsocial/reducers/statuses.js index 885cc221c..d41fc3834 100644 --- a/app/gabsocial/reducers/statuses.js +++ b/app/gabsocial/reducers/statuses.js @@ -10,9 +10,13 @@ import { STATUS_REVEAL, STATUS_HIDE, } from '../actions/statuses'; +import { + EMOJI_REACT_REQUEST, +} from '../actions/emoji_reacts'; import { TIMELINE_DELETE } from '../actions/timelines'; import { STATUS_IMPORT, STATUSES_IMPORT } from '../actions/importer'; import { Map as ImmutableMap, fromJS } from 'immutable'; +import { simulateEmojiReact } from 'gabsocial/utils/emoji_reacts'; const importStatus = (state, status) => state.set(status.id, fromJS(status)); @@ -37,6 +41,10 @@ export default function statuses(state = initialState, action) { return importStatuses(state, action.statuses); case FAVOURITE_REQUEST: return state.setIn([action.status.get('id'), 'favourited'], true); + case EMOJI_REACT_REQUEST: + const path = [action.status.get('id'), 'pleroma', 'emoji_reactions']; + const emojiReacts = state.getIn(path); + return state.setIn(path, simulateEmojiReact(emojiReacts, action.emoji)); case FAVOURITE_FAIL: return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'favourited'], false); case REBLOG_REQUEST: diff --git a/app/gabsocial/utils/__tests__/emoji_reacts-test.js b/app/gabsocial/utils/__tests__/emoji_reacts-test.js index b1b2c7bfc..fcce0a11b 100644 --- a/app/gabsocial/utils/__tests__/emoji_reacts-test.js +++ b/app/gabsocial/utils/__tests__/emoji_reacts-test.js @@ -5,6 +5,7 @@ import { oneEmojiPerAccount, reduceEmoji, getReactForStatus, + simulateEmojiReact, } from '../emoji_reacts'; import { fromJS } from 'immutable'; @@ -179,3 +180,28 @@ describe('getReactForStatus', () => { expect(getReactForStatus(status)).toEqual(undefined); }); }); + +describe('simulateEmojiReact', () => { + it('adds the emoji to the list', () => { + const emojiReacts = fromJS([ + { 'count': 2, 'me': false, 'name': '👍' }, + { 'count': 2, 'me': false, 'name': '❤' }, + ]); + expect(simulateEmojiReact(emojiReacts, '❤')).toEqual(fromJS([ + { 'count': 2, 'me': false, 'name': '👍' }, + { 'count': 3, 'me': true, 'name': '❤' }, + ])); + }); + + it('creates the emoji if it didn\'t already exist', () => { + const emojiReacts = fromJS([ + { 'count': 2, 'me': false, 'name': '👍' }, + { 'count': 2, 'me': false, 'name': '❤' }, + ]); + expect(simulateEmojiReact(emojiReacts, '😯')).toEqual(fromJS([ + { 'count': 2, 'me': false, 'name': '👍' }, + { 'count': 2, 'me': false, 'name': '❤' }, + { 'count': 1, 'me': true, 'name': '😯' }, + ])); + }); +}); diff --git a/app/gabsocial/utils/emoji_reacts.js b/app/gabsocial/utils/emoji_reacts.js index 4e378eced..2cb1b3fc3 100644 --- a/app/gabsocial/utils/emoji_reacts.js +++ b/app/gabsocial/utils/emoji_reacts.js @@ -83,3 +83,20 @@ export const getReactForStatus = status => { ).filter(e => e.get('me') === true) .getIn([0, 'name']); }; + +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, + })); + } +};