<commit_before>import localStorageAvailable from './localStorageAvailable';
import * as constants from 'app/constants';

const BASE_VAL = {
  showBanner: false,
  impressionUrl: '',
  clickUrl: '',
};

const TWO_WEEKS = 2 * 7 * 24 * 60 * 60;

const ALLOWED_PAGES = new Set([
  'index',
  'listing',
  'comments',
]);

const IOS_USER_AGENTS = [
  'iPhone',
  'iPod',
];

const ANDROID_USER_AGENTS = [
  'Android',
];

const ALLOWED_DEVICES = IOS_USER_AGENTS.concat(ANDROID_USER_AGENTS);

const PAGE_PERCENTAGES = {
  'comments': 5,
};

const USE_TUNE = 100;

const checkDeviceType = (allowedAgents, userAgentString) => {
  return allowedAgents.some(a => userAgentString.indexOf(a) > -1);
};

export function shouldShowBanner({ actionName, user, userAgent }={}) {
  // Lots of options we have to consider.
  // 1) Easiest. Make sure local storage exists
  if (!localStorageAvailable()) { return BASE_VAL; }

  // 2) Check if it's been dismissed recently
  const lastClosedStr = localStorage.getItem('bannerLastClosed');
  const lastClosed = lastClosedStr ? new Date(lastClosedStr).getTime() : null;
  const lastClosedLimit = Date.now() + TWO_WEEKS;
  if (lastClosed && lastClosed < lastClosedLimit) {
    return BASE_VAL;
  }

  // 3) Check if we're on the right page.
  if (!ALLOWED_PAGES.has(actionName)) { return BASE_VAL; }

  // 4) Check the user agent
  if (!checkDeviceType(ALLOWED_DEVICES, userAgent)) { return BASE_VAL; }

  // Create a bucket; a few rules are going to depend on that
  let userId = '';
  if (user) { userId = user.loid || user.id; }
  const userIdSum = userId.split('').reduce((sum, chr) => sum + chr.charCodeAt(0), 0);
  const bucket = userIdSum % 100;

  // 5) only show to a certain % of users that land on a given page
  for (const pageName in PAGE_PERCENTAGES) {
    if ((actionName === pageName) && (bucket > PAGE_PERCENTAGES[pageName])) {
      return BASE_VAL;
    }
  }

  // Ok, now we know we're actually going to show the banner. Next, we need to<commit_msg>Fix the SmartBanner cookie expiration code<commit_after>import localStorageAvailable from './localStorageAvailable';
import * as constants from 'app/constants';

const BASE_VAL = {
  showBanner: false,
  impressionUrl: '',
  clickUrl: '',
};

const TWO_WEEKS = 2 * 7 * 24 * 60 * 60 * 1000;

const ALLOWED_PAGES = new Set([
  'index',
  'listing',
  'comments',
]);

const IOS_USER_AGENTS = [
  'iPhone',
  'iPod',
];

const ANDROID_USER_AGENTS = [
  'Android',
];

const ALLOWED_DEVICES = IOS_USER_AGENTS.concat(ANDROID_USER_AGENTS);

const PAGE_PERCENTAGES = {
  'comments': 5,
};

const USE_TUNE = 100;

const checkDeviceType = (allowedAgents, userAgentString) => {
  return allowedAgents.some(a => userAgentString.indexOf(a) > -1);
};

export function shouldShowBanner({ actionName, user, userAgent }={}) {
  // Lots of options we have to consider.
  // 1) Easiest. Make sure local storage exists
  if (!localStorageAvailable()) { return BASE_VAL; }

  // 2) Check if it's been dismissed recently
  const lastClosedStr = localStorage.getItem('bannerLastClosed');
  const lastClosed = lastClosedStr ? new Date(lastClosedStr).getTime() : 0;
  const lastClosedLimit = lastClosed + TWO_WEEKS;
  if (lastClosedLimit > Date.now()) {
    return BASE_VAL;
  }

  // 3) Check if we're on the right page.
  if (!ALLOWED_PAGES.has(actionName)) { return BASE_VAL; }

  // 4) Check the user agent
  if (!checkDeviceType(ALLOWED_DEVICES, userAgent)) { return BASE_VAL; }

  // Create a bucket; a few rules are going to depend on that
  let userId = '';
  if (user) { userId = user.loid || user.id; }
  const userIdSum = userId.split('').reduce((sum, chr) => sum + chr.charCodeAt(0), 0);
  const bucket = userIdSum % 100;

  // 5) only show to a certain % of users that land on a given page
  for (const pageName in PAGE_PERCENTAGES) {
    if ((actionName === pageName) && (bucket > PAGE_PERCENTAGES[pageName])) {
      return BASE_VAL;
    }
  }

  // Ok, now we know we're actually going to show the banner. Next, we need to
{'fraction_non_alphanumeric': 0.1274632077824894, 'fraction_numerical': 0.012721376901970565, 'mean_word_length': 3.8902439024390243, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 14, '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': '5601303', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1349, 'n_words': 398}