Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/client/src/store/chat.ts

92 lines
1.8 KiB
TypeScript
Raw Normal View History

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,
2020-01-21 03:36:18 +13:00
})
export const getters = getterTree(state, {
//
})
export const mutations = mutationTree(state, {
addMessage(state, message: Message) {
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
},
reset(state) {
2020-01-24 04:23:26 +13:00
state.emotes = {}
2020-01-23 06:16:40 +13:00
state.history = []
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
},
)