 fetchAccount = (username, callback)->
   bongo.models.JAccount.one {"profile.nickname" : username }, callback
 
-
-generateFakeClient = (req, res, callback)->
+generateFakeClientFromReq = (req, res, callback)->
 
   fakeClient    =
     context     :

      groupName : 'koding'

  {clientId} = req.cookies

  # if client id is not set, check for pendingCookies
  if not clientId and req.pendingCookies?.clientId
    clientId = req.pendingCookies.clientId

  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


        return callback null, fakeClient

module.exports = { generateFakeClient: generateFakeClientFromReq }

