Skip to content

Commit

Permalink
handle bots as senders
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblum committed Jul 31, 2019
1 parent 9e4fc4a commit 32c5b5a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 19 deletions.
25 changes: 10 additions & 15 deletions go/chat/sender.go
Expand Up @@ -816,18 +816,6 @@ func (s *BlockingSender) applyTeamBotSettings(ctx context.Context, uid gregor1.U
return nil, nil
}

// Bots never get these
switch msg.ClientHeader.MessageType {
case chat1.MessageType_NONE,
chat1.MessageType_METADATA,
chat1.MessageType_TLFNAME,
chat1.MessageType_HEADLINE,
chat1.MessageType_JOIN,
chat1.MessageType_LEAVE,
chat1.MessageType_SYSTEM:
return nil, nil
}

// Skip checks if botUID already set
if msg.ClientHeader.BotUID != nil {
return nil, nil
Expand Down Expand Up @@ -874,14 +862,21 @@ func (s *BlockingSender) applyTeamBotSettings(ctx context.Context, uid gregor1.U

var botUIDs []gregor1.UID
for uv, botSettings := range teamBotSettings {
guid := gregor1.UID(uv.Uid.ToBytes())
isMatch, err := utils.ApplyTeamBotSettings(ctx, s.G(), guid, botSettings, msg,
botUID := gregor1.UID(uv.Uid.ToBytes())
isMatch, err := utils.ApplyTeamBotSettings(ctx, s.G(), botUID, botSettings, msg,
conv, mentionMap, s.DebugLabeler)
if err != nil {
return nil, err
}
// If the bot is the sender encrypt only for them.
if msg.ClientHeader.Sender.Eq(botUID) {
if !isMatch {
return nil, fmt.Errorf("Unable to send, bot restricted from this channel")
}
return []gregor1.UID{botUID}, nil
}
if isMatch {
botUIDs = append(botUIDs, guid)
botUIDs = append(botUIDs, botUID)
}
}
return botUIDs, nil
Expand Down
36 changes: 36 additions & 0 deletions go/chat/server_test.go
Expand Up @@ -7133,6 +7133,42 @@ func TestTeamBotSettings(t *testing.T) {
require.Fail(t, "no event received")
}
consumeNewMsgLocal(t, listener, chat1.MessageType_TEXT)

// send as a bot

// TODO HOTPOT-117 restrict messages RESTRICTEDBOT members receive.
// this part of the test fails since we cannot send a as
// RESTRICTEDBOT, because we try to unbox messages we don't have
// the keys for.
//larg := chat1.PostLocalArg{
// ConversationID: created.Id,
// Msg: chat1.MessagePlaintext{
// ClientHeader: chat1.MessageClientHeader{
// Conv: created.Triple,
// TlfName: created.TlfName,
// MessageType: chat1.MessageType_TEXT,
// },
// MessageBody: chat1.NewMessageBodyWithText(chat1.MessageText{Body: "blah"}),
// },
//}
//_, err = ctc.as(t, botua).chatLocalHandler().PostLocal(ctc.as(t, botua).startCtx, larg)
//require.Error(t, err)

//_, err = ctc.as(t, botua2).chatLocalHandler().PostLocal(ctc.as(t, botua2).startCtx, larg)
//require.NoError(t, err)
//consumeNewPendingMsg(t, listener)
//select {
//case info := <-listener.newMessageRemote:
// unboxed = info.Message
// require.True(t, unboxed.IsValid(), "invalid message")
// require.NotNil(t, unboxed.Valid().BotUID)
// require.EqualValues(t, botuaUID2, *unboxed.Valid().BotUID)
// require.EqualValues(t, botuaUID2, unboxed.Valid().SenderUID)
// require.Equal(t, chat1.MessageType_TEXT, unboxed.GetMessageType(), "invalid type")
//case <-time.After(20 * time.Second):
// require.Fail(t, "no event received")
//}
//consumeNewMsgLocal(t, listener, chat1.MessageType_TEXT)
})
})
}
17 changes: 16 additions & 1 deletion go/chat/utils/utils.go
Expand Up @@ -2449,10 +2449,25 @@ func ApplyTeamBotSettings(ctx context.Context, g *globals.Context, botUID gregor
return false, nil
}

// If the sender is the bot, always match
if msg.ClientHeader.Sender.Eq(botUID) {
return true, nil
}

switch msg.ClientHeader.MessageType {
// DELETEHISTORY messages are always keyed for bots in case they need to
// clear messages
if msg.ClientHeader.MessageType == chat1.MessageType_DELETEHISTORY {
case chat1.MessageType_DELETEHISTORY:
return true, nil
// Bots never get these
case chat1.MessageType_NONE,
chat1.MessageType_METADATA,
chat1.MessageType_TLFNAME,
chat1.MessageType_HEADLINE,
chat1.MessageType_JOIN,
chat1.MessageType_LEAVE,
chat1.MessageType_SYSTEM:
return false, nil
}

// check mentions
Expand Down
23 changes: 20 additions & 3 deletions go/chat/utils/utils_test.go
Expand Up @@ -564,12 +564,29 @@ func TestApplyTeamBotSettings(t *testing.T) {
}
assertMatch(true)

// restrict the bot to certain convs
botSettings.Convs = []string{"conv"}
assertMatch(false)
bannedTypes := []chat1.MessageType{
chat1.MessageType_NONE,
chat1.MessageType_METADATA,
chat1.MessageType_TLFNAME,
chat1.MessageType_HEADLINE,
chat1.MessageType_JOIN,
chat1.MessageType_LEAVE,
chat1.MessageType_SYSTEM,
}
for _, typ := range bannedTypes {
msg.ClientHeader.MessageType = typ
assertMatch(false)
}

// if the sender is botUID, always match
msg.ClientHeader.MessageType = chat1.MessageType_TEXT
msg.ClientHeader.Sender = botUID
assertMatch(true)

// restrict the bot to certain convs
botSettings.Convs = []string{"conv"}
assertMatch(false)
msg.ClientHeader.Sender = gregor1.UID("hi")
botSettings.Convs = nil

// mentions
Expand Down

0 comments on commit 32c5b5a

Please sign in to comment.