<commit_before>KodingError = require '../error'
{ Model, secure, signature } = require 'bongo'

module.exports = class JSession extends Model

  { v4: createId } = require 'node-uuid'

  @share()

  @set
    indexes             :
      clientId          : 'unique'
      otaToken          : 'sparse' # unique is also required
      username          : 'descending'
      clientIP          : 'sparse'
    schema              :
      clientId          : String
      clientIP          : String
      username          : String
      otaToken          : String
      groupName         :
        type            : String
        default         : -> 'koding'
      guestId           : Number
      terminalId        : String
      sessionBegan      :
        type            : Date
        default         : -> new Date
      guestSessionBegan : Date
      lastAccess        :
        type            : Date
        default         : -> new Date
      foreignAuth       :
        gitlab          : Object
        github          : Object
        facebook        : Object
        linkedin        : Object
      returnUrl         : String
      foreignAuthType   : String
      impersonating     : Boolean
    sharedEvents        :
      instance          : []
      static            : []
    sharedMethods       :
      instance          :
        remove: (signature Function)

  do ->
    JAccount  = require './account'

    JAccount.on 'UsernameChanged', ({ oldUsername, mustReauthenticate }) ->
      if mustReauthenticate
        console.log "JAccount.on 'UsernameChanged' JSession#remove", {
          oldUsername, mustReauthenticate }

        JSession.remove { username: oldUsername }, (err) ->
          console.error err  if err?


  # TODO not sure why we are creating session only for guest user
  @createSession = (callback) ->

    JUser    = require './user'
    clientId = createId()

    JUser.fetchGuestUser (err, resp) =>

      return @emit 'error', err  if err

      unless resp
        console.error message = 'Failed to create guest user :/ ~ This is critical!'
        return @emit 'error', { message }

      { account } = resp

      sessionOptions =
        clientId          : clientId
        username          : JUser.createGuestUsername()
        guestSessionBegan : new Date()

      session = new JSession sessionOptions
<commit_msg>JSession: createSession: allow creating session for a given group<commit_after>KodingError = require '../error'
{ Model, secure, signature } = require 'bongo'

module.exports = class JSession extends Model

  { v4: createId } = require 'node-uuid'

  @share()

  @set
    indexes             :
      clientId          : 'unique'
      otaToken          : 'sparse' # unique is also required
      username          : 'descending'
      clientIP          : 'sparse'
    schema              :
      clientId          : String
      clientIP          : String
      username          : String
      otaToken          : String
      groupName         :
        type            : String
        default         : -> 'koding'
      guestId           : Number
      terminalId        : String
      sessionBegan      :
        type            : Date
        default         : -> new Date
      guestSessionBegan : Date
      lastAccess        :
        type            : Date
        default         : -> new Date
      foreignAuth       :
        gitlab          : Object
        github          : Object
        facebook        : Object
        linkedin        : Object
      returnUrl         : String
      foreignAuthType   : String
      impersonating     : Boolean
    sharedEvents        :
      instance          : []
      static            : []
    sharedMethods       :
      instance          :
        remove: (signature Function)

  do ->
    JAccount  = require './account'

    JAccount.on 'UsernameChanged', ({ oldUsername, mustReauthenticate }) ->
      if mustReauthenticate
        console.log "JAccount.on 'UsernameChanged' JSession#remove", {
          oldUsername, mustReauthenticate }

        JSession.remove { username: oldUsername }, (err) ->
          console.error err  if err?


  # TODO not sure why we are creating session only for guest user
  @createSession = (options, callback) ->

    [ options, callback ] = [ callback, options ]  unless callback
    { group } = options ?= {}
    group    ?= 'koding'

    JUser    = require './user'
    clientId = createId()

    JUser.fetchGuestUser (err, resp) =>

      return @emit 'error', err  if err

      unless resp
        console.error message = 'Failed to create guest user :/ ~ This is critical!'
        return @emit 'error', { message }

      { account } = resp

      sessionOptions = {
        username          : JUser.createGuestUsername()
        groupName         : group
        guestSessionBegan : new Date()
        clientId
      }

      session = new JSession sessionOptions

{'fraction_non_alphanumeric': 0.07762464531820025, 'fraction_numerical': 0.00040535062829347385, 'mean_word_length': 1.4601196410767696, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '6952958', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1217, 'n_words': 358}