-
Notifications
You must be signed in to change notification settings - Fork 246
/
encoder.go
87 lines (75 loc) · 2.08 KB
/
encoder.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 incentivisation
import (
"bytes"
"io"
"reflect"
"time"
"github.com/russolsen/transit"
)
type StatusMessageContent struct {
ChatID string
Text string
}
type StatusMessage struct {
Text string
ContentT string
MessageT string
Clock int64
Timestamp int64
Content StatusMessageContent
}
// CreateTextStatusMessage creates a StatusMessage.
func CreateTextStatusMessage(text string, chatID string) StatusMessage {
ts := time.Now().Unix() * 1000
return StatusMessage{
Text: text,
ContentT: "text/plain",
MessageT: "public-group-user-message",
Clock: ts * 100,
Timestamp: ts,
Content: StatusMessageContent{ChatID: chatID, Text: text},
}
}
func EncodeMessage(content string, chatID string) ([]byte, error) {
value := CreateTextStatusMessage(content, chatID)
var buf bytes.Buffer
encoder := NewMessageEncoder(&buf)
if err := encoder.Encode(value); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// NewMessageEncoder returns a new Transit encoder
// that can encode StatusMessage values.
// More about Transit: https://github.com/cognitect/transit-format
func NewMessageEncoder(w io.Writer) *transit.Encoder {
encoder := transit.NewEncoder(w, false)
encoder.AddHandler(statusMessageType, defaultStatusMessageValueEncoder)
return encoder
}
var (
statusMessageType = reflect.TypeOf(StatusMessage{})
defaultStatusMessageValueEncoder = &statusMessageValueEncoder{}
)
type statusMessageValueEncoder struct{}
func (statusMessageValueEncoder) IsStringable(reflect.Value) bool {
return false
}
func (statusMessageValueEncoder) Encode(e transit.Encoder, value reflect.Value, asString bool) error {
message := value.Interface().(StatusMessage)
taggedValue := transit.TaggedValue{
Tag: "c4",
Value: []interface{}{
message.Text,
message.ContentT,
transit.Keyword(message.MessageT),
message.Clock,
message.Timestamp,
map[interface{}]interface{}{
transit.Keyword("chat-id"): message.Content.ChatID,
transit.Keyword("text"): message.Content.Text,
},
},
}
return e.EncodeInterface(taggedValue, false)
}