Skip to content

Commit 34ab4e6

Browse files
authored
display available bot commands when listing bot member settings; get public bot commands (keybase#20001)
1 parent b189d77 commit 34ab4e6

11 files changed

Lines changed: 114 additions & 6 deletions

File tree

‎go/chat/bots/commands.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ func (b *CachingBotCommandManager) createConv(ctx context.Context, param chat1.A
142142
}
143143
}
144144

145+
func (b *CachingBotCommandManager) PublicCommandsConv(ctx context.Context, username string) (chat1.ConversationID, error) {
146+
convs, err := b.G().ChatHelper.FindConversations(ctx, username, &commandsPublicTopicName,
147+
chat1.TopicType_DEV, chat1.ConversationMembersType_IMPTEAMNATIVE, keybase1.TLFVisibility_PUBLIC)
148+
if err != nil {
149+
return nil, err
150+
}
151+
if len(convs) != 1 {
152+
return nil, fmt.Errorf("unable to find conversation for %v, found %d convs instead of 1", username, len(convs))
153+
}
154+
return convs[0].GetConvID(), nil
155+
}
156+
145157
func (b *CachingBotCommandManager) Advertise(ctx context.Context, alias *string,
146158
ads []chat1.AdvertiseCommandsParam) (err error) {
147159
defer b.Trace(ctx, func() error { return err }, "Advertise")()
@@ -218,6 +230,7 @@ func (b *CachingBotCommandManager) ListCommands(ctx context.Context, convID chat
218230
}
219231
cmdDedup := make(map[string]bool)
220232
for _, ad := range s.Advertisements {
233+
ad.Username = libkb.NewNormalizedUsername(ad.Username).String()
221234
for _, cmd := range ad.Advertisement.Commands {
222235
key := cmd.Name + ad.Username
223236
if !cmdDedup[key] {

‎go/chat/bots/utils.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ func ApplyTeamBotSettings(ctx context.Context, g *globals.Context, botUID gregor
3434
botSettings keybase1.TeamBotSettings,
3535
msg chat1.MessagePlaintext, convID *chat1.ConversationID,
3636
mentionMap map[string]struct{}, debug utils.DebugLabeler) (bool, error) {
37+
3738
// First make sure bot can receive on the given conversation. This ID may
3839
// be null if we are creating the conversation.
39-
4040
if convID != nil {
4141
convAllowed := len(botSettings.Convs) == 0
4242
for _, convIDStr := range botSettings.Convs {
@@ -103,6 +103,13 @@ func ApplyTeamBotSettings(ctx context.Context, g *globals.Context, botUID gregor
103103
return false, err
104104
}
105105
if convID != nil {
106+
completeCh, err := g.BotCommandManager.UpdateCommands(ctx, *convID, nil)
107+
if err != nil {
108+
return false, err
109+
}
110+
if err := <-completeCh; err != nil {
111+
return false, err
112+
}
106113
cmds, err := g.BotCommandManager.ListCommands(ctx, *convID)
107114
if err != nil {
108115
return false, nil

‎go/chat/server.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,23 @@ func (h *Server) ClearBotCommandsLocal(ctx context.Context) (res chat1.ClearBotC
25792579
return res, nil
25802580
}
25812581

2582+
func (h *Server) ListPublicBotCommandsLocal(ctx context.Context, username string) (res chat1.ListBotCommandsLocalRes, err error) {
2583+
var identBreaks []keybase1.TLFIdentifyFailure
2584+
ctx = globals.ChatCtx(ctx, h.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, &identBreaks, h.identNotifier)
2585+
defer h.Trace(ctx, func() error { return err }, "ListPublicBotCommandsLocal")()
2586+
defer func() { h.setResultRateLimit(ctx, &res) }()
2587+
_, err = utils.AssertLoggedInUID(ctx, h.G())
2588+
if err != nil {
2589+
return res, err
2590+
}
2591+
2592+
convID, err := h.G().BotCommandManager.PublicCommandsConv(ctx, username)
2593+
if err != nil {
2594+
return res, err
2595+
}
2596+
return h.ListBotCommandsLocal(ctx, convID)
2597+
}
2598+
25822599
func (h *Server) ListBotCommandsLocal(ctx context.Context, convID chat1.ConversationID) (res chat1.ListBotCommandsLocalRes, err error) {
25832600
var identBreaks []keybase1.TLFIdentifyFailure
25842601
ctx = globals.ChatCtx(ctx, h.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, &identBreaks, h.identNotifier)

‎go/chat/types/interfaces.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ type BotCommandManager interface {
535535
Resumable
536536
Advertise(ctx context.Context, alias *string, ads []chat1.AdvertiseCommandsParam) error
537537
Clear(ctx context.Context) error
538+
PublicCommandsConv(ctx context.Context, username string) (chat1.ConversationID, error)
538539
ListCommands(ctx context.Context, convID chat1.ConversationID) ([]chat1.UserBotCommandOutput, error)
539540
UpdateCommands(ctx context.Context, convID chat1.ConversationID, info *chat1.BotInfo) (chan error, error)
540541
}

‎go/chat/types/types.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,10 @@ func (d DummyBotCommandManager) Advertise(ctx context.Context, alias *string,
586586

587587
func (d DummyBotCommandManager) Clear(context.Context) error { return nil }
588588

589+
func (d DummyBotCommandManager) PublicCommandsConv(ctx context.Context, username string) (chat1.ConversationID, error) {
590+
return nil, nil
591+
}
592+
589593
func (d DummyBotCommandManager) ListCommands(ctx context.Context, convID chat1.ConversationID) ([]chat1.UserBotCommandOutput, error) {
590594
return nil, nil
591595
}

‎go/client/cmd_chat_bot_member_settings.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (c *CmdChatBotMemberSettings) Run() (err error) {
7979
return err
8080
}
8181
}
82-
if err := renderBotSettings(c.G(), c.username, botSettings); err != nil {
82+
if err := renderBotSettings(c.G(), c.username, &conversationInfo.Id, botSettings); err != nil {
8383
return err
8484
}
8585

‎go/client/cmd_team_bot_settings.go‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,43 @@ func (c *CmdTeamBotSettings) Run() error {
8989
return err
9090
}
9191
}
92-
if err := renderBotSettings(c.G(), c.Username, botSettings); err != nil {
92+
if err := renderBotSettings(c.G(), c.Username, nil, botSettings); err != nil {
9393
return err
9494
}
9595
return nil
9696
}
9797

98-
func renderBotSettings(g *libkb.GlobalContext, username string, botSettings keybase1.TeamBotSettings) error {
98+
func renderBotSettings(g *libkb.GlobalContext, username string, convID *chat1.ConversationID, botSettings keybase1.TeamBotSettings) error {
9999
var output string
100100
if botSettings.Cmds {
101-
// TODO HOTPOT-661 call bot advertise list for public commands, build output
102-
output += "\t- command messages\n"
101+
chatClient, err := GetChatLocalClient(g)
102+
if err != nil {
103+
return fmt.Errorf("Getting chat service client error: %s", err)
104+
}
105+
var cmds chat1.ListBotCommandsLocalRes
106+
if convID == nil {
107+
cmds, err = chatClient.ListPublicBotCommandsLocal(context.TODO(), username)
108+
if err != nil {
109+
return err
110+
}
111+
} else {
112+
cmds, err = chatClient.ListBotCommandsLocal(context.TODO(), *convID)
113+
if err != nil {
114+
return err
115+
}
116+
}
117+
118+
if len(cmds.Commands) > 0 {
119+
output += "\t- command messages for the following commands: \n"
120+
} else {
121+
output += "\t- command messages\n"
122+
}
123+
username = libkb.NewNormalizedUsername(username).String()
124+
for _, cmd := range cmds.Commands {
125+
if cmd.Username == username {
126+
output += fmt.Sprintf("\t\t- !%s\n", cmd.Name)
127+
}
128+
}
103129
}
104130

105131
if botSettings.Mentions {

‎go/protocol/chat1/local.go‎

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎protocol/avdl/chat1/local.avdl‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,13 +1171,17 @@ protocol local {
11711171
record AdvertiseBotCommandsLocalRes {
11721172
array<RateLimit> rateLimits;
11731173
}
1174+
11741175
AdvertiseBotCommandsLocalRes advertiseBotCommandsLocal(union { null, string } alias,
11751176
array<AdvertiseCommandsParam> advertisements);
11761177
record ListBotCommandsLocalRes {
11771178
array<UserBotCommandOutput> commands;
11781179
array<RateLimit> rateLimits;
11791180
}
11801181
ListBotCommandsLocalRes listBotCommandsLocal(ConversationID convID);
1182+
1183+
ListBotCommandsLocalRes listPublicBotCommandsLocal(string username);
1184+
11811185
record ClearBotCommandsLocalRes {
11821186
array<RateLimit> rateLimits;
11831187
}

‎protocol/json/chat1/local.json‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)