<commit_before># Description
#   A hubot script like /star me except for slack reactions.
#
# Configuration:
#   SLACK_ACCESS_TOKEN
#
# Commands:
#   hubot react me -1year    - Show a random reaction from a year ago today
#   hubot react me @somebody - Show a message from someone with reactions
#   hubot react me :star:    - Show a message with :star: reactions
#
# Author:
#   <NAME> <<EMAIL>>

require 'datejs'

module.exports = (robot) ->
  robot.respond /(star|zap|reaction|react)( me)?(.*)/i, (msg) ->

    num = 1
    args = msg.match[3].split(/\s+/)
    terms = []

    for arg in args

      if arg.match(/^(today|yesterday|week|month|year)/)
        on_date = arg

      else if arg.match(/^(\-|\d)/)
        try
          if (parts = arg.split('+-')) and parts.length > 1
            date = Date.parse parts[0]
            dateRange = parts[1]

            m = dateRange.match(/^(\d+)(.+)$/)
            n = m[1]

            if m[2].match(/^mo/)
              dateRange = (n*4) + 'w'
            else if m[2].match(/^y/)
              dateRange = (n*52) + 'w'
            else if m[2].match(/^[smhdw]/)
              dateRange = (n) + m[2][0]
            else
              throw new Error 'invalid +- argument'
          else
            date = Date.parse(arg)
        catch e
          msg.send "Unknown date: #{arg}"
          return

      else if arg.match(/^x\d+$/)
        num = parseInt(arg.slice(1))

      else if arg.match(/^@/)
        user = arg.slice(1)

      else if arg.match(/^\:/)
        reaction = arg

      else
        terms.push(arg)

    q = ""
    if reaction
      q += "has:#{reaction}"
    else if msg.match[1] == "star"
      q += "has::star:"
    else if msg.match[1] == "zap"
      q += "has::zap:"
    else
      q += "has:reaction"

    q += " from:#{user}" if user
    q += " on:#{on_date}" if on_date
    q += " during:#{date.toString('yyyy-MM-dd')}" if date
    q += " #{terms.join(" ")}" if terms.length > 0

    msg
      .http("https://slack.com/api/search.all")
      .query
        query: q,
        pretty: 1,
        token: process.env.SLACK_ACCESS_TOKEN
      .get() (err, resp, body) ->
        if err?
          robot.emit 'error', err, msg
          return

        if resp.statusCode == 200
          result = JSON.parse(body)
          matches = result.messages.matches
          hits = []

          if result.messages.total == 0
            msg.send "No reactions found =("
            return

          for i in [1..num]
            hit = matches.splice(Math.floor(Math.random() * matches.length), 1)[0]
            hits.push(hit) if hit

          for star in hits<commit_msg>Only use messages in public groups<commit_after># Description
#   A hubot script like /star me except for slack reactions.
#
# Configuration:
#   SLACK_ACCESS_TOKEN
#
# Commands:
#   hubot react me -1year    - Show a random reaction from a year ago today
#   hubot react me @somebody - Show a message from someone with reactions
#   hubot react me :star:    - Show a message with :star: reactions
#
# Author:
#   <NAME> <<EMAIL>>

require 'datejs'

module.exports = (robot) ->
  robot.respond /(star|zap|reaction|react)( me)?(.*)/i, (msg) ->

    num = 1
    args = msg.match[3].split(/\s+/)
    terms = []

    for arg in args

      if arg.match(/^(today|yesterday|week|month|year)/)
        on_date = arg

      else if arg.match(/^(\-|\d)/)
        try
          if (parts = arg.split('+-')) and parts.length > 1
            date = Date.parse parts[0]
            dateRange = parts[1]

            m = dateRange.match(/^(\d+)(.+)$/)
            n = m[1]

            if m[2].match(/^mo/)
              dateRange = (n*4) + 'w'
            else if m[2].match(/^y/)
              dateRange = (n*52) + 'w'
            else if m[2].match(/^[smhdw]/)
              dateRange = (n) + m[2][0]
            else
              throw new Error 'invalid +- argument'
          else
            date = Date.parse(arg)
        catch e
          msg.send "Unknown date: #{arg}"
          return

      else if arg.match(/^x\d+$/)
        num = parseInt(arg.slice(1))

      else if arg.match(/^@/)
        user = arg.slice(1)

      else if arg.match(/^\:/)
        reaction = arg

      else
        terms.push(arg)

    q = ""
    if reaction
      q += "has:#{reaction}"
    else if msg.match[1] == "star"
      q += "has::star:"
    else if msg.match[1] == "zap"
      q += "has::zap:"
    else
      q += "has:reaction"

    q += " from:#{user}" if user
    q += " on:#{on_date}" if on_date
    q += " during:#{date.toString('yyyy-MM-dd')}" if date
    q += " #{terms.join(" ")}" if terms.length > 0

    msg
      .http("https://slack.com/api/search.messages")
      .query
        query: q,
        token: process.env.SLACK_ACCESS_TOKEN
      .get() (err, resp, body) ->
        if err?
          robot.emit 'error', err, msg
          return

        if resp.statusCode == 200
          result = JSON.parse(body)
          matches = result.messages.matches
          hits = []

          if result.messages.total == 0
            msg.send "No reactions found =("
            return

          matches = matches.filter (m) -> m.type != "group"

          for i in [1..num]
            hit = matches.splice(Math.floor(Math.random() * matches.length), 1)[0]
            hits.push(hit) if hit

          for star in hits
{'fraction_non_alphanumeric': 0.15515309932785662, 'fraction_numerical': 0.010268857356235997, 'mean_word_length': 1.6704885343968097, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1331040', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1737, 'n_words': 467}