Codebase list golang-github-nlopes-slack / e97966d
Revert unrelated files to master branch version Matt authored 5 years ago James committed 5 years ago
3 changed file(s) with 0 addition(s) and 169 deletion(s). Raw diff Collapse all Expand all
2121 Domain string `json:"domain"`
2222 EmailDomain string `json:"email_domain"`
2323 Icon map[string]interface{} `json:"icon"`
24 }
25
26 type TeamProfileResponse struct {
27 Profile TeamProfile `json:"profile"`
28 SlackResponse
29 }
30
31 type TeamProfile struct {
32 Fields []TeamProfileField `json:"fields"`
33 }
34
35 type TeamProfileField struct {
36 ID string `json:"id"`
37 Ordering int `json:"ordering"`
38 Label string `json:"label"`
39 Hint string `json:"hint"`
40 Type string `json:"type"`
41 PossibleValues []string `json:"possible_values"`
42 IsHidden bool `json:"is_hidden"`
43 Options map[string]bool `json:"options"`
4424 }
4525
4626 type LoginResponse struct {
9575 return response, response.Err()
9676 }
9777
98 func teamProfileRequest(ctx context.Context, client httpClient, path string, values url.Values, d debug) (*TeamProfileResponse, error) {
99 response := &TeamProfileResponse{}
100 err := postSlackMethod(ctx, client, path, values, response, d)
101 if err != nil {
102 return nil, err
103 }
104 return response, response.Err()
105 }
106
10778 func billableInfoRequest(ctx context.Context, client httpClient, path string, values url.Values, d debug) (map[string]BillingActive, error) {
10879 response := &BillableInfoResponse{}
10980 err := postSlackMethod(ctx, client, path, values, response, d)
139110 return nil, err
140111 }
141112 return &response.Team, nil
142 }
143
144 // GetTeamProfile gets the Team Profile settings of the user
145 func (api *Client) GetTeamProfile() (*TeamProfile, error) {
146 return api.GetTeamProfileContext(context.Background())
147 }
148
149 // GetTeamProfileContext gets the Team Profile settings of the user with a custom context
150 func (api *Client) GetTeamProfileContext(ctx context.Context) (*TeamProfile, error) {
151 values := url.Values{
152 "token": {api.token},
153 }
154
155 response, err := teamProfileRequest(ctx, api.httpclient, "team.profile.get", values, api)
156 if err != nil {
157 return nil, err
158 }
159 return &response.Profile, nil
160
161113 }
162114
163115 // GetAccessLogs retrieves a page of logins according to the parameters given
99 var (
1010 ErrIncorrectResponse = errors.New("Response is incorrect")
1111 )
12
13 func getTeamProfile(rw http.ResponseWriter, r *http.Request) {
14 rw.Header().Set("Content-Type", "application/json")
15 response := []byte(`{
16 "ok":true,
17 "profile":{
18 "fields":[
19 {
20 "id":"XXXD7KN555",
21 "ordering":2,
22 "field_name":"",
23 "label":"Skype",
24 "hint":"This will be displayed on your profile.",
25 "type":"text",
26 "possible_values":null,
27 "options":null,
28 "is_hidden":true
29 },
30 {
31 "id":"XXXGGE5AAN7",
32 "ordering":4,
33 "field_name":"title",
34 "label":"Title",
35 "hint":"",
36 "type":"text",
37 "possible_values":null,
38 "options":{
39 "is_protected":true,
40 "is_scim":true
41 },
42 "is_hidden":false
43 }
44 ]
45 }
46 }`)
47
48 rw.Write(response)
49 }
5012
5113 func getTeamInfo(rw http.ResponseWriter, r *http.Request) {
5214 rw.Header().Set("Content-Type", "application/json")
6224 }
6325 }}`)
6426 rw.Write(response)
65 }
66
67 func TestGetTeamProfile(t *testing.T) {
68 http.HandleFunc("/team.profile.get", getTeamProfile)
69
70 once.Do(startServer)
71 APIURL = "http://" + serverAddr + "/"
72 api := New("testing-token")
73
74 teamProfile, err := api.GetTeamProfile()
75 if err != nil {
76 t.Errorf("Unexpected error: %s", err)
77 return
78 }
79
80 // t.Fatal refers to -> t.Errorf & return
81 if teamProfile.Fields[0].ID != "XXXD7KN555" {
82 t.Fatal(ErrIncorrectResponse)
83 }
84 if teamProfile.Fields[0].Label != "Skype" {
85 t.Fatal(ErrIncorrectResponse)
86 }
87
88 if teamProfile.Fields[1].ID != "XXXGGE5AAN7" {
89 t.Fatal(ErrIncorrectResponse)
90 }
91 if teamProfile.Fields[1].Label != "Title" {
92 t.Fatal(ErrIncorrectResponse)
93 }
94 if !teamProfile.Fields[1].Options["is_protected"] {
95 t.Fatal(ErrIncorrectResponse)
96 }
97
9827 }
9928
10029 func TestGetTeamInfo(t *testing.T) {
475475 return response.Err()
476476 }
477477
478 // SetUserCustomFields sets Custom Profile fields on the provided users account. Due to the non-repeating elements
479 // within the request, a map fields is required. The key in the map signifies the field that will be updated.
480 //
481 // See GetTeamProfile for information to retrieve possible fields for your account.
482 func (api *Client) SetUserCustomFields(userID string, customFields map[string]UserProfileCustomField) error {
483 return api.SetUserCustomFieldsContext(context.Background(), userID, customFields)
484 }
485
486 // SetUserCustomFieldsContext will set a users custom profile field with context.
487 //
488 // For more information see SetUserCustomFields
489 func (api *Client) SetUserCustomFieldsContext(ctx context.Context, userID string, customFields map[string]UserProfileCustomField) error {
490
491 // Convert data to data type with custom marshall / unmarshall
492 // For more information, see UserProfileCustomFields definition.
493 updateFields := UserProfileCustomFields{}
494 updateFields.SetMap(customFields)
495
496 // This anonymous struct is needed to set the fields level of the request data. The base struct for
497 // UserProfileCustomFields has an unexported variable named fields that does not contain a struct tag,
498 // which has resulted in this configuration.
499 profile, err := json.Marshal(&struct {
500 Fields UserProfileCustomFields `json:"fields"`
501 }{
502 Fields: updateFields,
503 })
504
505 if err != nil {
506 return err
507 }
508
509 values := url.Values{
510 "token": {api.token},
511 "user": {userID},
512 "profile": {string(profile)},
513 }
514
515 response := &userResponseFull{}
516 if err := postForm(ctx, api.httpclient, APIURL+"users.profile.set", values, response, api); err != nil {
517 return err
518 }
519
520 if !response.Ok {
521 return errors.New(response.Error)
522 }
523
524 return nil
525
526 }
527
528478 // SetUserCustomStatus will set a custom status and emoji for the currently
529479 // authenticated user. If statusEmoji is "" and statusText is not, the Slack API
530480 // will automatically set it to ":speech_balloon:". Otherwise, if both are ""