<commit_before>Marbles.Views.UnreadCount = class UnreadCountView extends Marbles.View
  @view_name: 'unread_count'

  initialize: ->
    # find or fetch existing cursor post
    TentStatus.Models.CursorPost.find(
      {
        type: @constructor.cursor_post_type
        entity: TentStatus.config.current_user.entity
      },

      success: @fetchSuccess
      failure: @fetchFailure
      complete: =>
        @trigger('fetch:complete')
    )

  hide: =>
    Marbles.DOM.hide(@el, visibility: true)

  show: =>
    Marbles.DOM.removeClass(@el, 'hidden')
    Marbles.DOM.show(@el, visibility: true)

  clearCount: (ref) =>
    @count = 0
    @render()
    @frozen = true

    unless @post_cid
      return @once 'fetch:complete', => @clearCount(ref)

    post = @getPost()
    post.ref_post = ref
    post.set('refs', [{
      entity: ref.get('entity')
      type: ref.get('type')
      post: ref.get('id')
    }])
    post.set('permissions', { public: false })
    post.saveVersion(
      complete: =>
        if @fetch_count_pending
          @once 'fetch-count:complete', => @frozen = false
        else
          @frozen = false
    )

  getPost: =>
    TentStatus.Models.CursorPost.find(cid: @post_cid)

  fetchSuccess: (post) =>
    @post_cid = post.cid

    @fetchCount()

  fetchFailure: =>
    post = new TentStatus.Models.CursorPost(
      type: @constructor.cursor_post_type
      entity: TentStatus.config.current_user.entity
    )
    @post_cid = post.cid

    @fetchCount()

  fetchParams: =>
    params = {
      types: @constructor.post_types
    }

    post = @getPost()
    if _ref = post.get('ref_post')
      params.since = "#{_ref.received_at || _ref.published_at} #{_ref.version.id || ''}"

    params

  fetchCount: =>
    return if @frozen
    @fetch_count_pending = true

    callbackFn = (res, xhr) =>
      if xhr.status == 200
        @fetchCountSuccess(res, xhr)
      else
        @fetchCountFailure(res, xhr)
      @fetch_count_pending = false
      @trigger('fetch-count:complete')

    params = @fetchParams()
    TentStatus.tent_client.post.list(
      method: 'HEAD'
      params: params
      callback: callbackFn
    )

  fetchCountSuccess: (res, xhr) =>
    return if @frozen

    count = parseInt(xhr.getResponseHeader('Count'))
    return @fetchCountFailure(res, xhr) if _.isNaN(count)
    return if count == @count

    @count = count
    @render()

  fetchCountFailure: (res, xhr) =>
    console.log('fetchCountFailure', res, xhr)

  render: =>
    if @count > 0
      if @count.toString().length > 2
        Marbles.DOM.setInnerText(@el, "99+")
      else
        Marbles.DOM.setInnerText(@el, @count)
      @show()
    else<commit_msg>Add interval for updating replies and reposts badges<commit_after>Marbles.Views.UnreadCount = class UnreadCountView extends Marbles.View
  @view_name: 'unread_count'

  initialize: ->
    @interval = new TentStatus.FetchInterval fetch_callback: @fetchCount

    # find or fetch existing cursor post
    TentStatus.Models.CursorPost.find(
      {
        type: @constructor.cursor_post_type
        entity: TentStatus.config.current_user.entity
      },

      success: @fetchSuccess
      failure: @fetchFailure
      complete: =>
        @trigger('fetch:complete')
    )

  hide: =>
    Marbles.DOM.hide(@el, visibility: true)

  show: =>
    Marbles.DOM.removeClass(@el, 'hidden')
    Marbles.DOM.show(@el, visibility: true)

  clearCount: (ref) =>
    @count = 0
    @render()
    @frozen = true

    unless @post_cid
      return @once 'fetch:complete', => @clearCount(ref)

    post = @getPost()
    post.ref_post = ref
    post.set('refs', [{
      entity: ref.get('entity')
      type: ref.get('type')
      post: ref.get('id')
    }])
    post.set('permissions', { public: false })
    post.saveVersion(
      complete: =>
        if @fetch_count_pending
          @once 'fetch-count:complete', => @frozen = false
        else
          @frozen = false
    )

  getPost: =>
    TentStatus.Models.CursorPost.find(cid: @post_cid)

  fetchSuccess: (post) =>
    @post_cid = post.cid

    @reset()

  fetchFailure: =>
    post = new TentStatus.Models.CursorPost(
      type: @constructor.cursor_post_type
      entity: TentStatus.config.current_user.entity
    )
    @post_cid = post.cid

    @reset()

  reset: =>
    @interval.reset()

  fetchParams: =>
    params = {
      types: @constructor.post_types
    }

    post = @getPost()
    if _ref = post.get('ref_post')
      params.since = "#{_ref.received_at || _ref.published_at} #{_ref.version.id || ''}"

    params

  fetchCount: =>
    return if @frozen
    @fetch_count_pending = true

    callbackFn = (res, xhr) =>
      if xhr.status == 200
        @fetchCountSuccess(res, xhr)
      else
        @fetchCountFailure(res, xhr)
      @fetch_count_pending = false
      @trigger('fetch-count:complete')

    params = @fetchParams()
    TentStatus.tent_client.post.list(
      method: 'HEAD'
      params: params
      callback: callbackFn
    )

  fetchCountSuccess: (res, xhr) =>
    return if @frozen

    count = parseInt(xhr.getResponseHeader('Count'))
    return @fetchCountFailure(res, xhr) if _.isNaN(count)
    return @interval.increaseDelay() if count == @count

    @count = count
    @render()

    @interval.reset()

  fetchCountFailure: (res, xhr) =>
    @interval.increaseDelay()
    console.log('fetchCountFailure', res, xhr)

  render: =>
    if @count > 0
      if @count.toString().length > 2
        Marbles.DOM.setInnerText(@el, "99+")
      else
        Marbles.DOM.setInnerText(@el, @count)
      @show()
    else
{'fraction_non_alphanumeric': 0.13187600788389178, 'fraction_numerical': 0.002866869736606343, 'mean_word_length': 2.615284974093264, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 42, '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': '5101960', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1808, 'n_words': 366}