<commit_before>bongo = require './bongo'

handleError = (err, callback) ->
  console.error err
  return callback err


fetchGroupName = (req, callback)->
  [name, section] = req.params
  {JName} = bongo.models

  groupName = ""
  # this means it is not a group or profile feed
  # and it means it is koding group
  # it is like Activity -- Develop

  if not name or name[0].toUpperCase() is name[0]
    return callback null, "koding"
  else
    JName.fetchModels "#{name}/#{section}", (err, models)->
      return callback if err
      return callback new Error "JName is not found #{name}/#{section}" if not models and model.length < 1

      model = models.first
      modelName = model.bongo_.constructorName
      if modelName is 'JGroup'
        groupName = model.slug
      else
        groupName = "koding"

      callback null, groupName

fetchAccount = (username, callback)->
  bongo.models.JAccount.one {"profile.nickname" : username }, callback


generateFakeClient = (req, res, callback)->

  fakeClient = {}
  {clientId} = req.cookies
  return callback null, {} unless clientId?

  bongo.models.JSession.fetchSession clientId, (err, session)->
    return handleError err, callback if err
    return handleError new Error "Session is not set", callback unless session?

    fetchGroupName req, (err, groupName)->
      return handleError err, callback if err
      fetchAccount session.username, (err, account)->
        return handleError err, callback if err

        # set real client id if it is in the db
        fakeClient.sessionToken = session.clientId

        # set username into context
        fakeClient.context = {}
        fakeClient.context.group = groupName
        fakeClient.context.user  = session.username
<commit_msg>create fakeClient for guests<commit_after>bongo = require './bongo'

handleError = (err, callback) ->
  console.error err
  return callback err


fetchGroupName = (req, callback)->
  [name, section] = req.params
  {JName} = bongo.models

  groupName = ""
  # this means it is not a group or profile feed
  # and it means it is koding group
  # it is like Activity -- Develop

  if not name or name[0].toUpperCase() is name[0]
    return callback null, "koding"
  else
    JName.fetchModels "#{name}/#{section}", (err, models)->
      return callback if err
      return callback new Error "JName is not found #{name}/#{section}" if not models and model.length < 1

      model = models.first
      modelName = model.bongo_.constructorName
      if modelName is 'JGroup'
        groupName = model.slug
      else
        groupName = "koding"

      callback null, groupName

fetchAccount = (username, callback)->
  bongo.models.JAccount.one {"profile.nickname" : username }, callback


generateFakeClient = (req, res, callback)->

  fakeClient    =
    context     :
      group     : 'koding'
      user      : 'guest-1'
    connection  :
      delegate  : null
      groupName : 'koding'

  {clientId} = req.cookies

  return callback null, fakeClient unless clientId?

  bongo.models.JSession.fetchSession clientId, (err, session)->
    return handleError err, callback if err
    return handleError new Error "Session is not set", callback unless session?

    fetchGroupName req, (err, groupName)->
      return handleError err, callback if err
      fetchAccount session.username, (err, account)->
        return handleError err, callback if err

        # set real client id if it is in the db
        fakeClient.sessionToken = session.clientId

        # set username into context
        fakeClient.context = {}
        fakeClient.context.group = groupName
        fakeClient.context.user  = session.username

{'fraction_non_alphanumeric': 0.09351145038167939, 'fraction_numerical': 0.0019083969465648854, 'mean_word_length': 2.8621052631578947, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4646078', 'n_tokens_mistral': 1033, 'n_tokens_neox': 1012, 'n_words': 362}