-
Notifications
You must be signed in to change notification settings - Fork 0
/
memberCollector.go
89 lines (79 loc) · 2.17 KB
/
memberCollector.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
package main
import (
"log"
"github.com/slack-go/slack"
)
// MemberCollector decide member randomly and make slackAttachment.
type MemberCollector struct {
client *slack.Client
}
// Collect channnel members using slack api.
func (c *MemberCollector) Collect(channelID string) ([]Member, error) {
members, err := c.fetchConversationMembers(channelID)
if err != nil {
log.Println(err)
return nil, err
}
return c.filterActiveMember(members)
}
// CollectByUserGroup collect usergroup members using slack api.
func (c *MemberCollector) CollectByUserGroup(userGroupID string, channelID string) ([]Member, error) {
ugMembers, err := c.client.GetUserGroupMembers(userGroupID)
if err != nil {
log.Println(err)
return nil, err
}
chMembers, err := c.fetchConversationMembers(channelID)
chMemberMap := make(map[string]string, len(chMembers))
for _, chMember := range chMembers {
chMemberMap[chMember] = chMember
}
if err != nil {
log.Println(err)
return nil, err
}
var members []string
for _, ugMember := range ugMembers {
if m, ok := chMemberMap[ugMember]; ok {
members = append(members, m)
}
}
return c.filterActiveMember(members)
}
func (c *MemberCollector) fetchConversationMembers(channelID string) ([]string, error) {
nextCursor := ""
params := slack.GetUsersInConversationParameters{
ChannelID: channelID,
Cursor: nextCursor,
}
var members []string
fetchedMembers, nextCursor, err := c.client.GetUsersInConversation(¶ms)
if err != nil {
log.Println(err)
return nil, err
}
members = append(members, fetchedMembers...)
for len(nextCursor) > 0 {
fetchedMembers, nextCursor, err = c.client.GetUsersInConversation(¶ms)
if err != nil {
log.Println(err)
return nil, err
}
members = append(members, fetchedMembers...)
}
return members, err
}
func (c *MemberCollector) filterActiveMember(members []string) ([]Member, error) {
var activeMembers []Member
for _, mem := range members {
user, err := c.client.GetUserInfo(mem)
if err != nil {
log.Println(err)
return nil, err
}
if !(user.IsBot || user.Deleted) {
activeMembers = append(activeMembers, Member{ID: user.ID, Name: user.Name})
}
}
return activeMembers, nil
}