/
instance.go
152 lines (122 loc) · 3.24 KB
/
instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package events
import (
"encoding/json"
"hash/crc32"
"time"
"github.com/nats-io/nats.go"
"github.com/seventv/common/structures/v3"
"github.com/seventv/common/utils"
"go.uber.org/zap"
"github.com/seventv/api/data/model"
)
type Instance interface {
Dispatch(t EventType, cm ChangeMap, cond ...EventCondition)
DispatchWithEffect(t EventType, cm ChangeMap, opt DispatchOptions, cond ...EventCondition) Message[DispatchPayload]
}
type EventsInst struct {
nc *nats.Conn
subject string
}
func NewPublisher(nc *nats.Conn, subject string) Instance {
return &EventsInst{
nc: nc,
subject: subject,
}
}
// systemUser is a placeholder for the ChangeMap actor when no actor was provided
var systemUser = model.UserModel{
ID: structures.SystemUser.ID,
UserType: model.UserTypeModel(structures.SystemUser.UserType),
Username: structures.SystemUser.Username,
DisplayName: structures.SystemUser.DisplayName,
}
func (inst *EventsInst) Dispatch(t EventType, cm ChangeMap, cond ...EventCondition) {
if cm.Actor.ID.IsZero() {
cm.Actor = systemUser.ToPartial()
}
// Dedupe hash
// TODO: is this dedupe necessary?
var dedupeHash *uint32
if cm.Object != nil {
h := crc32.New(crc32.MakeTable(2596996162))
h.Write(cm.ID[:])
h.Write(utils.S2B(cm.Kind.String()))
h.Write(cm.Object)
dedupeHash = utils.PointerOf(h.Sum32())
}
msg := NewMessage(OpcodeDispatch, DispatchPayload{
Type: t,
Body: cm,
Hash: dedupeHash,
Conditions: cond,
})
data, err := json.Marshal(msg)
if err != nil {
zap.S().Warnw("failed to marshal event",
"error", err.Error(),
)
return
}
for _, c := range cond {
// TODO: check if it's necesary to publish for both bools
err = inst.nc.Publish(inst.subject+"."+CreateDispatchKey(t, c), data)
if err != nil {
zap.S().Errorw("nats publish", "error", err)
}
}
}
func (inst *EventsInst) DispatchWithEffect(t EventType, cm ChangeMap, opt DispatchOptions, cond ...EventCondition) Message[DispatchPayload] {
if cm.Actor.ID.IsZero() {
cm.Actor = systemUser.ToPartial()
}
// Dedupe hash
var dedupeHash *uint32
if !opt.DisableDedupe && cm.Object != nil {
h := crc32.New(crc32.MakeTable(2596996162))
h.Write(cm.ID[:])
h.Write(utils.S2B(cm.Kind.String()))
h.Write(cm.Object)
dedupeHash = utils.PointerOf(h.Sum32())
}
msg := NewMessage(OpcodeDispatch, DispatchPayload{
Type: t,
Body: cm,
Hash: dedupeHash,
Conditions: cond,
Effect: opt.Effect,
Whisper: opt.Whisper,
})
data, err := json.Marshal(msg)
if err != nil {
zap.S().Warnw("failed to marshal event",
"error", err.Error(),
)
return msg
}
payloads := make(map[string][]byte)
if opt.Whisper == "" {
for _, c := range cond {
payloads[CreateDispatchKey(t, c)] = data
}
} else {
payloads[CreateDispatchKey(EventTypeWhisper, EventCondition{"session_id": opt.Whisper})] = data
}
go func() {
if opt.Delay > 0 {
<-time.After(opt.Delay)
}
for key, data := range payloads {
err = inst.nc.Publish(inst.subject+"."+key, data)
if err != nil {
zap.S().Errorw("nats publish", "error", err)
}
}
}()
return msg
}
type DispatchOptions struct {
Delay time.Duration
Whisper string
Effect *SessionEffect
DisableDedupe bool
}