2019-08-26 19:00:31 +00:00
|
|
|
package kbchat
|
|
|
|
|
|
|
|
import (
|
2020-03-01 20:09:23 +00:00
|
|
|
"bytes"
|
2019-08-26 19:00:31 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-03-01 20:09:23 +00:00
|
|
|
"log"
|
2019-08-26 19:00:31 +00:00
|
|
|
"strings"
|
2020-02-08 15:50:16 +00:00
|
|
|
|
|
|
|
"github.com/keybase/go-keybase-chat-bot/kbchat/types/keybase1"
|
2019-08-26 19:00:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ListTeamMembers struct {
|
2020-02-08 15:50:16 +00:00
|
|
|
Result keybase1.TeamDetails `json:"result"`
|
|
|
|
Error Error `json:"error"`
|
2019-08-26 19:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ListMembersOutputMembersCategory struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
FullName string `json:"fullName"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ListUserMemberships struct {
|
2020-02-08 15:50:16 +00:00
|
|
|
Result keybase1.AnnotatedTeamList `json:"result"`
|
|
|
|
Error Error `json:"error"`
|
2019-08-26 19:00:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 15:50:16 +00:00
|
|
|
func (a *API) ListMembersOfTeam(teamName string) (res keybase1.TeamMembersDetails, err error) {
|
2020-05-23 22:06:21 +00:00
|
|
|
teamNameEscaped, err := json.Marshal(teamName)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
apiInput := fmt.Sprintf(`{"method": "list-team-memberships", "params": {"options": {"team": %s}}}`, teamNameEscaped)
|
2019-08-26 19:00:31 +00:00
|
|
|
cmd := a.runOpts.Command("team", "api")
|
|
|
|
cmd.Stdin = strings.NewReader(apiInput)
|
2020-03-01 20:09:23 +00:00
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
output, err := cmd.Output()
|
2019-08-26 19:00:31 +00:00
|
|
|
if err != nil {
|
2020-02-08 15:50:16 +00:00
|
|
|
return res, APIError{err}
|
2019-08-26 19:00:31 +00:00
|
|
|
}
|
2020-03-01 20:09:23 +00:00
|
|
|
if stderr.Len() != 0 {
|
|
|
|
log.Printf("ListMembersOfTeam error: %s", stderr.String())
|
|
|
|
}
|
2019-08-26 19:00:31 +00:00
|
|
|
|
|
|
|
members := ListTeamMembers{}
|
2020-03-01 20:09:23 +00:00
|
|
|
err = json.Unmarshal(output, &members)
|
2019-08-26 19:00:31 +00:00
|
|
|
if err != nil {
|
2020-02-08 15:50:16 +00:00
|
|
|
return res, UnmarshalError{err}
|
2019-08-26 19:00:31 +00:00
|
|
|
}
|
|
|
|
if members.Error.Message != "" {
|
2020-02-08 15:50:16 +00:00
|
|
|
return res, members.Error
|
2019-08-26 19:00:31 +00:00
|
|
|
}
|
|
|
|
return members.Result.Members, nil
|
|
|
|
}
|
|
|
|
|
2020-02-08 15:50:16 +00:00
|
|
|
func (a *API) ListUserMemberships(username string) ([]keybase1.AnnotatedMemberInfo, error) {
|
2020-05-23 22:06:21 +00:00
|
|
|
usernameEscaped, err := json.Marshal(username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
apiInput := fmt.Sprintf(`{"method": "list-user-memberships", "params": {"options": {"username": %s}}}`, usernameEscaped)
|
2019-08-26 19:00:31 +00:00
|
|
|
cmd := a.runOpts.Command("team", "api")
|
|
|
|
cmd.Stdin = strings.NewReader(apiInput)
|
2020-03-01 20:09:23 +00:00
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
output, err := cmd.Output()
|
2019-08-26 19:00:31 +00:00
|
|
|
if err != nil {
|
2020-02-08 15:50:16 +00:00
|
|
|
return nil, APIError{err}
|
2019-08-26 19:00:31 +00:00
|
|
|
}
|
2020-03-01 20:09:23 +00:00
|
|
|
if stderr.Len() != 0 {
|
|
|
|
log.Printf("ListUserMemberships error: %s", stderr.String())
|
|
|
|
}
|
2019-08-26 19:00:31 +00:00
|
|
|
|
|
|
|
members := ListUserMemberships{}
|
2020-03-01 20:09:23 +00:00
|
|
|
err = json.Unmarshal(output, &members)
|
2019-08-26 19:00:31 +00:00
|
|
|
if err != nil {
|
2020-02-08 15:50:16 +00:00
|
|
|
return nil, UnmarshalError{err}
|
2019-08-26 19:00:31 +00:00
|
|
|
}
|
|
|
|
if members.Error.Message != "" {
|
2020-02-08 15:50:16 +00:00
|
|
|
return nil, members.Error
|
2019-08-26 19:00:31 +00:00
|
|
|
}
|
|
|
|
return members.Result.Teams, nil
|
|
|
|
}
|