Codebase list golang-github-nlopes-slack / c4de7b6
Merge pull request #148 from joshheinrichs/master Add users.setPhoto and users.deletePhoto Florin Pățan authored 7 years ago GitHub committed 7 years ago
3 changed file(s) with 66 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
220220 values.Add("content", params.Content)
221221 err = post("files.upload", values, response, api.debug)
222222 } else if params.File != "" {
223 err = postWithMultipartResponse("files.upload", params.File, values, response, api.debug)
223 err = postWithMultipartResponse("files.upload", params.File, "file", values, response, api.debug)
224224 }
225225 if err != nil {
226226 return nil, err
2828 return string(s)
2929 }
3030
31 func fileUploadReq(path, fpath string, values url.Values) (*http.Request, error) {
31 func fileUploadReq(path, fpath, fieldname string, values url.Values) (*http.Request, error) {
3232 fullpath, err := filepath.Abs(fpath)
3333 if err != nil {
3434 return nil, err
4242 body := &bytes.Buffer{}
4343 wr := multipart.NewWriter(body)
4444
45 ioWriter, err := wr.CreateFormFile("file", filepath.Base(fullpath))
45 ioWriter, err := wr.CreateFormFile(fieldname, filepath.Base(fullpath))
4646 if err != nil {
4747 wr.Close()
4848 return nil, err
8989 return nil
9090 }
9191
92 func postWithMultipartResponse(path string, filepath string, values url.Values, intf interface{}, debug bool) error {
93 req, err := fileUploadReq(SLACK_API+path, filepath, values)
92 func postWithMultipartResponse(path, filepath, fieldname string, values url.Values, intf interface{}, debug bool) error {
93 req, err := fileUploadReq(SLACK_API+path, filepath, fieldname, values)
9494 resp, err := HTTPClient.Do(req)
9595 if err != nil {
9696 return err
22 import (
33 "errors"
44 "net/url"
5 )
6
7 const (
8 DEFAULT_USER_PHOTO_CROP_X = -1
9 DEFAULT_USER_PHOTO_CROP_Y = -1
10 DEFAULT_USER_PHOTO_CROP_W = -1
511 )
612
713 // UserProfile contains all the information details of a given user
96102 SlackResponse
97103 }
98104
105 type UserSetPhotoParams struct {
106 CropX int
107 CropY int
108 CropW int
109 }
110
111 func NewUserSetPhotoParams() UserSetPhotoParams {
112 return UserSetPhotoParams{
113 CropX: DEFAULT_USER_PHOTO_CROP_X,
114 CropY: DEFAULT_USER_PHOTO_CROP_Y,
115 CropW: DEFAULT_USER_PHOTO_CROP_W,
116 }
117 }
118
99119 func userRequest(path string, values url.Values, debug bool) (*userResponseFull, error) {
100120 response := &userResponseFull{}
101121 err := post(path, values, response, debug)
188208 }
189209 return response, nil
190210 }
211
212 // SetUserPhoto changes the currently authenticated user's profile image
213 func (api *Client) SetUserPhoto(image string, params UserSetPhotoParams) error {
214 response := &SlackResponse{}
215 values := url.Values{
216 "token": {api.config.token},
217 }
218 if params.CropX != DEFAULT_USER_PHOTO_CROP_X {
219 values.Add("crop_x", string(params.CropX))
220 }
221 if params.CropY != DEFAULT_USER_PHOTO_CROP_Y {
222 values.Add("crop_y", string(params.CropY))
223 }
224 if params.CropW != DEFAULT_USER_PHOTO_CROP_W {
225 values.Add("crop_w", string(params.CropW))
226 }
227 err := postWithMultipartResponse("users.setPhoto", image, "image", values, response, api.debug)
228 if err != nil {
229 return err
230 }
231 if !response.Ok {
232 return errors.New(response.Error)
233 }
234 return nil
235 }
236
237 // DeleteUserPhoto deletes the current authenticated user's profile image
238 func (api *Client) DeleteUserPhoto() error {
239 response := &SlackResponse{}
240 values := url.Values{
241 "token": {api.config.token},
242 }
243 err := post("users.deletePhoto", values, response, api.debug)
244 if err != nil {
245 return err
246 }
247 if !response.Ok {
248 return errors.New(response.Error)
249 }
250 return nil
251 }