From e7637e41960ef294c4cca44d357df744191abb19 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 18 Nov 2016 22:55:04 +0100 Subject: [PATCH] Add first notification. This adds a favorite notification. --- src/modules/statuses.js | 17 ++++++++++++++--- test/unit/specs/modules/statuses.spec.js | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 510f9dbb16..4f848e3289 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -6,6 +6,7 @@ import apiService from '../services/api/api.service.js' export const defaultState = { allStatuses: [], maxId: 0, + notifications: [], timelines: { public: { statuses: [], @@ -99,7 +100,7 @@ const mergeOrAdd = (arr, item) => { } export const mutations = { - addNewStatuses (state, { statuses, showImmediately = false, timeline }) { + addNewStatuses (state, { statuses, showImmediately = false, timeline, user = {} }) { const allStatuses = state.allStatuses const timelineObject = state.timelines[timeline] @@ -134,10 +135,17 @@ export const mutations = { return status } - const favoriteStatus = (favorite) => { + const addNotification = (type, status) => { + state.notifications.push({type, status}) + } + + const favoriteStatus = (favorite, user) => { const status = find(allStatuses, { id: toInteger(favorite.in_reply_to_status_id) }) if (status) { status.fave_num += 1 + if (status.user.id === user.id) { + addNotification('favorite', status) + } } return status } @@ -164,7 +172,7 @@ export const mutations = { }, 'favorite': (favorite) => { updateMaxId(favorite) - favoriteStatus(favorite) + favoriteStatus(favorite, user) }, 'deletion': ({uri}) => { remove(allStatuses, { tag: uri }) @@ -216,6 +224,9 @@ export const mutations = { const statuses = { state: defaultState, actions: { + addNewStatuses ({ rootState, commit }, { statuses, showImmediately = false, timeline }) { + commit('addNewStatuses', { statuses, showImmediately, timeline, user: rootState.users.currentUser }) + }, favorite ({ rootState, commit }, status) { // Optimistic favoriting... commit('setFavorited', { status, value: true }) diff --git a/test/unit/specs/modules/statuses.spec.js b/test/unit/specs/modules/statuses.spec.js index d6e1dc2027..871de491b1 100644 --- a/test/unit/specs/modules/statuses.spec.js +++ b/test/unit/specs/modules/statuses.spec.js @@ -4,6 +4,7 @@ import { defaultState, mutations, findMaxId, prepareStatus } from '../../../../s const makeMockStatus = ({id, text, is_post_verb = true}) => { return { id, + user: {id: 0}, name: 'status', text: text || `Text number ${id}`, fave_num: 0, @@ -193,4 +194,26 @@ describe('The Statuses module', () => { expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1) expect(state.timelines.public.maxId).to.eq(favorite.id) }) + + describe('notifications', () => { + it('adds a notfication when one of the user\'s status is favorited', () => { + const state = cloneDeep(defaultState) + const status = makeMockStatus({id: 1}) + const user = {id: 1} + status.user = user + + const favorite = { + id: 2, + is_post_verb: false, + in_reply_to_status_id: '1', // The API uses strings here... + uri: 'tag:shitposter.club,2016-08-21:fave:3895:note:773501:2016-08-21T16:52:15+00:00', + text: 'a favorited something by b' + } + + mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public', user }) + mutations.addNewStatuses(state, { statuses: [favorite], showImmediately: true, timeline: 'public', user }) + + expect(state.notifications).to.have.length(1) + }) + }) })