-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
mpim.go
71 lines (63 loc) · 1.77 KB
/
mpim.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
package structures
import (
"errors"
"fmt"
"strings"
"github.com/slack-go/slack"
)
const (
mpimNameSep = "--"
mpimPrefix = "mpdm-"
)
// FixMpIMmembers verifies Channel.Members to contain all channel members, if
// not, it attempts to populate it by parsing out the usernames from
// name_normalized, and populating Channel.Members with IDs of the users
// discovered. usernameIDs is a mapping of username -> user_id.
func FixMpIMmembers(ch *slack.Channel, users []slack.User) (*slack.Channel, error) {
if !isMpIM(ch) {
return nil, fmt.Errorf("not a MpIM channel: %s (IsMpIM=%v)", ch.ID, ch.IsMpIM)
}
if len(ch.Members) == mpimMemberCount(ch.NameNormalized) {
return ch, nil
}
members, err := parseMpIMmembers(ch.NameNormalized, usernameIDs(users))
if err != nil {
return nil, err
}
ch.Members = members
return ch, nil
}
func mpimMemberCount(nameNormalized string) int {
return strings.Count(nameNormalized, mpimNameSep) + 1
}
func isMpIM(ch *slack.Channel) bool {
return ch.IsMpIM && strings.HasPrefix(ch.NameNormalized, mpimPrefix)
}
func parseMpIMmembers(nn string, usernameIDs map[string]string) ([]string, error) {
if mpimMemberCount(nn) == 0 {
return nil, errors.New("no members in mpim")
}
if len(usernameIDs) == 0 {
return nil, errors.New("no user mapping")
}
nn = strings.TrimRight(strings.TrimLeft(nn, mpimPrefix), "-1")
names := strings.Split(nn, mpimNameSep)
var members = make([]string, len(names))
for i := range names {
var ok bool
members[i], ok = usernameIDs[names[i]]
if !ok {
// TODO: log?
continue
}
}
return members, nil
}
// UsernameIDs returns a mapping of user.name->user.id.
func usernameIDs(us []slack.User) map[string]string {
var ui = make(map[string]string)
for _, u := range us {
ui[u.Name] = u.ID
}
return ui
}