<commit_before>calcMD5 = require('./md5')

avatarResponder = (message) ->
  author = message.authorElement()
  body = getMessageWrapperElement(message)

  if author.visible()
    showAuthor(message, body)

  author.remove()

getMessageWrapperElement = (message) ->
  body = message.bodyCell

  if Campfire.USER_ACTIONS.include(message.kind)
    body.select('div:first')[0]
  else body

showAuthor = (message, body) ->
  if message.actsLikeTextMessage()
    image = getImage(message)
    message.authorElement().insert({after: image})

  name = getAuthorName(message)
  body.insert({top: name})

getAuthorName = (message) ->
  author = message.authorElement()
  separator = if Campfire.USER_ACTIONS.include(message.kind) then '&nbsp' else '<br>'

  return '' if message.kind == 'timestamp'

  "<strong class=\"authorName\">#{author.textContent}</strong>#{separator}"

getImage = (message) ->
  avatar = getAvatar(message.authorElement())

  return unless avatar

  image = document.createElement('img')
  image.alt    = message.author()
  image.title  = message.author()
  image.width  = 32
  image.height = 32
  image.align  = 'absmiddle'
  image.src    = avatar

  image.outerHTML

getAvatar = (author) ->
  email = author.getAttribute('data-email')

  return '' unless email

  hash = calcMD5(email.trim().toLowerCase())
  "https://secure.gravatar.com/avatar/#{hash}"

module.exports = avatarResponder<commit_msg>Improve naming and make some methods simpler<commit_after>calcMD5 = require('./md5')

avatarResponder = (message) ->
  author = message.authorElement()
  showAuthor(message) unless previousMessageBy(author)
  removeCampfireDefaultAuthorElement(author)

previousMessageBy = (author) ->
  !author.visible()

showAuthor = (message) ->
  showAuthorAvatar(message) if message.actsLikeTextMessage()
  showAuthorName(message)

showAuthorAvatar = (message) ->
  image = getImage(message)
  message.authorElement().insert({after: image})

getImage = (message) ->
  avatar = getAvatar(message.authorElement())

  return unless avatar

  image = document.createElement('img')
  image.alt    = message.author()
  image.title  = message.author()
  image.width  = 32
  image.height = 32
  image.align  = 'absmiddle'
  image.src    = avatar

  image.outerHTML

getAvatar = (author) ->
  email = author.getAttribute('data-email')

  return '' unless email

  hash = calcMD5(email.trim().toLowerCase())
  "https://secure.gravatar.com/avatar/#{hash}"

showAuthorName = (message) ->
  messageElement(message).insert({top: getAuthorName(message)})

getAuthorName = (message) ->
  author = message.authorElement()
  separator = if Campfire.USER_ACTIONS.include(message.kind) then '&nbsp' else '<br>'

  return '' if message.kind == 'timestamp'

  "<strong class=\"authorName\">#{author.textContent}</strong>#{separator}"

messageElement = (message) ->
  body = message.bodyCell
  if Campfire.USER_ACTIONS.include(message.kind)
    body.select('div:first')[0]
  else
    body

removeCampfireDefaultAuthorElement = (author) ->
  author.remove()

module.exports = avatarResponder
{'fraction_non_alphanumeric': 0.14104234527687295, 'fraction_numerical': 0.005211726384364821, 'mean_word_length': 4.493738819320215, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, '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': '6978541', 'n_tokens_mistral': 979, 'n_tokens_neox': 950, 'n_words': 183}