Remove JSONTimeString
The Slack API docs explicitly states that their funny timestamps are to be treated as a sort of message IDs and are guaranteed to be unique in the channel. In update and delete calls it is necessary to know the timestamp-string or the call will fail. Client code will use these timestamp-strings for things like checking for duplicate messages.
Instead of casting incoming timestamp-strings to (english speaking) human-readable strings like "Sun Jan 31" I think it's wise to let the client do its own formatting and leave the timestamp-strings be.
David Palm
8 years ago
3 | 3 | type ChannelCreatedEvent struct { |
4 | 4 | Type string `json:"type"` |
5 | 5 | Channel ChannelCreatedInfo `json:"channel"` |
6 | EventTimestamp JSONTimeString `json:"event_ts"` | |
6 | EventTimestamp string `json:"event_ts"` | |
7 | 7 | } |
8 | 8 | |
9 | 9 | // ChannelCreatedInfo represents the information associated with the Channel created event |
27 | 27 | // channel_deleted |
28 | 28 | // channel_archive |
29 | 29 | // channel_unarchive |
30 | Type string `json:"type"` | |
31 | Channel string `json:"channel"` | |
32 | User string `json:"user,omitempty"` | |
33 | Timestamp *JSONTimeString `json:"ts,omitempty"` | |
30 | Type string `json:"type"` | |
31 | Channel string `json:"channel"` | |
32 | User string `json:"user,omitempty"` | |
33 | Timestamp string `json:"ts,omitempty"` | |
34 | 34 | } |
35 | 35 | |
36 | 36 | // ChannelRenameEvent represents the Channel rename event |
42 | 42 | |
43 | 43 | // ChannelRenameInfo represents the information associated with a Channel rename event |
44 | 44 | type ChannelRenameInfo struct { |
45 | ID string `json:"id"` | |
46 | Name string `json:"name"` | |
47 | Created *JSONTimeString `json:"created"` | |
45 | ID string `json:"id"` | |
46 | Name string `json:"name"` | |
47 | Created string `json:"created"` | |
48 | 48 | } |
49 | 49 | |
50 | 50 | // ChannelHistoryChangedEvent represents the Channel history changed event |
51 | 51 | type ChannelHistoryChangedEvent struct { |
52 | Type string `json:"type"` | |
53 | Latest JSONTimeString `json:"latest"` | |
54 | Timestamp JSONTimeString `json:"ts"` | |
55 | EventTimestamp JSONTimeString `json:"event_ts"` | |
52 | Type string `json:"type"` | |
53 | Latest string `json:"latest"` | |
54 | Timestamp string `json:"ts"` | |
55 | EventTimestamp string `json:"event_ts"` | |
56 | 56 | } |
57 | 57 | |
58 | 58 | // ChannelMarkedEvent represents the Channel marked event |
1 | 1 | |
2 | 2 | // FileActionEvent represents the File action event |
3 | 3 | type fileActionEvent struct { |
4 | Type string `json:"type"` | |
5 | EventTimestamp JSONTimeString `json:"event_ts"` | |
6 | File File `json:"file"` | |
4 | Type string `json:"type"` | |
5 | EventTimestamp string `json:"event_ts"` | |
6 | File File `json:"file"` | |
7 | 7 | // FileID is used for FileDeletedEvent |
8 | 8 | FileID string `json:"file_id,omitempty"` |
9 | 9 | } |
75 | 75 | |
76 | 76 | // EmojiChangedEvent represents the emoji changed event |
77 | 77 | type EmojiChangedEvent struct { |
78 | Type string `json:"type"` | |
79 | EventTimestamp JSONTimeString `json:"event_ts"` | |
78 | Type string `json:"type"` | |
79 | EventTimestamp string `json:"event_ts"` | |
80 | 80 | } |
81 | 81 | |
82 | 82 | // CommandsChangedEvent represents the commands changed event |
83 | 83 | type CommandsChangedEvent struct { |
84 | Type string `json:"type"` | |
85 | EventTimestamp JSONTimeString `json:"event_ts"` | |
84 | Type string `json:"type"` | |
85 | EventTimestamp string `json:"event_ts"` | |
86 | 86 | } |
87 | 87 | |
88 | 88 | // EmailDomainChangedEvent represents the email domain changed event |
89 | 89 | type EmailDomainChangedEvent struct { |
90 | Type string `json:"type"` | |
91 | EventTimestamp JSONTimeString `json:"event_ts"` | |
92 | EmailDomain string `json:"email_domain"` | |
90 | Type string `json:"type"` | |
91 | EventTimestamp string `json:"event_ts"` | |
92 | EmailDomain string `json:"email_domain"` | |
93 | 93 | } |
94 | 94 | |
95 | 95 | // BotAddedEvent represents the bot added event |
0 | 0 | package slack |
1 | 1 | |
2 | 2 | type pinEvent struct { |
3 | Type string `json:"type"` | |
4 | User string `json:"user"` | |
5 | Item Item `json:"item"` | |
6 | Channel string `json:"channel_id"` | |
7 | EventTimestamp JSONTimeString `json:"event_ts"` | |
8 | HasPins bool `json:"has_pins,omitempty"` | |
3 | Type string `json:"type"` | |
4 | User string `json:"user"` | |
5 | Item Item `json:"item"` | |
6 | Channel string `json:"channel_id"` | |
7 | EventTimestamp string `json:"event_ts"` | |
8 | HasPins bool `json:"has_pins,omitempty"` | |
9 | 9 | } |
10 | 10 | |
11 | 11 | // PinAddedEvent represents the Pin added event |
0 | 0 | package slack |
1 | 1 | |
2 | 2 | type reactionEvent struct { |
3 | Type string `json:"type"` | |
4 | User string `json:"user"` | |
5 | Item ReactedItem `json:"item"` | |
6 | Reaction string `json:"reaction"` | |
7 | EventTimestamp JSONTimeString `json:"event_ts"` | |
3 | Type string `json:"type"` | |
4 | User string `json:"user"` | |
5 | Item ReactedItem `json:"item"` | |
6 | Reaction string `json:"reaction"` | |
7 | EventTimestamp string `json:"event_ts"` | |
8 | 8 | } |
9 | 9 | |
10 | 10 | // ReactionAddedEvent represents the Reaction added event |
0 | 0 | package slack |
1 | 1 | |
2 | 2 | type starEvent struct { |
3 | Type string `json:"type"` | |
4 | User string `json:"user"` | |
5 | Item StarredItem `json:"item"` | |
6 | EventTimestamp JSONTimeString `json:"event_ts"` | |
3 | Type string `json:"type"` | |
4 | User string `json:"user"` | |
5 | Item StarredItem `json:"item"` | |
6 | EventTimestamp string `json:"event_ts"` | |
7 | 7 | } |
8 | 8 | |
9 | 9 | // StarAddedEvent represents the Star added event |
7 | 7 | |
8 | 8 | // TeamRenameEvent represents the Team rename event |
9 | 9 | type TeamRenameEvent struct { |
10 | Type string `json:"type"` | |
11 | Name string `json:"name,omitempty"` | |
12 | EventTimestamp *JSONTimeString `json:"event_ts,omitempty"` | |
10 | Type string `json:"type"` | |
11 | Name string `json:"name,omitempty"` | |
12 | EventTimestamp string `json:"event_ts,omitempty"` | |
13 | 13 | } |
14 | 14 | |
15 | 15 | // TeamPrefChangeEvent represents the Team preference change event |
0 | 0 | package slack |
1 | 1 | |
2 | 2 | import ( |
3 | "fmt" | |
4 | "log" | |
5 | 3 | "net" |
6 | 4 | "net/url" |
7 | "strconv" | |
8 | "time" | |
9 | 5 | ) |
10 | ||
11 | // JSONTimeString is an auxiliary type to allow us to format the time as we wish | |
12 | type JSONTimeString string | |
13 | ||
14 | // String converts the unix timestamp into a string | |
15 | func (t JSONTimeString) String() string { | |
16 | tm := t.Time() | |
17 | if tm.IsZero() { | |
18 | return "" | |
19 | } | |
20 | return fmt.Sprintf("\"%s\"", tm.Format("Mon Jan _2")) | |
21 | } | |
22 | ||
23 | // Time converts the timestamp string to time.Time | |
24 | func (t JSONTimeString) Time() time.Time { | |
25 | if t == "" { | |
26 | return time.Time{} | |
27 | } | |
28 | floatN, err := strconv.ParseFloat(string(t), 64) | |
29 | if err != nil { | |
30 | log.Println("ERROR parsing a JSONTimeString!", err) | |
31 | return time.Time{} | |
32 | } | |
33 | return time.Unix(int64(floatN), 0) | |
34 | } | |
35 | 6 | |
36 | 7 | var portMapping = map[string]string{"ws": "80", "wss": "443"} |
37 | 8 |