| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/url" |
| 6 | "strconv" |
| 7 | ) |
| 8 | |
| 9 | // ItemReaction is the reactions that have happened on an item. |
| 10 | type ItemReaction struct { |
| 11 | Name string `json:"name"` |
| 12 | Count int `json:"count"` |
| 13 | Users []string `json:"users"` |
| 14 | } |
| 15 | |
| 16 | // ReactedItem is an item that was reacted to, and the details of the |
| 17 | // reactions. |
| 18 | type ReactedItem struct { |
| 19 | Item |
| 20 | Reactions []ItemReaction |
| 21 | } |
| 22 | |
| 23 | // GetReactionsParameters is the inputs to get reactions to an item. |
| 24 | type GetReactionsParameters struct { |
| 25 | Full bool |
| 26 | } |
| 27 | |
| 28 | // NewGetReactionsParameters initializes the inputs to get reactions to an item. |
| 29 | func NewGetReactionsParameters() GetReactionsParameters { |
| 30 | return GetReactionsParameters{ |
| 31 | Full: false, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | type getReactionsResponseFull struct { |
| 36 | Type string |
| 37 | M struct { |
| 38 | Reactions []ItemReaction |
| 39 | } `json:"message"` |
| 40 | F struct { |
| 41 | Reactions []ItemReaction |
| 42 | } `json:"file"` |
| 43 | FC struct { |
| 44 | Reactions []ItemReaction |
| 45 | } `json:"comment"` |
| 46 | SlackResponse |
| 47 | } |
| 48 | |
| 49 | func (res getReactionsResponseFull) extractReactions() []ItemReaction { |
| 50 | switch res.Type { |
| 51 | case "message": |
| 52 | return res.M.Reactions |
| 53 | case "file": |
| 54 | return res.F.Reactions |
| 55 | case "file_comment": |
| 56 | return res.FC.Reactions |
| 57 | } |
| 58 | return []ItemReaction{} |
| 59 | } |
| 60 | |
| 61 | const ( |
| 62 | DEFAULT_REACTIONS_USER = "" |
| 63 | DEFAULT_REACTIONS_COUNT = 100 |
| 64 | DEFAULT_REACTIONS_PAGE = 1 |
| 65 | DEFAULT_REACTIONS_FULL = false |
| 66 | ) |
| 67 | |
| 68 | // ListReactionsParameters is the inputs to find all reactions by a user. |
| 69 | type ListReactionsParameters struct { |
| 70 | User string |
| 71 | Count int |
| 72 | Page int |
| 73 | Full bool |
| 74 | } |
| 75 | |
| 76 | // NewListReactionsParameters initializes the inputs to find all reactions |
| 77 | // performed by a user. |
| 78 | func NewListReactionsParameters() ListReactionsParameters { |
| 79 | return ListReactionsParameters{ |
| 80 | User: DEFAULT_REACTIONS_USER, |
| 81 | Count: DEFAULT_REACTIONS_COUNT, |
| 82 | Page: DEFAULT_REACTIONS_PAGE, |
| 83 | Full: DEFAULT_REACTIONS_FULL, |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | type listReactionsResponseFull struct { |
| 88 | Items []struct { |
| 89 | Type string |
| 90 | Channel string |
| 91 | M struct { |
| 92 | *Message |
| 93 | } `json:"message"` |
| 94 | F struct { |
| 95 | *File |
| 96 | Reactions []ItemReaction |
| 97 | } `json:"file"` |
| 98 | FC struct { |
| 99 | *Comment |
| 100 | Reactions []ItemReaction |
| 101 | } `json:"comment"` |
| 102 | } |
| 103 | Paging `json:"paging"` |
| 104 | SlackResponse |
| 105 | } |
| 106 | |
| 107 | func (res listReactionsResponseFull) extractReactedItems() []ReactedItem { |
| 108 | items := make([]ReactedItem, len(res.Items)) |
| 109 | for i, input := range res.Items { |
| 110 | item := ReactedItem{} |
| 111 | item.Type = input.Type |
| 112 | switch input.Type { |
| 113 | case "message": |
| 114 | item.Channel = input.Channel |
| 115 | item.Message = input.M.Message |
| 116 | item.Reactions = input.M.Reactions |
| 117 | case "file": |
| 118 | item.File = input.F.File |
| 119 | item.Reactions = input.F.Reactions |
| 120 | case "file_comment": |
| 121 | item.File = input.F.File |
| 122 | item.Comment = input.FC.Comment |
| 123 | item.Reactions = input.FC.Reactions |
| 124 | } |
| 125 | items[i] = item |
| 126 | } |
| 127 | return items |
| 128 | } |
| 129 | |
| 130 | // AddReaction adds a reaction emoji to a message, file or file comment. |
| 131 | func (api *Client) AddReaction(name string, item ItemRef) error { |
| 132 | return api.AddReactionContext(context.Background(), name, item) |
| 133 | } |
| 134 | |
| 135 | // AddReactionContext adds a reaction emoji to a message, file or file comment with a custom context. |
| 136 | func (api *Client) AddReactionContext(ctx context.Context, name string, item ItemRef) error { |
| 137 | values := url.Values{ |
| 138 | "token": {api.token}, |
| 139 | } |
| 140 | if name != "" { |
| 141 | values.Set("name", name) |
| 142 | } |
| 143 | if item.Channel != "" { |
| 144 | values.Set("channel", item.Channel) |
| 145 | } |
| 146 | if item.Timestamp != "" { |
| 147 | values.Set("timestamp", item.Timestamp) |
| 148 | } |
| 149 | if item.File != "" { |
| 150 | values.Set("file", item.File) |
| 151 | } |
| 152 | if item.Comment != "" { |
| 153 | values.Set("file_comment", item.Comment) |
| 154 | } |
| 155 | |
| 156 | response := &SlackResponse{} |
| 157 | if err := api.postMethod(ctx, "reactions.add", values, response); err != nil { |
| 158 | return err |
| 159 | } |
| 160 | |
| 161 | return response.Err() |
| 162 | } |
| 163 | |
| 164 | // RemoveReaction removes a reaction emoji from a message, file or file comment. |
| 165 | func (api *Client) RemoveReaction(name string, item ItemRef) error { |
| 166 | return api.RemoveReactionContext(context.Background(), name, item) |
| 167 | } |
| 168 | |
| 169 | // RemoveReactionContext removes a reaction emoji from a message, file or file comment with a custom context. |
| 170 | func (api *Client) RemoveReactionContext(ctx context.Context, name string, item ItemRef) error { |
| 171 | values := url.Values{ |
| 172 | "token": {api.token}, |
| 173 | } |
| 174 | if name != "" { |
| 175 | values.Set("name", name) |
| 176 | } |
| 177 | if item.Channel != "" { |
| 178 | values.Set("channel", item.Channel) |
| 179 | } |
| 180 | if item.Timestamp != "" { |
| 181 | values.Set("timestamp", item.Timestamp) |
| 182 | } |
| 183 | if item.File != "" { |
| 184 | values.Set("file", item.File) |
| 185 | } |
| 186 | if item.Comment != "" { |
| 187 | values.Set("file_comment", item.Comment) |
| 188 | } |
| 189 | |
| 190 | response := &SlackResponse{} |
| 191 | if err := api.postMethod(ctx, "reactions.remove", values, response); err != nil { |
| 192 | return err |
| 193 | } |
| 194 | |
| 195 | return response.Err() |
| 196 | } |
| 197 | |
| 198 | // GetReactions returns details about the reactions on an item. |
| 199 | func (api *Client) GetReactions(item ItemRef, params GetReactionsParameters) ([]ItemReaction, error) { |
| 200 | return api.GetReactionsContext(context.Background(), item, params) |
| 201 | } |
| 202 | |
| 203 | // GetReactionsContext returns details about the reactions on an item with a custom context |
| 204 | func (api *Client) GetReactionsContext(ctx context.Context, item ItemRef, params GetReactionsParameters) ([]ItemReaction, error) { |
| 205 | values := url.Values{ |
| 206 | "token": {api.token}, |
| 207 | } |
| 208 | if item.Channel != "" { |
| 209 | values.Set("channel", item.Channel) |
| 210 | } |
| 211 | if item.Timestamp != "" { |
| 212 | values.Set("timestamp", item.Timestamp) |
| 213 | } |
| 214 | if item.File != "" { |
| 215 | values.Set("file", item.File) |
| 216 | } |
| 217 | if item.Comment != "" { |
| 218 | values.Set("file_comment", item.Comment) |
| 219 | } |
| 220 | if params.Full != DEFAULT_REACTIONS_FULL { |
| 221 | values.Set("full", strconv.FormatBool(params.Full)) |
| 222 | } |
| 223 | |
| 224 | response := &getReactionsResponseFull{} |
| 225 | if err := api.postMethod(ctx, "reactions.get", values, response); err != nil { |
| 226 | return nil, err |
| 227 | } |
| 228 | |
| 229 | if err := response.Err(); err != nil { |
| 230 | return nil, err |
| 231 | } |
| 232 | |
| 233 | return response.extractReactions(), nil |
| 234 | } |
| 235 | |
| 236 | // ListReactions returns information about the items a user reacted to. |
| 237 | func (api *Client) ListReactions(params ListReactionsParameters) ([]ReactedItem, *Paging, error) { |
| 238 | return api.ListReactionsContext(context.Background(), params) |
| 239 | } |
| 240 | |
| 241 | // ListReactionsContext returns information about the items a user reacted to with a custom context. |
| 242 | func (api *Client) ListReactionsContext(ctx context.Context, params ListReactionsParameters) ([]ReactedItem, *Paging, error) { |
| 243 | values := url.Values{ |
| 244 | "token": {api.token}, |
| 245 | } |
| 246 | if params.User != DEFAULT_REACTIONS_USER { |
| 247 | values.Add("user", params.User) |
| 248 | } |
| 249 | if params.Count != DEFAULT_REACTIONS_COUNT { |
| 250 | values.Add("count", strconv.Itoa(params.Count)) |
| 251 | } |
| 252 | if params.Page != DEFAULT_REACTIONS_PAGE { |
| 253 | values.Add("page", strconv.Itoa(params.Page)) |
| 254 | } |
| 255 | if params.Full != DEFAULT_REACTIONS_FULL { |
| 256 | values.Add("full", strconv.FormatBool(params.Full)) |
| 257 | } |
| 258 | |
| 259 | response := &listReactionsResponseFull{} |
| 260 | err := api.postMethod(ctx, "reactions.list", values, response) |
| 261 | if err != nil { |
| 262 | return nil, nil, err |
| 263 | } |
| 264 | |
| 265 | if err := response.Err(); err != nil { |
| 266 | return nil, nil, err |
| 267 | } |
| 268 | |
| 269 | return response.extractReactedItems(), &response.Paging, nil |
| 270 | } |