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