From c210bea4d9feb3b06b4f4efb5b8bbeb6c59fa8fd Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Tue, 18 Dec 2018 23:00:46 +0900 Subject: [PATCH] refs #769 Switch user streaming method --- src/main/index.js | 9 ++++---- src/main/streaming_manager.js | 23 ++++++++++--------- .../components/TimelineSpace/HeaderMenu.vue | 14 +++++++---- src/renderer/store/TimelineSpace.js | 17 ++++++++++---- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index d1f49533..f2775c3a 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -390,20 +390,21 @@ ipcMain.on('reset-badge', () => { // streaming let userStreaming = null -ipcMain.on('start-user-streaming', (event, ac) => { - accountManager.getAccount(ac._id) +ipcMain.on('start-user-streaming', (event, obj) => { + const { account, useWebsocket } = obj + accountManager.getAccount(account._id) .catch((err) => { log.error(err) event.sender.send('error-start-user-streaming', err) }) - .then((account) => { + .then((acct) => { // Stop old user streaming if (userStreaming !== null) { userStreaming.stop() userStreaming = null } - userStreaming = new StreamingManager(account) + userStreaming = new StreamingManager(acct, useWebsocket) userStreaming.startUser( (update) => { event.sender.send('update-start-user-streaming', update) diff --git a/src/main/streaming_manager.js b/src/main/streaming_manager.js index 4832238a..f0b5f727 100644 --- a/src/main/streaming_manager.js +++ b/src/main/streaming_manager.js @@ -4,11 +4,11 @@ import WebSocket from './websocket' import log from 'electron-log' export default class StreamingManager { - constructor (account) { + constructor (account, useWebsocket = false) { this.account = account this.streaming = new Streaming(account) this.websocket = new WebSocket(account) - this.mastodon = true + this.useWebsocket = useWebsocket } /** @@ -17,18 +17,19 @@ export default class StreamingManager { async detectPleroma () { const data = await Mastodon.get('/instance', {}, this.account.baseURL + '/api/v1') if (data.version.includes('Pleroma')) { - this.mastodon = false log.info('detect Pleroma') + return true } + return false } startUser (updateCallback, notificationCallback, errCallback) { this.detectPleroma() - .then(() => { - if (this.mastodon) { - this._startUserStreaming(updateCallback, notificationCallback, errCallback) - } else { + .then((isPleroma) => { + if (isPleroma || this.useWebsocket) { this._startUserSocket(updateCallback, notificationCallback, errCallback) + } else { + this._startUserStreaming(updateCallback, notificationCallback, errCallback) } }) .catch(err => errCallback(err)) @@ -36,11 +37,11 @@ export default class StreamingManager { start (path, params, updateCallback, errCallback) { this.detectPleroma() - .then(() => { - if (this.mastodon) { - this._startStreaming(path, params, updateCallback, errCallback) - } else { + .then((isPleroma) => { + if (isPleroma || this.useWebsocket) { this._startSocket(path, params, updateCallback, errCallback) + } else { + this._startStreaming(path, params, updateCallback, errCallback) } }) .catch(err => errCallback(err)) diff --git a/src/renderer/components/TimelineSpace/HeaderMenu.vue b/src/renderer/components/TimelineSpace/HeaderMenu.vue index 8ac3c36e..d4775967 100644 --- a/src/renderer/components/TimelineSpace/HeaderMenu.vue +++ b/src/renderer/components/TimelineSpace/HeaderMenu.vue @@ -66,13 +66,15 @@ export default { filter: '', filterVisible: false, showReblogs: true, - showReplies: true, - useWebsocket: false + showReplies: true } }, computed: { - ...mapState({ - title: state => state.TimelineSpace.HeaderMenu.title + ...mapState('TimelineSpace/HeaderMenu', { + title: state => state.title + }), + ...mapState('TimelineSpace', { + useWebsocket: state => state.useWebsocket }) }, created () { @@ -134,7 +136,9 @@ export default { } }, switchStreaming () { - this.useWebsocket = !this.useWebsocket + this.$store.dispatch('TimelineSpace/stopStreamings') + this.$store.commit('TimelineSpace/changeUseWebsocket', !this.useWebsocket) + this.$store.dispatch('TimelineSpace/startStreamings') }, openNewTootModal () { this.$store.dispatch('TimelineSpace/Modals/NewToot/openModal') diff --git a/src/renderer/store/TimelineSpace.js b/src/renderer/store/TimelineSpace.js index c98d149c..6b1b22f0 100644 --- a/src/renderer/store/TimelineSpace.js +++ b/src/renderer/store/TimelineSpace.js @@ -29,7 +29,8 @@ const TimelineSpace = { direct: unreadSettings.Direct.default, local: unreadSettings.Local.default, public: unreadSettings.Public.default - } + }, + useWebsocket: false }, mutations: { updateAccount (state, account) { @@ -55,6 +56,9 @@ const TimelineSpace = { }, updateUnreadNotification (state, settings) { state.unreadNotification = settings + }, + changeUseWebsocket (state, use) { + state.useWebsocket = use } }, actions: { @@ -200,8 +204,8 @@ const TimelineSpace = { dispatch('bindPublicStreaming') } }, - startStreamings ({ dispatch, state }, account) { - dispatch('startUserStreaming', account) + startStreamings ({ dispatch, state }) { + dispatch('startUserStreaming') if (state.unreadNotification.direct) { dispatch('startDirectMessagesStreaming') } @@ -250,9 +254,12 @@ const TimelineSpace = { commit('TimelineSpace/SideMenu/changeUnreadNotifications', true, { root: true }) }) }, - startUserStreaming (_, account) { + startUserStreaming ({ state }) { return new Promise((resolve, reject) => { - ipcRenderer.send('start-user-streaming', account) + ipcRenderer.send('start-user-streaming', { + account: state.account, + useWebsocket: state.useWebsocket + }) ipcRenderer.once('error-start-user-streaming', (event, err) => { reject(err) })