diff --git a/src/modules/api.js b/src/modules/api.js deleted file mode 100644 index 1395752e0..000000000 --- a/src/modules/api.js +++ /dev/null @@ -1,53 +0,0 @@ -import { Socket } from 'phoenix' - -import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.js' -import { useShoutStore } from 'src/stores/shout.js' - -const api = { - state: { - fetchers: {}, - socket: null, - }, - mutations: { - addFetcher(state, { fetcherName, fetcher }) { - state.fetchers[fetcherName] = fetcher - }, - removeFetcher(state, { fetcherName }) { - state.fetchers[fetcherName].stop() - delete state.fetchers[fetcherName] - }, - setWsToken(state, token) { - state.wsToken = token - }, - setSocket(state, socket) { - state.socket = socket - }, - }, - actions: { - // Pleroma websocket - setWsToken(store, token) { - store.commit('setWsToken', token) - }, - initializeSocket({ commit, state, rootState }) { - // Set up websocket connection - const token = state.wsToken - if ( - useInstanceCapabilitiesStore().shoutAvailable && - token !== undefined && - state.socket === null - ) { - const socket = new Socket('/socket', { params: { token } }) - socket.connect() - - commit('setSocket', socket) - useShoutStore().initializeShout(socket) - } - }, - disconnectFromSocket({ commit, state }) { - state.socket?.disconnect() - commit('setSocket', null) - }, - }, -} - -export default api diff --git a/src/modules/index.js b/src/modules/index.js index 6aa236257..61b7fd996 100644 --- a/src/modules/index.js +++ b/src/modules/index.js @@ -1,9 +1,7 @@ -import api from './api.js' import drafts from './drafts.js' import profileConfig from './profileConfig.js' export default { - api, profileConfig, drafts, } diff --git a/src/stores/shout.js b/src/stores/shout.js index 79268bd57..f76b37f5e 100644 --- a/src/stores/shout.js +++ b/src/stores/shout.js @@ -1,14 +1,33 @@ -import { defineStore } from 'pinia' +import { Socket } from 'phoenix' +import { defineStore } from 'pinia' +import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.js' +import { useUsersStore } from 'src/stores/users.js' + +// Maybe rename it to PhoenixSocket if we ever utilize this socket more export const useShoutStore = defineStore('shout', { state: () => ({ messages: [], channel: { state: '' }, joined: false, + token: null, + socket: null, }), + getters: { + token: () => useUsersStore().currentUser?.token + }, actions: { - initializeShout(socket) { - const channel = socket.channel('chat:public') + initializeSocket() { + if (this.token === null) return + if (!useInstanceCapabilitiesStore().shoutAvailable) return + if (this.socket !== null) throw new Error('Shout socket already exist!') + + this.socket = new Socket('/socket', { params: { token: this.token } }) + this.socket.connect() + }, + initializeShout() { + const channel = this.socket.channel('chat:public') + channel.joinPush.receive('ok', () => { this.joined = true }) @@ -28,5 +47,9 @@ export const useShoutStore = defineStore('shout', { channel.join() this.channel = channel }, + disconnectSocket() { + this.socket?.disconnect() + this.socket = null + } }, }) diff --git a/src/stores/users.js b/src/stores/users.js index 78527b4c5..305d0fc5b 100644 --- a/src/stores/users.js +++ b/src/stores/users.js @@ -11,6 +11,7 @@ import { useInstanceStore } from 'src/stores/instance.js' import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.js' import { useInterfaceStore } from 'src/stores/interface.js' import { useListsStore } from 'src/stores/lists.js' +import { useShoutStore } from 'src/stores/shout.js' import { useMergedConfigStore } from 'src/stores/merged_config.js' import { useNotificationsStore } from 'src/stores/notifications.js' import { useOAuthStore } from 'src/stores/oauth.js' @@ -663,8 +664,8 @@ export const useUsersStore = defineStore('users', { if (user.token) { // Shoutbox - dispatch('setWsToken', user.token) - dispatch('initializeSocket') + useShoutStore().initializeSocket() + useShoutStore().initializeShout() } // DMs and Home @@ -747,6 +748,7 @@ export const useUsersStore = defineStore('users', { }) .then(() => { oauth.clearToken() + useShoutStore().disconnectSocket() this.currentUser = null