From 4f45c12d13d5efba64a4df09389ac5c1dd784e95 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 9 Oct 2018 15:08:31 +0900 Subject: [PATCH] Fix bug and refactor --- src/client/app/common/scripts/stream.ts | 86 ----------------- .../components/games/reversi/reversi.vue | 3 +- .../common/views/widgets/posts-monitor.vue | 3 +- .../views/widgets/server.cpu-memory.vue | 3 +- .../views/components/notifications.vue | 3 +- .../views/pages/admin/admin.cpu-memory.vue | 3 +- .../views/pages/deck/deck.notifications.vue | 3 +- src/client/app/mios.ts | 95 ++++++++++++++++++- .../mobile/views/components/notifications.vue | 3 +- src/client/app/mobile/views/components/ui.vue | 3 +- 10 files changed, 101 insertions(+), 104 deletions(-) diff --git a/src/client/app/common/scripts/stream.ts b/src/client/app/common/scripts/stream.ts index e95306183..98a7efaa4 100644 --- a/src/client/app/common/scripts/stream.ts +++ b/src/client/app/common/scripts/stream.ts @@ -26,92 +26,6 @@ export default class Stream extends EventEmitter { this.stream.addEventListener('open', this.onOpen); this.stream.addEventListener('close', this.onClose); this.stream.addEventListener('message', this.onMessage); - - if (user) { - const main = this.useSharedConnection('main'); - - // 自分の情報が更新されたとき - main.on('meUpdated', i => { - os.store.dispatch('mergeMe', i); - }); - - main.on('readAllNotifications', () => { - os.store.dispatch('mergeMe', { - hasUnreadNotification: false - }); - }); - - main.on('unreadNotification', () => { - os.store.dispatch('mergeMe', { - hasUnreadNotification: true - }); - }); - - main.on('readAllMessagingMessages', () => { - os.store.dispatch('mergeMe', { - hasUnreadMessagingMessage: false - }); - }); - - main.on('unreadMessagingMessage', () => { - os.store.dispatch('mergeMe', { - hasUnreadMessagingMessage: true - }); - }); - - main.on('unreadMention', () => { - os.store.dispatch('mergeMe', { - hasUnreadMentions: true - }); - }); - - main.on('readAllUnreadMentions', () => { - os.store.dispatch('mergeMe', { - hasUnreadMentions: false - }); - }); - - main.on('unreadSpecifiedNote', () => { - os.store.dispatch('mergeMe', { - hasUnreadSpecifiedNotes: true - }); - }); - - main.on('readAllUnreadSpecifiedNotes', () => { - os.store.dispatch('mergeMe', { - hasUnreadSpecifiedNotes: false - }); - }); - - main.on('clientSettingUpdated', x => { - os.store.commit('settings/set', { - key: x.key, - value: x.value - }); - }); - - main.on('homeUpdated', x => { - os.store.commit('settings/setHome', x); - }); - - main.on('mobileHomeUpdated', x => { - os.store.commit('settings/setMobileHome', x); - }); - - main.on('widgetUpdated', x => { - os.store.commit('settings/setWidget', { - id: x.id, - data: x.data - }); - }); - - // トークンが再生成されたとき - // このままではMisskeyが利用できないので強制的にサインアウトさせる - main.on('myTokenRegenerated', () => { - alert('%i18n:common.my-token-regenerated%'); - os.signout(); - }); - } } public useSharedConnection = (channel: string): SharedConnection => { diff --git a/src/client/app/common/views/components/games/reversi/reversi.vue b/src/client/app/common/views/components/games/reversi/reversi.vue index f2156bc41..7c9e1b6a5 100644 --- a/src/client/app/common/views/components/games/reversi/reversi.vue +++ b/src/client/app/common/views/components/games/reversi/reversi.vue @@ -71,8 +71,7 @@ export default Vue.extend({ this.pingClock = setInterval(() => { if (this.matching) { - this.connection.send({ - type: 'ping', + this.connection.send('ping', { id: this.matching.id }); } diff --git a/src/client/app/common/views/widgets/posts-monitor.vue b/src/client/app/common/views/widgets/posts-monitor.vue index 1c70e6dbc..cb307715d 100644 --- a/src/client/app/common/views/widgets/posts-monitor.vue +++ b/src/client/app/common/views/widgets/posts-monitor.vue @@ -113,8 +113,7 @@ export default define({ this.connection.on('stats', this.onStats); this.connection.on('statsLog', this.onStatsLog); - this.connection.send({ - type: 'requestLog', + this.connection.send('requestLog',{ id: Math.random().toString() }); }, diff --git a/src/client/app/common/views/widgets/server.cpu-memory.vue b/src/client/app/common/views/widgets/server.cpu-memory.vue index 55aa1ea89..119a77af8 100644 --- a/src/client/app/common/views/widgets/server.cpu-memory.vue +++ b/src/client/app/common/views/widgets/server.cpu-memory.vue @@ -91,8 +91,7 @@ export default Vue.extend({ mounted() { this.connection.on('stats', this.onStats); this.connection.on('statsLog', this.onStatsLog); - this.connection.send({ - type: 'requestLog', + this.connection.send('requestLog', { id: Math.random().toString() }); }, diff --git a/src/client/app/desktop/views/components/notifications.vue b/src/client/app/desktop/views/components/notifications.vue index 95b8e1355..a572f43ad 100644 --- a/src/client/app/desktop/views/components/notifications.vue +++ b/src/client/app/desktop/views/components/notifications.vue @@ -181,8 +181,7 @@ export default Vue.extend({ onNotification(notification) { // TODO: ユーザーが画面を見てないと思われるとき(ブラウザやタブがアクティブじゃないなど)は送信しない - this.connection.send({ - type: 'readNotification', + (this as any).os.stream.send('readNotification', { id: notification.id }); diff --git a/src/client/app/desktop/views/pages/admin/admin.cpu-memory.vue b/src/client/app/desktop/views/pages/admin/admin.cpu-memory.vue index 63b24cea4..0f486df2d 100644 --- a/src/client/app/desktop/views/pages/admin/admin.cpu-memory.vue +++ b/src/client/app/desktop/views/pages/admin/admin.cpu-memory.vue @@ -77,8 +77,7 @@ export default Vue.extend({ mounted() { this.connection.on('stats', this.onStats); this.connection.on('statsLog', this.onStatsLog); - this.connection.send({ - type: 'requestLog', + this.connection.send('requestLog', { id: Math.random().toString(), length: 200 }); diff --git a/src/client/app/desktop/views/pages/deck/deck.notifications.vue b/src/client/app/desktop/views/pages/deck/deck.notifications.vue index 29de691fe..b68757b73 100644 --- a/src/client/app/desktop/views/pages/deck/deck.notifications.vue +++ b/src/client/app/desktop/views/pages/deck/deck.notifications.vue @@ -113,8 +113,7 @@ export default Vue.extend({ onNotification(notification) { // TODO: ユーザーが画面を見てないと思われるとき(ブラウザやタブがアクティブじゃないなど)は送信しない - this.connection.send({ - type: 'readNotification', + (this as any).os.stream.send('readNotification', { id: notification.id }); diff --git a/src/client/app/mios.ts b/src/client/app/mios.ts index 42171e71f..558c53dcf 100644 --- a/src/client/app/mios.ts +++ b/src/client/app/mios.ts @@ -212,7 +212,7 @@ export default class MiOS extends EventEmitter { const fetched = () => { this.emit('signedin'); - this.stream = new Stream(this); + this.initStream(); // Finish init callback(); @@ -247,12 +247,103 @@ export default class MiOS extends EventEmitter { // Finish init callback(); - this.stream = new Stream(this); + this.initStream(); } }); } } + @autobind + private initStream() { + this.stream = new Stream(this); + + if (this.store.getters.isSignedIn) { + const main = this.stream.useSharedConnection('main'); + + // 自分の情報が更新されたとき + main.on('meUpdated', i => { + this.store.dispatch('mergeMe', i); + }); + + main.on('readAllNotifications', () => { + this.store.dispatch('mergeMe', { + hasUnreadNotification: false + }); + }); + + main.on('unreadNotification', () => { + this.store.dispatch('mergeMe', { + hasUnreadNotification: true + }); + }); + + main.on('readAllMessagingMessages', () => { + this.store.dispatch('mergeMe', { + hasUnreadMessagingMessage: false + }); + }); + + main.on('unreadMessagingMessage', () => { + this.store.dispatch('mergeMe', { + hasUnreadMessagingMessage: true + }); + }); + + main.on('unreadMention', () => { + this.store.dispatch('mergeMe', { + hasUnreadMentions: true + }); + }); + + main.on('readAllUnreadMentions', () => { + this.store.dispatch('mergeMe', { + hasUnreadMentions: false + }); + }); + + main.on('unreadSpecifiedNote', () => { + this.store.dispatch('mergeMe', { + hasUnreadSpecifiedNotes: true + }); + }); + + main.on('readAllUnreadSpecifiedNotes', () => { + this.store.dispatch('mergeMe', { + hasUnreadSpecifiedNotes: false + }); + }); + + main.on('clientSettingUpdated', x => { + this.store.commit('settings/set', { + key: x.key, + value: x.value + }); + }); + + main.on('homeUpdated', x => { + this.store.commit('settings/setHome', x); + }); + + main.on('mobileHomeUpdated', x => { + this.store.commit('settings/setMobileHome', x); + }); + + main.on('widgetUpdated', x => { + this.store.commit('settings/setWidget', { + id: x.id, + data: x.data + }); + }); + + // トークンが再生成されたとき + // このままではMisskeyが利用できないので強制的にサインアウトさせる + main.on('myTokenRegenerated', () => { + alert('%i18n:common.my-token-regenerated%'); + this.signout(); + }); + } + } + /** * Register service worker */ diff --git a/src/client/app/mobile/views/components/notifications.vue b/src/client/app/mobile/views/components/notifications.vue index e43ec41a3..1cdcad1e4 100644 --- a/src/client/app/mobile/views/components/notifications.vue +++ b/src/client/app/mobile/views/components/notifications.vue @@ -98,8 +98,7 @@ export default Vue.extend({ onNotification(notification) { // TODO: ユーザーが画面を見てないと思われるとき(ブラウザやタブがアクティブじゃないなど)は送信しない - this.connection.send({ - type: 'readNotification', + (this as any).os.stream.send('readNotification', { id: notification.id }); diff --git a/src/client/app/mobile/views/components/ui.vue b/src/client/app/mobile/views/components/ui.vue index b16c246b1..6f77f4445 100644 --- a/src/client/app/mobile/views/components/ui.vue +++ b/src/client/app/mobile/views/components/ui.vue @@ -58,8 +58,7 @@ export default Vue.extend({ methods: { onNotification(notification) { // TODO: ユーザーが画面を見てないと思われるとき(ブラウザやタブがアクティブじゃないなど)は送信しない - this.connection.send({ - type: 'readNotification', + (this as any).os.stream.send('readNotification', { id: notification.id });