-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparsers.go
102 lines (81 loc) · 1.7 KB
/
parsers.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
package dggchat
import (
"encoding/json"
"strings"
"time"
)
func parseMessage(s string) (Message, error) {
var m message
err := json.Unmarshal([]byte(s), &m)
if err != nil {
return Message{}, err
}
user := User{
Nick: m.Nick,
Features: m.Features,
}
message := Message{
Sender: user,
Timestamp: unixToTime(m.Timestamp),
Message: m.Data,
}
return message, nil
}
func parseNames(s string) (namesMessage, error) {
var nm namesMessage
err := json.Unmarshal([]byte(s), &nm)
if err != nil {
return namesMessage{}, err
}
return nm, nil
}
func parseRoomAction(s string) (RoomAction, error) {
var ra roomAction
err := json.Unmarshal([]byte(s), &ra)
if err != nil {
return RoomAction{}, err
}
roomAction := RoomAction{
User: User{
Nick: ra.Nick,
Features: ra.Features,
},
Timestamp: unixToTime(ra.Timestamp),
}
return roomAction, nil
}
func parseErrorMessage(s string) string {
return strings.Replace(s, `"`, "", -1)
}
func parsePrivateMessage(s string) (PrivateMessage, error) {
var pm privateMessage
err := json.Unmarshal([]byte(s), &pm)
if err != nil {
return PrivateMessage{}, err
}
privateMessage := PrivateMessage{
User: User{
Nick: pm.Nick,
Features: make([]string, 0),
},
ID: pm.MessageID,
Message: pm.Data,
Timestamp: unixToTime(pm.Timestamp),
}
return privateMessage, nil
}
func parseBroadcast(s string) (Broadcast, error) {
var b broadcast
err := json.Unmarshal([]byte(s), &b)
if err != nil {
return Broadcast{}, err
}
broadcast := Broadcast{
Message: b.Data,
Timestamp: unixToTime(b.Timestamp),
}
return broadcast, nil
}
func unixToTime(stamp int64) time.Time {
return time.Unix(stamp/1000, 0)
}