2020-01-21 03:36:18 +13:00
|
|
|
import { getterTree, mutationTree, actionTree } from 'typed-vuex'
|
2020-01-24 04:23:26 +13:00
|
|
|
import { makeid } from '~/utils'
|
|
|
|
import { EVENT } from '~/neko/events'
|
2020-01-23 06:16:40 +13:00
|
|
|
import { accessor } from '~/store'
|
2020-01-21 03:36:18 +13:00
|
|
|
|
|
|
|
export const namespaced = true
|
|
|
|
|
2020-01-24 04:23:26 +13:00
|
|
|
interface Emote {
|
|
|
|
type: string
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Emotes {
|
|
|
|
[id: string]: Emote
|
|
|
|
}
|
|
|
|
|
2020-01-21 03:36:18 +13:00
|
|
|
interface Message {
|
|
|
|
id: string
|
|
|
|
content: string
|
|
|
|
created: Date
|
2020-01-23 06:16:40 +13:00
|
|
|
type: 'text' | 'event'
|
2020-01-21 03:36:18 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
export const state = () => ({
|
2020-01-23 06:16:40 +13:00
|
|
|
history: [] as Message[],
|
2020-01-24 04:23:26 +13:00
|
|
|
emotes: {} as Emotes,
|
2021-07-17 22:38:12 +12:00
|
|
|
texts: 0,
|
2020-01-21 03:36:18 +13:00
|
|
|
})
|
|
|
|
|
|
|
|
export const getters = getterTree(state, {
|
|
|
|
//
|
|
|
|
})
|
|
|
|
|
|
|
|
export const mutations = mutationTree(state, {
|
|
|
|
addMessage(state, message: Message) {
|
2021-07-17 22:38:12 +12:00
|
|
|
if (message.type == 'text') {
|
|
|
|
state.texts++
|
|
|
|
}
|
|
|
|
|
2020-01-23 06:16:40 +13:00
|
|
|
state.history = state.history.concat([message])
|
|
|
|
},
|
2020-01-24 04:23:26 +13:00
|
|
|
|
|
|
|
addEmote(state, { id, emote }: { id: string; emote: Emote }) {
|
|
|
|
state.emotes = {
|
|
|
|
...state.emotes,
|
|
|
|
[id]: emote,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
delEmote(state, id: string) {
|
|
|
|
const emotes = {
|
|
|
|
...state.emotes,
|
|
|
|
}
|
|
|
|
delete emotes[id]
|
|
|
|
state.emotes = emotes
|
|
|
|
},
|
|
|
|
|
2020-02-05 04:37:56 +13:00
|
|
|
reset(state) {
|
2020-01-24 04:23:26 +13:00
|
|
|
state.emotes = {}
|
2020-01-23 06:16:40 +13:00
|
|
|
state.history = []
|
2021-07-17 22:38:12 +12:00
|
|
|
state.texts = 0
|
2020-01-21 03:36:18 +13:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
export const actions = actionTree(
|
|
|
|
{ state, getters, mutations },
|
|
|
|
{
|
2020-01-24 04:23:26 +13:00
|
|
|
newEmote(store, emote: Emote) {
|
2020-01-31 12:58:26 +13:00
|
|
|
if (accessor.settings.ignore_emotes || document.visibilityState === 'hidden') {
|
2020-01-24 04:23:26 +13:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = makeid(10)
|
|
|
|
accessor.chat.addEmote({ id, emote })
|
|
|
|
},
|
|
|
|
|
|
|
|
newMessage({ state }, message: Message) {
|
|
|
|
if (accessor.settings.chat_sound) {
|
|
|
|
new Audio('chat.mp3').play().catch(console.error)
|
|
|
|
}
|
|
|
|
accessor.chat.addMessage(message)
|
|
|
|
},
|
|
|
|
|
2020-01-23 06:16:40 +13:00
|
|
|
sendMessage(store, content: string) {
|
|
|
|
if (!accessor.connected || accessor.user.muted) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$client.sendMessage(EVENT.CHAT.MESSAGE, { content })
|
|
|
|
},
|
|
|
|
|
2020-01-24 04:23:26 +13:00
|
|
|
sendEmote(store, emote: string) {
|
|
|
|
if (!accessor.connected || accessor.user.muted) {
|
2020-01-23 06:16:40 +13:00
|
|
|
return
|
|
|
|
}
|
2020-01-24 04:23:26 +13:00
|
|
|
$client.sendMessage(EVENT.CHAT.EMOTE, { emote })
|
2020-01-23 06:16:40 +13:00
|
|
|
},
|
2020-01-21 03:36:18 +13:00
|
|
|
},
|
|
|
|
)
|