{"size": 3078, "ext": "js", "max_stars_repo_path": "client/chat/action-creators.js", "max_stars_repo_name": "AleXoundOS/ShieldBattery", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "client/chat/action-creators.js", "max_issues_repo_name": "AleXoundOS/ShieldBattery", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "client/chat/action-creators.js", "max_forks_repo_name": "AleXoundOS/ShieldBattery", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.9701492537, "max_line_length": 92, "alphanum_fraction": 0.6608187135}
import siteSocket from '../network/site-socket'
import { push } from '../navigation/routing'
import {
  CHAT_CHANNEL_ACTIVATE,
  CHAT_CHANNEL_DEACTIVATE,
  CHAT_JOIN_CHANNEL_BEGIN,
  CHAT_JOIN_CHANNEL,
  CHAT_LEAVE_CHANNEL_BEGIN,
  CHAT_LEAVE_CHANNEL,
  CHAT_LOAD_CHANNEL_HISTORY_BEGIN,
  CHAT_LOAD_CHANNEL_HISTORY,
  CHAT_LOAD_USER_LIST_BEGIN,
  CHAT_LOAD_USER_LIST,
  CHAT_SEND_MESSAGE_BEGIN,
  CHAT_SEND_MESSAGE,
} from '../actions'

export function joinChannel(channel) {
  return dispatch => {
    const params = { channel }
    dispatch({
      type: CHAT_JOIN_CHANNEL_BEGIN,
      payload: params,
    })
    dispatch({
      type: CHAT_JOIN_CHANNEL,
      payload: siteSocket.invoke('/chat/join', params),
      meta: params,
    })
  }
}

export function leaveChannel(channel) {
  return dispatch => {
    const params = { channel }
    dispatch({
      type: CHAT_LEAVE_CHANNEL_BEGIN,
      payload: params,
    })
    dispatch({
      type: CHAT_LEAVE_CHANNEL,
      payload: siteSocket.invoke('/chat/leave', params),
      meta: params,
    })
  }
}

export function sendMessage(channel, message) {
  return dispatch => {
    const params = { channel, message }
    dispatch({
      type: CHAT_SEND_MESSAGE_BEGIN,
      payload: params,
    })
    dispatch({
      type: CHAT_SEND_MESSAGE,
      payload: siteSocket.invoke('/chat/send', params),
      meta: params,
    })
  }
}

export function getMessageHistory(channel, limit) {
  return (dispatch, getStore) => {
    const {
      chat: { byName },
    } = getStore()
    const lowerCaseChannel = channel.toLowerCase()
    if (!byName.has(lowerCaseChannel)) {
      return
    }

    const chanData = byName.get(lowerCaseChannel)
    const earliestMessageTime = chanData.messages.size ? chanData.messages.first().time : -1
    const params = { channel, limit, beforeTime: earliestMessageTime }

    dispatch({
      type: CHAT_LOAD_CHANNEL_HISTORY_BEGIN,
      payload: params,
    })
    dispatch({
      type: CHAT_LOAD_CHANNEL_HISTORY,
      payload: siteSocket.invoke('/chat/getHistory', params),
      meta: params,
    })
  }
}

export function retrieveUserList(channel) {
  return (dispatch, getStore) => {
    const {
      chat: { byName },
    } = getStore()
    const lowerCaseChannel = channel.toLowerCase()
    if (!byName.has(lowerCaseChannel)) {
      return
    }

    const chanData = byName.get(lowerCaseChannel)
    if (chanData.hasLoadedUserList || chanData.loadingUserList) {
      return
    }

    const params = { channel }
    dispatch({
      type: CHAT_LOAD_USER_LIST_BEGIN,
      payload: params,
    })
    dispatch({
      type: CHAT_LOAD_USER_LIST,
      payload: siteSocket.invoke('/chat/getUsers', params),
      meta: params,
    })
  }
}

export function activateChannel(channel) {
  return {
    type: CHAT_CHANNEL_ACTIVATE,
    payload: { channel },
  }
}

export function deactivateChannel(channel) {
  return {
    type: CHAT_CHANNEL_DEACTIVATE,
    payload: { channel },
  }
}

export function navigateToChannel(channel) {
  push(`/chat/${encodeURIComponent(channel)}`)
}
