<commit_before>import escapeRegExp from 'escape-string-regexp';
import values from 'object-values';
import ms from 'ms';
import splitargs from 'splitargs';
import parseChatMarkup from 'u-wave-parse-chat-markup';

import {
  SEND_MESSAGE, RECEIVE_MESSAGE, LOG,
  REMOVE_MESSAGE, REMOVE_USER_MESSAGES, REMOVE_ALL_MESSAGES,
  MUTE_USER, UNMUTE_USER
} from '../constants/actionTypes/chat';
import { sendMessage } from '../utils/Socket';
import { execute } from '../utils/ChatCommands';
import {
  muteTimeoutsSelector,
  mutedUserIDsSelector,
  currentUserMuteSelector
} from '../selectors/chatSelectors';
import { settingsSelector } from '../selectors/settingSelectors';
import { currentUserSelector, usersSelector, userListSelector } from '../selectors/userSelectors';
import { currentTimeSelector } from '../selectors/timeSelectors';

let logIdx = Date.now();
export function log(text) {
  return {
    type: LOG,
    payload: {
      _id: logIdx++,
      text
    }
  };
}

export function prepareMessage(user, text, parseOpts = {}) {
  return {
    type: SEND_MESSAGE,
    payload: {
      user,
      message: text,
      parsed: parseChatMarkup(text, parseOpts)
    }
  };
}

export function sendChat(user, text) {
  return (dispatch, getState) => {
    const mute = currentUserMuteSelector(getState());
    if (mute) {
      const timeLeft = ms(mute.expiresAt - Date.now(), { long: true });
      dispatch(log(
        `You have been muted, and cannot chat for another ${timeLeft}.`
      ));
      return;
    }

    const users = userListSelector(getState());
    const message = prepareMessage(user, text, { users });
    dispatch(message);
    sendMessage(message);
  };
}

export function inputMessage(text) {
  return (dispatch, getState) => {
    if (text[0] === '/') {
      const [ command, ...params ] = splitargs(text.slice(1));
      if (command) {
        const result = execute(getState(), command, params);
        if (result) {
          dispatch(result);
        }
        return;
      }
    }
    const user = currentUserSelector(getState());
    dispatch(sendChat(user, text));
  };
}

let mentionSound;
function playMentionSound() {
  if (!mentionSound) {
    mentionSound = new Audio('assets/audio/mention.mp3');
  }
  mentionSound.play();
}

function hasMention(text, username) {
  const rx = new RegExp(`@${escapeRegExp(username)}\\b`, 'i');
  return rx.test(text);
}

function isMuted(state, userID) {
  return mutedUserIDsSelector(state).indexOf(userID) !== -1;
}

export function receive(message) {
  return (dispatch, getState) => {
    const settings = settingsSelector(getState());
    const user = currentUserSelector(getState());
    const users = usersSelector(getState());

    if (isMuted(getState(), message.userID)) {
      return;
    }

    const isMention = user<commit_msg>chat: preload mention sound on page load<commit_after>import escapeRegExp from 'escape-string-regexp';
import values from 'object-values';
import ms from 'ms';
import splitargs from 'splitargs';
import parseChatMarkup from 'u-wave-parse-chat-markup';

import {
  SEND_MESSAGE, RECEIVE_MESSAGE, LOG,
  REMOVE_MESSAGE, REMOVE_USER_MESSAGES, REMOVE_ALL_MESSAGES,
  MUTE_USER, UNMUTE_USER
} from '../constants/actionTypes/chat';
import { sendMessage } from '../utils/Socket';
import { execute } from '../utils/ChatCommands';
import {
  muteTimeoutsSelector,
  mutedUserIDsSelector,
  currentUserMuteSelector
} from '../selectors/chatSelectors';
import { settingsSelector } from '../selectors/settingSelectors';
import { currentUserSelector, usersSelector, userListSelector } from '../selectors/userSelectors';
import { currentTimeSelector } from '../selectors/timeSelectors';

let logIdx = Date.now();
export function log(text) {
  return {
    type: LOG,
    payload: {
      _id: logIdx++,
      text
    }
  };
}

export function prepareMessage(user, text, parseOpts = {}) {
  return {
    type: SEND_MESSAGE,
    payload: {
      user,
      message: text,
      parsed: parseChatMarkup(text, parseOpts)
    }
  };
}

export function sendChat(user, text) {
  return (dispatch, getState) => {
    const mute = currentUserMuteSelector(getState());
    if (mute) {
      const timeLeft = ms(mute.expiresAt - Date.now(), { long: true });
      dispatch(log(
        `You have been muted, and cannot chat for another ${timeLeft}.`
      ));
      return;
    }

    const users = userListSelector(getState());
    const message = prepareMessage(user, text, { users });
    dispatch(message);
    sendMessage(message);
  };
}

export function inputMessage(text) {
  return (dispatch, getState) => {
    if (text[0] === '/') {
      const [ command, ...params ] = splitargs(text.slice(1));
      if (command) {
        const result = execute(getState(), command, params);
        if (result) {
          dispatch(result);
        }
        return;
      }
    }
    const user = currentUserSelector(getState());
    dispatch(sendChat(user, text));
  };
}

let mentionSound;
if (typeof window !== 'undefined' && window.Audio) {
  mentionSound = new window.Audio('assets/audio/mention.mp3');
}
function playMentionSound() {
  if (mentionSound) {
    mentionSound.play();
  }
}

function hasMention(text, username) {
  const rx = new RegExp(`@${escapeRegExp(username)}\\b`, 'i');
  return rx.test(text);
}

function isMuted(state, userID) {
  return mutedUserIDsSelector(state).indexOf(userID) !== -1;
}

export function receive(message) {
  return (dispatch, getState) => {
    const settings = settingsSelector(getState());
    const user = currentUserSelector(getState());
    const users = usersSelector(getState());

    if (isMuted(getState(), message.userID)) {
      return;
    }

    const isMention = user
{'fraction_non_alphanumeric': 0.13714185883997204, 'fraction_numerical': 0.001397624039133473, 'mean_word_length': 3.5508744038155804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6794801', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1761, 'n_words': 418}