diff options
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/chat.js | 2 | ||||
| -rw-r--r-- | src/modules/oauth.js | 8 | ||||
| -rw-r--r-- | src/modules/polls.js | 70 | ||||
| -rw-r--r-- | src/modules/statuses.js | 17 | ||||
| -rw-r--r-- | src/modules/users.js | 2 |
5 files changed, 83 insertions, 16 deletions
diff --git a/src/modules/chat.js b/src/modules/chat.js index 2804e577..4d8d6699 100644 --- a/src/modules/chat.js +++ b/src/modules/chat.js @@ -21,7 +21,7 @@ const chat = { }, actions: { disconnectFromChat (store) { - store.state.socket.disconnect() + store.state.socket && store.state.socket.disconnect() }, initializeChat (store, socket) { const channel = socket.channel('chat:public') diff --git a/src/modules/oauth.js b/src/modules/oauth.js index 11cb10fe..a2a83450 100644 --- a/src/modules/oauth.js +++ b/src/modules/oauth.js @@ -1,3 +1,5 @@ +import { delete as del } from 'vue' + const oauth = { state: { clientId: false, @@ -22,6 +24,12 @@ const oauth = { }, setToken (state, token) { state.userToken = token + }, + clearToken (state) { + state.userToken = false + // state.token is userToken with older name, coming from persistent state + // let's clear it as well, since it is being used as a fallback of state.userToken + del(state, 'token') } }, getters: { diff --git a/src/modules/polls.js b/src/modules/polls.js new file mode 100644 index 00000000..e6158b63 --- /dev/null +++ b/src/modules/polls.js @@ -0,0 +1,70 @@ +import { merge } from 'lodash' +import { set } from 'vue' + +const polls = { + state: { + // Contains key = id, value = number of trackers for this poll + trackedPolls: {}, + pollsObject: {} + }, + mutations: { + mergeOrAddPoll (state, poll) { + const existingPoll = state.pollsObject[poll.id] + // Make expired-state change trigger re-renders properly + poll.expired = Date.now() > Date.parse(poll.expires_at) + if (existingPoll) { + set(state.pollsObject, poll.id, merge(existingPoll, poll)) + } else { + set(state.pollsObject, poll.id, poll) + } + }, + trackPoll (state, pollId) { + const currentValue = state.trackedPolls[pollId] + if (currentValue) { + set(state.trackedPolls, pollId, currentValue + 1) + } else { + set(state.trackedPolls, pollId, 1) + } + }, + untrackPoll (state, pollId) { + const currentValue = state.trackedPolls[pollId] + if (currentValue) { + set(state.trackedPolls, pollId, currentValue - 1) + } else { + set(state.trackedPolls, pollId, 0) + } + } + }, + actions: { + mergeOrAddPoll ({ commit }, poll) { + commit('mergeOrAddPoll', poll) + }, + updateTrackedPoll ({ rootState, dispatch, commit }, pollId) { + rootState.api.backendInteractor.fetchPoll(pollId).then(poll => { + setTimeout(() => { + if (rootState.polls.trackedPolls[pollId]) { + dispatch('updateTrackedPoll', pollId) + } + }, 30 * 1000) + commit('mergeOrAddPoll', poll) + }) + }, + trackPoll ({ rootState, commit, dispatch }, pollId) { + if (!rootState.polls.trackedPolls[pollId]) { + setTimeout(() => dispatch('updateTrackedPoll', pollId), 30 * 1000) + } + commit('trackPoll', pollId) + }, + untrackPoll ({ commit }, pollId) { + commit('untrackPoll', pollId) + }, + votePoll ({ rootState, commit }, { id, pollId, choices }) { + return rootState.api.backendInteractor.vote(pollId, choices).then(poll => { + commit('mergeOrAddPoll', poll) + return poll + }) + } + } +} + +export default polls diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 5f09b8f5..9b11a13e 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -146,7 +146,8 @@ const removeStatusFromGlobalStorage = (state, status) => { } } -const addNewStatuses = (state, { statuses, showImmediately = false, timeline, user = {}, noIdUpdate = false, userId }) => { +const addNewStatuses = (state, { statuses, showImmediately = false, timeline, user = {}, + noIdUpdate = false, userId }) => { // Sanity check if (!isArray(statuses)) { return false @@ -543,7 +544,7 @@ const statuses = { }, fetchPinnedStatuses ({ rootState, dispatch }, userId) { rootState.api.backendInteractor.fetchPinnedStatuses(userId) - .then(statuses => dispatch('addNewStatuses', { statuses, timeline: 'user', userId, showImmediately: true })) + .then(statuses => dispatch('addNewStatuses', { statuses, timeline: 'user', userId, showImmediately: true, noIdUpdate: true })) }, pinStatus ({ rootState, commit }, statusId) { return rootState.api.backendInteractor.pinOwnStatus(statusId) @@ -582,18 +583,6 @@ const statuses = { ]).then(([favoritedByUsers, rebloggedByUsers]) => commit('addFavsAndRepeats', { id, favoritedByUsers, rebloggedByUsers }) ) - }, - votePoll ({ rootState, commit }, { id, pollId, choices }) { - return rootState.api.backendInteractor.vote(pollId, choices).then(poll => { - commit('updateStatusWithPoll', { id, poll }) - return poll - }) - }, - refreshPoll ({ rootState, commit }, { id, pollId }) { - return rootState.api.backendInteractor.fetchPoll(pollId).then(poll => { - commit('updateStatusWithPoll', { id, poll }) - return poll - }) } }, mutations diff --git a/src/modules/users.js b/src/modules/users.js index 22340271..1e0b16f5 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -399,7 +399,7 @@ const users = { logout (store) { store.commit('clearCurrentUser') store.dispatch('disconnectFromChat') - store.commit('setToken', false) + store.commit('clearToken') store.dispatch('stopFetching', 'friends') store.commit('setBackendInteractor', backendInteractorService(store.getters.getToken())) store.dispatch('stopFetching', 'notifications') |
