Initialize chat only if user is logged in and it wasn't initialized before

This commit is contained in:
Sergey Suprunenko 2019-08-17 08:18:42 +00:00 committed by Shpuld Shpludson
parent 58713e342d
commit d74f6ed6ea
7 changed files with 17 additions and 30 deletions

View file

@ -109,12 +109,6 @@ const setSettings = async ({ apiConfig, staticConfig, store }) => {
copyInstanceOption('noAttachmentLinks') copyInstanceOption('noAttachmentLinks')
copyInstanceOption('showFeaturesPanel') copyInstanceOption('showFeaturesPanel')
if ((config.chatDisabled)) {
store.dispatch('disableChat')
} else {
store.dispatch('initializeSocket')
}
return store.dispatch('setTheme', config['theme']) return store.dispatch('setTheme', config['theme'])
} }

View file

@ -1,8 +1,6 @@
const FeaturesPanel = { const FeaturesPanel = {
computed: { computed: {
chat: function () { chat: function () { return this.$store.state.instance.chatAvailable },
return this.$store.state.instance.chatAvailable && (!this.$store.state.chatDisabled)
},
gopher: function () { return this.$store.state.instance.gopherAvailable }, gopher: function () { return this.$store.state.instance.gopherAvailable },
whoToFollow: function () { return this.$store.state.instance.suggestionsEnabled }, whoToFollow: function () { return this.$store.state.instance.suggestionsEnabled },
mediaProxy: function () { return this.$store.state.instance.mediaProxyAvailable }, mediaProxy: function () { return this.$store.state.instance.mediaProxyAvailable },

View file

@ -6,7 +6,6 @@ const api = {
backendInteractor: backendInteractorService(), backendInteractor: backendInteractorService(),
fetchers: {}, fetchers: {},
socket: null, socket: null,
chatDisabled: false,
followRequests: [] followRequests: []
}, },
mutations: { mutations: {
@ -25,9 +24,6 @@ const api = {
setSocket (state, socket) { setSocket (state, socket) {
state.socket = socket state.socket = socket
}, },
setChatDisabled (state, value) {
state.chatDisabled = value
},
setFollowRequests (state, value) { setFollowRequests (state, value) {
state.followRequests = value state.followRequests = value
} }
@ -55,17 +51,20 @@ const api = {
setWsToken (store, token) { setWsToken (store, token) {
store.commit('setWsToken', token) store.commit('setWsToken', token)
}, },
initializeSocket (store) { initializeSocket ({ dispatch, commit, state, rootState }) {
// Set up websocket connection // Set up websocket connection
if (!store.state.chatDisabled) { const token = state.wsToken
const token = store.state.wsToken if (rootState.instance.chatAvailable && typeof token !== 'undefined' && state.socket === null) {
const socket = new Socket('/socket', { params: { token } }) const socket = new Socket('/socket', { params: { token } })
socket.connect() socket.connect()
store.dispatch('initializeChat', socket)
commit('setSocket', socket)
dispatch('initializeChat', socket)
} }
}, },
disableChat (store) { disconnectFromSocket ({ commit, state }) {
store.commit('setChatDisabled', true) state.socket && state.socket.disconnect()
commit('setSocket', null)
}, },
removeFollowRequest (store, request) { removeFollowRequest (store, request) {
let requests = store.state.followRequests.filter((it) => it !== request) let requests = store.state.followRequests.filter((it) => it !== request)

View file

@ -1,16 +1,12 @@
const chat = { const chat = {
state: { state: {
messages: [], messages: [],
channel: { state: '' }, channel: { state: '' }
socket: null
}, },
mutations: { mutations: {
setChannel (state, channel) { setChannel (state, channel) {
state.channel = channel state.channel = channel
}, },
setSocket (state, socket) {
state.socket = socket
},
addMessage (state, message) { addMessage (state, message) {
state.messages.push(message) state.messages.push(message)
state.messages = state.messages.slice(-19, 20) state.messages = state.messages.slice(-19, 20)
@ -20,12 +16,8 @@ const chat = {
} }
}, },
actions: { actions: {
disconnectFromChat (store) {
store.state.socket && store.state.socket.disconnect()
},
initializeChat (store, socket) { initializeChat (store, socket) {
const channel = socket.channel('chat:public') const channel = socket.channel('chat:public')
store.commit('setSocket', socket)
channel.on('new_msg', (msg) => { channel.on('new_msg', (msg) => {
store.commit('addMessage', msg) store.commit('addMessage', msg)
}) })

View file

@ -79,6 +79,11 @@ const instance = {
case 'name': case 'name':
dispatch('setPageTitle') dispatch('setPageTitle')
break break
case 'chatAvailable':
if (value) {
dispatch('initializeSocket')
}
break
} }
}, },
setTheme ({ commit }, themeName) { setTheme ({ commit }, themeName) {

View file

@ -410,7 +410,7 @@ const users = {
}) })
.then(() => { .then(() => {
store.commit('clearCurrentUser') store.commit('clearCurrentUser')
store.dispatch('disconnectFromChat') store.dispatch('disconnectFromSocket')
store.commit('clearToken') store.commit('clearToken')
store.dispatch('stopFetching', 'friends') store.dispatch('stopFetching', 'friends')
store.commit('setBackendInteractor', backendInteractorService(store.getters.getToken())) store.commit('setBackendInteractor', backendInteractorService(store.getters.getToken()))

View file

@ -6,7 +6,6 @@
"logoMargin": ".1em", "logoMargin": ".1em",
"redirectRootNoLogin": "/main/all", "redirectRootNoLogin": "/main/all",
"redirectRootLogin": "/main/friends", "redirectRootLogin": "/main/friends",
"chatDisabled": false,
"showInstanceSpecificPanel": false, "showInstanceSpecificPanel": false,
"collapseMessageWithSubject": false, "collapseMessageWithSubject": false,
"scopeCopy": true, "scopeCopy": true,