<commit_before>import fetch from 'isomorphic-fetch'

import {updateJWT} from '../actions/updateJWT'

/* global __SERVER__ */
const APP_BASEURL = __SERVER__ ? process.env.APP_BASEURL : ''

export function getGraphQLFetcher(dispatch, auth, baseUrl = APP_BASEURL, throwErrors = true) {
  return graphQLParams => {
    const options = {
      method: 'post',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(graphQLParams),
    }
    if (auth.lgJWT) {
      options.headers = Object.assign(options.headers, {
        Authorization: `Bearer ${auth.lgJWT}`,
      })
    }

    return fetch(`${baseUrl}/graphql`, options)
      .then(resp => {
        if (!resp.ok) {
          console.error('GraphQL ERROR:', resp.statusText)
          if (throwErrors) {
            throw new Error(`GraphQL ERROR: ${resp.statusText}`)
          }
        }
        // for sliding-sessions, update our JWT from the LearnersGuild-JWT header
        const lgJWT = resp.headers.get('LearnersGuild-JWT')
        if (lgJWT) {
          dispatch(updateJWT(lgJWT))
        }
        return resp.json()
      })
      .then(graphQLResponse => {
        if (graphQLResponse.errors && graphQLResponse.errors.length) {
          if (throwErrors) {
            // throw the first error
            throw new Error(graphQLResponse.errors[0].message)
          }
        }
        return graphQLResponse
      })
  }
}

const CAPABILITY_ROLES = {
  createChapter: [
    'backoffice',
  ],
  editChapter: [
    'backoffice',
  ],
  listChapters: [
    'backoffice',
    'moderator',
  ],
  createInviteCode: [
    'backoffice',
  ],
  editCycleDuration: [
    'backoffice',
    'moderator',
  ],
}

export function userCan(currentUser, capability) {
  // console.log('user', currentUser.name, 'can', capability, '?')
  if (!currentUser) {
    // console.log(false)
    return false
  }
  const {roles} = currentUser
  if (!roles) {
    // console.log(false)
    return false
  }
  if (!CAPABILITY_ROLES[capability]) {
    // console.log(false)
    return false
  }<commit_msg>add a few more capabilities<commit_after>import fetch from 'isomorphic-fetch'

import {updateJWT} from '../actions/updateJWT'

/* global __SERVER__ */
const APP_BASEURL = __SERVER__ ? process.env.APP_BASEURL : ''

export function getGraphQLFetcher(dispatch, auth, baseUrl = APP_BASEURL, throwErrors = true) {
  return graphQLParams => {
    const options = {
      method: 'post',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(graphQLParams),
    }
    if (auth.lgJWT) {
      options.headers = Object.assign(options.headers, {
        Authorization: `Bearer ${auth.lgJWT}`,
      })
    }

    return fetch(`${baseUrl}/graphql`, options)
      .then(resp => {
        if (!resp.ok) {
          console.error('GraphQL ERROR:', resp.statusText)
          if (throwErrors) {
            throw new Error(`GraphQL ERROR: ${resp.statusText}`)
          }
        }
        // for sliding-sessions, update our JWT from the LearnersGuild-JWT header
        const lgJWT = resp.headers.get('LearnersGuild-JWT')
        if (lgJWT) {
          dispatch(updateJWT(lgJWT))
        }
        return resp.json()
      })
      .then(graphQLResponse => {
        if (graphQLResponse.errors && graphQLResponse.errors.length) {
          if (throwErrors) {
            // throw the first error
            throw new Error(graphQLResponse.errors[0].message)
          }
        }
        return graphQLResponse
      })
  }
}

const CAPABILITY_ROLES = {
  createChapter: [
    'backoffice',
  ],
  editChapter: [
    'backoffice',
  ],
  listChapters: [
    'backoffice',
    'moderator',
    'player',
  ],
  createInviteCode: [
    'backoffice',
  ],
  editCycleDuration: [
    'backoffice',
    'moderator',
  ],
  listPlayers: [
    'player',
  ],
  reassignPlayersToChapter: [
    'backoffice',
  ]
}

export function userCan(currentUser, capability) {
  // console.log('user', currentUser.name, 'can', capability, '?')
  if (!currentUser) {
    // console.log(false)
    return false
  }
  const {roles} = currentUser
  if (!roles) {
    // console.log(false)
    return false
  }
  if (!CAPABILITY_ROLES[capability]) {
    // console.log(false)
    return false
  }
{'fraction_non_alphanumeric': 0.1337371854613234, 'fraction_numerical': 0.00046598322460391424, 'mean_word_length': 2.380314960629921, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2840209', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1362, 'n_words': 265}