Skip to content

Commit

Permalink
Add support for granting/revoking karma to multiple users (#162)
Browse files Browse the repository at this point in the history
* Support granting multiple people karma in one msg

* Fix regex

* Add multi-user karma test
  • Loading branch information
saidmasoud authored Feb 22, 2021
1 parent a48cdc1 commit be671c3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 43 deletions.
94 changes: 52 additions & 42 deletions plugins/karma.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}
}
Expand Down
7 changes: 6 additions & 1 deletion plugins/karma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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", ""},
Expand Down

0 comments on commit be671c3

Please sign in to comment.