forked from lug/matterbridge
Update vendor (nlopes/slack)
This commit is contained in:
187
vendor/github.com/nlopes/slack/users.go
generated
vendored
187
vendor/github.com/nlopes/slack/users.go
generated
vendored
@@ -1,10 +1,18 @@
|
||||
package slack
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_USER_PHOTO_CROP_X = -1
|
||||
DEFAULT_USER_PHOTO_CROP_Y = -1
|
||||
DEFAULT_USER_PHOTO_CROP_W = -1
|
||||
)
|
||||
|
||||
// UserProfile contains all the information details of a given user
|
||||
type UserProfile struct {
|
||||
FirstName string `json:"first_name"`
|
||||
@@ -23,6 +31,8 @@ type UserProfile struct {
|
||||
Title string `json:"title"`
|
||||
BotID string `json:"bot_id,omitempty"`
|
||||
ApiAppID string `json:"api_app_id,omitempty"`
|
||||
StatusText string `json:"status_text,omitempty"`
|
||||
StatusEmoji string `json:"status_emoji,omitempty"`
|
||||
}
|
||||
|
||||
// User contains all the information of a user
|
||||
@@ -97,9 +107,23 @@ type userResponseFull struct {
|
||||
SlackResponse
|
||||
}
|
||||
|
||||
func userRequest(path string, values url.Values, debug bool) (*userResponseFull, error) {
|
||||
type UserSetPhotoParams struct {
|
||||
CropX int
|
||||
CropY int
|
||||
CropW int
|
||||
}
|
||||
|
||||
func NewUserSetPhotoParams() UserSetPhotoParams {
|
||||
return UserSetPhotoParams{
|
||||
CropX: DEFAULT_USER_PHOTO_CROP_X,
|
||||
CropY: DEFAULT_USER_PHOTO_CROP_Y,
|
||||
CropW: DEFAULT_USER_PHOTO_CROP_W,
|
||||
}
|
||||
}
|
||||
|
||||
func userRequest(ctx context.Context, path string, values url.Values, debug bool) (*userResponseFull, error) {
|
||||
response := &userResponseFull{}
|
||||
err := post(path, values, response, debug)
|
||||
err := post(ctx, path, values, response, debug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -111,11 +135,16 @@ func userRequest(path string, values url.Values, debug bool) (*userResponseFull,
|
||||
|
||||
// GetUserPresence will retrieve the current presence status of given user.
|
||||
func (api *Client) GetUserPresence(user string) (*UserPresence, error) {
|
||||
return api.GetUserPresenceContext(context.Background(), user)
|
||||
}
|
||||
|
||||
// GetUserPresenceContext will retrieve the current presence status of given user with a custom context.
|
||||
func (api *Client) GetUserPresenceContext(ctx context.Context, user string) (*UserPresence, error) {
|
||||
values := url.Values{
|
||||
"token": {api.config.token},
|
||||
"user": {user},
|
||||
}
|
||||
response, err := userRequest("users.getPresence", values, api.debug)
|
||||
response, err := userRequest(ctx, "users.getPresence", values, api.debug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -124,11 +153,16 @@ func (api *Client) GetUserPresence(user string) (*UserPresence, error) {
|
||||
|
||||
// GetUserInfo will retrieve the complete user information
|
||||
func (api *Client) GetUserInfo(user string) (*User, error) {
|
||||
return api.GetUserInfoContext(context.Background(), user)
|
||||
}
|
||||
|
||||
// GetUserInfoContext will retrieve the complete user information with a custom context
|
||||
func (api *Client) GetUserInfoContext(ctx context.Context, user string) (*User, error) {
|
||||
values := url.Values{
|
||||
"token": {api.config.token},
|
||||
"user": {user},
|
||||
}
|
||||
response, err := userRequest("users.info", values, api.debug)
|
||||
response, err := userRequest(ctx, "users.info", values, api.debug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -137,11 +171,16 @@ func (api *Client) GetUserInfo(user string) (*User, error) {
|
||||
|
||||
// GetUsers returns the list of users (with their detailed information)
|
||||
func (api *Client) GetUsers() ([]User, error) {
|
||||
return api.GetUsersContext(context.Background())
|
||||
}
|
||||
|
||||
// GetUsersContext returns the list of users (with their detailed information) with a custom context
|
||||
func (api *Client) GetUsersContext(ctx context.Context) ([]User, error) {
|
||||
values := url.Values{
|
||||
"token": {api.config.token},
|
||||
"presence": {"1"},
|
||||
}
|
||||
response, err := userRequest("users.list", values, api.debug)
|
||||
response, err := userRequest(ctx, "users.list", values, api.debug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -150,10 +189,15 @@ func (api *Client) GetUsers() ([]User, error) {
|
||||
|
||||
// SetUserAsActive marks the currently authenticated user as active
|
||||
func (api *Client) SetUserAsActive() error {
|
||||
return api.SetUserAsActiveContext(context.Background())
|
||||
}
|
||||
|
||||
// SetUserAsActiveContext marks the currently authenticated user as active with a custom context
|
||||
func (api *Client) SetUserAsActiveContext(ctx context.Context) error {
|
||||
values := url.Values{
|
||||
"token": {api.config.token},
|
||||
}
|
||||
_, err := userRequest("users.setActive", values, api.debug)
|
||||
_, err := userRequest(ctx, "users.setActive", values, api.debug)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -162,11 +206,16 @@ func (api *Client) SetUserAsActive() error {
|
||||
|
||||
// SetUserPresence changes the currently authenticated user presence
|
||||
func (api *Client) SetUserPresence(presence string) error {
|
||||
return api.SetUserPresenceContext(context.Background(), presence)
|
||||
}
|
||||
|
||||
// SetUserPresenceContext changes the currently authenticated user presence with a custom context
|
||||
func (api *Client) SetUserPresenceContext(ctx context.Context, presence string) error {
|
||||
values := url.Values{
|
||||
"token": {api.config.token},
|
||||
"presence": {presence},
|
||||
}
|
||||
_, err := userRequest("users.setPresence", values, api.debug)
|
||||
_, err := userRequest(ctx, "users.setPresence", values, api.debug)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -176,11 +225,16 @@ func (api *Client) SetUserPresence(presence string) error {
|
||||
|
||||
// GetUserIdentity will retrieve user info available per identity scopes
|
||||
func (api *Client) GetUserIdentity() (*UserIdentityResponse, error) {
|
||||
return api.GetUserIdentityContext(context.Background())
|
||||
}
|
||||
|
||||
// GetUserIdentityContext will retrieve user info available per identity scopes with a custom context
|
||||
func (api *Client) GetUserIdentityContext(ctx context.Context) (*UserIdentityResponse, error) {
|
||||
values := url.Values{
|
||||
"token": {api.config.token},
|
||||
}
|
||||
response := &UserIdentityResponse{}
|
||||
err := post("users.identity", values, response, api.debug)
|
||||
err := post(ctx, "users.identity", values, response, api.debug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -189,3 +243,120 @@ func (api *Client) GetUserIdentity() (*UserIdentityResponse, error) {
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// SetUserPhoto changes the currently authenticated user's profile image
|
||||
func (api *Client) SetUserPhoto(ctx context.Context, image string, params UserSetPhotoParams) error {
|
||||
return api.SetUserPhoto(context.Background(), image, params)
|
||||
}
|
||||
|
||||
// SetUserPhotoContext changes the currently authenticated user's profile image using a custom context
|
||||
func (api *Client) SetUserPhotoContext(ctx context.Context, image string, params UserSetPhotoParams) error {
|
||||
response := &SlackResponse{}
|
||||
values := url.Values{
|
||||
"token": {api.config.token},
|
||||
}
|
||||
if params.CropX != DEFAULT_USER_PHOTO_CROP_X {
|
||||
values.Add("crop_x", string(params.CropX))
|
||||
}
|
||||
if params.CropY != DEFAULT_USER_PHOTO_CROP_Y {
|
||||
values.Add("crop_y", string(params.CropY))
|
||||
}
|
||||
if params.CropW != DEFAULT_USER_PHOTO_CROP_W {
|
||||
values.Add("crop_w", string(params.CropW))
|
||||
}
|
||||
err := postLocalWithMultipartResponse(ctx, "users.setPhoto", image, "image", values, response, api.debug)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !response.Ok {
|
||||
return errors.New(response.Error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteUserPhoto deletes the current authenticated user's profile image
|
||||
func (api *Client) DeleteUserPhoto() error {
|
||||
return api.DeleteUserPhotoContext(context.Background())
|
||||
}
|
||||
|
||||
// DeleteUserPhotoContext deletes the current authenticated user's profile image with a custom context
|
||||
func (api *Client) DeleteUserPhotoContext(ctx context.Context) error {
|
||||
response := &SlackResponse{}
|
||||
values := url.Values{
|
||||
"token": {api.config.token},
|
||||
}
|
||||
err := post(ctx, "users.deletePhoto", values, response, api.debug)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !response.Ok {
|
||||
return errors.New(response.Error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetUserCustomStatus will set a custom status and emoji for the currently
|
||||
// authenticated user. If statusEmoji is "" and statusText is not, the Slack API
|
||||
// will automatically set it to ":speech_balloon:". Otherwise, if both are ""
|
||||
// the Slack API will unset the custom status/emoji.
|
||||
func (api *Client) SetUserCustomStatus(statusText, statusEmoji string) error {
|
||||
return api.SetUserCustomStatusContext(context.Background(), statusText, statusEmoji)
|
||||
}
|
||||
|
||||
// SetUserCustomStatusContext will set a custom status and emoji for the currently authenticated user with a custom context
|
||||
//
|
||||
// For more information see SetUserCustomStatus
|
||||
func (api *Client) SetUserCustomStatusContext(ctx context.Context, statusText, statusEmoji string) error {
|
||||
// XXX(theckman): this anonymous struct is for making requests to the Slack
|
||||
// API for setting and unsetting a User's Custom Status/Emoji. To change
|
||||
// these values we must provide a JSON document as the profile POST field.
|
||||
//
|
||||
// We use an anonymous struct over UserProfile because to unset the values
|
||||
// on the User's profile we cannot use the `json:"omitempty"` tag. This is
|
||||
// because an empty string ("") is what's used to unset the values. Check
|
||||
// out the API docs for more details:
|
||||
//
|
||||
// - https://api.slack.com/docs/presence-and-status#custom_status
|
||||
profile, err := json.Marshal(
|
||||
&struct {
|
||||
StatusText string `json:"status_text"`
|
||||
StatusEmoji string `json:"status_emoji"`
|
||||
}{
|
||||
StatusText: statusText,
|
||||
StatusEmoji: statusEmoji,
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
values := url.Values{
|
||||
"token": {api.config.token},
|
||||
"profile": {string(profile)},
|
||||
}
|
||||
|
||||
response := &userResponseFull{}
|
||||
|
||||
if err = post(ctx, "users.profile.set", values, response, api.debug); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !response.Ok {
|
||||
return errors.New(response.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnsetUserCustomStatus removes the custom status message for the currently
|
||||
// authenticated user. This is a convenience method that wraps (*Client).SetUserCustomStatus().
|
||||
func (api *Client) UnsetUserCustomStatus() error {
|
||||
return api.UnsetUserCustomStatusContext(context.Background())
|
||||
}
|
||||
|
||||
// UnsetUserCustomStatusContext removes the custom status message for the currently authenticated user
|
||||
// with a custom context. This is a convenience method that wraps (*Client).SetUserCustomStatus().
|
||||
func (api *Client) UnsetUserCustomStatusContext(ctx context.Context) error {
|
||||
return api.SetUserCustomStatusContext(ctx, "", "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user