<commit_before>defmodule Slacktapped.Comments do
  @instance_name System.get_env("INSTANCE_NAME") ||
    Application.get_env(:slacktapped, :instance_name)
  @redis Application.get_env(:slacktapped, :redis)

  @doc """
  Processes a checkins comments.

  Loops through all comments and determines if they are eligible to be
  posted to Slack. If they are, they're processed and added to the
  checkins attachments.
  """
  def process_comments(checkin) do
    if is_map(checkin["comments"]) do
      get_in(checkin, ["comments", "items"])
        |> Enum.reduce([], fn(comment, acc) ->
            if is_eligible_comment(comment) do
              acc ++ [process_comment(comment, checkin)]
            else
              acc
            end
          end)
        |> Slacktapped.Utils.add_attachments(checkin)
    else
      {:ok, checkin}
    end
  end

  @doc """
  Reports the comment as posted to Redis, and returns a parsed comment as
  an attachment for Slack.
  """
  def process_comment(comment, checkin) do
    report_comment(comment)
    parse_comment(comment, checkin)
  end

  @doc """
  Determines if a comment is eligible to be posted to Slack.

  Comment is ineligible if we have already posted it to Slack.

  ## Examples

      iex> Slacktapped.Comments.is_eligible_comment(%{"comment_id" => 3344})
      true

      iex> Slacktapped.Comments.is_eligible_comment(%{"comment_id" => 3355})
      false

  """
  def is_eligible_comment(comment) do
    comment_id = comment["comment_id"]
    get_key = "GET #{@instance_name}:comment-#{comment_id}"

    @redis.command("#{get_key}") == {:ok, nil}
  end

  @doc ~S"""
  Parses a comment into an attachment for Slack.

  ## Example

      iex> Slacktapped.Comments.parse_comment(
      ...> %{
      ...>   "comment_id" => 3593,
      ...>   "comment" => "Great beer!",
      ...>   "user" => %{
      ...>     "first_name" => "George",
      ...>     "last_name" => "C.",
      ...>     "user_link" => "http://path/to/comment/user",
      ...>     "user_name" => "george"
      ...>   }
      ...> },
      ...> %{
      ...>   "attachments" => [],
      ...>   "user" => %{
      ...>     "user_name" => "nicksergeant",
      ...>     "user_avatar" => "http://path/to/user/avatar"
      ...>   },
      ...>   "beer" => %{
      ...>     "bid" => 123,
      ...>     "beer_name" => "IPA",
      ...>     "beer_slug" => "two-lake-ipa"
      ...>   },
      ...>   "checkin_id" => 567
      ...> })
      %{
        "author_icon" => "http://path/to/user/avatar",
        "author_link" => "https://untappd.com/user/nicksergeant",
        "author_name" => "nicksergeant",
        "color" => "#FFCF0B",
        "fallback" => "Checkin comment.",
        "mrkdwn_in" => ["text"],
        "text" => "<http://path/to/comment/user|George C.> commented on " <>
          "<https://untappd.com/user/nicksergeant/checkin/567|nicksergeant's " <>
          "checkin> and said:\n>Great beer!"
      }

  """
  def parse_comment(comment, checkin) do
    c = Slacktapped.Utils.checkin_parts(checkin)

    comment_text = comment["comment"]
    comment_user_link = comment["user"]["user_link"]
    comment_user_name = Slacktapped.Utils.parse_name(comment["user"])

    text = "<#{comment_user_link}|#{comment_user_name}> commented on " <>
      "<#{c.checkin_url}|#{c.user_name}'s checkin> and said:\n>#{comment_text}"

    %{
      "author_icon" => c.user_avatar,
      "author_link" => c.user_url,
      "author_name" => c.user_username,
      "color" => "#FFCF0B",
      "mrkdwn_in" => ["text"],
      "fallback" => "Checkin comment.",<commit_msg>Show beer name in comment.<commit_after>defmodule Slacktapped.Comments do
  @instance_name System.get_env("INSTANCE_NAME") ||
    Application.get_env(:slacktapped, :instance_name)
  @redis Application.get_env(:slacktapped, :redis)

  @doc """
  Processes a checkins comments.

  Loops through all comments and determines if they are eligible to be
  posted to Slack. If they are, they're processed and added to the
  checkins attachments.
  """
  def process_comments(checkin) do
    if is_map(checkin["comments"]) do
      get_in(checkin, ["comments", "items"])
        |> Enum.reduce([], fn(comment, acc) ->
            if is_eligible_comment(comment) do
              acc ++ [process_comment(comment, checkin)]
            else
              acc
            end
          end)
        |> Slacktapped.Utils.add_attachments(checkin)
    else
      {:ok, checkin}
    end
  end

  @doc """
  Reports the comment as posted to Redis, and returns a parsed comment as
  an attachment for Slack.
  """
  def process_comment(comment, checkin) do
    report_comment(comment)
    parse_comment(comment, checkin)
  end

  @doc """
  Determines if a comment is eligible to be posted to Slack.

  Comment is ineligible if we have already posted it to Slack.

  ## Examples

      iex> Slacktapped.Comments.is_eligible_comment(%{"comment_id" => 3344})
      true

      iex> Slacktapped.Comments.is_eligible_comment(%{"comment_id" => 3355})
      false

  """
  def is_eligible_comment(comment) do
    comment_id = comment["comment_id"]
    get_key = "GET #{@instance_name}:comment-#{comment_id}"

    @redis.command("#{get_key}") == {:ok, nil}
  end

  @doc ~S"""
  Parses a comment into an attachment for Slack.

  ## Example

      iex> Slacktapped.Comments.parse_comment(
      ...> %{
      ...>   "comment_id" => 3593,
      ...>   "comment" => "Great beer!",
      ...>   "user" => %{
      ...>     "first_name" => "George",
      ...>     "last_name" => "C.",
      ...>     "user_link" => "http://path/to/comment/user",
      ...>     "user_name" => "george"
      ...>   }
      ...> },
      ...> %{
      ...>   "attachments" => [],
      ...>   "user" => %{
      ...>     "user_name" => "nicksergeant",
      ...>     "user_avatar" => "http://path/to/user/avatar"
      ...>   },
      ...>   "beer" => %{
      ...>     "bid" => 123,
      ...>     "beer_name" => "IPA",
      ...>     "beer_slug" => "two-lake-ipa"
      ...>   },
      ...>   "checkin_id" => 567
      ...> })
      %{
        "author_icon" => "http://path/to/user/avatar",
        "author_link" => "https://untappd.com/user/nicksergeant",
        "author_name" => "nicksergeant",
        "color" => "#FFCF0B",
        "fallback" => "Checkin comment.",
        "mrkdwn_in" => ["text"],
        "text" => "<http://path/to/comment/user|George C.> commented on " <>
          "<https://untappd.com/user/nicksergeant/checkin/567|nicksergeant's " <>
          "checkin> of <https://untappd.com/b/two-lake-ipa/123|IPA> and " <>
          "said:\n>Great beer!"
      }

  """
  def parse_comment(comment, checkin) do
    c = Slacktapped.Utils.checkin_parts(checkin)

    comment_text = comment["comment"]
    comment_user_link = comment["user"]["user_link"]
    comment_user_name = Slacktapped.Utils.parse_name(comment["user"])

    text = "<#{comment_user_link}|#{comment_user_name}> commented on " <>
      "<#{c.checkin_url}|#{c.user_name}'s checkin> of " <>
      "#{c.beer} and said:\n>#{comment_text}"

    %{
      "author_icon" => c.user_avatar,
      "author_link" => c.user_url,
      "author_name" => c.user_username,
      "color" => "#FFCF0B",
      "mrkdwn_in" => ["text"],
      "fallback" => "Checkin comment.",
{'fraction_non_alphanumeric': 0.17339373970345964, 'fraction_numerical': 0.006727073036792971, 'mean_word_length': 2.368007397133611, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 144, 'https://': 5, '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': '1625775', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2491, 'n_words': 481}