From ba2e05bc63e0b36d5a126b422c9f87a7ace43305 Mon Sep 17 00:00:00 2001 From: dave Date: Thu, 28 Feb 2019 14:03:44 -0500 Subject: [PATCH 1/4] #392: stale data served to new user account --- src/components/notifications/notifications.js | 3 ++- src/modules/statuses.js | 15 ++++++++++++--- src/modules/users.js | 4 ++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/components/notifications/notifications.js b/src/components/notifications/notifications.js index 5e95631a..9fc5e38a 100644 --- a/src/components/notifications/notifications.js +++ b/src/components/notifications/notifications.js @@ -11,7 +11,8 @@ const Notifications = { const store = this.$store const credentials = store.state.users.currentUser.credentials - notificationsFetcher.startFetching({ store, credentials }) + const fetcherId = notificationsFetcher.startFetching({ store, credentials }) + this.$store.commit('setNotificationFetcher', { fetcherId }) }, data () { return { diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 826b544c..2d1313a5 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -1,4 +1,4 @@ -import { remove, slice, each, find, maxBy, minBy, merge, last, isArray } from 'lodash' +import { remove, slice, each, find, maxBy, minBy, merge, last, isArray, cloneDeep } from 'lodash' import apiService from '../services/api/api.service.js' // import parse from '../services/status_parser/status_parser.js' @@ -29,7 +29,8 @@ export const defaultState = { data: [], idStore: {}, loading: false, - error: false + error: false, + fetcherId: null }, favorites: new Set(), error: false, @@ -321,6 +322,14 @@ export const mutations = { oldTimeline.visibleStatusesObject = {} each(oldTimeline.visibleStatuses, (status) => { oldTimeline.visibleStatusesObject[status.id] = status }) }, + setNotificationFetcher (state, { fetcherId }) { + state.notifications.fetcherId = fetcherId + }, + resetStatuses (state) { + Object.keys(state).forEach(key => { + state[key] = cloneDeep(defaultState[key]) + }) + }, clearTimeline (state, { timeline }) { state.timelines[timeline] = emptyTl(state.timelines[timeline].userId) }, @@ -371,7 +380,7 @@ export const mutations = { } const statuses = { - state: defaultState, + state: cloneDeep(defaultState), actions: { addNewStatuses ({ rootState, commit }, { statuses, showImmediately = false, timeline = false, noIdUpdate = false, userId }) { commit('addNewStatuses', { statuses, showImmediately, timeline, noIdUpdate, user: rootState.users.currentUser, userId }) diff --git a/src/modules/users.js b/src/modules/users.js index 77df7168..84fe039b 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -290,6 +290,10 @@ const users = { store.commit('setToken', false) store.dispatch('stopFetching', 'friends') store.commit('setBackendInteractor', backendInteractorService()) + if (store.rootState.statuses.notifications.fetcherId) { + window.clearInterval(store.rootState.statuses.notifications.fetcherId) + } + store.commit('resetStatuses') }, loginUser (store, accessToken) { return new Promise((resolve, reject) => { From f5adb62e2e6efdf6bd9a9c7a8c2677c618de53f9 Mon Sep 17 00:00:00 2001 From: dave Date: Thu, 28 Feb 2019 14:27:47 -0500 Subject: [PATCH 2/4] #392: update defaultState into a function --- src/modules/statuses.js | 11 +++++----- test/unit/specs/modules/statuses.spec.js | 27 ++++++++++++------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 2d1313a5..2af23a9d 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -1,4 +1,4 @@ -import { remove, slice, each, find, maxBy, minBy, merge, last, isArray, cloneDeep } from 'lodash' +import { remove, slice, each, find, maxBy, minBy, merge, last, isArray } from 'lodash' import apiService from '../services/api/api.service.js' // import parse from '../services/status_parser/status_parser.js' @@ -18,7 +18,7 @@ const emptyTl = (userId = 0) => ({ flushMarker: 0 }) -export const defaultState = { +export const defaultState = () => ({ allStatuses: [], allStatusesObject: {}, maxId: 0, @@ -45,7 +45,7 @@ export const defaultState = { tag: emptyTl(), dms: emptyTl() } -} +}) export const prepareStatus = (status) => { // Set deleted flag @@ -326,8 +326,9 @@ export const mutations = { state.notifications.fetcherId = fetcherId }, resetStatuses (state) { + const emptyState = defaultState() Object.keys(state).forEach(key => { - state[key] = cloneDeep(defaultState[key]) + state[key] = emptyState[key] }) }, clearTimeline (state, { timeline }) { @@ -380,7 +381,7 @@ export const mutations = { } const statuses = { - state: cloneDeep(defaultState), + state: defaultState(), actions: { addNewStatuses ({ rootState, commit }, { statuses, showImmediately = false, timeline = false, noIdUpdate = false, userId }) { commit('addNewStatuses', { statuses, showImmediately, timeline, noIdUpdate, user: rootState.users.currentUser, userId }) diff --git a/test/unit/specs/modules/statuses.spec.js b/test/unit/specs/modules/statuses.spec.js index 01d2ce06..864b798d 100644 --- a/test/unit/specs/modules/statuses.spec.js +++ b/test/unit/specs/modules/statuses.spec.js @@ -1,4 +1,3 @@ -import { cloneDeep } from 'lodash' import { defaultState, mutations, prepareStatus } from '../../../../src/modules/statuses.js' // eslint-disable-next-line camelcase @@ -24,7 +23,7 @@ describe('Statuses.prepareStatus', () => { describe('The Statuses module', () => { it('adds the status to allStatuses and to the given timeline', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' }) @@ -36,7 +35,7 @@ describe('The Statuses module', () => { }) it('counts the status as new if it has not been seen on this timeline', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' }) @@ -54,7 +53,7 @@ describe('The Statuses module', () => { }) it('add the statuses to allStatuses if no timeline is given', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) mutations.addNewStatuses(state, { statuses: [status] }) @@ -66,7 +65,7 @@ describe('The Statuses module', () => { }) it('adds the status to allStatuses and to the given timeline, directly visible', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) @@ -78,7 +77,7 @@ describe('The Statuses module', () => { }) it('removes statuses by tag on deletion', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const otherStatus = makeMockStatus({id: '3'}) status.uri = 'xxx' @@ -96,7 +95,7 @@ describe('The Statuses module', () => { }) it('does not update the maxId when the noIdUpdate flag is set', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const secondStatus = makeMockStatus({id: '2'}) @@ -110,7 +109,7 @@ describe('The Statuses module', () => { }) it('keeps a descending by id order in timeline.visibleStatuses and timeline.statuses', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const nonVisibleStatus = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '3'}) const statusTwo = makeMockStatus({id: '2'}) @@ -130,7 +129,7 @@ describe('The Statuses module', () => { }) it('splits retweets from their status and links them', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const retweet = makeMockStatus({id: '2', type: 'retweet'}) const modStatus = makeMockStatus({id: '1', text: 'something else'}) @@ -155,7 +154,7 @@ describe('The Statuses module', () => { }) it('replaces existing statuses with the same id', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const modStatus = makeMockStatus({id: '1', text: 'something else'}) @@ -172,7 +171,7 @@ describe('The Statuses module', () => { }) it('replaces existing statuses with the same id, coming from a retweet', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const modStatus = makeMockStatus({id: '1', text: 'something else'}) const retweet = makeMockStatus({id: '2', type: 'retweet'}) @@ -193,7 +192,7 @@ describe('The Statuses module', () => { }) it('handles favorite actions', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const favorite = { @@ -241,7 +240,7 @@ describe('The Statuses module', () => { }) it('keeps userId when clearing user timeline', () => { - const state = cloneDeep(defaultState) + const state = defaultState() state.timelines.user.userId = 123 mutations.clearTimeline(state, { timeline: 'user' }) @@ -252,7 +251,7 @@ describe('The Statuses module', () => { describe('notifications', () => { it('removes a notification when the notice gets removed', () => { const user = { id: '1' } - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const otherStatus = makeMockStatus({id: '3'}) const mentionedStatus = makeMockStatus({id: '2'}) From bbe1821be7c098f3a23bd0dd54b4ec89a159e9c1 Mon Sep 17 00:00:00 2001 From: dave Date: Thu, 28 Feb 2019 14:44:43 -0500 Subject: [PATCH 3/4] #392: update object.entries --- src/modules/statuses.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 2af23a9d..028053cf 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -327,8 +327,8 @@ export const mutations = { }, resetStatuses (state) { const emptyState = defaultState() - Object.keys(state).forEach(key => { - state[key] = emptyState[key] + Object.entries(emptyState).forEach(([key, value]) => { + state[key] = value }) }, clearTimeline (state, { timeline }) { From f3f9fbe3027bf5284adc05387e89d904884490b2 Mon Sep 17 00:00:00 2001 From: dave Date: Fri, 1 Mar 2019 11:59:50 -0500 Subject: [PATCH 4/4] #392: clean up notification stopping section --- src/modules/statuses.js | 6 ++++++ src/modules/users.js | 4 +--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 028053cf..77a8ec72 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -401,6 +401,12 @@ const statuses = { setNotificationsSilence ({ rootState, commit }, { value }) { commit('setNotificationsSilence', { value }) }, + stopFetchingNotifications ({ rootState, commit }) { + if (rootState.statuses.notifications.fetcherId) { + window.clearInterval(rootState.statuses.notifications.fetcherId) + } + commit('setNotificationFetcher', { fetcherId: null }) + }, deleteStatus ({ rootState, commit }, status) { commit('setDeleted', { status }) apiService.deleteStatus({ id: status.id, credentials: rootState.users.currentUser.credentials }) diff --git a/src/modules/users.js b/src/modules/users.js index 84fe039b..0e1dd166 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -290,9 +290,7 @@ const users = { store.commit('setToken', false) store.dispatch('stopFetching', 'friends') store.commit('setBackendInteractor', backendInteractorService()) - if (store.rootState.statuses.notifications.fetcherId) { - window.clearInterval(store.rootState.statuses.notifications.fetcherId) - } + store.dispatch('stopFetchingNotifications') store.commit('resetStatuses') }, loginUser (store, accessToken) {