diff options
Diffstat (limited to 'vendor/github.com/mattermost/platform/model/client.go')
-rw-r--r-- | vendor/github.com/mattermost/platform/model/client.go | 143 |
1 files changed, 141 insertions, 2 deletions
diff --git a/vendor/github.com/mattermost/platform/model/client.go b/vendor/github.com/mattermost/platform/model/client.go index e8ce21ab..2f1e846c 100644 --- a/vendor/github.com/mattermost/platform/model/client.go +++ b/vendor/github.com/mattermost/platform/model/client.go @@ -7,7 +7,9 @@ import ( "bytes" "fmt" l4g "github.com/alecthomas/log4go" + "io" "io/ioutil" + "mime/multipart" "net/http" "net/url" "strconv" @@ -106,6 +108,10 @@ func (c *Client) GetChannelNameRoute(channelName string) string { return fmt.Sprintf("/teams/%v/channels/name/%v", c.GetTeamId(), channelName) } +func (c *Client) GetEmojiRoute() string { + return "/emoji" +} + func (c *Client) GetGeneralRoute() string { return "/general" } @@ -185,6 +191,17 @@ func (c *Client) Must(result *Result, err *AppError) *Result { return result } +// MustGeneric is a convenience function used for testing. +func (c *Client) MustGeneric(result interface{}, err *AppError) interface{} { + if err != nil { + l4g.Close() + time.Sleep(time.Second) + panic(err) + } + + return result +} + // CheckStatusOK is a convenience function for checking the return of Web Service // call that return the a map of status=OK. func (c *Client) CheckStatusOK(r *http.Response) bool { @@ -328,10 +345,18 @@ func (c *Client) FindTeamByName(name string) (*Result, *AppError) { } } -func (c *Client) AddUserToTeam(userId string) (*Result, *AppError) { +// Adds a user directly to the team without sending an invite. +// The teamId and userId are required. You must be a valid member of the team and/or +// have the correct role to add new users to the team. Returns a map of user_id=userId +// if successful, otherwise returns an AppError. +func (c *Client) AddUserToTeam(teamId string, userId string) (*Result, *AppError) { + if len(teamId) == 0 { + teamId = c.GetTeamId() + } + data := make(map[string]string) data["user_id"] = userId - if r, err := c.DoApiPost(c.GetTeamRoute()+"/add_user_to_team", MapToJson(data)); err != nil { + if r, err := c.DoApiPost(fmt.Sprintf("/teams/%v", teamId)+"/add_user_to_team", MapToJson(data)); err != nil { return nil, err } else { defer closeBody(r) @@ -354,6 +379,26 @@ func (c *Client) AddUserToTeamFromInvite(hash, dataToHash, inviteId string) (*Re } } +// Removes a user directly from the team. +// The teamId and userId are required. You must be a valid member of the team and/or +// have the correct role to remove a user from the team. Returns a map of user_id=userId +// if successful, otherwise returns an AppError. +func (c *Client) RemoveUserFromTeam(teamId string, userId string) (*Result, *AppError) { + if len(teamId) == 0 { + teamId = c.GetTeamId() + } + + data := make(map[string]string) + data["user_id"] = userId + if r, err := c.DoApiPost(fmt.Sprintf("/teams/%v", teamId)+"/remove_user_from_team", MapToJson(data)); err != nil { + return nil, err + } else { + defer closeBody(r) + return &Result{r.Header.Get(HEADER_REQUEST_ID), + r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil + } +} + func (c *Client) InviteMembers(invites *Invites) (*Result, *AppError) { if r, err := c.DoApiPost(c.GetTeamRoute()+"/invite_members", invites.ToJson()); err != nil { return nil, err @@ -843,6 +888,20 @@ func (c *Client) GetSystemAnalytics(name string) (*Result, *AppError) { } } +// Initiate immediate synchronization of LDAP users. +// The synchronization will be performed asynchronously and this function will +// always return OK unless you don't have permissions. +// You must be the system administrator to use this function. +func (c *Client) LdapSyncNow() (*Result, *AppError) { + if r, err := c.DoApiPost("/admin/ldap_sync_now", ""); err != nil { + return nil, err + } else { + defer closeBody(r) + return &Result{r.Header.Get(HEADER_REQUEST_ID), + r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil + } +} + func (c *Client) CreateChannel(channel *Channel) (*Result, *AppError) { if r, err := c.DoApiPost(c.GetTeamRoute()+"/channels/create", channel.ToJson()); err != nil { return nil, err @@ -949,6 +1008,7 @@ func (c *Client) JoinChannel(id string) (*Result, *AppError) { if r, err := c.DoApiPost(c.GetChannelRoute(id)+"/join", ""); err != nil { return nil, err } else { + defer closeBody(r) return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), nil}, nil } @@ -958,6 +1018,7 @@ func (c *Client) JoinChannelByName(name string) (*Result, *AppError) { if r, err := c.DoApiPost(c.GetChannelNameRoute(name)+"/join", ""); err != nil { return nil, err } else { + defer closeBody(r) return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), nil}, nil } @@ -967,6 +1028,7 @@ func (c *Client) LeaveChannel(id string) (*Result, *AppError) { if r, err := c.DoApiPost(c.GetChannelRoute(id)+"/leave", ""); err != nil { return nil, err } else { + defer closeBody(r) return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), nil}, nil } @@ -976,6 +1038,7 @@ func (c *Client) DeleteChannel(id string) (*Result, *AppError) { if r, err := c.DoApiPost(c.GetChannelRoute(id)+"/delete", ""); err != nil { return nil, err } else { + defer closeBody(r) return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), nil}, nil } @@ -987,6 +1050,7 @@ func (c *Client) AddChannelMember(id, user_id string) (*Result, *AppError) { if r, err := c.DoApiPost(c.GetChannelRoute(id)+"/add", MapToJson(data)); err != nil { return nil, err } else { + defer closeBody(r) return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), nil}, nil } @@ -998,6 +1062,7 @@ func (c *Client) RemoveChannelMember(id, user_id string) (*Result, *AppError) { if r, err := c.DoApiPost(c.GetChannelRoute(id)+"/remove", MapToJson(data)); err != nil { return nil, err } else { + defer closeBody(r) return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), nil}, nil } @@ -1007,6 +1072,7 @@ func (c *Client) UpdateLastViewedAt(channelId string) (*Result, *AppError) { if r, err := c.DoApiPost(c.GetChannelRoute(channelId)+"/update_last_viewed_at", ""); err != nil { return nil, err } else { + defer closeBody(r) return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), nil}, nil } @@ -1376,6 +1442,7 @@ func (c *Client) PostToWebhook(id, payload string) (*Result, *AppError) { if r, err := c.DoPost("/hooks/"+id, payload, "application/x-www-form-urlencoded"); err != nil { return nil, err } else { + defer closeBody(r) return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), nil}, nil } @@ -1417,6 +1484,7 @@ func (c *Client) SetPreferences(preferences *Preferences) (*Result, *AppError) { if r, err := c.DoApiPost("/preferences/save", preferences.ToJson()); err != nil { return nil, err } else { + defer closeBody(r) return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), preferences}, nil } @@ -1509,3 +1577,74 @@ func (c *Client) GetInitialLoad() (*Result, *AppError) { r.Header.Get(HEADER_ETAG_SERVER), InitialLoadFromJson(r.Body)}, nil } } + +// ListEmoji returns a list of all user-created emoji for the server. +func (c *Client) ListEmoji() ([]*Emoji, *AppError) { + if r, err := c.DoApiGet(c.GetEmojiRoute()+"/list", "", ""); err != nil { + return nil, err + } else { + defer closeBody(r) + c.fillInExtraProperties(r) + return EmojiListFromJson(r.Body), nil + } +} + +// CreateEmoji will save an emoji to the server if the current user has permission +// to do so. If successful, the provided emoji will be returned with its Id field +// filled in. Otherwise, an error will be returned. +func (c *Client) CreateEmoji(emoji *Emoji, image []byte, filename string) (*Emoji, *AppError) { + c.clearExtraProperties() + + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + + if part, err := writer.CreateFormFile("image", filename); err != nil { + return nil, NewLocAppError("CreateEmoji", "model.client.create_emoji.image.app_error", nil, err.Error()) + } else if _, err = io.Copy(part, bytes.NewBuffer(image)); err != nil { + return nil, NewLocAppError("CreateEmoji", "model.client.create_emoji.image.app_error", nil, err.Error()) + } + + if err := writer.WriteField("emoji", emoji.ToJson()); err != nil { + return nil, NewLocAppError("CreateEmoji", "model.client.create_emoji.emoji.app_error", nil, err.Error()) + } + + if err := writer.Close(); err != nil { + return nil, NewLocAppError("CreateEmoji", "model.client.create_emoji.writer.app_error", nil, err.Error()) + } + + rq, _ := http.NewRequest("POST", c.ApiUrl+c.GetEmojiRoute()+"/create", body) + rq.Header.Set("Content-Type", writer.FormDataContentType()) + + if len(c.AuthToken) > 0 { + rq.Header.Set(HEADER_AUTH, "BEARER "+c.AuthToken) + } + + if r, err := c.HttpClient.Do(rq); err != nil { + return nil, NewLocAppError("CreateEmoji", "model.client.connecting.app_error", nil, err.Error()) + } else if r.StatusCode >= 300 { + return nil, AppErrorFromJson(r.Body) + } else { + defer closeBody(r) + c.fillInExtraProperties(r) + return EmojiFromJson(r.Body), nil + } +} + +// DeleteEmoji will delete an emoji from the server if the current user has permission +// to do so. If successful, it will return status=ok. Otherwise, an error will be returned. +func (c *Client) DeleteEmoji(id string) (bool, *AppError) { + data := map[string]string{"id": id} + + if r, err := c.DoApiPost(c.GetEmojiRoute()+"/delete", MapToJson(data)); err != nil { + return false, err + } else { + c.fillInExtraProperties(r) + return c.CheckStatusOK(r), nil + } +} + +// GetCustomEmojiImageUrl returns the API route that can be used to get the image used by +// the given emoji. +func (c *Client) GetCustomEmojiImageUrl(id string) string { + return c.GetEmojiRoute() + "/" + id +} |