-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathactivity.go
97 lines (87 loc) · 2.67 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
package stream
import (
"encoding/json"
"errors"
"github.com/fatih/structs"
)
// Activity is a Stream activity entity.
type Activity struct {
ID string `json:"id,omitempty"`
Actor string `json:"actor,omitempty"`
Verb string `json:"verb,omitempty"`
Object string `json:"object,omitempty"`
ForeignID string `json:"foreign_id,omitempty"`
Target string `json:"target,omitempty"`
Time Time `json:"time,omitempty"`
Origin string `json:"origin,omitempty"`
To []string `json:"to,omitempty"`
Score float64 `json:"score,omitempty"`
Extra map[string]any `json:"-"`
ScoreVars map[string]any `json:"score_vars,omitempty"`
}
// UnmarshalJSON decodes the provided JSON payload into the Activity. It's required
// because of the custom JSON fields and time formats.
func (a *Activity) UnmarshalJSON(b []byte) error {
var data map[string]any
if err := json.Unmarshal(b, &data); err != nil {
return err
}
if _, ok := data["to"]; ok {
tos := data["to"].([]any)
simpleTos := make([]string, len(tos))
for i := range tos {
switch to := tos[i].(type) {
case string:
simpleTos[i] = to
case []any:
tos, ok := to[0].(string)
if !ok {
return errors.New("invalid format for to targets")
}
simpleTos[i] = tos
}
}
data["to"] = simpleTos
}
return a.decode(data)
}
// MarshalJSON encodes the Activity to a valid JSON bytes slice. It's required because of
// the custom JSON fields and time formats.
func (a Activity) MarshalJSON() ([]byte, error) {
data := structs.New(a).Map()
for k, v := range a.Extra {
data[k] = v
}
if _, ok := data["time"]; ok {
data["time"] = a.Time.Format(TimeLayout)
}
return json.Marshal(data)
}
func (a *Activity) decode(data map[string]any) error {
meta, err := decodeData(data, a)
if err != nil {
return err
}
if len(meta.Unused) > 0 {
a.Extra = make(map[string]any)
for _, k := range meta.Unused {
a.Extra[k] = data[k]
}
}
return nil
}
// baseActivityGroup is the common part of responses obtained from reading normal or enriched aggregated feeds.
type baseActivityGroup struct {
ActivityCount int `json:"activity_count,omitempty"`
ActorCount int `json:"actor_count,omitempty"`
Group string `json:"group,omitempty"`
ID string `json:"id,omitempty"`
Verb string `json:"verb,omitempty"`
CreatedAt Time `json:"created_at,omitempty"`
UpdatedAt Time `json:"updated_at,omitempty"`
}
// ActivityGroup is a group of Activity obtained from aggregated feeds.
type ActivityGroup struct {
baseActivityGroup
Activities []Activity `json:"activities,omitempty"`
}