-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
113 lines (88 loc) · 3.02 KB
/
message.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
package core
import (
"encoding/json"
"github.com/imperviousai/imp-daemon/comm"
"github.com/imperviousai/imp-daemon/messages"
"go.uber.org/zap"
)
func (c *core) SendMessage(message string, toDid string, amt int64, replyToId string) (string, error) {
zap.L().Debug("[Core] SendMessage",
zap.String("to_did", toDid),
)
didCommEnvelope := comm.NewBasicMsg(message)
didCommEnvelope.To = []string{toDid}
didCommEnvelope.THID = replyToId
didCommEnvelope.AddAmountMetadata(amt)
msgId, err := c.didComm.SendMsg(didCommEnvelope, amt, nil, nil)
if err != nil {
zap.L().Error("[Core] SendMessage failed", zap.String("error", err.Error()))
return "", err
}
zap.L().Debug("[Core] SendMessage success", zap.String("message_id", msgId))
return msgId, nil
}
func (c *core) SendMessageV2(body string, didCommType string, dids []string, amt int64, replyToId string, groupId string, settings *comm.MessageSettings) (string, error) {
zap.L().Debug("[Core] SendMessageV2",
zap.Strings("to_did", dids),
)
simpleMsg, err := createDIDCommMessageFromParts(body, didCommType, dids, amt, replyToId, groupId)
if err != nil {
return "", err
}
msgId, err := c.didComm.SendMsg(simpleMsg, amt, nil, settings)
if err != nil {
zap.L().Error("[Core] SendMessageV2 failed", zap.String("error", err.Error()))
return "", err
}
zap.L().Debug("[Core] SendMessageV2 success", zap.String("message_id", msgId))
return msgId, nil
}
func createDIDCommMessageFromParts(body string, didCommType string, dids []string, amt int64, replyToId string, groupId string) (*comm.DIDCommMsg, error) {
// TODO if it is a groupId and not list of dids, then find and fill group
// create a didcomm simple message and stick body/type/toDid there
simpleMsg := comm.NewDIDCommMsg()
simpleMsg.Type = didCommType
simpleMsg.To = dids
// unmarshal body to json
var mapData map[string]interface{}
if err := json.Unmarshal([]byte(body), &mapData); err != nil {
return nil, err
}
simpleMsg.Body = mapData
simpleMsg.AddAmountMetadata(amt)
if replyToId != "" {
simpleMsg.THID = replyToId
}
return simpleMsg, nil
}
func (c *core) GetMessages() ([]*messages.MessageInfo, error) {
return c.messageManager.GetMessages()
}
func (c *core) SaveMessage(body string, didCommType string, dids []string, from string, replyToId string, groupId string) (string, error) {
zap.L().Debug("[Core] SaveMessage",
zap.Strings("to_did", dids),
)
simpleMsg, err := createDIDCommMessageFromParts(body, didCommType, dids, 0, replyToId, groupId)
if err != nil {
return "", nil
}
// Always send long form, for now
simpleMsg.From = from
envelopeBytes, err := json.Marshal(simpleMsg)
if err != nil {
return "", err
}
err = c.messageManager.SaveMessage(&messages.MessageInfo{
Id: simpleMsg.ID,
Type: simpleMsg.Type,
Recipients: append(simpleMsg.To, simpleMsg.From),
Data: envelopeBytes,
})
if err != nil {
return "", err
}
return simpleMsg.ID, nil
}
func (c *core) DeleteMessage(id, groupId string) error {
return c.messageManager.DeleteMessage(id, groupId)
}