Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to expand coverage of users endpoints #57

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions models/team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package models

import "time"

type Team struct {
ID string `json:"_id"`
Name string `json:"name"`
Type int `json:"type"`
CreatedAt time.Time `json:"createdAt"`
CreatedBy struct {
ID string `json:"_id"`
Username string `json:"username"`
} `json:"createdBy"`
UpdatedAt time.Time `json:"_updatedAt"`
RoomID string `json:"roomId"`
IsOwner bool `json:"isOwner"`
}
57 changes: 57 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,60 @@ type UpdateUserRequest struct {
CustomFields map[string]string `json:"customFields,omitempty"`
} `json:"data"`
}

type Preferences struct {
User string `json:"user,omitempty"`
Language string `json:"language,omitempty"`
EnableAutoAway bool `json:"enableAutoAway,omitempty"`
IdleTimeoutLimit int `json:"idleTimeoutLimit,omitempty"`
DesktopNotificationDuration int `json:"desktopNotificationDuration,omitempty"`
AudioNotifications string `json:"audioNotifications,omitempty"`
DesktopNotifications string `json:"desktopNotifications,omitempty"`
MobileNotifications string `json:"mobileNotifications,omitempty"`
UnreadAlert bool `json:"unreadAlert,omitempty"`
UseEmojis bool `json:"useEmojis,omitempty"`
ConvertASCIIEmoji bool `json:"convertAsciiEmoji,omitempty"`
AutoImageLoad bool `json:"autoImageLoad,omitempty"`
SaveMobileBandwidth bool `json:"saveMobileBandwidth,omitempty"`
CollapseMediaByDefault bool `json:"collapseMediaByDefault,omitempty"`
HideUsernames bool `json:"hideUsernames,omitempty"`
HideRoles bool `json:"hideRoles,omitempty"`
HideFlexTab bool `json:"hideFlexTab,omitempty"`
HideAvatars bool `json:"hideAvatars,omitempty"`
RoomsListExhibitionMode string `json:"roomsListExhibitionMode,omitempty"`
SidebarViewMode string `json:"sidebarViewMode,omitempty"`
SidebarHideAvatar bool `json:"sidebarHideAvatar,omitempty"`
SidebarShowUnread bool `json:"sidebarShowUnread,omitempty"`
SidebarShowFavorites bool `json:"sidebarShowFavorites,omitempty"`
SendOnEnter string `json:"sendOnEnter,omitempty"`
MessageViewMode int `json:"messageViewMode,omitempty"`
EmailNotificationMode string `json:"emailNotificationMode,omitempty"`
RoomCounterSidebar bool `json:"roomCounterSidebar,omitempty"`
NewRoomNotification string `json:"newRoomNotification,omitempty"`
NewMessageNotification string `json:"newMessageNotification,omitempty"`
MuteFocusedConversations bool `json:"muteFocusedConversations,omitempty"`
NotificationsSoundVolume int `json:"notificationsSoundVolume,omitempty"`
}

type Settings struct {
Preferences `json:"preferences"`
}

type Email struct {
Address string `json:"address"`
Verified bool `json:"verified"`
}

type Me struct {
User
Emails []Email `json:"emails"`
StatusConnection string `json:"statusConnection"`
UtcOffset int `json:"utcOffset"`
Active bool `json:"active"`
Roles []string `json:"roles"`
Settings Settings `json:"settings"`
CustomFields struct {
Twitter string `json:"twitter,omitempty"`
} `json:"customFields,omitempty"`
AvatarURL string `json:"avatarUrl"`
}
133 changes: 133 additions & 0 deletions rest/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"

"github.com/RocketChat/Rocket.Chat.Go.SDK/models"
Expand Down Expand Up @@ -50,6 +51,44 @@ type CreateUserResponse struct {
} `json:"user"`
}

type userInfoResponse struct {
User *models.User `json:"user"`
Status
}

type meResponse struct {
models.Me
Status
}

type avatarResponse struct {
Url string `json:"url"`
Status
}

type usersResponse struct {
Status
Users []models.User `json:"users"`
}

type preferencesResponse struct {
Status
Preferenses models.Preferences `json:"preferences"`
}

type settingsResponse struct {
Status
Settings models.Settings `json:"settings"`
}

type teamsRequest struct {
UserID string `json:"userId"`
}
type teamsResponse struct {
Teams []models.Team `json:"teams"`
Status
}

type UserStatusResponse struct {
ID string `json:"_id"`
ConnectionStatus string `json:"connectionStatus"`
Expand Down Expand Up @@ -158,6 +197,32 @@ func (c *Client) UpdateUser(req *models.UpdateUserRequest) (*CreateUserResponse,
return response, err
}

// UserInfo retrieves information about a user.
// The result is only limited to what the callee has access to view.
//
// https://developer.rocket.chat/reference/api/rest-api/endpoints/core-endpoints/users-endpoints/get-users-info
func (c *Client) UserInfo(username string) (*models.User, error) {
params := url.Values{
"username": []string{username},
}
response := new(userInfoResponse)
err := c.Get("users.info", params, response)
return response.User, err

}

// GetAvatar gets the URL for a user’s avatar.
//
// https://developer.rocket.chat/reference/api/rest-api/endpoints/core-endpoints/users-endpoints/get-avatar
func (c *Client) GetAvatar(username string) (string, error) {
params := url.Values{
"username": []string{username},
}
response := new(avatarResponse)
err := c.Get("users.getAvatar", params, response)
return response.Url, err
}

// SetUserAvatar updates a user's avatar being logged in with a user that has permission to do so.
// Currently only passing an URL is possible.
//
Expand All @@ -178,3 +243,71 @@ func (c *Client) GetUserStatus(username string) (*UserStatusResponse, error) {
err := c.Get("users.getStatus", params, response)
return response, err
}

// Me returns quick information about the authenticated user.
//
// https://developer.rocket.chat/reference/api/rest-api/endpoints/other-important-endpoints/authentication-endpoints/me
func (c *Client) Me() (*models.Me, error) {
response := new(meResponse)
err := c.Get("me", nil, response)
return &response.Me, err
}

// GetUsers gets all of the users in the system and their information.
// The result is only limited to what the callee has access to view.
// It supports the Offset, Count, and Sort Query Parameters.
//
// https://developer.rocket.chat/reference/api/rest-api/endpoints/core-endpoints/users-endpoints/get-users-list
func (c *Client) GetUsers(page *models.Pagination) ([]models.User, error) {
params := url.Values{}
if page != nil {
params.Add("count", strconv.Itoa(page.Count))
params.Add("offset", strconv.Itoa(page.Offset))
params.Add("total", strconv.Itoa(page.Total))
}
response := new(usersResponse)
if err := c.Get("users.list", params, response); err != nil {
return nil, err
}
return response.Users, nil
}

// GetPreferences gets all preferences of the user.
//
// https://developer.rocket.chat/reference/api/rest-api/endpoints/core-endpoints/users-endpoints/get-user-preferences
func (c *Client) GetPreferences() (*models.Preferences, error) {
response := new(preferencesResponse)
err := c.Get("users.getPreferences", nil, response)
return &response.Preferenses, err
}

// SetPreferences sets preferences of the user.
//
// https://developer.rocket.chat/reference/api/rest-api/endpoints/core-endpoints/users-endpoints/set-preferences
func (c *Client) SetPreferences(preferences *models.Preferences) (*models.Preferences, error) {

body, err := json.Marshal(preferences)
if err != nil {
return nil, err
}

response := new(settingsResponse)
err = c.Post("users.saveUserPreferences", bytes.NewBuffer(body), response)
return &response.Settings.Preferences, err
}

// ListTeams lists users teams
//
// https://developer.rocket.chat/reference/api/rest-api/endpoints/core-endpoints/users-endpoints/list-users-teams
func (c *Client) ListTeams(user *models.User) ([]models.Team, error) {
request := teamsRequest{UserID: user.ID}
body, err := json.Marshal(request)
if err != nil {
return nil, err
}

response := new(teamsResponse)
err = c.Post("users.listTeams", bytes.NewBuffer(body), response)
return response.Teams, err

}
60 changes: 57 additions & 3 deletions rest/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,69 @@ import (
"testing"

"github.com/RocketChat/Rocket.Chat.Go.SDK/common_testing"
"github.com/RocketChat/Rocket.Chat.Go.SDK/models"
"github.com/stretchr/testify/assert"
)

func TestRocket_LoginLogout(t *testing.T) {
client := getAuthenticatedClient(t, common_testing.GetRandomString(), common_testing.GetRandomEmail(), common_testing.GetRandomString())
assert.NotNil(t, client)
_, logoutErr := client.Logout()
assert.Nil(t, logoutErr)
}

func TestRocket_UserInfo(t *testing.T) {
rocket := getDefaultClient(t)
user, err := rocket.UserInfo(testUserName)
assert.Nil(t, err)
assert.NotNil(t, user)
assert.Equal(t, user.UserName, testUserName)
}

func TestRocket_Me(t *testing.T) {
rocket := getDefaultClient(t)
me, err := rocket.Me()
assert.Nil(t, err)
assert.NotNil(t, me)
assert.Equal(t, testUserEmail, me.Emails[0].Address)
assert.Equal(t, testUserName, me.UserName)
}

func TestRocket_GetAvatar(t *testing.T) {
rocket := getDefaultClient(t)
url, err := rocket.GetAvatar(testUserName)
assert.Nil(t, err)
assert.NotNil(t, url)
}

func TestRocket_GetUsers(t *testing.T) {
rocket := getDefaultClient(t)
users, err := rocket.GetUsers(nil)
assert.Nil(t, err)
assert.NotNil(t, users)
assert.GreaterOrEqual(t, len(users), 1)
}

func TestRocket_GetPreferences(t *testing.T) {
rocket := getDefaultClient(t)
preferences, err := rocket.GetPreferences()
assert.Nil(t, err)
assert.NotNil(t, preferences)
}

func TestRocket_SetPreferences(t *testing.T) {
rocket := getDefaultClient(t)
preferences := &models.Preferences{User: testUserName}
updatedPreferences, err := rocket.SetPreferences(preferences)
assert.Nil(t, err)
assert.NotNil(t, updatedPreferences)
}

// channels, err := client.GetJoinedChannels()
// assert.Nil(t, channels)
// assert.NotNil(t, err)
func TestRocket_ListTeams(t *testing.T) {
rocket := getDefaultClient(t)
user, err := rocket.UserInfo(testUserName)
assert.Nil(t, err)
teams, err := rocket.ListTeams(user)
assert.Nil(t, err)
assert.NotNil(t, teams)
}