move phoenix socket stuff into shoutstore, removing api vuex module

This commit is contained in:
Henry Jameson 2026-08-31 19:22:12 +03:00
commit d6c6c44e89
4 changed files with 30 additions and 60 deletions

View file

@ -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

View file

@ -1,9 +1,7 @@
import api from './api.js'
import drafts from './drafts.js'
import profileConfig from './profileConfig.js'
export default {
api,
profileConfig,
drafts,
}

View file

@ -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
}
},
})

View file

@ -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