Skip to content

Commit

Permalink
feat: add ability to prevent responding in threads
Browse files Browse the repository at this point in the history
fixes: #92
  • Loading branch information
wass3r committed Aug 15, 2019
1 parent 800d839 commit 8473a43
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ func handleChatServiceRule(outputMsgs chan<- models.Message, message models.Mess
bot.Log.Debugf("Rule '%s' has both 'args' and 'hear' set. To use 'args', use 'respond' instead of 'hear'", rule.Name)
}

if hit && message.ThreadTimestamp != "" && rule.IgnoreThreads {
bot.Log.Debug("Response suppressed due to 'ignore_thread' being set")
return true, true
}

// if it's a 'respond' rule, make sure the bot was mentioned
if hit && rule.Respond != "" && !message.BotMentioned && message.Type != models.MsgTypeDirect {
return match, stopSearch
Expand All @@ -96,6 +101,8 @@ func handleChatServiceRule(outputMsgs chan<- models.Message, message models.Mess
// Capture untouched user input

message.Vars["_raw_user_input"] = message.Input
message.Vars["_is_thread_message"] = strconv.FormatBool(message.ThreadTimestamp != "")

// Do additional checks on the rule before running
if !isValidHitChatRule(&message, rule, processedInput, bot) {
outputMsgs <- message
Expand Down
22 changes: 22 additions & 0 deletions core/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,18 @@ func Test_handleChatServiceRule(t *testing.T) {
HelpText: "foo <arg1> <arg2>",
}

ruleHearWithArgs := models.Rule{
Name: "Hear rule with Args set",
Hear: "/hi/",
Args: []string{"arg1", "arg2"},
}

ruleIgnoreThread := models.Rule{
Name: "Test rule that ignores thread",
Hear: "/thread/",
IgnoreThreads: true,
}

testBot := new(models.Bot)
testBot.Name = "Testbot"

Expand Down Expand Up @@ -708,6 +720,13 @@ func Test_handleChatServiceRule(t *testing.T) {
BotMentioned: true,
}

testMessageIgnoreThread := models.Message{
Input: "we have a thread",
Vars: map[string]string{},
Timestamp: "x",
ThreadTimestamp: "x",
}

tests := []struct {
name string
args args
Expand All @@ -716,12 +735,15 @@ func Test_handleChatServiceRule(t *testing.T) {
expectMsg string
}{
{"basic", args{}, false, false, ""},
{"respone + hear", args{rule: models.Rule{Respond: "hi", Hear: "/hi/"}, hit: false, bot: testBot, message: testMessage}, false, false, ""},
{"hear + rule args", args{rule: ruleHearWithArgs, hit: false, bot: testBot, message: testMessage}, false, false, ""},
{"respond rule - hit false", args{rule: rule, hit: false}, false, false, ""},
{"respond rule - hit true - valid", args{rule: rule, hit: true, bot: testBot, message: testMessage, processedInput: "arg1 arg2"}, true, true, "hmm, the 'format_output' field in your configuration is empty"},
{"respond rule - hit true - bot not mentioned", args{rule: rule, hit: true, bot: testBot, message: testMessageBotNotMentioned, processedInput: "arg1 arg2"}, false, false, ""},
{"respond rule - hit true - valid - not enough args", args{rule: rule, hit: true, bot: testBot, message: testMessageNotEnoughArgs, processedInput: "arg1"}, true, true, "You might be missing an argument or two. This is what I'm looking for\n```foo <arg1> <arg2>```"},
{"respond rule - hit true - valid optional arg", args{rule: ruleOpt, hit: true, bot: testBot, message: testMessageOptionalArgs, processedInput: "arg1"}, true, true, ""},
{"respond rule - hit true - invalid", args{rule: rule, hit: true, bot: testBot, message: testMessage}, true, true, "You might be missing an argument or two. This is what I'm looking for\n```foo <arg1> <arg2>```"},
{"hear rule - ignore thread", args{rule: ruleIgnoreThread, hit: true, bot: testBot, message: testMessageIgnoreThread}, true, true, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions models/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Rule struct {
IgnoreUsers []string `mapstructure:"ignore_users" binding:"omitempty"`
IgnoreUserGroups []string `mapstructure:"ignore_usergroups" binding:"omitempty"`
StartMessageThread bool `mapstructure:"start_message_thread" binding:"omitempty"`
IgnoreThreads bool `mapstructure:"ignore_threads" binding:"omitempty"`
FormatOutput string `mapstructure:"format_output"`
HelpText string `mapstructure:"help_text"`
IncludeInHelp bool `mapstructure:"include_in_help" binding:"required"`
Expand Down

0 comments on commit 8473a43

Please sign in to comment.