-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.go
146 lines (125 loc) · 4.4 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package rc
import (
"net/url"
"time"
)
// Generated by https://quicktype.io
type Me struct {
ID string `json:"_id"`
Name string `json:"name"`
Emails []Email `json:"emails"`
Status string `json:"status"`
StatusConnection string `json:"statusConnection"`
Username string `json:"username"`
UTCOffset int64 `json:"utcOffset"`
Active bool `json:"active"`
Roles []string `json:"roles"`
Settings Settings `json:"settings"`
CustomFields CustomFields `json:"customFields"`
Success bool `json:"success"`
}
type CustomFields map[string]interface{}
type Email struct {
Address string `json:"address"`
Verified bool `json:"verified"`
}
type Settings struct {
Preferences Preferences `json:"preferences"`
}
type Preferences struct {
EnableAutoAway bool `json:"enableAutoAway"`
IdleTimeoutLimit int64 `json:"idleTimeoutLimit"`
DesktopNotificationDuration int64 `json:"desktopNotificationDuration"`
AudioNotifications string `json:"audioNotifications"`
DesktopNotifications string `json:"desktopNotifications"`
MobileNotifications string `json:"mobileNotifications"`
UnreadAlert bool `json:"unreadAlert"`
UseEmojis bool `json:"useEmojis"`
ConvertASCIIEmoji bool `json:"convertAsciiEmoji"`
AutoImageLoad bool `json:"autoImageLoad"`
SaveMobileBandwidth bool `json:"saveMobileBandwidth"`
CollapseMediaByDefault bool `json:"collapseMediaByDefault"`
HideUsernames bool `json:"hideUsernames"`
HideRoles bool `json:"hideRoles"`
HideFlexTab bool `json:"hideFlexTab"`
HideAvatars bool `json:"hideAvatars"`
RoomsListExhibitionMode string `json:"roomsListExhibitionMode"`
SidebarViewMode string `json:"sidebarViewMode"`
SidebarHideAvatar bool `json:"sidebarHideAvatar"`
SidebarShowUnread bool `json:"sidebarShowUnread"`
SidebarShowFavorites bool `json:"sidebarShowFavorites"`
SendOnEnter string `json:"sendOnEnter"`
MessageViewMode int64 `json:"messageViewMode"`
EmailNotificationMode string `json:"emailNotificationMode"`
RoomCounterSidebar bool `json:"roomCounterSidebar"`
NewRoomNotification string `json:"newRoomNotification"`
NewMessageNotification string `json:"newMessageNotification"`
MuteFocusedConversations bool `json:"muteFocusedConversations"`
NotificationsSoundVolume int64 `json:"notificationsSoundVolume"`
}
type prefEnv struct {
Success bool `json:"success"`
Settings
}
type UserList struct {
Users []User `json:"users"`
Status string `json:"status"`
}
type UserEnv struct {
User User `json:"user"`
Success bool `json:"success"`
}
type User struct {
ID string `json:"_id"`
Name string `json:"name"`
Username string `json:"username"`
Status string `json:"status"`
UTCOffset int64 `json:"utcOffset"`
Active bool `json:"active"`
Type string `json:"type"`
}
type UserPresence struct {
Users []User `json:"users"`
Full bool `json:"full"`
Success bool `json:"success"`
}
func (c *Client) GetUsers() ([]User, error) {
users := &UserList{}
if err := c.c.get("/users.list", nil).JSON(users); err != nil {
return nil, err
}
return users.Users, nil
}
func (c *Client) getUser(vals url.Values) (*User, error) {
userW := &UserEnv{}
if err := c.c.get("/users.info", vals).JSON(userW); err != nil {
return nil, err
}
user := userW.User
return &user, nil
}
func (c *Client) GetUserByName(username string) (*User, error) {
return c.getUser(query("username", username).Q())
}
func (c *Client) GetUserByID(id string) (*User, error) {
return c.getUser(query("userId", id).Q())
}
func (c *Client) GetUsersPresence(from *time.Time) ([]User, error) {
userP := &UserPresence{}
vals := url.Values{}
if from != nil {
vals = query("from", from.Format(TimeFormat)).Q()
}
if err := c.c.get("/users.presence", vals).JSON(userP); err != nil {
return nil, err
}
return userP.Users, nil
}
func (c *Client) GetMyPreferences() (*Preferences, error) {
userP := &prefEnv{}
if err := c.c.get("/users/getPreferences", nil).JSON(userP); err != nil {
return nil, err
}
p := userP.Preferences
return &p, nil
}