<commit_before>{ Base, secure, signature } = require 'bongo'

{ Kite } = require 'kite.js'
SockJs   = require 'node-sockjs-client'
KONFIG   = require 'koding-config-manager'

# Kloud wrapper on social backend
#
module.exports = class Kloud extends Base

  @trait __dirname, '../../traits/protected'
  { permit } = require '../group/permissionset'

  @share()

  @set
    permissions    :
      'kite ping'  : ['member']
    sharedMethods  :
      static       :
        ping       : (signature Function)

  TIMEOUT = 10000

  getArgs = (client, args) ->

    args[0] ?= {}
    args[0].impersonate = client.connection.delegate.profile.nickname
    return args


  # calls kite.ping on Kloud on behalf of loggedin user
  #
  @ping  = (client, callback) ->
    @tell client, 'kite.ping', [], callback

  @ping$ = permit 'kite ping', { success: @ping }


  @tell = (client, method, args = [], callback) ->

    @transport
      .tell method, getArgs client, args
      .then  (res) ->
        callback null, res
        return res
      .timeout TIMEOUT
      .catch (err) ->
        callback err


  do ->

    options               =
      url                 : "http://localhost:#{KONFIG.publicPort}/kloud/kite"
      autoConnect         : yes
      autoReconnect       : yes
      transportClass      : SockJs
      auth                :<commit_msg>Kloud: tell: better payload handling<commit_after>{ Base, secure, signature } = require 'bongo'

{ Kite } = require 'kite.js'
SockJs   = require 'node-sockjs-client'
KONFIG   = require 'koding-config-manager'

# Kloud wrapper on social backend
#
module.exports = class Kloud extends Base

  @trait __dirname, '../../traits/protected'
  { permit } = require '../group/permissionset'

  @share()

  @set
    permissions    :
      'kite ping'  : ['member']
    sharedMethods  :
      static       :
        ping       : (signature Function)

  TIMEOUT = 10000


  getPayload = (client, args) ->

    args   ?= []
    payload = args[0] ? {}

    payload.impersonate = client.connection.delegate.profile.nickname
    payload.groupName   = client.context.group

    { provider, machineId, stackId } = payload

    if not provider and (machineId or stackId)
      return [ new KodingError 'Provider is required' ]

    return [ null, [ payload ] ]


  # calls kite.ping on Kloud on behalf of loggedin user
  #
  @ping  = (client, callback) ->
    @tell client, 'kite.ping', [], callback

  @ping$ = permit 'kite ping', { success: @ping }


  @tell = (client, method, args, callback) ->

    [ err, payload ] = getPayload client, args
    return callback err  if err

    @transport
      .tell method, payload
      .then  (res) ->
        callback null, res
      .timeout TIMEOUT
      .catch (err) ->
        callback err


  do ->

    options               =
      url                 : "http://localhost:#{KONFIG.publicPort}/kloud/kite"
      autoConnect         : yes
      autoReconnect       : yes
      transportClass      : SockJs
      auth                :
{'fraction_non_alphanumeric': 0.11786188579017265, 'fraction_numerical': 0.004316069057104913, 'mean_word_length': 2.0651068158697865, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '4640212', 'n_tokens_mistral': 950, 'n_tokens_neox': 890, 'n_words': 250}