Codebase list golang-github-nlopes-slack / e01e312
finish Item structs and use it in Reactions Ryan Carver 8 years ago
4 changed file(s) with 47 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
00 package slack
11
22 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"
66 )
77
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.
99 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"`
1415 }
1516
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}
1920 }
2021
2122 // NewFileItem turns a file into a typed file struct.
2324 return Item{Type: TYPE_FILE, File: f}
2425 }
2526
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}
2930 }
3031
3132 // ItemRef is a reference to a message of any type. One of FileID,
22 import "testing"
33
44 func TestNewMessageItem(t *testing.T) {
5 c := "C1"
56 m := &Message{}
6 mi := NewMessageItem(m)
7 mi := NewMessageItem(c, m)
78 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)
910 }
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)
1216 }
1317 }
1418
1620 f := &File{}
1721 fi := NewFileItem(f)
1822 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)
2024 }
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)
2327 }
2428 }
2529
26 func TestNewCommentItem(t *testing.T) {
30 func TestNewFileCommentItem(t *testing.T) {
31 f := &File{}
2732 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)
3136 }
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)
3442 }
3543 }
3644
8585
8686 type listReactionsResponseFull struct {
8787 Items []struct {
88 Type string
89 M struct {
88 Type string
89 Channel string
90 M struct {
9091 *Message
9192 Reactions []ItemReaction
9293 } `json:"message"`
110111 item.Type = input.Type
111112 switch input.Type {
112113 case "message":
114 item.Channel = input.Channel
113115 item.Message = input.M.Message
114116 item.Reactions = input.M.Reactions
115117 case "file":
116118 item.File = input.F.File
117119 item.Reactions = input.F.Reactions
118120 case "file_comment":
121 item.File = input.F.File
119122 item.Comment = input.FC.Comment
120123 item.Reactions = input.FC.Reactions
121124 }
261261 "items": [
262262 {
263263 "type": "message",
264 "channel": "C1",
264265 "message": {
265266 "text": "hello",
266267 "reactions": [
292293 },
293294 {
294295 "type": "file_comment",
295 "file": {},
296 "file": {
297 "name": "toy"
298 },
296299 "comment": {
297300 "comment": "cool toy",
298301 "reactions": [
313316 }}`
314317 want := []ReactedItem{
315318 ReactedItem{
316 Item: NewMessageItem(&Message{Msg: Msg{Text: "hello"}}),
319 Item: NewMessageItem("C1", &Message{Msg: Msg{Text: "hello"}}),
317320 Reactions: []ItemReaction{
318321 ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
319322 ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
326329 },
327330 },
328331 ReactedItem{
329 Item: NewCommentItem(&Comment{Comment: "cool toy"}),
332 Item: NewFileCommentItem(&File{Name: "toy"}, &Comment{Comment: "cool toy"}),
330333 Reactions: []ItemReaction{
331334 ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
332335 },