<commit_before>
const BCRYPT_ENCRYPTION_ROUNDS = 12

import { User, Rat, db} from '../db'
import bcrypt from 'bcrypt'
import NickServ from '../Anope/NickServ'
import HostServ from '../Anope/HostServ'
import BotServ from '../Anope/BotServ'
import UserQuery from '../query/UserQuery'
import API, {
  POST,
  required
} from '../classes/API'
import Users from './Users'
import { ConflictAPIError,UnprocessableEntityAPIError } from '../classes/APIError'

const platforms = ['pc', 'xb', 'ps']

export default class Register extends API {
  @POST('/register')
  @required('email', 'password', 'name', 'platform', 'nickname')
  async create (ctx) {
    // let captcha = ctx.data['g-recaptcha-response']
    // let captchaResult = await new Request(POST, {
    //   host: 'www.google.com',
    //   path: '/recaptcha/api/siteverify'
    // }, {
    //   secret: config.recaptcha.secret,
    //   response: captcha,
    //   remoteip: ctx.inet
    // })
    //
    // if (captchaResult.body.success === false) {
    //   throw Errors.template('invalid_parameter', 'g-recaptcha-response')
    // }

    let { email, name, nickname, password, IRCpassword } = ctx.data

    let existingUser = await User.findOne({
      where: {
        email: {
          $iLike: email
        }
      }
    })
    if (existingUser) {
      throw ConflictAPIError({
        pointer: '/data/attributes/email'
      })
    }

    let transaction = await db.transaction()

    try {
      let encryptedPassword = await bcrypt.hash(password, BCRYPT_ENCRYPTION_ROUNDS)

      let user = await User.create({
        email: email,
        password: <PASSWORD>
      }, {
        transaction: transaction
      })

      name = name.replace(/CMDR/i, '')
      let { platform } = ctx.data
      if (platforms.includes(platform) === false) {
        // noinspection ExceptionCaughtLocallyJS
        throw UnprocessableEntityAPIError({
          pointer: '/data/attributes/platform'
        })
      }

      await Rat.create({
        name: name,
        platform: platform,
        userId: user.id
      }, {
        transaction: transaction
      })

      nickname = nickname.replace(/\[.*]/i, '')

      if (!IRCpassword) {
        IRCpassword = password
      }
      await NickServ.register(nickname, IRCpassword, email)

      await User.update({ nicknames: db.cast([nickname], 'citext[]') }, {
        where: { id: user.id }
      })

      await transaction.commit()

      let userQuery = new UserQuery({ id: user.id }, ctx)
      let result = await User.scope('public').findAndCountAll(userQuery.toSequelize)
      await HostServ.update(user[0])
      process.emit('registration', ctx, ctx.data)

      ctx.body = Users.presenter.render(result.rows, API.meta(result, userQuery))<commit_msg>Chagne capitalisation of ircPassword because code style<commit_after>
const BCRYPT_ENCRYPTION_ROUNDS = 12

import { User, Rat, db} from '../db'
import bcrypt from 'bcrypt'
import NickServ from '../Anope/NickServ'
import HostServ from '../Anope/HostServ'
import BotServ from '../Anope/BotServ'
import UserQuery from '../query/UserQuery'
import API, {
  POST,
  required
} from '../classes/API'
import Users from './Users'
import { ConflictAPIError,UnprocessableEntityAPIError } from '../classes/APIError'

const platforms = ['pc', 'xb', 'ps']

export default class Register extends API {
  @POST('/register')
  @required('email', 'password', 'name', 'platform', 'nickname')
  async create (ctx) {
    // let captcha = ctx.data['g-recaptcha-response']
    // let captchaResult = await new Request(POST, {
    //   host: 'www.google.com',
    //   path: '/recaptcha/api/siteverify'
    // }, {
    //   secret: config.recaptcha.secret,
    //   response: captcha,
    //   remoteip: ctx.inet
    // })
    //
    // if (captchaResult.body.success === false) {
    //   throw Errors.template('invalid_parameter', 'g-recaptcha-response')
    // }

    let { email, name, nickname, password, ircPassword } = ctx.data

    let existingUser = await User.findOne({
      where: {
        email: {
          $iLike: email
        }
      }
    })
    if (existingUser) {
      throw ConflictAPIError({
        pointer: '/data/attributes/email'
      })
    }

    let transaction = await db.transaction()

    try {
      let encryptedPassword = await bcrypt.hash(password, BCRYPT_ENCRYPTION_ROUNDS)

      let user = await User.create({
        email: email,
        password: encryptedPassword
      }, {
        transaction: transaction
      })

      name = name.replace(/CMDR/i, '')
      let { platform } = ctx.data
      if (platforms.includes(platform) === false) {
        // noinspection ExceptionCaughtLocallyJS
        throw UnprocessableEntityAPIError({
          pointer: '/data/attributes/platform'
        })
      }

      await Rat.create({
        name: name,
        platform: platform,
        userId: user.id
      }, {
        transaction: transaction
      })

      nickname = nickname.replace(/\[.*]/i, '')

      if (!ircPassword) {
        ircPassword = password
      }
      await NickServ.register(nickname, ircPassword, email)

      await User.update({ nicknames: db.cast([nickname], 'citext[]') }, {
        where: { id: user.id }
      })

      await transaction.commit()

      let userQuery = new UserQuery({ id: user.id }, ctx)
      let result = await User.scope('public').findAndCountAll(userQuery.toSequelize)
      await HostServ.update(user[0])
      process.emit('registration', ctx, ctx.data)

      ctx.body = Users.presenter.render(result.rows, API.meta(result, userQuery))
{'fraction_non_alphanumeric': 0.1388340807174888, 'fraction_numerical': 0.0010762331838565023, 'mean_word_length': 2.4569125852448854, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3912570', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1658, 'n_words': 391}