<commit_before>{Model} = require 'bongo'

class JToken extends Model

  @setSchema
    token       : String
    expires     : Date
    authority   : String
    requester   : String

module.exports = class JSession extends Model

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

  @set
    indexes         :
      clientId      : 'unique'
      username      : 'descending'
      clientIP      : 'sparse'
    schema          :
      clientId      : String
      clientIP      : String
      username      : String
      guestId       : Number
      terminalId    : String
      sessionBegan  :
        type        : Date
        default     : -> new Date
      lastAccess    :
        type        : Date
        get         : -> new Date
      foreignAuth   :
        github      : Object
        odesk       : Object
        facebook    : Object
        linkedin    : Object
      foreignAuthType : String
      impersonating : Boolean
    sharedEvents    :
      instance      : []
      static        : []

  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
      username  = JUser.createGuestUsername()
      session   = new JSession { clientId, username }

      session.save (err)->
        if err then callback err
        else callback null, { session, account }


  @fetchSession = (clientId, callback)->

    return @createSession callback  unless clientId

    @one {clientId}, (err, session)=>
      if err
        callback err<commit_msg>session: add createNewSession method<commit_after>{Model} = require 'bongo'

class JToken extends Model

  @setSchema
    token       : String
    expires     : Date
    authority   : String
    requester   : String

module.exports = class JSession extends Model

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

  @set
    indexes         :
      clientId      : 'unique'
      username      : 'descending'
      clientIP      : 'sparse'
    schema          :
      clientId      : String
      clientIP      : String
      username      : String
      guestId       : Number
      terminalId    : String
      sessionBegan  :
        type        : Date
        default     : -> new Date
      lastAccess    :
        type        : Date
        get         : -> new Date
      foreignAuth   :
        github      : Object
        odesk       : Object
        facebook    : Object
        linkedin    : Object
      foreignAuthType : String
      impersonating : Boolean
    sharedEvents    :
      instance      : []
      static        : []

  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
      username  = JUser.createGuestUsername()
      session   = new JSession { clientId, username }

      session.save (err)->
        if err then callback err
        else callback null, { session, account }


  @createNewSession = (username, callback) ->
    clientId = createId()

    session = new JSession { clientId, username }
    session.save (err) ->
      return callback err  if err
      return callback null, session

  @fetchSession = (clientId, callback)->

    return @createSession callback  unless clientId

    @one {clientId}, (err, session)=>
      if err
        callback err
{'fraction_non_alphanumeric': 0.08229098090849243, 'fraction_numerical': 0.00043888523151195964, 'mean_word_length': 1.831055900621118, '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': '4325174', 'n_tokens_mistral': 1253, 'n_tokens_neox': 1200, 'n_words': 377}