<commit_before>import got from 'got';
import ms from 'ms';

function stripSlashes(url) {
  return url.replace(/\/+$/, '');
}

module.exports = function announcePlugin(options) {
  const hubHost = options.hub || 'https://u-wave-announce.now.sh';
  const url = stripSlashes(options.url);

  const announceUrl = `${stripSlashes(hubHost)}/announce`;

  return (uw) => {
    async function announce() {
      // TODO add something to üWave Core so we don't have to manually populate
      // the relationships.
      const entry = await uw.booth.getCurrentEntry();
      if (entry) {
        entry.populate('user media.media');
        await entry.execPopulate();
      }

      await got.post(announceUrl, {
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({
          name: options.name,
          subtitle: options.subtitle,
          description: options.description || null,

          booth: entry ? {
            media: {
              artist: entry.media.artist,
              title: entry.media.title,
              thumbnail: entry.media.media.thumbnail,
            },
            dj: entry.user ? {
              username: entry.user.username,
            } : null,
          } : null,

          url,
          // Derive URLs if not given.
          apiUrl: options.apiUrl || `${url}/v1`,
          socketUrl: options.socketUrl || `${url.replace(/^http/, 'ws')}/`,
        }),
      });
    }

    announce();

    const interval = setInterval(announce, ms('1 minute'));
    uw.on('stop', () => {
      clearInterval(interval);
    });
  };
};<commit_msg>plugin: Pull announcement data builder into own function.<commit_after>import got from 'got';
import ms from 'ms';

function stripSlashes(url) {
  return url.replace(/\/+$/, '');
}

async function getAnnounceData(uw, options) {
  // TODO add something to üWave Core so we don't have to manually populate
  // the relationships.
  const entry = await uw.booth.getCurrentEntry();
  if (entry) {
    entry.populate('user media.media');
    await entry.execPopulate();
  }

  return {
    name: options.name,
    subtitle: options.subtitle,
    description: options.description || null,

    booth: entry ? {
      media: {
        artist: entry.media.artist,
        title: entry.media.title,
        thumbnail: entry.media.media.thumbnail,
      },
      dj: entry.user ? {
        username: entry.user.username,
      } : null,
    } : null,

    url,
    // Derive URLs if not given.
    apiUrl: options.apiUrl || `${url}/v1`,
    socketUrl: options.socketUrl || `${url.replace(/^http/, 'ws')}/`,
  };
}

module.exports = function announcePlugin(options) {
  const hubHost = options.hub || 'https://u-wave-announce.now.sh';
  const url = stripSlashes(options.url);

  const announceUrl = `${stripSlashes(hubHost)}/announce`;

  return (uw) => {
    async function announce() {
      const announcement = await getAnnounceData(uw, options);

      await got.post(announceUrl, {
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify(announcement),
      });
    }

    announce();

    const interval = setInterval(announce, ms('1 minute'));
    uw.on('stop', () => {
      clearInterval(interval);
    });
  };
};
{'fraction_non_alphanumeric': 0.14765721331689272, 'fraction_numerical': 0.0012330456226880395, 'mean_word_length': 2.4050367261280168, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '312155', 'n_tokens_mistral': 979, 'n_tokens_neox': 979, 'n_words': 227}