<commit_before>class ActivityListController extends KDListViewController

  hiddenItems               = []
  hiddenNewMemberItemGroups = [[]]
  hiddenItemCount           = 0

  prepareNewMemberGroup = ->

    # this is a bit tricky here
    # if the previous member group isn't empty
    # create a new group for later new member items
    if hiddenNewMemberItemGroups.last.length isnt 0
      hiddenNewMemberItemGroups.push []

  resetNewMemberGroups = -> hiddenNewMemberItemGroups = [[]]

  constructor:(options,data)->

    viewOptions = options.viewOptions or {}
    viewOptions.cssClass      or= 'activity-related'
    viewOptions.comments      or= yes
    viewOptions.itemClass     or= options.itemClass
    options.view              or= new KDListView viewOptions, data
    options.startWithLazyLoader = yes
    options.showHeader         ?= yes

    options.noItemFoundWidget or= new KDCustomHTMLView
      cssClass : "lazy-loader"
      partial  : "There is no activity."

    options.noMoreItemFoundWidget or= new KDCustomHTMLView
      cssClass : "lazy-loader"
      partial  : "There is no more activity."

    super

    @resetList()

    @_state = 'public'

    @getListView().on "ItemIsBeingDestroyed", @bound "addNoItemFoundWidget"

    @getListView().on "ItemWasAdded", @bound "removeNoItemFoundWidget"

    @scrollView.on 'scroll', (event) =>
      if event.delegateTarget.scrollTop > 0
        @activityHeader.setClass "scrolling-up-outset"
        @activityHeader.liveUpdateButton.setValue off
      else
        @emit "scrolledToTopOfPage"

        @activityHeader.unsetClass "scrolling-up-outset"
        @activityHeader.liveUpdateButton.setValue on

  removeNoItemFoundWidget:->
    if @getItemCount() is 1
      {noItemFoundWidget, noMoreItemFoundWidget} = @getOptions()
      @scrollView.addSubView noMoreItemFoundWidget if noMoreItemFoundWidget
      noItemFoundWidget.destroy() if noItemFoundWidget

  addNoItemFoundWidget:->
    if @getItemCount() is 0
      {noItemFoundWidget, noMoreItemFoundWidget} = @getOptions()
      noMoreItemFoundWidget.destroy() if noMoreItemFoundWidget
      @scrollView.addSubView noItemFoundWidget if noItemFoundWidget

  resetList:->
    @newActivityArrivedList = {}
    @lastItemTimeStamp = null

  loadView:(mainView)->

    @hideNoItemWidget()
    console.log("list activities ---- load view")
    data = @getData()
    console.log("got data")
    mainView.addSubView @activityHeader = new ActivityListHeader
      cssClass : 'feeder-header clearfix'

    @activityHeader.hide() unless @getOptions().showHeader

    @scrollView.on 'scroll', (event) =>
      if event.delegateTarget.scrollTop > 10
        @activityHeader.setClass "scrolling-up-outset"
      else
        @activityHeader.unsetClass "scrolling-up-outset"

    @activityHeader.on "UnhideHiddenNewItems", =>
      firstHiddenItem = @getListView().$('.hidden-item').eq(0)
      if firstHiddenItem.length > 0
        top   = firstHiddenItem.position().top
        top or= 0
        @scrollView.scrollTo {top, duration : 200}, =>
          unhideNewHiddenItems hiddenItems
    super

  isMine:(activity)->
    id = KD.whoami().getId()
    id? and id in [activity.originId, activity.anchor?.id]

  listActivities:(activities)->
    console.log("list activities ---- 1")
    activityIds = []
    for activity in activities when activity
      @addItem activity
      activityIds.push activity._id

    @checkIfLikedBefore activityIds

    modifiedAt = activities.first.meta.createdAt
    unless @lastItemTimeStamp > modifiedAt
      # HACK: activity timestamp is off -200 ms on first get
      time = new Date(modifiedAt).getTime()+1000
      @lastItemTimeStamp = new Date(time).toISOString()

    KD.logToMixpanel "populateActivity.success", 5

    @emit "teasersLoaded"

  listActivitiesFromCache:(cache)->
    return @showNoItemWidget() unless cache.overview?

    activityIds = []
    for overviewItem in cache.overview when overviewItem
      if overviewItem.ids.length > 1 and overviewItem.type is "CNewMemberBucketActivity"
        @addItem new NewMemberBucketData
          type                : "CNewMemberBucketActivity"
          anchors             : (cache.activities[id].teaser.anchor for id in overviewItem.ids)
          count               : overviewItem.count
          createdAtTimestamps : overviewItem.createdAt
      else
        activity = cache.activities[overviewItem.ids.first]
        if activity?.teaser
          activity.teaser.createdAtTimestamps = overviewItem.createdAt
          @addItem activity.teaser
          activityIds.push activity.teaser._id

    @checkIfLikedBefore activityIds

    @lastItemTimeStamp = cache.from
    console.log "lastItemTimeStamp", @lastItemTimeStamp

    KD.logToMixpanel "populateActivity.cache.success", 5

    @emit "teasersLoaded"

  checkIfLikedBefore:(activityIds)->

    KD.remote.api.CActivity.checkIfLikedBefore activityIds, (err, likedIds)=>
      for activity in @getListView().items when activity.data._id.toString() in likedIds
        likeView = activity.subViews.first.actionLinks?.likeView
        if likeView
          likeView.likeLink.updatePartial 'Unlike'
          likeView._currentState = yes

  getLastItemTimeStamp: ->

    if item = hiddenItems.first
      item.getData().createdAt or item.getData().createdAtTimestamps.last
    else
      @lastItemTimeStamp

  followedActivityArrived: (activity) ->

    if @_state is 'private'
      view = @addHiddenItem activity, 0
      @activityHeader?.newActivityArrived()

  logNewActivityArrived:(activity)->
    id = activity.getId?()
    return unless id

    if @newActivityArrivedList[id]<commit_msg>pagination complete<commit_after>class ActivityListController extends KDListViewController

  hiddenItems               = []
  hiddenNewMemberItemGroups = [[]]
  hiddenItemCount           = 0

  prepareNewMemberGroup = ->

    # this is a bit tricky here
    # if the previous member group isn't empty
    # create a new group for later new member items
    if hiddenNewMemberItemGroups.last.length isnt 0
      hiddenNewMemberItemGroups.push []

  resetNewMemberGroups = -> hiddenNewMemberItemGroups = [[]]

  constructor:(options,data)->

    viewOptions = options.viewOptions or {}
    viewOptions.cssClass      or= 'activity-related'
    viewOptions.comments      or= yes
    viewOptions.itemClass     or= options.itemClass
    options.view              or= new KDListView viewOptions, data
    options.startWithLazyLoader = yes
    options.showHeader         ?= yes

    options.noItemFoundWidget or= new KDCustomHTMLView
      cssClass : "lazy-loader"
      partial  : "There is no activity."

    options.noMoreItemFoundWidget or= new KDCustomHTMLView
      cssClass : "lazy-loader"
      partial  : "There is no more activity."

    super

    @resetList()

    @_state = 'public'

    @getListView().on "ItemIsBeingDestroyed", @bound "addNoItemFoundWidget"

    @getListView().on "ItemWasAdded", @bound "removeNoItemFoundWidget"

    @scrollView.on 'scroll', (event) =>
      if event.delegateTarget.scrollTop > 0
        @activityHeader.setClass "scrolling-up-outset"
        @activityHeader.liveUpdateButton.setValue off
      else
        @emit "scrolledToTopOfPage"

        @activityHeader.unsetClass "scrolling-up-outset"
        @activityHeader.liveUpdateButton.setValue on

  removeNoItemFoundWidget:->
    if @getItemCount() is 1
      {noItemFoundWidget, noMoreItemFoundWidget} = @getOptions()
      @scrollView.addSubView noMoreItemFoundWidget if noMoreItemFoundWidget
      noItemFoundWidget.destroy() if noItemFoundWidget

  addNoItemFoundWidget:->
    if @getItemCount() is 0
      {noItemFoundWidget, noMoreItemFoundWidget} = @getOptions()
      noMoreItemFoundWidget.destroy() if noMoreItemFoundWidget
      @scrollView.addSubView noItemFoundWidget if noItemFoundWidget

  resetList:->
    @newActivityArrivedList = {}
    @lastItemTimeStamp = null

  loadView:(mainView)->

    @hideNoItemWidget()
    console.log("list activities ---- load view")
    data = @getData()
    console.log("got data")
    mainView.addSubView @activityHeader = new ActivityListHeader
      cssClass : 'feeder-header clearfix'

    @activityHeader.hide() unless @getOptions().showHeader

    @scrollView.on 'scroll', (event) =>
      if event.delegateTarget.scrollTop > 10
        @activityHeader.setClass "scrolling-up-outset"
      else
        @activityHeader.unsetClass "scrolling-up-outset"

    @activityHeader.on "UnhideHiddenNewItems", =>
      firstHiddenItem = @getListView().$('.hidden-item').eq(0)
      if firstHiddenItem.length > 0
        top   = firstHiddenItem.position().top
        top or= 0
        @scrollView.scrollTo {top, duration : 200}, =>
          unhideNewHiddenItems hiddenItems
    super

  isMine:(activity)->
    id = KD.whoami().getId()
    id? and id in [activity.originId, activity.anchor?.id]

  listActivities:(activities)->
    console.log("list activities ---- 1")
    activityIds = []
    for activity in activities when activity
      @addItem activity
      activityIds.push activity._id

    @checkIfLikedBefore activityIds

    console.log("trying to set lastitemtimestamp - ", @lastItemTimeStamp)
    pivot = @lastItemTimeStamp || (new Date()).getTime()
    console.log("my pivot - " + pivot)
    for obj in activities
      obj_ts = (new Date(obj.meta.createdAt)).getTime()
      console.log(">>>>", obj_ts, pivot, obj_ts < pivot, (new Date(obj.meta.createdAt)))
      if obj_ts < pivot
        console.log("changing pivot")
        pivot = obj_ts
    @lastItemTimeStamp = pivot
    console.log("lastitemtimestamp is now - ", @lastItemTimeStamp)

    #modifiedAt = activities.first.meta.createdAt
    #
    #unless @lastItemTimeStamp > modifiedAt
    #  # HACK: activity timestamp is off -200 ms on first get
    #  time = new Date(modifiedAt).getTime()+1000
    #  @lastItemTimeStamp = new Date(time).toISOString()

    KD.logToMixpanel "populateActivity.success", 5

    @emit "teasersLoaded"

  listActivitiesFromCache:(cache)->
    return @showNoItemWidget() unless cache.overview?

    activityIds = []
    for overviewItem in cache.overview when overviewItem
      if overviewItem.ids.length > 1 and overviewItem.type is "CNewMemberBucketActivity"
        @addItem new NewMemberBucketData
          type                : "CNewMemberBucketActivity"
          anchors             : (cache.activities[id].teaser.anchor for id in overviewItem.ids)
          count               : overviewItem.count
          createdAtTimestamps : overviewItem.createdAt
      else
        activity = cache.activities[overviewItem.ids.first]
        if activity?.teaser
          activity.teaser.createdAtTimestamps = overviewItem.createdAt
          @addItem activity.teaser
          activityIds.push activity.teaser._id

    @checkIfLikedBefore activityIds

    @lastItemTimeStamp = cache.from
    console.log "lastItemTimeStamp", @lastItemTimeStamp

    KD.logToMixpanel "populateActivity.cache.success", 5

    @emit "teasersLoaded"

  checkIfLikedBefore:(activityIds)->

    KD.remote.api.CActivity.checkIfLikedBefore activityIds, (err, likedIds)=>
      for activity in @getListView().items when activity.data._id.toString() in likedIds
        likeView = activity.subViews.first.actionLinks?.likeView
        if likeView
          likeView.likeLink.updatePartial 'Unlike'
          likeView._currentState = yes

  getLastItemTimeStamp: ->
    item = hiddenItems.first
    if item
      console.log("hidden items first ")
      item.getData().createdAt or item.getData().createdAtTimestamps.last
    else
      console.log("last time stamp - " + @lastItemTimeStamp)
      @lastItemTimeStamp

  followedActivityArrived: (activity) ->

    if @_state is 'private'
      view = @addHiddenItem activity, 0
      @activityHeader?.newActivityArrived()

  logNewActivityArrived:(activity)->
    id = activity.getId?()
    return unless id

    if @newActivityArrivedList[id]
{'fraction_non_alphanumeric': 0.09341577540106952, 'fraction_numerical': 0.004177807486631016, 'mean_word_length': 3.091965811965812, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, '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': '423018', 'n_tokens_mistral': 3550, 'n_tokens_neox': 3363, 'n_words': 757}