<commit_before>class LikeView extends KDView

  constructor:(options={}, data)->

    options.tagName            or= 'span'
    options.cssClass           or= 'like-view'
    options.tooltipPosition    or= 'se'
    options.checkIfLikedBefore  ?= yes

    super options, data

    @_lastUpdatedCount = -1
    @_currentState = no

    @likeCount    = new ActivityLikeCount
      tooltip     :
        gravity   : options.tooltipPosition
        title     : ""
      bind        : "mouseenter"
      mouseenter  : => @fetchLikeInfo()
      attributes  :
        href      : "#"
      click       : (event)=>
        # event.preventDefault()
        if data.meta.likes > 0
          data.fetchLikedByes {},
            sort : timestamp : -1
          , (err, likes) =>
            new FollowedModalView {title:"Members who liked <cite>#{data.body}</cite>"}, likes
      , data

    @likeLink = new ActivityActionLink

    @setTemplate @pistachio()

    # We need to getridoff this asap FIXME ~HK
    if options.checkIfLikedBefore
      data.checkIfLikedBefore (err, likedBefore)=>
        @likeLink.updatePartial if likedBefore then "Unlike" else "Like"
        @_currentState = likedBefore

  fetchLikeInfo:->

    data = @getData()

    return if @_lastUpdatedCount is data.meta.likes
    @likeCount.getTooltip().update title: "Loading..."

    if data.meta.likes is 0
      @likeLink.updatePartial "Like"
      return

    data.fetchLikedByes {},
      limit : 3
      sort  : timestamp : -1
    , (err, likes) =>

      peopleWhoLiked   = []

      if likes

        likes.forEach (item)=>
          if peopleWhoLiked.length < 3
            {firstName, lastName} = item.profile
            peopleWhoLiked.push "<strong>" + firstName + " " + lastName + "</strong>"
          else return

        sep = ', '
        tooltip =
          switch data.meta.likes
            when 0 then ""
            when 1 then "#{peopleWhoLiked[0]}"
            when 2 then "#{peopleWhoLiked[0]} and #{peopleWhoLiked[1]}"
            when 3 then "#{peopleWhoLiked[0]}#{sep}#{peopleWhoLiked[1]} and #{peopleWhoLiked[2]}"
            else "#{peopleWhoLiked[0]}#{sep}#{peopleWhoLiked[1]}#{sep}#{peopleWhoLiked[2]} and <strong>#{data.meta.likes - 3} more.</stron>"

        @likeCount.getTooltip().update { title: tooltip }
        @_lastUpdatedCount = likes.length

  click:(event)->
    event.preventDefault()

    if $(event.target).is("a.action-link")
      @getData().like (err)=>

        KD.showError err,
          AccessDenied : 'Permission denied to like activities'
          KodingError  : 'Something went wrong while like'

        unless err
          @_currentState = not @_currentState
          @likeLink.updatePartial if @_currentState is yes then "Unlike" else "Like"

  pistachio:->
    """{{> @likeLink}}{{> @likeCount}}"""
<commit_msg>Show guests correctly in LikeView tooltips<commit_after>class LikeView extends KDView

  constructor:(options={}, data)->

    options.tagName            or= 'span'
    options.cssClass           or= 'like-view'
    options.tooltipPosition    or= 'se'
    options.checkIfLikedBefore  ?= yes

    super options, data

    @_lastUpdatedCount = -1
    @_currentState = no

    @likeCount    = new ActivityLikeCount
      tooltip     :
        gravity   : options.tooltipPosition
        title     : ""
      bind        : "mouseenter"
      mouseenter  : => @fetchLikeInfo()
      attributes  :
        href      : "#"
      click       : (event)=>
        # event.preventDefault()
        if data.meta.likes > 0
          data.fetchLikedByes {},
            sort : timestamp : -1
          , (err, likes) =>
            new FollowedModalView {title:"Members who liked <cite>#{data.body}</cite>"}, likes
      , data

    @likeLink = new ActivityActionLink

    @setTemplate @pistachio()

    # We need to getridoff this asap FIXME ~HK
    if options.checkIfLikedBefore
      data.checkIfLikedBefore (err, likedBefore)=>
        @likeLink.updatePartial if likedBefore then "Unlike" else "Like"
        @_currentState = likedBefore

  fetchLikeInfo:->

    data = @getData()

    return if @_lastUpdatedCount is data.meta.likes
    @likeCount.getTooltip().update title: "Loading..."

    if data.meta.likes is 0
      @likeLink.updatePartial "Like"
      return

    data.fetchLikedByes {},
      limit : 3
      sort  : timestamp : -1
    , (err, likes) =>

      peopleWhoLiked = []
      guestsWhoLiked = 0

      if likes

        for item in likes
          if item.type is 'unregistered'
            guestsWhoLiked++
          else
            {firstName, lastName} = item.profile
            peopleWhoLiked.push "<strong>" + firstName + " " + lastName + "</strong>"

        sep = ', '

        guestLikes =
          switch guestsWhoLiked
            when 0 then ""
            when 1 then "and <strong>a guest</strong>"
            when 2 then "and <strong>2 guests</strong>"
            when 3 then "<strong>3 guests</strong>"

        tooltip =
          switch peopleWhoLiked.length
            when 0 then "#{guestLikes}"
            when 1 then "#{peopleWhoLiked[0]} #{guestLikes}"
            when 2 then "#{peopleWhoLiked[0]} and #{peopleWhoLiked[1]} #{guestLikes}"
            else "#{peopleWhoLiked[0]}#{sep}#{peopleWhoLiked[1]}#{sep}#{peopleWhoLiked[2]} and <strong>#{data.meta.likes - 3} more.</strong>"

        @likeCount.getTooltip().update { title: tooltip }
        @_lastUpdatedCount = likes.length

  click:(event)->
    event.preventDefault()

    if $(event.target).is("a.action-link")
      @getData().like (err)=>

        KD.showError err,
          AccessDenied : 'Permission denied to like activities'
          KodingError  : 'Something went wrong while like'

        unless err
          @_currentState = not @_currentState
          @likeLink.updatePartial if @_currentState is yes then "Unlike" else "Like"

  pistachio:->
    """{{> @likeLink}}{{> @likeCount}}"""

{'fraction_non_alphanumeric': 0.12049207954162454, 'fraction_numerical': 0.00741489720256151, 'mean_word_length': 2.175494917067951, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 48, 'https://': 0, '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': '2971126', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1748, 'n_words': 428}