<commit_before>/* eslint-disable
    camelcase,
    handle-callback-err,
    max-len,
    no-return-assign,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
 * decaffeinate suggestions:
 * DS102: Remove unnecessary code created because of implicit returns
 * DS207: Consider shorter variations of null checks
 * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
 */
const { Project } = require('../../models/Project')
const logger = require('logger-sharelatex')
const _ = require('underscore')
const settings = require('settings-sharelatex')

const safeCompilers = ['xelatex', 'pdflatex', 'latex', 'lualatex']

module.exports = {
  setCompiler(project_id, compiler, callback) {
    if (callback == null) {
      callback = function() {}
    }
    logger.log({ project_id, compiler }, 'setting the compiler')
    compiler = compiler.toLowerCase()
    if (!_.contains(safeCompilers, compiler)) {
      return callback()
    }
    const conditions = { _id: project_id }
    const update = { compiler }
    return Project.update(conditions, update, {}, function(err) {
      if (callback != null) {
        return callback()
      }
    })
  },

  setImageName(project_id, imageName, callback) {
    if (callback == null) {
      callback = function() {}
    }
    logger.log({ project_id, imageName }, 'setting the imageName')
    imageName = imageName.toLowerCase()
    if (
      !_.some(
        settings.allowedImageNames,
        allowed => imageName === allowed.imageName
      )
    ) {
      return callback()
    }
    const conditions = { _id: project_id }
    const update = { imageName: settings.imageRoot + '/' + imageName }
    return Project.update(conditions, update, {}, function(err) {
      if (callback != null) {
        return callback()
      }
    })
  },

  setSpellCheckLanguage(project_id, languageCode, callback) {
    if (callback == null) {
      callback = function() {}
    }
    logger.log({ project_id, languageCode }, 'setting the spell check language')
    let languageIsSafe = false
    settings.languages.forEach(function(safeLang) {
      if (safeLang.code === languageCode) {
        return (languageIsSafe = true)
      }
    })

    if (languageCode === '') {
      languageIsSafe = true
    }

    if (languageIsSafe) {
      const conditions = { _id: project_id }
      const update = { spellCheckLanguage: languageCode }
      return Project.update(conditions, update, {}, err => callback())
    } else {
      logger.err({ project_id, languageCode }, 'tryed to set unsafe language')
      return callback()
    }
  },

  setBrandVariationId(project_id, brandVariationId, callback) {
    if (callback == null) {
      callback = function() {}
    }
    logger.log(
      { project_id, brandVariationId },
      'setting the brand variation id'
    )
    if (brandVariationId == null || brandVariationId === '') {
      return callback()
    }
    const conditions = { _id: project_id }
    const update = { brandVariationId }
    return Project.update(conditions, update, {}, function(err) {
      if (err != null) {
        logger.err({ err }, 'error setting brandVariationId')
      }
      return callback()
    })
  },

  unsetBrandVariationId(project_id, callback) {
    if (callback == null) {
      callback = function() {}
    }
    logger.log({ project_id }, 'unsetting the brand variation id')
    const conditions = { _id: project_id }
    const update = { $unset: { brandVariationId: 1 } }
    return Project.update(conditions, update, {}, function(err) {
      if (err != null) {
        logger.warn({ err }, 'error unsetting brandVariationId')
        return callback(err)
      }
      return callback()
    })
  }
}<commit_msg>return errors and clean up ProjectOptionsHandler<commit_after>const { Project } = require('../../models/Project')
const logger = require('logger-sharelatex')
const settings = require('settings-sharelatex')

const safeCompilers = ['xelatex', 'pdflatex', 'latex', 'lualatex']

module.exports = {
  setCompiler(projectId, compiler, callback) {
    logger.log({ projectId, compiler }, 'setting the compiler')
    if (!compiler) {
      return callback()
    }
    compiler = compiler.toLowerCase()
    if (!safeCompilers.includes(compiler)) {
      return callback(new Error(`invalid compiler: ${compiler}`))
    }
    const conditions = { _id: projectId }
    const update = { compiler }
    Project.update(conditions, update, {}, callback)
  },

  setImageName(projectId, imageName, callback) {
    logger.log({ projectId, imageName }, 'setting the imageName')
    if (!imageName || !Array.isArray(settings.allowedImageNames)) {
      return callback()
    }
    imageName = imageName.toLowerCase()
    const isAllowed = settings.allowedImageNames.find(
      allowed => imageName === allowed.imageName
    )
    if (!isAllowed) {
      return callback(new Error(`invalid imageName: ${imageName}`))
    }
    const conditions = { _id: projectId }
    const update = { imageName: settings.imageRoot + '/' + imageName }
    Project.update(conditions, update, {}, callback)
  },

  setSpellCheckLanguage(projectId, languageCode, callback) {
    logger.log({ projectId, languageCode }, 'setting the spell check language')
    if (!Array.isArray(settings.languages)) {
      return callback()
    }
    const language = settings.languages.find(
      language => language.code === languageCode
    )
    if (languageCode && !language) {
      return callback(new Error(`invalid languageCode: ${languageCode}`))
    }
    const conditions = { _id: projectId }
    const update = { spellCheckLanguage: languageCode }
    Project.update(conditions, update, {}, callback)
  },

  setBrandVariationId(projectId, brandVariationId, callback) {
    logger.log(
      { projectId, brandVariationId },
      'setting the brand variation id'
    )
    if (!brandVariationId) {
      return callback()
    }
    const conditions = { _id: projectId }
    const update = { brandVariationId }
    Project.update(conditions, update, {}, callback)
  },

  unsetBrandVariationId(projectId, callback) {
    logger.log({ projectId }, 'unsetting the brand variation id')
    const conditions = { _id: projectId }
    const update = { $unset: { brandVariationId: 1 } }
    Project.update(conditions, update, {}, callback)
  }
}
{'fraction_non_alphanumeric': 0.12831372549019607, 'fraction_numerical': 0.0012549019607843138, 'mean_word_length': 3.1188630490956073, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 1, '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': '1888891', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1816, 'n_words': 456}