extract common Item struct for Message, File and Comment types
Ryan Carver
8 years ago
0 | 0 |
package slack
|
|
1 |
|
|
2 |
const (
|
|
3 |
TYPE_MESSAGE = "message"
|
|
4 |
TYPE_FILE = "file"
|
|
5 |
TYPE_COMMENT = "comment"
|
|
6 |
)
|
|
7 |
|
|
8 |
// Item is any type of slack message - message, file, or comment.
|
|
9 |
type Item struct {
|
|
10 |
Type string
|
|
11 |
Message *Message
|
|
12 |
File *File
|
|
13 |
Comment *Comment
|
|
14 |
}
|
|
15 |
|
|
16 |
// NewMessageItem turns a message into a typed message struct.
|
|
17 |
func NewMessageItem(m *Message) Item {
|
|
18 |
return Item{Type: TYPE_MESSAGE, Message: m}
|
|
19 |
}
|
|
20 |
|
|
21 |
// NewFileItem turns a file into a typed file struct.
|
|
22 |
func NewFileItem(f *File) Item {
|
|
23 |
return Item{Type: TYPE_FILE, File: f}
|
|
24 |
}
|
|
25 |
|
|
26 |
// NewCommentItem turns a comment into a typed comment struct.
|
|
27 |
func NewCommentItem(c *Comment) Item {
|
|
28 |
return Item{Type: TYPE_COMMENT, Comment: c}
|
|
29 |
}
|
1 | 30 |
|
2 | 31 |
// ItemRef is a reference to a message of any type. One of FileID,
|
3 | 32 |
// CommentId, or the combination of ChannelId and Timestamp must be
|
0 | 0 |
package slack
|
1 | 1 |
|
2 | 2 |
import "testing"
|
|
3 |
|
|
4 |
func TestNewMessageItem(t *testing.T) {
|
|
5 |
m := &Message{}
|
|
6 |
mi := NewMessageItem(m)
|
|
7 |
if mi.Type != TYPE_MESSAGE {
|
|
8 |
t.Errorf("want Type %s, got %s", TYPE_MESSAGE, mi.Type)
|
|
9 |
}
|
|
10 |
if m != mi.Message {
|
|
11 |
t.Errorf("want Message %v, got %v", m, mi.Message)
|
|
12 |
}
|
|
13 |
}
|
|
14 |
|
|
15 |
func TestNewFileItem(t *testing.T) {
|
|
16 |
f := &File{}
|
|
17 |
fi := NewFileItem(f)
|
|
18 |
if fi.Type != TYPE_FILE {
|
|
19 |
t.Errorf("want Type %s, got %s", TYPE_FILE, fi.Type)
|
|
20 |
}
|
|
21 |
if f != fi.File {
|
|
22 |
t.Errorf("want File %v, got %v", f, fi.File)
|
|
23 |
}
|
|
24 |
}
|
|
25 |
|
|
26 |
func TestNewCommentItem(t *testing.T) {
|
|
27 |
c := &Comment{}
|
|
28 |
ci := NewCommentItem(c)
|
|
29 |
if ci.Type != TYPE_COMMENT {
|
|
30 |
t.Errorf("want Type %s, got %s", TYPE_COMMENT, ci.Type)
|
|
31 |
}
|
|
32 |
if c != ci.Comment {
|
|
33 |
t.Errorf("want Comment %v, got %v", c, ci.Comment)
|
|
34 |
}
|
|
35 |
}
|
3 | 36 |
|
4 | 37 |
func TestNewRefToMessage(t *testing.T) {
|
5 | 38 |
ref := NewRefToMessage("chan", "ts")
|
15 | 15 |
// ReactedItem is an item that was reacted to, and the details of the
|
16 | 16 |
// reactions.
|
17 | 17 |
type ReactedItem struct {
|
18 | |
Type string
|
19 | |
Message *Message
|
20 | |
File *File
|
21 | |
Comment *Comment
|
|
18 |
Item
|
22 | 19 |
Reactions []ItemReaction
|
23 | 20 |
}
|
24 | 21 |
|
|
109 | 106 |
func (res listReactionsResponseFull) extractReactedItems() []ReactedItem {
|
110 | 107 |
items := make([]ReactedItem, len(res.Items))
|
111 | 108 |
for i, input := range res.Items {
|
112 | |
item := ReactedItem{
|
113 | |
Type: input.Type,
|
114 | |
}
|
|
109 |
item := ReactedItem{}
|
|
110 |
item.Type = input.Type
|
115 | 111 |
switch input.Type {
|
116 | 112 |
case "message":
|
117 | 113 |
item.Message = input.M.Message
|
313 | 313 |
}}`
|
314 | 314 |
want := []ReactedItem{
|
315 | 315 |
ReactedItem{
|
316 | |
Type: "message",
|
317 | |
Message: &Message{Msg: Msg{Text: "hello"}},
|
|
316 |
Item: NewMessageItem(&Message{Msg: Msg{Text: "hello"}}),
|
318 | 317 |
Reactions: []ItemReaction{
|
319 | 318 |
ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
|
320 | 319 |
ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
|
321 | 320 |
},
|
322 | 321 |
},
|
323 | 322 |
ReactedItem{
|
324 | |
Type: "file",
|
325 | |
File: &File{Name: "toy"},
|
|
323 |
Item: NewFileItem(&File{Name: "toy"}),
|
326 | 324 |
Reactions: []ItemReaction{
|
327 | 325 |
ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
|
328 | 326 |
},
|
329 | 327 |
},
|
330 | 328 |
ReactedItem{
|
331 | |
Type: "file_comment",
|
332 | |
Comment: &Comment{Comment: "cool toy"},
|
|
329 |
Item: NewCommentItem(&Comment{Comment: "cool toy"}),
|
333 | 330 |
Reactions: []ItemReaction{
|
334 | 331 |
ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
|
335 | 332 |
},
|