finish Item structs and use it in Reactions
Ryan Carver
8 years ago
0 | 0 | package slack |
1 | 1 | |
2 | 2 | const ( |
3 | TYPE_MESSAGE = "message" | |
4 | TYPE_FILE = "file" | |
5 | TYPE_COMMENT = "comment" | |
3 | TYPE_MESSAGE = "message" | |
4 | TYPE_FILE = "file" | |
5 | TYPE_FILE_COMMENT = "file_comment" | |
6 | 6 | ) |
7 | 7 | |
8 | // Item is any type of slack message - message, file, or comment. | |
8 | // Item is any type of slack message - message, file, or file comment. | |
9 | 9 | type Item struct { |
10 | Type string | |
11 | Message *Message | |
12 | File *File | |
13 | Comment *Comment | |
10 | Type string `json:"type"` | |
11 | Channel string `json:"channel,omitempty"` | |
12 | Message *Message `json:"message,omitempty"` | |
13 | File *File `json:"file,omitempty"` | |
14 | Comment *Comment `json:"comment,omitempty"` | |
14 | 15 | } |
15 | 16 | |
16 | // NewMessageItem turns a message into a typed message struct. | |
17 | func NewMessageItem(m *Message) Item { | |
18 | return Item{Type: TYPE_MESSAGE, Message: m} | |
17 | // NewMessageItem turns a message on a channel into a typed message struct. | |
18 | func NewMessageItem(ch string, m *Message) Item { | |
19 | return Item{Type: TYPE_MESSAGE, Channel: ch, Message: m} | |
19 | 20 | } |
20 | 21 | |
21 | 22 | // NewFileItem turns a file into a typed file struct. |
23 | 24 | return Item{Type: TYPE_FILE, File: f} |
24 | 25 | } |
25 | 26 | |
26 | // NewCommentItem turns a comment into a typed comment struct. | |
27 | func NewCommentItem(c *Comment) Item { | |
28 | return Item{Type: TYPE_COMMENT, Comment: c} | |
27 | // NewFileCommentItem turns a file and comment into a typed file_comment struct. | |
28 | func NewFileCommentItem(f *File, c *Comment) Item { | |
29 | return Item{Type: TYPE_FILE_COMMENT, File: f, Comment: c} | |
29 | 30 | } |
30 | 31 | |
31 | 32 | // ItemRef is a reference to a message of any type. One of FileID, |
2 | 2 | import "testing" |
3 | 3 | |
4 | 4 | func TestNewMessageItem(t *testing.T) { |
5 | c := "C1" | |
5 | 6 | m := &Message{} |
6 | mi := NewMessageItem(m) | |
7 | mi := NewMessageItem(c, m) | |
7 | 8 | if mi.Type != TYPE_MESSAGE { |
8 | t.Errorf("want Type %s, got %s", TYPE_MESSAGE, mi.Type) | |
9 | t.Errorf("want Type %s, got %s", mi.Type, TYPE_MESSAGE) | |
9 | 10 | } |
10 | if m != mi.Message { | |
11 | t.Errorf("want Message %v, got %v", m, mi.Message) | |
11 | if mi.Channel != c { | |
12 | t.Errorf("got Channel %s, want %s", mi.Channel, c) | |
13 | } | |
14 | if mi.Message != m { | |
15 | t.Errorf("got Message %v, want %v", mi.Message, m) | |
12 | 16 | } |
13 | 17 | } |
14 | 18 | |
16 | 20 | f := &File{} |
17 | 21 | fi := NewFileItem(f) |
18 | 22 | if fi.Type != TYPE_FILE { |
19 | t.Errorf("want Type %s, got %s", TYPE_FILE, fi.Type) | |
23 | t.Errorf("got Type %s, want %s", fi.Type, TYPE_FILE) | |
20 | 24 | } |
21 | if f != fi.File { | |
22 | t.Errorf("want File %v, got %v", f, fi.File) | |
25 | if fi.File != f { | |
26 | t.Errorf("got File %v, want %v", fi.File, f) | |
23 | 27 | } |
24 | 28 | } |
25 | 29 | |
26 | func TestNewCommentItem(t *testing.T) { | |
30 | func TestNewFileCommentItem(t *testing.T) { | |
31 | f := &File{} | |
27 | 32 | c := &Comment{} |
28 | ci := NewCommentItem(c) | |
29 | if ci.Type != TYPE_COMMENT { | |
30 | t.Errorf("want Type %s, got %s", TYPE_COMMENT, ci.Type) | |
33 | fci := NewFileCommentItem(f, c) | |
34 | if fci.Type != TYPE_FILE_COMMENT { | |
35 | t.Errorf("got Type %s, want %s", fci.Type, TYPE_FILE_COMMENT) | |
31 | 36 | } |
32 | if c != ci.Comment { | |
33 | t.Errorf("want Comment %v, got %v", c, ci.Comment) | |
37 | if fci.File != f { | |
38 | t.Errorf("got File %v, want %v", fci.File, f) | |
39 | } | |
40 | if fci.Comment != c { | |
41 | t.Errorf("got Comment %v, want %v", fci.Comment, c) | |
34 | 42 | } |
35 | 43 | } |
36 | 44 |
85 | 85 | |
86 | 86 | type listReactionsResponseFull struct { |
87 | 87 | Items []struct { |
88 | Type string | |
89 | M struct { | |
88 | Type string | |
89 | Channel string | |
90 | M struct { | |
90 | 91 | *Message |
91 | 92 | Reactions []ItemReaction |
92 | 93 | } `json:"message"` |
110 | 111 | item.Type = input.Type |
111 | 112 | switch input.Type { |
112 | 113 | case "message": |
114 | item.Channel = input.Channel | |
113 | 115 | item.Message = input.M.Message |
114 | 116 | item.Reactions = input.M.Reactions |
115 | 117 | case "file": |
116 | 118 | item.File = input.F.File |
117 | 119 | item.Reactions = input.F.Reactions |
118 | 120 | case "file_comment": |
121 | item.File = input.F.File | |
119 | 122 | item.Comment = input.FC.Comment |
120 | 123 | item.Reactions = input.FC.Reactions |
121 | 124 | } |
261 | 261 | "items": [ |
262 | 262 | { |
263 | 263 | "type": "message", |
264 | "channel": "C1", | |
264 | 265 | "message": { |
265 | 266 | "text": "hello", |
266 | 267 | "reactions": [ |
292 | 293 | }, |
293 | 294 | { |
294 | 295 | "type": "file_comment", |
295 | "file": {}, | |
296 | "file": { | |
297 | "name": "toy" | |
298 | }, | |
296 | 299 | "comment": { |
297 | 300 | "comment": "cool toy", |
298 | 301 | "reactions": [ |
313 | 316 | }}` |
314 | 317 | want := []ReactedItem{ |
315 | 318 | ReactedItem{ |
316 | Item: NewMessageItem(&Message{Msg: Msg{Text: "hello"}}), | |
319 | Item: NewMessageItem("C1", &Message{Msg: Msg{Text: "hello"}}), | |
317 | 320 | Reactions: []ItemReaction{ |
318 | 321 | ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}}, |
319 | 322 | ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}}, |
326 | 329 | }, |
327 | 330 | }, |
328 | 331 | ReactedItem{ |
329 | Item: NewCommentItem(&Comment{Comment: "cool toy"}), | |
332 | Item: NewFileCommentItem(&File{Name: "toy"}, &Comment{Comment: "cool toy"}), | |
330 | 333 | Reactions: []ItemReaction{ |
331 | 334 | ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}}, |
332 | 335 | }, |