<commit_before>const fetch = require('node-fetch');
const moment = require('moment');
moment.locale('fi');

class SlackChannelStatus {

  constructor(apiKey) {
    this.apiKey = apiKey;
  }

  async getChannelHistoryJson(channelId, startFrom = 0) {
    // If oldest is used then it will return that and amount of count newer ones. Have to do decision when we have anough manually.
    const result = await fetch(`https://slack.com/api/channels.history?token=${this.apiKey}&channel=${channelId}&latest=${startFrom}&count=1000&pretty=0`);
    return await result.json();
  }

  async getUser(userId) {
    const result = await fetch(`https://slack.com/api/users.info?token=${this.apiKey}&user=${userId}&pretty=0`);
    const json = await result.json();
    return json.ok ? json.user : {};
  }

  async getActivity(channelId, days = 7, top = 5) {
    const lastWeek = moment().add(-1 * days, 'days');
    const oldestTimeStamp = lastWeek.unix();

    let messages = [];
    let startFrom = 0;

    while (true) {
      let json = await this.getChannelHistoryJson(channelId, startFrom);

      if (json.ok === false)
        return null;

      let lastInArray = json.messages[json.messages.length - 1].ts;

      if (oldestTimeStamp >= lastInArray) {
        let toAdd = json.messages.filter(e => e.ts >= oldestTimeStamp);
        messages = messages.concat(toAdd);
        break;
      }

      messages = messages.concat(json.messages);

      if (json.has_more === false)
        break;

      startFrom = lastInArray;
    }

    const userGrouped = messages.reduce((grouped, item) => {
      if (!item || item.subtype == "bot_message") return grouped;

      const key = item.user || item.comment.user;
      grouped[key] = grouped[key] || [];
      grouped[key].push(item);
      return grouped
    }, {});

    const groupedArray = Object.keys(userGrouped).map(key => {<commit_msg>Slack channel history to use different api list method for private and public channels<commit_after>const fetch = require('node-fetch');
const moment = require('moment');
moment.locale('fi');

class SlackChannelStatus {

  constructor(apiKey) {
    this.apiKey = apiKey;
  }

  async getChannelHistoryJson(method, channelId, startFrom = 0) {
    // If oldest is used then it will return that and amount of count newer ones. Have to do decision when we have anough manually.
    const result = await fetch(`https://slack.com/api/${method}?token=${this.apiKey}&channel=${channelId}&latest=${startFrom}&count=1000&pretty=0`);
    return await result.json();
  }

  async getUser(userId) {
    const result = await fetch(`https://slack.com/api/users.info?token=${this.apiKey}&user=${userId}&pretty=0`);
    const json = await result.json();
    return json.ok ? json.user : {};
  }

  async isChannelPublic(channelId) {
    const result = await fetch(`https://slack.com/api/channels.info?token=${this.apiKey}&channel=${channelId}`);
    const json = await result.json();
    return json.ok;
  }

  async getActivity(channelId, days = 7, top = 5) {

    const listMethod = await this.isChannelPublic(channelId) ? 'channels.history' : 'groups.history';

    const lastWeek = moment().add(-1 * days, 'days');
    const oldestTimeStamp = lastWeek.unix();

    let messages = [];
    let startFrom = 0;

    while (true) {
      let json = await this.getChannelHistoryJson(listMethod, channelId, startFrom);

      if (json.ok === false)
        return null;

      let lastInArray = json.messages[json.messages.length - 1].ts;

      if (oldestTimeStamp >= lastInArray) {
        let toAdd = json.messages.filter(e => e.ts >= oldestTimeStamp);
        messages = messages.concat(toAdd);
        break;
      }

      messages = messages.concat(json.messages);

      if (json.has_more === false)
        break;

      startFrom = lastInArray;
    }

    const userGrouped = messages.reduce((grouped, item) => {
      if (!item || item.subtype == "bot_message") return grouped;

      const key = item.user || item.comment.user;
      grouped[key] = grouped[key] || [];
      grouped[key].push(item);
      return grouped
    }, {});

    const groupedArray = Object.keys(userGrouped).map(key => {
{'fraction_non_alphanumeric': 0.14234363767074049, 'fraction_numerical': 0.005751258087706686, 'mean_word_length': 3.2375634517766496, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 5, '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': '206249', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1242, 'n_words': 305}