forked from jeroenvanmaanen/dendrite
/
aggregate.go
87 lines (75 loc) · 2.57 KB
/
aggregate.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
package trusted
import (
log "log"
proto "github.com/golang/protobuf/proto"
axon_utils "github.com/dendrite2go/dendrite/src/pkg/axon_utils"
axon_server "github.com/dendrite2go/dendrite/src/pkg/grpc/axon_server"
grpc_config "github.com/dendrite2go/dendrite/src/pkg/grpc/dendrite_config"
)
const AggregateIdentifier = "trusted-keys-aggregate"
func HandleRegisterTrustedKeyCommand(commandMessage *axon_server.Command, clientConnection *axon_utils.ClientConnection) (*axon_utils.Error, error) {
command := grpc_config.RegisterTrustedKeyCommand{}
e := proto.Unmarshal(commandMessage.Payload.Data, &command)
if e != nil {
log.Printf("Could not unmarshal RegisterTrustedKeyCommand")
return nil, e
}
projection := RestoreProjection(AggregateIdentifier, clientConnection)
currentValue := projection.TrustedKeys[command.PublicKey.Name]
newValue := command.PublicKey.PublicKey
if newValue == currentValue {
return nil, nil
}
var eventType string
var event axon_utils.Event
if len(newValue) > 0 {
eventType = "TrustedKeyAddedEvent"
event = &TrustedKeyAddedEvent{
grpc_config.TrustedKeyAddedEvent{
PublicKey: command.PublicKey,
},
}
} else {
eventType = "TrustedKeyRemovedEvent"
event = &TrustedKeyRemovedEvent{
grpc_config.TrustedKeyRemovedEvent{
Name: command.PublicKey.Name,
},
}
}
log.Printf("Trusted aggregate: emit: %v: %v", eventType, event)
return axon_utils.AppendEvent(event, AggregateIdentifier, projection, clientConnection)
}
func HandleRegisterKeyManagerCommand(commandMessage *axon_server.Command, clientConnection *axon_utils.ClientConnection) (*axon_utils.Error, error) {
command := grpc_config.RegisterKeyManagerCommand{}
e := proto.Unmarshal(commandMessage.Payload.Data, &command)
if e != nil {
log.Printf("Could not unmarshal RegisterKeyManagerCommand")
return nil, e
}
projection := RestoreProjection(AggregateIdentifier, clientConnection)
currentValue := projection.KeyManagers[command.PublicKey.Name]
newValue := command.PublicKey.PublicKey
if newValue == currentValue {
return nil, nil
}
var eventType string
var event axon_utils.Event
if len(newValue) > 0 {
eventType = "KeyManagerAddedEvent"
event = &KeyManagerAddedEvent{
grpc_config.KeyManagerAddedEvent{
PublicKey: command.PublicKey,
},
}
} else {
eventType = "KeyManagerRemovedEvent"
event = &KeyManagerRemovedEvent{
grpc_config.KeyManagerRemovedEvent{
Name: command.PublicKey.Name,
},
}
}
log.Printf("Trusted aggregate: emit: %v: %v", eventType, event)
return axon_utils.AppendEvent(event, AggregateIdentifier, projection, clientConnection)
}