Skip to content

Commit

Permalink
Move karma parsing out to allow recursion
Browse files Browse the repository at this point in the history
In order to parse karma for > 1 user at a time, we're going to need to
be able to call a function doing the same thing over with a subset of
the message.

re: jetpackworkflow#10
  • Loading branch information
diachini committed Feb 10, 2019
1 parent 3aa124c commit 25f4a9d
Showing 1 changed file with 47 additions and 19 deletions.
66 changes: 47 additions & 19 deletions src/mega-plusplus.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,33 @@ clark = require('clark')
querystring = require('querystring')
ScoreKeeper = require('./scorekeeper')

module.exports = (robot) ->
scoreKeeper = new ScoreKeeper(robot)
class ParseKarma
scoreKeyword = process.env.HUBOT_PLUSPLUS_KEYWORD or 'score'
reasonsKeyword = process.env.HUBOT_PLUSPLUS_REASONS or 'raisins'
maxPoints = process.env.HUBOT_PLUSPLUS_MAX_POINTS or 5
reasonConjunctions = process.env.HUBOT_PLUSPLUS_CONJUNCTIONS or 'for|because|cause|cuz|as'

# sweet regex bro
robot.hear ///
# the thing being upvoted, which is any number of words.
# Internal Spaces are allowed only when quoted.
# Internal +. - allowed
((?:[\w@.\-:\u3040-\u30FF\uFF01-\uFF60\u4E00-\u9FA0]+(?<![+-]))|(?:['"][^'"]*['"]))
# allow for spaces after the thing being upvoted (@user ++)
\s*
# the increment/decrement operator ++ or --
(\+{2,}|-{2,})
# optional reason for the plusplus
(?:\s+(?:#{reasonConjunctions})\s+(.+))?
///i, (msg) ->
doKarma: (messageObj, inputMessage, reasonConjunctions, robot) ->
scoreKeeper = new ScoreKeeper(robot)

msg = inputMessage.match(///
# the thing being upvoted, which is any number of words.
# Internal Spaces are allowed only when quoted.
# Internal +. - allowed
((?:[\w@.\-:\u3040-\u30FF\uFF01-\uFF60\u4E00-\u9FA0]+(?<![+-]))|(?:['"][^'"]*['"]))
# allow for spaces after the thing being upvoted (@user ++)
\s*
# the increment/decrement operator ++ or --
(\+{2,}|-{2,})
# optional reason for the plusplus
(?:\s+(?:#{reasonConjunctions})\s+(.+))?
///i)

# let's get our local vars in place
[dummy, name, operator, reason] = msg.match
from = msg.message.user.name.toLowerCase()
room = msg.message.room
[dummy, name, operator, reason] = msg

from = messageObj.message.user.name.toLowerCase()
room = messageObj.message.room

# do some sanitizing
reason = reason?.trim().toLowerCase()
Expand Down Expand Up @@ -101,7 +104,7 @@ module.exports = (robot) ->
"#{name} has #{score} points"


msg.send message
messageObj.send message

robot.emit "plus-one", {
name: name
Expand All @@ -111,6 +114,31 @@ module.exports = (robot) ->
from: from
}

module.exports = (robot) ->
scoreKeeper = new ScoreKeeper(robot)
scoreKeyword = process.env.HUBOT_PLUSPLUS_KEYWORD or 'score'
reasonsKeyword = process.env.HUBOT_PLUSPLUS_REASONS or 'raisins'
maxPoints = process.env.HUBOT_PLUSPLUS_MAX_POINTS or 5
reasonConjunctions = process.env.HUBOT_PLUSPLUS_CONJUNCTIONS or 'for|because|cause|cuz|as'

parseKarma = new ParseKarma(robot)

# sweet regex bro
robot.hear ///
# the thing being upvoted, which is any number of words.
# Internal Spaces are allowed only when quoted.
# Internal +. - allowed
((?:[\w@.\-:\u3040-\u30FF\uFF01-\uFF60\u4E00-\u9FA0]+(?<![+-]))|(?:['"][^'"]*['"]))
# allow for spaces after the thing being upvoted (@user ++)
\s*
# the increment/decrement operator ++ or --
(\+{2,}|-{2,})
# optional reason for the plusplus
(?:\s+(?:#{reasonConjunctions})\s+(.+))?
///i, (msg) ->

parseKarma.doKarma(msg, msg.message, reasonConjunctions, robot)

robot.respond ///
(?:erase\s+)
# thing to be erased
Expand Down

0 comments on commit 25f4a9d

Please sign in to comment.