Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
m1k1o
GitHub Repository: m1k1o/neko
Path: blob/master/client/src/store/chat.ts
1301 views
1
import { getterTree, mutationTree, actionTree } from 'typed-vuex'
2
import { makeid } from '~/utils'
3
import { EVENT } from '~/neko/events'
4
import { accessor } from '~/store'
5
6
export const namespaced = true
7
8
interface Emote {
9
type: string
10
}
11
12
interface Emotes {
13
[id: string]: Emote
14
}
15
16
interface Message {
17
id: string
18
content: string
19
created: Date
20
type: 'text' | 'event'
21
}
22
23
export const state = () => ({
24
history: [] as Message[],
25
emotes: {} as Emotes,
26
texts: 0,
27
})
28
29
export const getters = getterTree(state, {
30
//
31
})
32
33
export const mutations = mutationTree(state, {
34
addMessage(state, message: Message) {
35
if (message.type == 'text') {
36
state.texts++
37
}
38
39
state.history = state.history.concat([message])
40
},
41
42
addEmote(state, { id, emote }: { id: string; emote: Emote }) {
43
state.emotes = {
44
...state.emotes,
45
[id]: emote,
46
}
47
},
48
49
delEmote(state, id: string) {
50
const emotes = {
51
...state.emotes,
52
}
53
delete emotes[id]
54
state.emotes = emotes
55
},
56
57
reset(state) {
58
state.emotes = {}
59
state.history = []
60
state.texts = 0
61
},
62
})
63
64
export const actions = actionTree(
65
{ state, getters, mutations },
66
{
67
newEmote(store, emote: Emote) {
68
if (accessor.settings.ignore_emotes || document.visibilityState === 'hidden') {
69
return
70
}
71
72
const id = makeid(10)
73
accessor.chat.addEmote({ id, emote })
74
},
75
76
newMessage(store, message: Message) {
77
if (accessor.settings.chat_sound) {
78
new Audio('chat.mp3').play().catch(console.error)
79
}
80
accessor.chat.addMessage(message)
81
},
82
83
sendMessage(store, content: string) {
84
if (!accessor.connected || accessor.user.muted) {
85
return
86
}
87
$client.sendMessage(EVENT.CHAT.MESSAGE, { content })
88
},
89
90
sendEmote(store, emote: string) {
91
if (!accessor.connected || accessor.user.muted) {
92
return
93
}
94
$client.sendMessage(EVENT.CHAT.EMOTE, { emote })
95
},
96
},
97
)
98
99