-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub_types.go
227 lines (199 loc) · 5.79 KB
/
sub_types.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package slk
import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"strconv"
"time"
)
// types used throughout
type (
Message struct {
Type string `json:"type"`
IsStarred bool `json:"is_starred"`
Reactions []struct {
Count int `json:"count"`
Name string `json:"name"`
Users []string `json:"users"`
} `json:"reactions"`
Text string `json:"text"`
Timestamp Time `json:"ts"`
User string `json:"user"`
}
ResultMessage struct {
Type string `json:"type"`
Channel struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"channel"`
Next struct {
Text string `json:"text"`
Timestamp Time `json:"ts"`
Type string `json:"type"`
User string `json:"user"`
Username string `json:"username"`
} `json:"next"`
Next2 struct {
Text string `json:"text"`
Timestamp Time `json:"ts"`
Type string `json:"type"`
User string `json:"user"`
Username string `json:"username"`
} `json:"next_2"`
Permalink string `json:"permalink"`
Previous struct {
Text string `json:"text"`
Timestamp Time `json:"ts"`
Type string `json:"type"`
User string `json:"user"`
Username string `json:"username"`
} `json:"previous"`
Previous2 struct {
Text string `json:"text"`
Timestamp Time `json:"ts"`
Type string `json:"type"`
User string `json:"user"`
Username string `json:"username"`
} `json:"previous_2"`
Text string `json:"text"`
Timestamp Time `json:"ts"`
User string `json:"user"`
Username string `json:"username"`
}
Comment struct {
ID string `json:"id"`
Created Time `json:"created"`
Timestamp Time `json:"timestamp"`
User string `json:"user"`
Comment string `json:"comment"`
}
// TODO: something
Item struct {
Type string `json:"type"`
Channel json.RawMessage `json:"channel"`
Message json.RawMessage `json:"message"`
File json.RawMessage `json:"file"`
Comment json.RawMessage `json:"comment"`
Created Time `json:"created"`
Reactions []struct {
Name string `json:"name"`
Count int `json:"count"`
Users []string `json:"users"`
} `json:"reactions"`
Timestamp Time `json:"ts"`
}
Team struct {
ID string `json:"id"`
Domain string `json:"domain"`
EmailDomain string `json:"email_domain"`
Icon struct {
Image102 string `json:"image_102"`
Image132 string `json:"image_132"`
Image34 string `json:"image_34"`
Image44 string `json:"image_44"`
Image68 string `json:"image_68"`
Image88 string `json:"image_88"`
ImageDefault bool `json:"image_default"`
} `json:"icon"`
MsgEditWindow Duration `json:"msg_edit_window_mins"`
Name string `json:"name"`
OverStorageLimit bool `json:"over_storage_limit"`
Plan string `json:"plan"`
Prefs map[string]interface{} `json:"prefs"`
}
Login struct {
Count int `json:"count"`
Country string `json:"country"`
DateFirst Time `json:"date_first"`
DateLast Time `json:"date_last"`
IP string `json:"ip"`
Isp string `json:"isp"`
Region string `json:"region"`
UserAgent string `json:"user_agent"`
UserID string `json:"user_id"`
Username string `json:"username"`
}
LogEntry struct {
ChangeType string `json:"change_type"`
Channel string `json:"channel"`
Date Time `json:"date"`
Scope string `json:"scope"`
ServiceID string `json:"service_id"`
ServiceType string `json:"service_type"`
UserID string `json:"user_id"`
UserName string `json:"user_name"`
}
FileType string
Presence string
Time struct {
time.Time
}
Duration struct {
time.Duration
}
)
const (
FileTypeAll FileType = `all` // All files
FileTypePosts FileType = `posts` // Posts
FileTypeSnippets FileType = `snippets` // Snippets
FileTypeImages FileType = `images` // Image files
FileTypeGdocs FileType = `gdocs` // Google docs
FileTypeZips FileType = `zips` // Zip files
FileTypePdfs FileType = `pdfs` // PDF files
PresenceAuto Presence = `auto`
PresenceAway Presence = `away`
ItemTypeChannel = `channel`
ItemTypeFile = `file`
ItemTypeFileComment = `file_comment`
ItemTypeGroup = `group`
ItemTypeIM = `im`
ItemTypeMessage = `message`
)
var unixNanoRegexp = regexp.MustCompile(`^\d+(?:\.\d+)?$`)
func (p *Presence) UnmarshallJSON(data []byte) error {
data = bytes.Trim(data, `"`)
*p = Presence(string(data))
return nil
}
func NewTime(t time.Time) Time {
return Time{t}
}
func (t Time) MarshalJSON() ([]byte, error) {
return []byte(`"` + t.SlackString() + `"`), nil
}
func (t *Time) UnmarshalJSON(data []byte) error {
var err error
data = bytes.Trim(data, `"`)
if unixNanoRegexp.Match(data) {
var sec, nsec int64
if i := bytes.IndexByte(data, '.'); i > -1 {
sec, _ = strconv.ParseInt(string(data[:i]), 10, 64)
nsec, _ = strconv.ParseInt(string(data[i+1:]), 10, 64)
} else {
sec, _ = strconv.ParseInt(string(data), 10, 64)
}
t.Time = time.Unix(sec, nsec)
} else {
t.Time, err = time.Parse(time.RFC3339, string(data))
}
return err
}
func (t Time) SlackString() string {
u := time.Duration(t.Time.UnixNano())
return fmt.Sprintf("%d.%06d", u/time.Second, u%time.Second)
}
func NewDuration(t time.Duration) Duration {
return Duration{t}
}
func (d Duration) MarshalJSON() ([]byte, error) {
return []byte(d.SlackString()), nil
}
func (d *Duration) UnmarshalJSON(data []byte) error {
sec, _ := strconv.ParseInt(string(data), 10, 64)
d.Duration = time.Duration(sec) * time.Second
return nil
}
func (d Duration) SlackString() string {
return strconv.Itoa(int(d.Duration / time.Second))
}