<commit_before>class MessagePane extends KDTabPaneView

  MOST_LIKED   = 0
  MOST_RECENT  = 1

  ITEMS_COUNT  = 25

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

    options.type    or= ''
    options.cssClass  = "message-pane #{options.type}"
    options.wrapper     ?= yes
    options.lastToFirst ?= no

    super options, data

    @lastScrollTops =
      window        : 0
      parent        : 0
      self          : 0
      body          : 0

    {itemClass, type, lastToFirst, wrapper, channelId} = @getOptions()
    {typeConstant} = @getData()
    viewOptions = {itemOptions: {channelId}}

    @listController = new ActivityListController { wrapper, itemClass, type: typeConstant, viewOptions, lastToFirst}

    @createChannelTitle()
    @createInputWidget()
    @createFilterLinks()
    @bindInputEvents()

    @fakeMessageMap = {}
    @mostLikedPostsCount = 0

    @changeFilter MOST_LIKED
    @realFilter = MOST_LIKED

    {socialapi} = KD.singletons
    @once 'ChannelReady', @bound 'bindChannelEvents'
    socialapi.onChannelReady data, @lazyBound 'emit', 'ChannelReady'

    if typeConstant in ['group', 'topic']
      @on 'LazyLoadThresholdReached', @bound 'lazyLoad'

    @on "ItemsArrived", (items)->
      return  if @currentFilter isnt MOST_LIKED
      @mostLikedPostsCount += items.length

      return  if items.length isnt 0

      # when we run out of posts in most liked list, we switch
      # to getting most recent posts in DESC order
      @changeFilter MOST_RECENT
      if @mostLikedPostsCount is 0 then @populate()

    KD.singletons.windowController.addFocusListener @bound 'handleFocus'

    switch typeConstant
      when 'post'
        @listController.getListView().once 'ItemWasAdded', (item) =>
          listView = @listController.getListItems().first.commentBox.controller.getListView()
          listView.on 'ItemWasAdded', @bound 'scrollDown'
      when 'privatemessage'
        @listController.getListView().on 'ItemWasAdded', @bound 'scrollDown'
      when 'group'
      else
        @listController.getListView().on 'ItemWasAdded', @bound 'scrollUp'

  changeFilter: (filter)->
    @currentFilter       = filter
    @skipItemCount       = 0
    @mostLikedPostsCount = 0

  bindInputEvents: ->

    return  unless @input

    @input
      .on 'SubmitStarted',   @bound 'handleEnter'
      .on 'SubmitSucceeded', @bound 'replaceFakeItemView'
      .on 'SubmitFailed',    @bound 'messageSubmitFailed'


  replaceFakeItemView: (message) ->

    @putMessage message, @removeFakeMessage message.clientRequestId


  removeFakeMessage: (identifier) ->

    return  unless item = @fakeMessageMap[identifier]

    index = @listController.getListView().getItemIndex item

    @listController.removeItem item

    return index


  handleEnter: (value, clientRequestId) ->

    return  unless value

    @input.reset yes
    @createFakeItemView value, clientRequestId


  putMessage: (message, index = 0) -> @appendMessage message, index


  createFakeItemView: (value, clientRequestId) ->

    fakeData = KD.utils.generateDummyMessage value

    item = @putMessage fakeData

    # save it to a map so that we have a reference
    # to it to be deleted.
    @fakeMessageMap[clientRequestId] = item


  messageSubmitFailed: (err, clientRequestId) ->
    view = @fakeMessageMap[clientRequestId]
    view.showResend()
    view.on 'SubmitSucceeded', (message) =>
      message.clientRequestId = clientRequestId
      @replaceFakeItemView message


  handleFocus: (focused) -> @glance()  if focused and @active


  scrollDown: (item) ->

    return  unless @active

    {typeConstant} = @getData()

    if item.getDelegate().addSubView
      listView = item.getDelegate()
    else
      listView = item.getDelegate().getListView()

    unless @separator
      @separator = new KDView cssClass : 'new-messages'
      listView.addSubView @separator

    return  unless item is listView.items.last

    KD.utils.defer -> window.scrollTo 0, document.body.scrollHeight


  scrollUp: ->

    return  unless @active

    window.scrollTo 0, 0


  setScrollTops: ->

    super

    @lastScrollTops.window = window.scrollTop or 0
    @lastScrollTops.body   = document.body.scrollTop


  applyScrollTops: ->

    super

    KD.utils.defer =>
      window.scrollTo 0, @lastScrollTops.window
      document.body.scrollTop = @lastScrollTops.body


  createChannelTitle: ->

    type = @getOption 'type'

    if type is 'privatemessage' or type is 'post' then return

    {name, isParticipant} = @getData()

    @channelTitleView = new KDCustomHTMLView
      partial   : "##{name}"
      cssClass  : "channel-title #{if isParticipant then 'participant' else ''}"

    unless name is 'public'
      @channelTitleView.addSubView new TopicFollowButton null, @getData()


  createInputWidget: ->

    return  if @getOption("type") is 'post'

    channel = @getData()

    @input = new ActivityInputWidget {channel}


  createFilterLinks: ->

    type = @getOption 'type'

    if type is 'privatemessage' or type is 'post' then return

    @filterLinks = new FilterLinksView {},
      'Most Recent'  :
        active      : yes

    @filterLinks.on "filterLinkClicked", (data)=>
      @listController.removeAllItems()
      @listController.showLazyLoader()

      filter = if data.active then MOST_LIKED else MOST_RECENT
      @changeFilter filter
      @realFilter = filter

      @skipItemCount = 0

      @populate()


  bindChannelEvents: (channel) ->

    return  unless channel

    channel
      .on 'MessageAdded',   @bound 'addMessage'
      .on 'MessageRemoved', @bound 'removeMessage'


  addMessage: (message) ->

    return  if message.account._id is KD.whoami()._id

    {lastToFirst} = @getOptions()
    index = if lastToFirst then @listController.getItemCount() else 0
    @prependMessage message, index


  loadMessage: (message) ->

    {lastToFirst} = @getOptions()
    index = if lastToFirst then 0 else @listController.getItemCount()
    @appendMessage message, index


  appendMessage: (message, index) -> @listController.addItem message, index


  prependMessage: (message, index) ->
    KD.getMessageOwner message, (err, owner) =>
      return error err  if err
      return if KD.filterTrollActivity owner
      @listController.addItem message, index

  removeMessage: (message) -> @listController.removeItem null, message


  viewAppended: ->

    @addSubView @channelTitleView  if @channelTitleView
    @addSubView @input             if @input
    @addSubView @filterLinks       if @filterLinks
    @addSubView @listController.getView()
    @populate()


  show: ->

    super

    KD.utils.wait 1000, @bound 'glance'
    KD.utils.defer @bound 'focus'


  glance: ->

    {socialapi, appManager}  = KD.singletons
    {id, typeConstant, name} = @getData()

    app  = appManager.get 'Activity'
    item = app.getView().sidebar.selectedItem

    return  unless item?.count
    # no need to send updatelastSeenTime or glance when checking publicfeeds
    return  if name is 'public'

    if typeConstant is 'post'
    then socialapi.channel.glancePinnedPost   messageId : id, @bound 'glanced'
    else socialapi.channel.updateLastSeenTime channelId : id, @bound 'glanced'


  glanced: ->

    @separator?.destroy()
    @separator = null


  focus: ->

    if @input
      @input.focus()
    else
      @listController.getListItems().first?.commentBox.input.focus()


  populate: ->

    @fetch null, (err, items = []) =>

      return KD.showError err  if err

      @listController.hideLazyLoader()
      items.forEach (item) => KD.utils.defer => @appendMessage item

      KD.utils.defer @bound 'focus'


  fetch: (options = {}, callback)->

    {
      name
      type
      channelId
    }            = @getOptions()
    data         = @getData()
    {appManager} = KD.singletons

    options.name      = name
    options.type      = type
    options.channelId = channelId
    options.mostLiked = yes  if @currentFilter is MOST_LIKED

    # if it is a post it means we already have the data
    if type is 'post'
    then KD.utils.defer -> callback null, [data]
    else appManager.tell 'Activity', 'fetch', options, (err, items)=>
      return callback err, items  if err

      @emit "ItemsArrived", items

      callback err, items

  lazyLoad: ->

    @listController.showLazyLoader()

    if @realFilter is MOST_LIKED and @mostLikedPostsCount > 0
      return @listController.hideLazyLoader()
<commit_msg>popularpost: format fixes<commit_after>class MessagePane extends KDTabPaneView

  MOST_LIKED   = 0
  MOST_RECENT  = 1

  ITEMS_COUNT  = 25

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

    options.type    or= ''
    options.cssClass  = "message-pane #{options.type}"
    options.wrapper     ?= yes
    options.lastToFirst ?= no

    super options, data

    @lastScrollTops =
      window        : 0
      parent        : 0
      self          : 0
      body          : 0

    {itemClass, type, lastToFirst, wrapper, channelId} = @getOptions()
    {typeConstant} = @getData()
    viewOptions = {itemOptions: {channelId}}

    @listController = new ActivityListController { wrapper, itemClass, type: typeConstant, viewOptions, lastToFirst}

    @createChannelTitle()
    @createInputWidget()
    @createFilterLinks()
    @bindInputEvents()

    @fakeMessageMap = {}
    @mostLikedPostsCount = 0

    @changeFilter MOST_LIKED
    @realFilter = MOST_LIKED

    {socialapi} = KD.singletons
    @once 'ChannelReady', @bound 'bindChannelEvents'
    socialapi.onChannelReady data, @lazyBound 'emit', 'ChannelReady'

    if typeConstant in ['group', 'topic']
      @on 'LazyLoadThresholdReached', @bound 'lazyLoad'

    @on "ItemsArrived", (items)->
      return  if @currentFilter isnt MOST_LIKED
      @mostLikedPostsCount += items.length

      return  if items.length isnt 0
      @changeFilter MOST_RECENT

      # when we run out of posts in most liked list, we switch
      # to getting most recent posts in DESC order
      if @mostLikedPostsCount is 0 then @populate()

    KD.singletons.windowController.addFocusListener @bound 'handleFocus'

    switch typeConstant
      when 'post'
        @listController.getListView().once 'ItemWasAdded', (item) =>
          listView = @listController.getListItems().first.commentBox.controller.getListView()
          listView.on 'ItemWasAdded', @bound 'scrollDown'
      when 'privatemessage'
        @listController.getListView().on 'ItemWasAdded', @bound 'scrollDown'
      when 'group'
      else
        @listController.getListView().on 'ItemWasAdded', @bound 'scrollUp'

  changeFilter: (filter)->
    @currentFilter       = filter
    @skipItemCount       = 0
    @mostLikedPostsCount = 0

  bindInputEvents: ->

    return  unless @input

    @input
      .on 'SubmitStarted',   @bound 'handleEnter'
      .on 'SubmitSucceeded', @bound 'replaceFakeItemView'
      .on 'SubmitFailed',    @bound 'messageSubmitFailed'


  replaceFakeItemView: (message) ->

    @putMessage message, @removeFakeMessage message.clientRequestId


  removeFakeMessage: (identifier) ->

    return  unless item = @fakeMessageMap[identifier]

    index = @listController.getListView().getItemIndex item

    @listController.removeItem item

    return index


  handleEnter: (value, clientRequestId) ->

    return  unless value

    @input.reset yes
    @createFakeItemView value, clientRequestId


  putMessage: (message, index = 0) -> @appendMessage message, index


  createFakeItemView: (value, clientRequestId) ->

    fakeData = KD.utils.generateDummyMessage value

    item = @putMessage fakeData

    # save it to a map so that we have a reference
    # to it to be deleted.
    @fakeMessageMap[clientRequestId] = item


  messageSubmitFailed: (err, clientRequestId) ->
    view = @fakeMessageMap[clientRequestId]
    view.showResend()
    view.on 'SubmitSucceeded', (message) =>
      message.clientRequestId = clientRequestId
      @replaceFakeItemView message


  handleFocus: (focused) -> @glance()  if focused and @active


  scrollDown: (item) ->

    return  unless @active

    {typeConstant} = @getData()

    if item.getDelegate().addSubView
      listView = item.getDelegate()
    else
      listView = item.getDelegate().getListView()

    unless @separator
      @separator = new KDView cssClass : 'new-messages'
      listView.addSubView @separator

    return  unless item is listView.items.last

    KD.utils.defer -> window.scrollTo 0, document.body.scrollHeight


  scrollUp: ->

    return  unless @active

    window.scrollTo 0, 0


  setScrollTops: ->

    super

    @lastScrollTops.window = window.scrollTop or 0
    @lastScrollTops.body   = document.body.scrollTop


  applyScrollTops: ->

    super

    KD.utils.defer =>
      window.scrollTo 0, @lastScrollTops.window
      document.body.scrollTop = @lastScrollTops.body


  createChannelTitle: ->

    type = @getOption 'type'

    if type is 'privatemessage' or type is 'post' then return

    {name, isParticipant} = @getData()

    @channelTitleView = new KDCustomHTMLView
      partial   : "##{name}"
      cssClass  : "channel-title #{if isParticipant then 'participant' else ''}"

    unless name is 'public'
      @channelTitleView.addSubView new TopicFollowButton null, @getData()


  createInputWidget: ->

    return  if @getOption("type") is 'post'

    channel = @getData()

    @input = new ActivityInputWidget {channel}


  createFilterLinks: ->

    type = @getOption 'type'

    if type is 'privatemessage' or type is 'post' then return

    @filterLinks = new FilterLinksView {},
      'Most Recent'  :
        active      : yes

    @filterLinks.on "filterLinkClicked", (data)=>
      @listController.removeAllItems()
      @listController.showLazyLoader()

      filter = if data.active then MOST_LIKED else MOST_RECENT
      @changeFilter filter
      @realFilter = filter

      @skipItemCount = 0

      @populate()


  bindChannelEvents: (channel) ->

    return  unless channel

    channel
      .on 'MessageAdded',   @bound 'addMessage'
      .on 'MessageRemoved', @bound 'removeMessage'


  addMessage: (message) ->

    return  if message.account._id is KD.whoami()._id

    {lastToFirst} = @getOptions()
    index = if lastToFirst then @listController.getItemCount() else 0
    @prependMessage message, index


  loadMessage: (message) ->

    {lastToFirst} = @getOptions()
    index = if lastToFirst then 0 else @listController.getItemCount()
    @appendMessage message, index


  appendMessage: (message, index) -> @listController.addItem message, index


  prependMessage: (message, index) ->
    KD.getMessageOwner message, (err, owner) =>
      return error err  if err
      return if KD.filterTrollActivity owner
      @listController.addItem message, index

  removeMessage: (message) -> @listController.removeItem null, message


  viewAppended: ->

    @addSubView @channelTitleView  if @channelTitleView
    @addSubView @input             if @input
    @addSubView @filterLinks       if @filterLinks
    @addSubView @listController.getView()
    @populate()


  show: ->

    super

    KD.utils.wait 1000, @bound 'glance'
    KD.utils.defer @bound 'focus'


  glance: ->

    {socialapi, appManager}  = KD.singletons
    {id, typeConstant, name} = @getData()

    app  = appManager.get 'Activity'
    item = app.getView().sidebar.selectedItem

    return  unless item?.count
    # no need to send updatelastSeenTime or glance when checking publicfeeds
    return  if name is 'public'

    if typeConstant is 'post'
    then socialapi.channel.glancePinnedPost   messageId : id, @bound 'glanced'
    else socialapi.channel.updateLastSeenTime channelId : id, @bound 'glanced'


  glanced: ->

    @separator?.destroy()
    @separator = null


  focus: ->

    if @input
      @input.focus()
    else
      @listController.getListItems().first?.commentBox.input.focus()


  populate: ->

    @fetch null, (err, items = []) =>

      return KD.showError err  if err

      @listController.hideLazyLoader()
      items.forEach (item) => KD.utils.defer => @appendMessage item

      KD.utils.defer @bound 'focus'


  fetch: (options = {}, callback)->

    {
      name
      type
      channelId
    }            = @getOptions()
    data         = @getData()
    {appManager} = KD.singletons

    options.name      = name
    options.type      = type
    options.channelId = channelId
    options.mostLiked = yes  if @currentFilter is MOST_LIKED

    # if it is a post it means we already have the data
    if type is 'post'
    then KD.utils.defer -> callback null, [data]
    else appManager.tell 'Activity', 'fetch', options, (err, items)=>
      return callback err, items  if err

      @emit "ItemsArrived", items

      callback err, items

  lazyLoad: ->

    @listController.showLazyLoader()

    # this is to prevent lazy loading from getting most recent
    # posts when we're in most liked
    if @realFilter is MOST_LIKED and @mostLikedPostsCount > 0
      return @listController.hideLazyLoader()

{'fraction_non_alphanumeric': 0.10017657445556209, 'fraction_numerical': 0.0031783402001177162, 'mean_word_length': 2.897912365221381, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 91, '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': '1452577', 'n_tokens_mistral': 5355, 'n_tokens_neox': 4945, 'n_words': 1344}