-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
87 lines (71 loc) · 1.84 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
package utils
import (
"github.com/slack-go/slack"
)
// EmailsToSlackIDs takes in an array of email addresses and finds the IDs of
// any workplace members with those emails
func EmailsToSlackIDs(client *slack.Client, emails []string) ([]string, error) {
users, err := getAll(client)
if err != nil {
return nil, err
}
if len(users) == 0 {
return nil, ErrNoUsersInWorkplace
}
return toSlackIDs(users, emails), nil
}
// EmailToSlackIDsInclusive takes in an array of email addresses, finds the IDs
// of any workplace members with those emails, and returns both values
func EmailsToSlackIDsInclusive(client *slack.Client, emails []string) ([][]string, error) {
users, err := getAll(client)
if err != nil {
return nil, err
}
if len(users) == 0 {
return nil, ErrNoUsersInWorkplace
}
return toSlackIDsInclusive(users, emails), nil
}
func toSlackIDs(users []slack.User, emails []string) []string {
var ids []string
for _, email := range emails {
for _, user := range users {
if user.Profile.Email == email {
ids = append(ids, user.ID)
}
}
}
return ids
}
func toSlackIDsInclusive(users []slack.User, emails []string) [][]string {
var emailIDPairs [][]string
for _, email := range emails {
for _, user := range users {
if user.Profile.Email == email {
emailIDPairs = append(emailIDPairs, []string{email, user.ID})
}
}
}
return emailIDPairs
}
func toEmails(users []slack.User, userIDs []string) []string {
var emails []string
for _, id := range userIDs {
for _, user := range users {
if user.ID == id && user.Profile.Email != "" {
emails = append(emails, user.Profile.Email)
}
}
}
return emails
}
func getAll(client *slack.Client) ([]slack.User, error) {
users, err := client.GetUsers()
if err != nil {
return nil, err
}
if len(users) == 0 {
return nil, ErrNoUsersInWorkplace
}
return users, nil
}