This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
forked from mrhenry/go-getstream
-
Notifications
You must be signed in to change notification settings - Fork 10
/
activity.go
211 lines (176 loc) · 4.25 KB
/
activity.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package getstream
import (
"encoding/json"
"regexp"
"strings"
"time"
)
// Activity is a getstream Activity
// Use it to post activities to Feeds
// It is also the response from Fetch and List Requests
type Activity struct {
ID string
Actor string
Verb string
Object string
Target string
Origin FeedID
TimeStamp *time.Time
ForeignID string
Data *json.RawMessage
MetaData map[string]string
To []Feed
}
// MarshalJSON is the custom marshal function for Activities
// It will be used by json.Marshal()
func (a Activity) MarshalJSON() ([]byte, error) {
payload := make(map[string]interface{})
for key, value := range a.MetaData {
payload[key] = value
}
payload["actor"] = a.Actor
payload["verb"] = a.Verb
payload["object"] = a.Object
payload["origin"] = a.Origin.Value()
if a.ID != "" {
payload["id"] = a.ID
}
if a.Target != "" {
payload["target"] = a.Target
}
if a.Data != nil {
payload["data"] = a.Data
}
if a.ForeignID != "" {
payload["foreign_id"] = a.ForeignID
}
if a.TimeStamp == nil {
payload["time"] = time.Now().Format("2006-01-02T15:04:05.999999")
} else {
payload["time"] = a.TimeStamp.Format("2006-01-02T15:04:05.999999")
}
var tos []string
for _, feed := range a.To {
to := feed.FeedID().Value()
if feed.Token() != "" {
to += " " + feed.Token()
}
tos = append(tos, to)
}
if len(tos) > 0 {
payload["to"] = tos
}
return json.Marshal(payload)
}
// UnmarshalJSON is the custom unmarshal function for Activities
// It will be used by json.Unmarshal()
func (a *Activity) UnmarshalJSON(b []byte) (err error) {
rawPayload := make(map[string]*json.RawMessage)
metadata := make(map[string]string)
err = json.Unmarshal(b, &rawPayload)
if err != nil {
return err
}
for key, value := range rawPayload {
lowerKey := strings.ToLower(key)
if value == nil {
continue
}
if lowerKey == "id" {
var strValue string
json.Unmarshal(*value, &strValue)
a.ID = strValue
} else if lowerKey == "actor" {
var strValue string
json.Unmarshal(*value, &strValue)
a.Actor = strValue
} else if lowerKey == "verb" {
var strValue string
json.Unmarshal(*value, &strValue)
a.Verb = strValue
} else if lowerKey == "foreign_id" {
var strValue string
json.Unmarshal(*value, &strValue)
a.ForeignID = strValue
} else if lowerKey == "object" {
var strValue string
json.Unmarshal(*value, &strValue)
a.Object = strValue
} else if lowerKey == "origin" {
var strValue string
json.Unmarshal(*value, &strValue)
a.Origin = FeedID(strValue)
} else if lowerKey == "target" {
var strValue string
json.Unmarshal(*value, &strValue)
a.Target = strValue
} else if lowerKey == "time" {
var strValue string
err := json.Unmarshal(*value, &strValue)
if err != nil {
continue
}
timeStamp, err := time.Parse("2006-01-02T15:04:05.999999", strValue)
if err != nil {
continue
}
a.TimeStamp = &timeStamp
} else if lowerKey == "data" {
a.Data = value
} else if lowerKey == "to" {
var to1D []string
var to2D [][]string
err := json.Unmarshal(*value, &to1D)
if err != nil {
err = nil
err = json.Unmarshal(*value, &to2D)
if err != nil {
continue
}
for _, to := range to2D {
if len(to) == 2 {
feedStr := to[0] + " " + to[1]
to1D = append(to1D, feedStr)
} else if len(to) == 1 {
to1D = append(to1D, to[0])
}
}
}
for _, to := range to1D {
feed := GeneralFeed{}
match, err := regexp.MatchString(`^\w+:\w+ .*?$`, to)
if err != nil {
continue
}
if match {
firstSplit := strings.Split(to, ":")
secondSplit := strings.Split(firstSplit[1], " ")
feed.FeedSlug = firstSplit[0]
feed.UserID = secondSplit[0]
feed.token = secondSplit[1]
a.To = append(a.To, &feed)
continue
}
match = false
err = nil
match, err = regexp.MatchString(`^\w+:\w+$`, to)
if err != nil {
continue
}
if match {
firstSplit := strings.Split(to, ":")
feed.FeedSlug = firstSplit[0]
feed.UserID = firstSplit[1]
a.To = append(a.To, &feed)
continue
}
}
} else {
var strValue string
json.Unmarshal(*value, &strValue)
metadata[key] = strValue
}
}
a.MetaData = metadata
return nil
}