diff --git a/src/boot/after_store.js b/src/boot/after_store.js index bece8e000..2b2c75362 100644 --- a/src/boot/after_store.js +++ b/src/boot/after_store.js @@ -37,6 +37,7 @@ import { useInterfaceStore } from 'src/stores/interface.js' import { useLocalConfigStore } from 'src/stores/local_config.js' import { useMergedConfigStore } from 'src/stores/merged_config.js' import { useOAuthStore } from 'src/stores/oauth.js' +import { useStatusesStore } from 'src/stores/statuses.js' import { useSyncConfigStore } from 'src/stores/sync_config.js' import { useUserHighlightStore } from 'src/stores/user_highlight.js' import { useUsersStore } from 'src/stores/users.js' @@ -590,6 +591,10 @@ const afterStoreSetup = async ({ pinia, store, storageError, i18n }) => { useI18nStore().setI18n(i18n) + // Global WS handlers + useInterfaceStore().attachSocket() + useStatusesStore().attachSocket() + app.use(router) app.use(store) app.use(i18n) diff --git a/src/stores/chats.js b/src/stores/chats.js index 2a65fc25f..f12a33db1 100644 --- a/src/stores/chats.js +++ b/src/stores/chats.js @@ -5,6 +5,7 @@ import { maybeShowChatNotification } from '../services/chat_utils/chat_utils.js' import { promiseInterval } from '../services/promise_interval/promise_interval.js' import { useOAuthStore } from 'src/stores/oauth.js' +import { useStreamingStore } from 'src/stores/streaming.js' import { useUsersStore } from 'src/stores/users.js' import { chats } from 'src/api/chats.js' @@ -34,6 +35,14 @@ export const useChatsStore = defineStore('chats', { }, }, actions: { + attachSocket() { + const et = new EventTarget() + const socket = { et } + + et.addEventListener('pleroma:chat_update', this.updateChat) + + useStreamingStore().addSubscriber(socket) + }, startFetchingChats() { const fetcher = () => this.fetchChats() this.setChatListFetcher(() => promiseInterval(fetcher, 5000)) diff --git a/src/stores/interface.js b/src/stores/interface.js index 6eede1d3a..19aa37221 100644 --- a/src/stores/interface.js +++ b/src/stores/interface.js @@ -9,9 +9,11 @@ import { deserialize } from '../services/theme_data/iss_deserializer.js' import { useInstanceStore } from 'src/stores/instance.js' import { useMergedConfigStore } from 'src/stores/merged_config.js' +import { useStreamingStore } from 'src/stores/streaming.js' import { useSyncConfigStore } from 'src/stores/sync_config.js' import { useUsersStore } from 'src/stores/users.js' +import { WSConnectionStatus } from 'src/api/websocket.js' import { CURRENT_VERSION, generatePreset, @@ -74,6 +76,34 @@ export const useInterfaceStore = defineStore('interface', { foreignProfileBackground: null, }), actions: { + attachSocket() { + const et = new EventTarget() + const socket = { et } + + et.addEventListener('open', this.onStreamConnect) + et.addEventListener('close', this.onStreamDisconnect) + + useStreamingStore().addSubscriber(socket) + }, + onStreamConnect() { + if (useStreamingStore().state !== WSConnectionStatus.STARTING_INITIAL) { + this.pushGlobalNotice({ + level: 'success', + messageKey: 'timeline.socket_reconnected', + timeout: 5000, + }) + } + }, + onStreamDisconnect(closeEvent) { + // TODO better explanation/localization + const { code } = closeEvent + this.pushGlobalNotice({ + level: 'error', + messageKey: 'timeline.socket_broke', + messageArgs: [code], + timeout: 5000, + }) + }, setTemporaryChanges({ confirm, revert }) { this.temporaryChangesCountdown = 10 this.temporaryChangesConfirm = confirm diff --git a/src/stores/users.js b/src/stores/users.js index 06b3f0386..009d86c26 100644 --- a/src/stores/users.js +++ b/src/stores/users.js @@ -725,7 +725,6 @@ export const useUsersStore = defineStore('users', { } // DMs and Home - useStatusesStore().attachSocket() useNotificationsStore().activate() useTimelinesStore().activatePersistents() @@ -741,9 +740,9 @@ export const useUsersStore = defineStore('users', { dispatch('startFetchingFollowRequests') } - useStreamingStore().initSocket() + useStreamingStore().initSocket(true) if (useMergedConfigStore().mergedConfig.useStreamingApi) { - useStreamingStore().initSocket() + useStreamingStore().initSocket(true) } // Start fetching things that don't need to block the UI