<commit_before>import CiscoSpark from 'ciscospark'
import uniqBy from 'lodash/uniqBy'
import map from 'lodash/map'

const flatMapInSeries = async (items, cb) => {
  const collection = []
  for (const item of items) {
    collection.push(...await cb(item))
  }
  return collection
}

export default class {
  constructor (user) {
    this.user = user
  }

  _sparkClient = () => {
    this.__sparkClient = this.__sparkClient || this._buildSparkClient()
    return this.__sparkClient
  }

  _buildSparkClient = () => CiscoSpark.init({
    authorization: {
      access_token: this.user.accessToken
    }
  })

  // requires scope spark:teams_read
  listTeams = async () => (await this._sparkClient().teams.list()).items

  // requires scope spark:rooms_read
  listRoomsInTeam = async ({id}) =>
    (await this._sparkClient().rooms.list({ type: 'group', teamId: id })).items

  // requires scope spark:rooms_read
  listNonTeamRooms = async () =>
    (await this._sparkClient().rooms.list({ type: 'group' })).items

  // requires scope spark:memberships_read
  listRoomMembers = async roomId => {
    const { items } = await this._sparkClient().memberships.list({ roomId })
    return items
  }

  listRooms = async () => {
    const teams = await this.listTeams()
    const teamRooms = await flatMapInSeries(teams, async team =>
      map(await this.listRoomsInTeam(team), room => ({
        ...room, teamName: team.name
      })
    ))
    const nonTeamRooms = await this.listNonTeamRooms()
    return uniqBy(teamRooms.concat(nonTeamRooms), 'id')
  }
}<commit_msg>Fetch Spaces in parallel for survey form (#24)<commit_after>import CiscoSpark from 'ciscospark'
import uniqBy from 'lodash/uniqBy'
import flatten from 'lodash/flatten'

const asyncFlatMap = async (...args) => flatten(await asyncMap(...args))
const asyncMap = (items, cb) => Promise.all(items.map(cb))

export default class {
  constructor (user) {
    this.user = user
  }

  _sparkClient = () => CiscoSpark.init({
    authorization: {
      access_token: this.user.accessToken
    }
  })

  // requires scope spark:teams_read
  listTeams = () => this._list('teams')

  // requires scope spark:rooms_read
  listRoomsInTeam = ({id}) => this._list('rooms', { type: 'group', teamId: id })

  // requires scope spark:rooms_read
  listNonTeamRooms = () => this._list('rooms', { type: 'group' })

  // requires scope spark:memberships_read
  listRoomMembers = roomId => this._list('memberships', { roomId })

  _list = async (resource, args = {}) =>
    (await this._sparkClient()[resource].list(args)).items

  listTeamRooms = async () => asyncFlatMap(await this.listTeams(), async team =>
    (await this.listRoomsInTeam(team)).map(room => ({
      ...room, teamName: team.name
    }))
  )

  listRooms = async () => {
    const [teamRooms, nonTeamRooms] = await Promise.all([
      this.listTeamRooms(),
      this.listNonTeamRooms()
    ])
    return uniqBy(teamRooms.concat(nonTeamRooms), 'id')
  }
}
{'fraction_non_alphanumeric': 0.14420803782505912, 'fraction_numerical': 0.0006754474839581223, 'mean_word_length': 3.542944785276074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '3182544', 'n_tokens_mistral': 987, 'n_tokens_neox': 954, 'n_words': 214}