5 | 5 |
"net/url"
|
6 | 6 |
)
|
7 | 7 |
|
8 | |
// Reaction is the act of reacting to an item.
|
9 | |
type Reaction struct {
|
10 | |
Name string `json:"name"`
|
11 | |
ItemRef
|
12 | |
}
|
13 | |
|
14 | 8 |
// ItemReaction is the reactions that have happened on an item.
|
15 | 9 |
type ItemReaction struct {
|
16 | 10 |
Name string `json:"name"`
|
|
18 | 12 |
Users []string `json:"users"`
|
19 | 13 |
}
|
20 | 14 |
|
21 | |
// ReactedToItem is an item that was reacted to, and the details of the
|
22 | |
// reaction.
|
23 | |
type ReactedToItem struct {
|
24 | |
Item
|
25 | |
ItemReaction
|
|
15 |
// ReactedItem is an item that was reacted to, and the details of the
|
|
16 |
// reactions.
|
|
17 |
type ReactedItem struct {
|
|
18 |
Type string
|
|
19 |
Message *Message
|
|
20 |
File *File
|
|
21 |
Comment *Comment
|
|
22 |
Reactions []ItemReaction
|
26 | 23 |
}
|
27 | 24 |
|
28 | 25 |
// AddReactionParameters is the inputs to create a new reaction.
|
29 | 26 |
type AddReactionParameters struct {
|
|
27 |
Name string
|
30 | 28 |
ItemRef
|
31 | |
Name string
|
32 | 29 |
}
|
33 | 30 |
|
34 | 31 |
// NewAddReactionParameters initialies the inputs to react to an item.
|
|
36 | 33 |
return AddReactionParameters{Name: name, ItemRef: ref}
|
37 | 34 |
}
|
38 | 35 |
|
39 | |
// GetReactionParameters is the inputs to get reactions on an item.
|
|
36 |
// GetReactionParameters is the inputs to get reactions to an item.
|
40 | 37 |
type GetReactionParameters struct {
|
|
38 |
Full bool
|
41 | 39 |
ItemRef
|
42 | |
Full bool
|
43 | |
}
|
44 | |
|
45 | |
// NewGetReactionParameters initializes the inputs to get reactions on an item.
|
|
40 |
}
|
|
41 |
|
|
42 |
// NewGetReactionParameters initializes the inputs to get reactions to an item.
|
46 | 43 |
func NewGetReactionParameters(ref ItemRef) GetReactionParameters {
|
47 | 44 |
return GetReactionParameters{ItemRef: ref}
|
48 | 45 |
}
|
49 | 46 |
|
50 | 47 |
type getReactionsResponseFull struct {
|
51 | |
Message struct {
|
52 | |
Type string
|
53 | |
Message struct {
|
54 | |
Reactions []ItemReaction
|
55 | |
}
|
56 | |
File struct {
|
57 | |
Reactions []ItemReaction
|
58 | |
}
|
59 | |
FileComment struct {
|
|
48 |
M struct {
|
|
49 |
Type string
|
|
50 |
M struct {
|
|
51 |
Reactions []ItemReaction
|
|
52 |
} `json:"message"`
|
|
53 |
F struct {
|
|
54 |
Reactions []ItemReaction
|
|
55 |
} `json:"file"`
|
|
56 |
FC struct {
|
60 | 57 |
Comment struct {
|
61 | 58 |
Reactions []ItemReaction
|
62 | 59 |
}
|
63 | 60 |
} `json:"file_comment"`
|
64 | |
}
|
|
61 |
} `json:"message"`
|
65 | 62 |
SlackResponse
|
66 | 63 |
}
|
67 | 64 |
|
68 | |
func (res getReactionsResponseFull) FindReactions() []ItemReaction {
|
69 | |
switch res.Message.Type {
|
|
65 |
func (res getReactionsResponseFull) extractReactions() []ItemReaction {
|
|
66 |
switch res.M.Type {
|
70 | 67 |
case "message":
|
71 | |
return res.Message.Message.Reactions
|
|
68 |
return res.M.M.Reactions
|
72 | 69 |
case "file":
|
73 | |
return res.Message.File.Reactions
|
|
70 |
return res.M.F.Reactions
|
74 | 71 |
case "file_comment":
|
75 | |
return res.Message.FileComment.Comment.Reactions
|
|
72 |
return res.M.FC.Comment.Reactions
|
76 | 73 |
}
|
77 | 74 |
return []ItemReaction{}
|
78 | 75 |
}
|
|
92 | 89 |
Full bool
|
93 | 90 |
}
|
94 | 91 |
|
95 | |
// NewListReactionsParameters initializes the inputs to find all reactions by a user.
|
|
92 |
// NewListReactionsParameters initializes the inputs to find all reactions
|
|
93 |
// performed by a user.
|
96 | 94 |
func NewListReactionsParameters(userID string) ListReactionsParameters {
|
97 | 95 |
return ListReactionsParameters{
|
98 | 96 |
User: userID,
|
|
104 | 102 |
|
105 | 103 |
type listReactionsResponseFull struct {
|
106 | 104 |
Items []struct {
|
107 | |
Message struct {
|
108 | |
Reactions []ItemReaction
|
|
105 |
Type string
|
|
106 |
M struct {
|
|
107 |
*Message
|
|
108 |
Reactions []ItemReaction
|
|
109 |
} `json:"message"`
|
|
110 |
F struct {
|
|
111 |
*File
|
|
112 |
Reactions []ItemReaction
|
|
113 |
} `json:"file"`
|
|
114 |
FC struct {
|
|
115 |
C struct {
|
|
116 |
*Comment
|
|
117 |
Reactions []ItemReaction
|
|
118 |
} `json:"comment"`
|
|
119 |
} `json:"file_comment"`
|
|
120 |
}
|
|
121 |
Paging `json:"paging"`
|
|
122 |
SlackResponse
|
|
123 |
}
|
|
124 |
|
|
125 |
func (res listReactionsResponseFull) extractReactedItems() []ReactedItem {
|
|
126 |
items := make([]ReactedItem, len(res.Items))
|
|
127 |
for i, input := range res.Items {
|
|
128 |
item := ReactedItem{
|
|
129 |
Type: input.Type,
|
109 | 130 |
}
|
110 | |
}
|
111 | |
SlackResponse
|
|
131 |
switch input.Type {
|
|
132 |
case "message":
|
|
133 |
item.Message = input.M.Message
|
|
134 |
item.Reactions = input.M.Reactions
|
|
135 |
case "file":
|
|
136 |
item.File = input.F.File
|
|
137 |
item.Reactions = input.F.Reactions
|
|
138 |
case "file_comment":
|
|
139 |
item.Comment = input.FC.C.Comment
|
|
140 |
item.Reactions = input.FC.C.Reactions
|
|
141 |
}
|
|
142 |
items[i] = item
|
|
143 |
}
|
|
144 |
return items
|
112 | 145 |
}
|
113 | 146 |
|
114 | 147 |
// AddReaction adds a reaction emoji to a message, file or file comment.
|
|
168 | 201 |
if !response.Ok {
|
169 | 202 |
return nil, errors.New(response.Error)
|
170 | 203 |
}
|
171 | |
return response.FindReactions(), nil
|
|
204 |
return response.extractReactions(), nil
|
172 | 205 |
}
|
173 | 206 |
|
174 | 207 |
// ListReactions returns information about the items a user reacted to.
|
175 | |
func (api *Slack) ListReactions(params ListReactionsParameters) ([]ReactedToItem, Paging, error) {
|
|
208 |
func (api *Slack) ListReactions(params ListReactionsParameters) ([]ReactedItem, Paging, error) {
|
176 | 209 |
values := url.Values{
|
177 | 210 |
"token": {api.config.token},
|
178 | 211 |
}
|
|
180 | 213 |
values.Add("user", params.User)
|
181 | 214 |
}
|
182 | 215 |
if params.Count != DEFAULT_REACTIONS_COUNT {
|
183 | |
values.Add("count", string(params.Count))
|
|
216 |
values.Add("count", fmt.Sprintf("%d", params.Count))
|
184 | 217 |
}
|
185 | 218 |
if params.Page != DEFAULT_REACTIONS_PAGE {
|
186 | |
values.Add("count", string(params.Page))
|
|
219 |
values.Add("page", fmt.Sprintf("%d", params.Page))
|
187 | 220 |
}
|
188 | 221 |
if params.Full != DEFAULT_REACTIONS_FULL {
|
189 | |
values.Add("count", fmt.Sprintf("%t", params.Full))
|
|
222 |
values.Add("full", fmt.Sprintf("%t", params.Full))
|
190 | 223 |
}
|
191 | 224 |
response := &listReactionsResponseFull{}
|
192 | 225 |
err := parseResponse("reactions.list", values, response, api.debug)
|
|
196 | 229 |
if !response.Ok {
|
197 | 230 |
return nil, Paging{}, errors.New(response.Error)
|
198 | 231 |
}
|
199 | |
return nil, Paging{}, nil
|
200 | |
}
|
|
232 |
return response.extractReactedItems(), response.Paging, nil
|
|
233 |
}
|