simplify calling all reactions methods
Ryan Carver
8 years ago
66 | 66 | msgRef := slack.NewRefToMessage(channelID, timestamp) |
67 | 67 | |
68 | 68 | // React with :+1: |
69 | reactionParams := slack.NewAddReactionParameters("+1", msgRef) | |
70 | if err := api.AddReaction(reactionParams); err != nil { | |
69 | if err := api.AddReaction("+1", msgRef); err != nil { | |
71 | 70 | fmt.Printf("Error adding reaction: %s\n", err) |
72 | 71 | return |
73 | 72 | } |
74 | 73 | |
75 | 74 | // React with :-1: |
76 | reactionParams = slack.NewAddReactionParameters("cry", msgRef) | |
77 | if err := api.AddReaction(reactionParams); err != nil { | |
75 | if err := api.AddReaction("cry", msgRef); err != nil { | |
78 | 76 | fmt.Printf("Error adding reaction: %s\n", err) |
79 | 77 | return |
80 | 78 | } |
81 | 79 | |
82 | 80 | // Get all reactions on the message. |
83 | getReactionsParams := slack.NewGetReactionsParameters(msgRef) | |
84 | msgReactions, err := api.GetReactions(getReactionsParams) | |
81 | msgReactions, err := api.GetReactions(msgRef, slack.NewGetReactionsParameters()) | |
85 | 82 | if err != nil { |
86 | 83 | fmt.Printf("Error getting reactions: %s\n", err) |
87 | 84 | return |
93 | 90 | } |
94 | 91 | |
95 | 92 | // List all of the users reactions. |
96 | listReactionsParams := slack.NewListReactionsParameters(postAsUserID) | |
97 | listReactions, _, err := api.ListReactions(listReactionsParams) | |
93 | listReactions, _, err := api.ListReactions(slack.NewListReactionsParameters()) | |
98 | 94 | if err != nil { |
99 | 95 | fmt.Printf("Error listing reactions: %s\n", err) |
100 | 96 | return |
109 | 105 | } |
110 | 106 | |
111 | 107 | // Remove the :cry: reaction. |
112 | removeReactionParams := slack.NewRemoveReactionParameters("cry", msgRef) | |
113 | err = api.RemoveReaction(removeReactionParams) | |
108 | err = api.RemoveReaction("cry", msgRef) | |
114 | 109 | if err != nil { |
115 | 110 | fmt.Printf("Error remove reaction: %s\n", err) |
116 | 111 | return |
117 | 112 | } |
118 | 113 | |
119 | 114 | // Get all reactions on the message. |
120 | getReactionsParams = slack.NewGetReactionsParameters(msgRef) | |
121 | msgReactions, err = api.GetReactions(getReactionsParams) | |
115 | msgReactions, err = api.GetReactions(msgRef, slack.NewGetReactionsParameters()) | |
122 | 116 | if err != nil { |
123 | 117 | fmt.Printf("Error getting reactions: %s\n", err) |
124 | 118 | return |
22 | 22 | Reactions []ItemReaction |
23 | 23 | } |
24 | 24 | |
25 | // AddReactionParameters is the inputs to create a new reaction. | |
26 | type AddReactionParameters struct { | |
27 | Name string | |
28 | ItemRef | |
29 | } | |
30 | ||
31 | // NewAddReactionParameters initialies the inputs to react to an item. | |
32 | func NewAddReactionParameters(name string, ref ItemRef) AddReactionParameters { | |
33 | return AddReactionParameters{Name: name, ItemRef: ref} | |
34 | } | |
35 | ||
36 | // RemoveReactionParameters is the inputs to remove an existing reaction. | |
37 | type RemoveReactionParameters struct { | |
38 | Name string | |
39 | ItemRef | |
40 | } | |
41 | ||
42 | // NewAddReactionParameters initialies the inputs to react to an item. | |
43 | func NewRemoveReactionParameters(name string, ref ItemRef) RemoveReactionParameters { | |
44 | return RemoveReactionParameters{Name: name, ItemRef: ref} | |
45 | } | |
46 | ||
47 | 25 | // GetReactionsParameters is the inputs to get reactions to an item. |
48 | 26 | type GetReactionsParameters struct { |
49 | 27 | Full bool |
50 | ItemRef | |
51 | 28 | } |
52 | 29 | |
53 | 30 | // NewGetReactionsParameters initializes the inputs to get reactions to an item. |
54 | func NewGetReactionsParameters(ref ItemRef) GetReactionsParameters { | |
55 | return GetReactionsParameters{ItemRef: ref} | |
31 | func NewGetReactionsParameters() GetReactionsParameters { | |
32 | return GetReactionsParameters{ | |
33 | Full: false, | |
34 | } | |
56 | 35 | } |
57 | 36 | |
58 | 37 | type getReactionsResponseFull struct { |
90 | 69 | |
91 | 70 | // ListReactionsParameters is the inputs to find all reactions by a user. |
92 | 71 | type ListReactionsParameters struct { |
93 | User string | |
94 | Count int | |
95 | Page int | |
96 | Full bool | |
72 | UserId string | |
73 | Count int | |
74 | Page int | |
75 | Full bool | |
97 | 76 | } |
98 | 77 | |
99 | 78 | // NewListReactionsParameters initializes the inputs to find all reactions |
100 | 79 | // performed by a user. |
101 | func NewListReactionsParameters(userID string) ListReactionsParameters { | |
80 | func NewListReactionsParameters() ListReactionsParameters { | |
102 | 81 | return ListReactionsParameters{ |
103 | User: userID, | |
104 | Count: DEFAULT_REACTIONS_COUNT, | |
105 | Page: DEFAULT_REACTIONS_PAGE, | |
106 | Full: DEFAULT_REACTIONS_FULL, | |
82 | UserId: DEFAULT_REACTIONS_USERID, | |
83 | Count: DEFAULT_REACTIONS_COUNT, | |
84 | Page: DEFAULT_REACTIONS_PAGE, | |
85 | Full: DEFAULT_REACTIONS_FULL, | |
107 | 86 | } |
108 | 87 | } |
109 | 88 | |
150 | 129 | } |
151 | 130 | |
152 | 131 | // AddReaction adds a reaction emoji to a message, file or file comment. |
153 | func (api *Slack) AddReaction(params AddReactionParameters) error { | |
154 | values := url.Values{ | |
155 | "token": {api.config.token}, | |
156 | } | |
157 | if params.Name != "" { | |
158 | values.Set("name", params.Name) | |
159 | } | |
160 | if params.ChannelId != "" { | |
161 | values.Set("channel", string(params.ChannelId)) | |
162 | } | |
163 | if params.Timestamp != "" { | |
164 | values.Set("timestamp", string(params.Timestamp)) | |
165 | } | |
166 | if params.FileId != "" { | |
167 | values.Set("file", string(params.FileId)) | |
168 | } | |
169 | if params.FileCommentId != "" { | |
170 | values.Set("file_comment", string(params.FileCommentId)) | |
132 | func (api *Slack) AddReaction(name string, item ItemRef) error { | |
133 | values := url.Values{ | |
134 | "token": {api.config.token}, | |
135 | } | |
136 | if name != "" { | |
137 | values.Set("name", name) | |
138 | } | |
139 | if item.ChannelId != "" { | |
140 | values.Set("channel", string(item.ChannelId)) | |
141 | } | |
142 | if item.Timestamp != "" { | |
143 | values.Set("timestamp", string(item.Timestamp)) | |
144 | } | |
145 | if item.FileId != "" { | |
146 | values.Set("file", string(item.FileId)) | |
147 | } | |
148 | if item.FileCommentId != "" { | |
149 | values.Set("file_comment", string(item.FileCommentId)) | |
171 | 150 | } |
172 | 151 | response := &SlackResponse{} |
173 | 152 | if err := parseResponse("reactions.add", values, response, api.debug); err != nil { |
180 | 159 | } |
181 | 160 | |
182 | 161 | // RemoveReaction removes a reaction emoji from a message, file or file comment. |
183 | func (api *Slack) RemoveReaction(params RemoveReactionParameters) error { | |
184 | values := url.Values{ | |
185 | "token": {api.config.token}, | |
186 | } | |
187 | if params.Name != "" { | |
188 | values.Set("name", params.Name) | |
189 | } | |
190 | if params.ChannelId != "" { | |
191 | values.Set("channel", string(params.ChannelId)) | |
192 | } | |
193 | if params.Timestamp != "" { | |
194 | values.Set("timestamp", string(params.Timestamp)) | |
195 | } | |
196 | if params.FileId != "" { | |
197 | values.Set("file", string(params.FileId)) | |
198 | } | |
199 | if params.FileCommentId != "" { | |
200 | values.Set("file_comment", string(params.FileCommentId)) | |
162 | func (api *Slack) RemoveReaction(name string, item ItemRef) error { | |
163 | values := url.Values{ | |
164 | "token": {api.config.token}, | |
165 | } | |
166 | if name != "" { | |
167 | values.Set("name", name) | |
168 | } | |
169 | if item.ChannelId != "" { | |
170 | values.Set("channel", string(item.ChannelId)) | |
171 | } | |
172 | if item.Timestamp != "" { | |
173 | values.Set("timestamp", string(item.Timestamp)) | |
174 | } | |
175 | if item.FileId != "" { | |
176 | values.Set("file", string(item.FileId)) | |
177 | } | |
178 | if item.FileCommentId != "" { | |
179 | values.Set("file_comment", string(item.FileCommentId)) | |
201 | 180 | } |
202 | 181 | response := &SlackResponse{} |
203 | 182 | if err := parseResponse("reactions.remove", values, response, api.debug); err != nil { |
210 | 189 | } |
211 | 190 | |
212 | 191 | // GetReactions returns details about the reactions on an item. |
213 | func (api *Slack) GetReactions(params GetReactionsParameters) ([]ItemReaction, error) { | |
214 | values := url.Values{ | |
215 | "token": {api.config.token}, | |
216 | } | |
217 | if params.ChannelId != "" { | |
218 | values.Set("channel", string(params.ChannelId)) | |
219 | } | |
220 | if params.Timestamp != "" { | |
221 | values.Set("timestamp", string(params.Timestamp)) | |
222 | } | |
223 | if params.FileId != "" { | |
224 | values.Set("file", string(params.FileId)) | |
225 | } | |
226 | if params.FileCommentId != "" { | |
227 | values.Set("file_comment", string(params.FileCommentId)) | |
192 | func (api *Slack) GetReactions(item ItemRef, params GetReactionsParameters) ([]ItemReaction, error) { | |
193 | values := url.Values{ | |
194 | "token": {api.config.token}, | |
195 | } | |
196 | if item.ChannelId != "" { | |
197 | values.Set("channel", string(item.ChannelId)) | |
198 | } | |
199 | if item.Timestamp != "" { | |
200 | values.Set("timestamp", string(item.Timestamp)) | |
201 | } | |
202 | if item.FileId != "" { | |
203 | values.Set("file", string(item.FileId)) | |
204 | } | |
205 | if item.FileCommentId != "" { | |
206 | values.Set("file_comment", string(item.FileCommentId)) | |
228 | 207 | } |
229 | 208 | if params.Full != DEFAULT_REACTIONS_FULL { |
230 | 209 | values.Set("full", fmt.Sprintf("%t", params.Full)) |
244 | 223 | values := url.Values{ |
245 | 224 | "token": {api.config.token}, |
246 | 225 | } |
247 | if params.User != DEFAULT_REACTIONS_USERID { | |
248 | values.Add("user", params.User) | |
226 | if params.UserId != DEFAULT_REACTIONS_USERID { | |
227 | values.Add("user", params.UserId) | |
249 | 228 | } |
250 | 229 | if params.Count != DEFAULT_REACTIONS_COUNT { |
251 | 230 | values.Add("count", fmt.Sprintf("%d", params.Count)) |
43 | 43 | SLACK_API = "http://" + serverAddr + "/" |
44 | 44 | api := New("testing-token") |
45 | 45 | tests := []struct { |
46 | params AddReactionParameters | |
46 | name string | |
47 | ref ItemRef | |
47 | 48 | wantParams map[string]string |
48 | 49 | }{ |
49 | 50 | { |
50 | NewAddReactionParameters("thumbsup", NewRefToMessage("ChannelID", "123")), | |
51 | "thumbsup", | |
52 | NewRefToMessage("ChannelID", "123"), | |
51 | 53 | map[string]string{ |
52 | 54 | "name": "thumbsup", |
53 | 55 | "channel": "ChannelID", |
55 | 57 | }, |
56 | 58 | }, |
57 | 59 | { |
58 | NewAddReactionParameters("thumbsup", NewRefToFile("FileID")), | |
60 | "thumbsup", | |
61 | NewRefToFile("FileID"), | |
59 | 62 | map[string]string{ |
60 | 63 | "name": "thumbsup", |
61 | 64 | "file": "FileID", |
62 | 65 | }, |
63 | 66 | }, |
64 | 67 | { |
65 | NewAddReactionParameters("thumbsup", NewRefToFileComment("FileCommentID")), | |
68 | "thumbsup", | |
69 | NewRefToFileComment("FileCommentID"), | |
66 | 70 | map[string]string{ |
67 | 71 | "name": "thumbsup", |
68 | 72 | "file_comment": "FileCommentID", |
73 | 77 | http.HandleFunc("/reactions.add", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) |
74 | 78 | for i, test := range tests { |
75 | 79 | rh = newReactionsHandler() |
76 | err := api.AddReaction(test.params) | |
80 | err := api.AddReaction(test.name, test.ref) | |
77 | 81 | if err != nil { |
78 | 82 | t.Fatalf("%d: Unexpected error: %s", i, err) |
79 | 83 | } |
88 | 92 | SLACK_API = "http://" + serverAddr + "/" |
89 | 93 | api := New("testing-token") |
90 | 94 | tests := []struct { |
91 | params RemoveReactionParameters | |
95 | name string | |
96 | ref ItemRef | |
92 | 97 | wantParams map[string]string |
93 | 98 | }{ |
94 | 99 | { |
95 | NewRemoveReactionParameters("thumbsup", NewRefToMessage("ChannelID", "123")), | |
100 | "thumbsup", | |
101 | NewRefToMessage("ChannelID", "123"), | |
96 | 102 | map[string]string{ |
97 | 103 | "name": "thumbsup", |
98 | 104 | "channel": "ChannelID", |
100 | 106 | }, |
101 | 107 | }, |
102 | 108 | { |
103 | NewRemoveReactionParameters("thumbsup", NewRefToFile("FileID")), | |
109 | "thumbsup", | |
110 | NewRefToFile("FileID"), | |
104 | 111 | map[string]string{ |
105 | 112 | "name": "thumbsup", |
106 | 113 | "file": "FileID", |
107 | 114 | }, |
108 | 115 | }, |
109 | 116 | { |
110 | NewRemoveReactionParameters("thumbsup", NewRefToFileComment("FileCommentID")), | |
117 | "thumbsup", | |
118 | NewRefToFileComment("FileCommentID"), | |
111 | 119 | map[string]string{ |
112 | 120 | "name": "thumbsup", |
113 | 121 | "file_comment": "FileCommentID", |
118 | 126 | http.HandleFunc("/reactions.remove", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) |
119 | 127 | for i, test := range tests { |
120 | 128 | rh = newReactionsHandler() |
121 | err := api.RemoveReaction(test.params) | |
129 | err := api.RemoveReaction(test.name, test.ref) | |
122 | 130 | if err != nil { |
123 | 131 | t.Fatalf("%d: Unexpected error: %s", i, err) |
124 | 132 | } |
133 | 141 | SLACK_API = "http://" + serverAddr + "/" |
134 | 142 | api := New("testing-token") |
135 | 143 | tests := []struct { |
144 | ref ItemRef | |
136 | 145 | params GetReactionsParameters |
137 | 146 | wantParams map[string]string |
138 | 147 | json string |
139 | 148 | wantReactions []ItemReaction |
140 | 149 | }{ |
141 | 150 | { |
142 | ||
143 | GetReactionsParameters{ItemRef: NewRefToMessage("ChannelID", "123")}, | |
151 | NewRefToMessage("ChannelID", "123"), | |
152 | GetReactionsParameters{}, | |
144 | 153 | map[string]string{ |
145 | 154 | "channel": "ChannelID", |
146 | 155 | "timestamp": "123", |
167 | 176 | }, |
168 | 177 | }, |
169 | 178 | { |
170 | GetReactionsParameters{ItemRef: NewRefToFile("FileID"), Full: true}, | |
179 | NewRefToFile("FileID"), | |
180 | GetReactionsParameters{Full: true}, | |
171 | 181 | map[string]string{ |
172 | 182 | "file": "FileID", |
173 | 183 | "full": "true", |
195 | 205 | }, |
196 | 206 | { |
197 | 207 | |
198 | GetReactionsParameters{ItemRef: NewRefToFileComment("FileCommentID")}, | |
208 | NewRefToFileComment("FileCommentID"), | |
209 | GetReactionsParameters{}, | |
199 | 210 | map[string]string{ |
200 | 211 | "file_comment": "FileCommentID", |
201 | 212 | }, |
227 | 238 | for i, test := range tests { |
228 | 239 | rh = newReactionsHandler() |
229 | 240 | rh.response = test.json |
230 | got, err := api.GetReactions(test.params) | |
241 | got, err := api.GetReactions(test.ref, test.params) | |
231 | 242 | if err != nil { |
232 | 243 | t.Fatalf("%d: Unexpected error: %s", i, err) |
233 | 244 | } |
330 | 341 | "page": "2", |
331 | 342 | "full": "true", |
332 | 343 | } |
333 | params := NewListReactionsParameters("UserID") | |
344 | params := NewListReactionsParameters() | |
345 | params.UserId = "UserID" | |
334 | 346 | params.Count = 200 |
335 | 347 | params.Page = 2 |
336 | 348 | params.Full = true |