diff --git a/plugins/karma.go b/plugins/karma.go index 1336991..8ac28c5 100644 --- a/plugins/karma.go +++ b/plugins/karma.go @@ -26,7 +26,7 @@ const ( defaultItemCount = 5 ) -var karmaRegex = regexp.MustCompile("(?:\\A|\\W)(?:(<(@[\\w']+)>\\s?))(\\+{2,6}|\\-{2,6}).*") +var karmaRegex = regexp.MustCompile("(?:(<(@[\\w']+)>\\s?))(\\+{2,6}|\\-{2,6})") // Ranker represents attributes and behavior to process a ranking list type ranker struct { @@ -159,58 +159,68 @@ func matchKarmaReset(m *slackscot.IncomingMessage) bool { // recordKarma records a karma increase or decrease and answers with a message including // the recorded word with its associated karma value func (k *Karma) recordKarma(message *slackscot.IncomingMessage) *slackscot.Answer { - match := karmaRegex.FindAllStringSubmatch(message.Text, -1)[0] + matches := karmaRegex.FindAllStringSubmatch(message.Text, -1) + answerText := "" + + for idx, match := range matches { + + // only add newlines if more than one match + if idx > 0 { + answerText += "\n" + } + + thing := match[2] + // Prevent a user from attributing karma to self + if strings.TrimPrefix(thing, "@") == message.User { + return &slackscot.Answer{Text: "*Attributing yourself karma is frown upon* :face_with_raised_eyebrow:", Options: []slackscot.AnswerOption{slackscot.AnswerEphemeral(message.User)}} + } - thing := match[2] - // Prevent a user from attributing karma to self - if strings.TrimPrefix(thing, "@") == message.User { - return &slackscot.Answer{Text: "*Attributing yourself karma is frown upon* :face_with_raised_eyebrow:", Options: []slackscot.AnswerOption{slackscot.AnswerEphemeral(message.User)}} - } + rawValue, err := k.karmaStorer.GetSiloString(message.Channel, thing) + if err != nil { + rawValue = "0" + } + karma, err := strconv.Atoi(rawValue) + if err != nil { + k.Logger.Printf("[%s] Error parsing current karma value [%s], something's wrong and resetting to 0: %v", KarmaPluginName, rawValue, err) + karma = 0 + } - rawValue, err := k.karmaStorer.GetSiloString(message.Channel, thing) - if err != nil { - rawValue = "0" - } - karma, err := strconv.Atoi(rawValue) - if err != nil { - k.Logger.Printf("[%s] Error parsing current karma value [%s], something's wrong and resetting to 0: %v", KarmaPluginName, rawValue, err) - karma = 0 - } + log.Printf("thing is [%s]\n", thing) + renderedThing := k.renderThing(thing) - log.Printf("thing is [%s]\n", thing) - answerText := "" - renderedThing := k.renderThing(thing) + instruction := match[3] + if strings.HasPrefix(instruction, "+") { + incrementSymbols := strings.TrimPrefix(instruction, "+") + increment := len(incrementSymbols) + karma = karma + increment - instruction := match[3] - if strings.HasPrefix(instruction, "+") { - incrementSymbols := strings.TrimPrefix(instruction, "+") - increment := len(incrementSymbols) - karma = karma + increment + if increment == 1 { + answerText += fmt.Sprintf("`%s` just gained karma (`%s`: %d)", renderedThing, renderedThing, karma) + } else { + answerText += fmt.Sprintf("`%s` just gained %d karma points (`%s`: %d)", renderedThing, increment, renderedThing, karma) + } - if increment == 1 { - answerText = fmt.Sprintf("`%s` just gained karma (`%s`: %d)", renderedThing, renderedThing, karma) } else { - answerText = fmt.Sprintf("`%s` just gained %d karma points (`%s`: %d)", renderedThing, increment, renderedThing, karma) - } + decrementSymbols := strings.TrimPrefix(instruction, "-") + decrement := len(decrementSymbols) + karma = karma - decrement - } else { - decrementSymbols := strings.TrimPrefix(instruction, "-") - decrement := len(decrementSymbols) - karma = karma - decrement + if decrement == 1 { + answerText += fmt.Sprintf("`%s` just lost karma (`%s`: %d)", renderedThing, renderedThing, karma) + } else { + answerText += fmt.Sprintf("`%s` just lost %d karma points (`%s`: %d)", renderedThing, decrement, renderedThing, karma) + } + } - if decrement == 1 { - answerText = fmt.Sprintf("`%s` just lost karma (`%s`: %d)", renderedThing, renderedThing, karma) - } else { - answerText = fmt.Sprintf("`%s` just lost %d karma points (`%s`: %d)", renderedThing, decrement, renderedThing, karma) + // Store new value + err = k.karmaStorer.PutSiloString(message.Channel, thing, strconv.Itoa(karma)) + if err != nil { + k.Logger.Printf("[%s] Error persisting karma: %v", KarmaPluginName, err) + return nil } } - // Store new value - err = k.karmaStorer.PutSiloString(message.Channel, thing, strconv.Itoa(karma)) - if err != nil { - k.Logger.Printf("[%s] Error persisting karma: %v", KarmaPluginName, err) - return nil - } + return &slackscot.Answer{Text: answerText} } diff --git a/plugins/karma_test.go b/plugins/karma_test.go index e714f29..b32dc86 100644 --- a/plugins/karma_test.go +++ b/plugins/karma_test.go @@ -21,7 +21,11 @@ type userInfoFinder struct { } func (u userInfoFinder) GetUserInfo(userID string) (user *slack.User, err error) { - return &slack.User{ID: userID, RealName: "Bernard Tremblay"}, nil + users := make(map[string]string) + users["U21355"] = "Bernard Tremblay" + users["U21356"] = "Great Scott" + + return &slack.User{ID: userID, RealName: users[userID]}, nil } func TestKarmaMatchesAndAnswers(t *testing.T) { @@ -43,6 +47,7 @@ func TestKarmaMatchesAndAnswers(t *testing.T) { {"<@U21355>+++++", "Cgeneral", "`Bernard Tremblay` just gained 4 karma points (`Bernard Tremblay`: 16)"}, {"<@U21355>++++++", "Cgeneral", "`Bernard Tremblay` just gained 5 karma points (`Bernard Tremblay`: 21)"}, {"<@U21355>+++++++", "Cgeneral", "`Bernard Tremblay` just gained 5 karma points (`Bernard Tremblay`: 26)"}, + {"<@U21355>++ <@U21356>++", "Cgeneral", "`Bernard Tremblay` just gained karma (`Bernard Tremblay`: 27)\n`Great Scott` just gained karma (`Great Scott`: 1)"}, {"+----------+", "Cgeneral", ""}, {"---", "Cgeneral", ""}, {"+++", "Cgeneral", ""},