From d74f6ed6eaf3eeef263d9bf7ec7c73bd4298cced Mon Sep 17 00:00:00 2001 From: Sergey Suprunenko Date: Sat, 17 Aug 2019 08:18:42 +0000 Subject: [PATCH] Initialize chat only if user is logged in and it wasn't initialized before --- src/boot/after_store.js | 6 ------ .../features_panel/features_panel.js | 4 +--- src/modules/api.js | 19 +++++++++---------- src/modules/chat.js | 10 +--------- src/modules/instance.js | 5 +++++ src/modules/users.js | 2 +- static/config.json | 1 - 7 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/boot/after_store.js b/src/boot/after_store.js index 3799359f..5a94194c 100644 --- a/src/boot/after_store.js +++ b/src/boot/after_store.js @@ -109,12 +109,6 @@ const setSettings = async ({ apiConfig, staticConfig, store }) => { copyInstanceOption('noAttachmentLinks') copyInstanceOption('showFeaturesPanel') - if ((config.chatDisabled)) { - store.dispatch('disableChat') - } else { - store.dispatch('initializeSocket') - } - return store.dispatch('setTheme', config['theme']) } diff --git a/src/components/features_panel/features_panel.js b/src/components/features_panel/features_panel.js index 5f0b7b25..5f80a079 100644 --- a/src/components/features_panel/features_panel.js +++ b/src/components/features_panel/features_panel.js @@ -1,8 +1,6 @@ const FeaturesPanel = { computed: { - chat: function () { - return this.$store.state.instance.chatAvailable && (!this.$store.state.chatDisabled) - }, + chat: function () { return this.$store.state.instance.chatAvailable }, gopher: function () { return this.$store.state.instance.gopherAvailable }, whoToFollow: function () { return this.$store.state.instance.suggestionsEnabled }, mediaProxy: function () { return this.$store.state.instance.mediaProxyAvailable }, diff --git a/src/modules/api.js b/src/modules/api.js index d51b31f3..eb6a7980 100644 --- a/src/modules/api.js +++ b/src/modules/api.js @@ -6,7 +6,6 @@ const api = { backendInteractor: backendInteractorService(), fetchers: {}, socket: null, - chatDisabled: false, followRequests: [] }, mutations: { @@ -25,9 +24,6 @@ const api = { setSocket (state, socket) { state.socket = socket }, - setChatDisabled (state, value) { - state.chatDisabled = value - }, setFollowRequests (state, value) { state.followRequests = value } @@ -55,17 +51,20 @@ const api = { setWsToken (store, token) { store.commit('setWsToken', token) }, - initializeSocket (store) { + initializeSocket ({ dispatch, commit, state, rootState }) { // Set up websocket connection - if (!store.state.chatDisabled) { - const token = store.state.wsToken + const token = state.wsToken + if (rootState.instance.chatAvailable && typeof token !== 'undefined' && state.socket === null) { const socket = new Socket('/socket', { params: { token } }) socket.connect() - store.dispatch('initializeChat', socket) + + commit('setSocket', socket) + dispatch('initializeChat', socket) } }, - disableChat (store) { - store.commit('setChatDisabled', true) + disconnectFromSocket ({ commit, state }) { + state.socket && state.socket.disconnect() + commit('setSocket', null) }, removeFollowRequest (store, request) { let requests = store.state.followRequests.filter((it) => it !== request) diff --git a/src/modules/chat.js b/src/modules/chat.js index e1b03bca..c798549d 100644 --- a/src/modules/chat.js +++ b/src/modules/chat.js @@ -1,16 +1,12 @@ const chat = { state: { messages: [], - channel: { state: '' }, - socket: null + channel: { state: '' } }, mutations: { setChannel (state, channel) { state.channel = channel }, - setSocket (state, socket) { - state.socket = socket - }, addMessage (state, message) { state.messages.push(message) state.messages = state.messages.slice(-19, 20) @@ -20,12 +16,8 @@ const chat = { } }, actions: { - disconnectFromChat (store) { - store.state.socket && store.state.socket.disconnect() - }, initializeChat (store, socket) { const channel = socket.channel('chat:public') - store.commit('setSocket', socket) channel.on('new_msg', (msg) => { store.commit('addMessage', msg) }) diff --git a/src/modules/instance.js b/src/modules/instance.js index 93b56577..7d602aa1 100644 --- a/src/modules/instance.js +++ b/src/modules/instance.js @@ -79,6 +79,11 @@ const instance = { case 'name': dispatch('setPageTitle') break + case 'chatAvailable': + if (value) { + dispatch('initializeSocket') + } + break } }, setTheme ({ commit }, themeName) { diff --git a/src/modules/users.js b/src/modules/users.js index 4d78aef5..4aebe04b 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -410,7 +410,7 @@ const users = { }) .then(() => { store.commit('clearCurrentUser') - store.dispatch('disconnectFromChat') + store.dispatch('disconnectFromSocket') store.commit('clearToken') store.dispatch('stopFetching', 'friends') store.commit('setBackendInteractor', backendInteractorService(store.getters.getToken())) diff --git a/static/config.json b/static/config.json index 5cdb33a0..c8267869 100644 --- a/static/config.json +++ b/static/config.json @@ -6,7 +6,6 @@ "logoMargin": ".1em", "redirectRootNoLogin": "/main/all", "redirectRootLogin": "/main/friends", - "chatDisabled": false, "showInstanceSpecificPanel": false, "collapseMessageWithSubject": false, "scopeCopy": true,