<commit_before>_                      = require 'lodash'
kd                     = require 'kd'
whoami                 = require 'app/util/whoami'
actionTypes            = require './actiontypes'
generateFakeIdentifier = require 'app/util/generateFakeIdentifier'
messageHelpers         = require '../helpers/message'
realtimeActionCreators = require './realtime/actioncreators'
embedlyHelpers         = require '../helpers/embedly'
Promise                = require 'bluebird'

dispatch = (args...) -> kd.singletons.reactor.dispatch args...

###*
 * Action to load messages of channel with given channelId.
 *
 * @param {string} channelId
###
loadMessages = (channelId, options = {}) ->

  options.limit ?= 50
  { socialapi } = kd.singletons
  { LOAD_MESSAGES_BEGIN, LOAD_MESSAGES_FAIL,
    LOAD_MESSAGES_SUCCESS, LOAD_MESSAGE_SUCCESS
    SET_ALL_MESSAGES_LOADED, UNSET_ALL_MESSAGES_LOADED
    SET_LOADED_WITH_SCROLL, UNSET_LOADED_WITH_SCROLL } = actionTypes

  dispatch LOAD_MESSAGES_BEGIN, { channelId, options }

  _options = _.assign {}, options, { id: channelId }

  new Promise (resolve, reject) ->
    socialapi.channel.fetchActivities _options, (err, messages) ->
      if err
        dispatch LOAD_MESSAGES_FAIL, { err, channelId }
        return reject err

      # clean load more markers of given messages first.
      # kd.utils.defer -> cleanLoaderMarkers channelId, messages

      if messages.length < options.limit
        dispatch SET_ALL_MESSAGES_LOADED, { channelId }
      else
        dispatch UNSET_ALL_MESSAGES_LOADED, { channelId }

      kd.singletons.reactor.batch ->
        messages.forEach (message) ->
          dispatchLoadMessageSuccess channelId, message
        dispatch LOAD_MESSAGES_SUCCESS, { channelId, messages }

        if options.loadedWithScroll
          dispatch SET_LOADED_WITH_SCROLL, { channelId }

      resolve { messages }


###*
 * An action creator to group load message action operations.
 * It wraps given message with realtimeActionCreators to transform realtime
 * events to flux actions.
 *
 * @param {string} channelId
 * @param {SocialMessage} message
 * @api private
###
dispatchLoadMessageSuccess = (channelId, message) ->

  channel = kd.singletons.socialapi.retrieveCachedItemById channelId

  realtimeActionCreators.bindMessageEvents message
  dispatch actionTypes.LOAD_MESSAGE_SUCCESS, { channelId, channel, message }


###*
 * An empty promise resolver. It's being used for default cases that returns<commit_msg>Client: Activity: Deleted unnecessary event dispatcher for loadedWithScroll flag and added defer to the batch<commit_after>_                      = require 'lodash'
kd                     = require 'kd'
whoami                 = require 'app/util/whoami'
actionTypes            = require './actiontypes'
generateFakeIdentifier = require 'app/util/generateFakeIdentifier'
messageHelpers         = require '../helpers/message'
realtimeActionCreators = require './realtime/actioncreators'
embedlyHelpers         = require '../helpers/embedly'
Promise                = require 'bluebird'

dispatch = (args...) -> kd.singletons.reactor.dispatch args...

###*
 * Action to load messages of channel with given channelId.
 *
 * @param {string} channelId
###
loadMessages = (channelId, options = {}) ->

  options.limit ?= 50
  { socialapi } = kd.singletons
  { LOAD_MESSAGES_BEGIN, LOAD_MESSAGES_FAIL,
    LOAD_MESSAGES_SUCCESS, LOAD_MESSAGE_SUCCESS
    SET_ALL_MESSAGES_LOADED, UNSET_ALL_MESSAGES_LOADED
    SET_LOADED_WITH_SCROLL, UNSET_LOADED_WITH_SCROLL } = actionTypes

  dispatch LOAD_MESSAGES_BEGIN, { channelId, options }

  _options = _.assign {}, options, { id: channelId }

  new Promise (resolve, reject) ->
    socialapi.channel.fetchActivities _options, (err, messages) ->
      if err
        dispatch LOAD_MESSAGES_FAIL, { err, channelId }
        return reject err

      # clean load more markers of given messages first.
      # kd.utils.defer -> cleanLoaderMarkers channelId, messages

      if messages.length < options.limit
        dispatch SET_ALL_MESSAGES_LOADED, { channelId }
      else
        dispatch UNSET_ALL_MESSAGES_LOADED, { channelId }

      kd.utils.defer ->
        kd.singletons.reactor.batch ->
          for message in messages
            dispatchLoadMessageSuccess channelId, message

          dispatch LOAD_MESSAGES_SUCCESS, { channelId, messages }
        resolve { messages }


###*
 * An action creator to group load message action operations.
 * It wraps given message with realtimeActionCreators to transform realtime
 * events to flux actions.
 *
 * @param {string} channelId
 * @param {SocialMessage} message
 * @api private
###
dispatchLoadMessageSuccess = (channelId, message) ->

  channel = kd.singletons.socialapi.retrieveCachedItemById channelId

  realtimeActionCreators.bindMessageEvents message
  dispatch actionTypes.LOAD_MESSAGE_SUCCESS, { channelId, channel, message }


###*
 * An empty promise resolver. It's being used for default cases that returns
{'fraction_non_alphanumeric': 0.08523866827115925, 'fraction_numerical': 0.0008022462896109105, 'mean_word_length': 3.2406462585034013, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '2971953', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1376, 'n_words': 369}