Codebase list golang-github-nlopes-slack / 6edc895
extract common Item struct for Message, File and Comment types Ryan Carver 8 years ago
4 changed file(s) with 68 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
00 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 }
130
231 // ItemRef is a reference to a message of any type. One of FileID,
332 // CommentId, or the combination of ChannelId and Timestamp must be
00 package slack
11
22 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 }
336
437 func TestNewRefToMessage(t *testing.T) {
538 ref := NewRefToMessage("chan", "ts")
1515 // ReactedItem is an item that was reacted to, and the details of the
1616 // reactions.
1717 type ReactedItem struct {
18 Type string
19 Message *Message
20 File *File
21 Comment *Comment
18 Item
2219 Reactions []ItemReaction
2320 }
2421
109106 func (res listReactionsResponseFull) extractReactedItems() []ReactedItem {
110107 items := make([]ReactedItem, len(res.Items))
111108 for i, input := range res.Items {
112 item := ReactedItem{
113 Type: input.Type,
114 }
109 item := ReactedItem{}
110 item.Type = input.Type
115111 switch input.Type {
116112 case "message":
117113 item.Message = input.M.Message
313313 }}`
314314 want := []ReactedItem{
315315 ReactedItem{
316 Type: "message",
317 Message: &Message{Msg: Msg{Text: "hello"}},
316 Item: NewMessageItem(&Message{Msg: Msg{Text: "hello"}}),
318317 Reactions: []ItemReaction{
319318 ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
320319 ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
321320 },
322321 },
323322 ReactedItem{
324 Type: "file",
325 File: &File{Name: "toy"},
323 Item: NewFileItem(&File{Name: "toy"}),
326324 Reactions: []ItemReaction{
327325 ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}},
328326 },
329327 },
330328 ReactedItem{
331 Type: "file_comment",
332 Comment: &Comment{Comment: "cool toy"},
329 Item: NewCommentItem(&Comment{Comment: "cool toy"}),
333330 Reactions: []ItemReaction{
334331 ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}},
335332 },