<commit_before>{ secure, signature, Model, JsPath:{ getAt } } = require 'bongo'

module.exports = class JGroupData extends Model
  JPermissionSet = require './permissionset'
  { permit }     = JPermissionSet

  @share()

  @set
    indexes       :
      slug        : 'unique'

    sharedEvents  :
      static      : []
      instance    : []

    schema        :
      slug        :
        type      : String
        validate  : require('../name').validateName
        set       : (value) -> value.toLowerCase()
      payload     : Object

    sharedMethods :
      static      :
        fetchByKey: [
          (signature String, Function)
        ]

  @create = (slug, callback) ->
    data = new JGroupData { slug }
    data.save (err) ->
      # this happens if socialapi creates the document in between fetch/create
      # operations.
      if err?.code is 11000 # duplicate key error
        return JGroupData.fetchData slug, callback

      return callback err  if err
      callback null, data

  @fetchData: (slug, callback) ->
    return callback new Error 'slug is required' unless slug

    JGroupData.one { slug }, (err, data) ->
      return callback err  if err
      return callback null, data if data

      JGroupData.create slug, callback

  # fetchByKey fetches given path from GroupData if only they are available
  # within the group.
  #
  # @param {String} path
  #   Provide the path you would like to fetch.
  #
  # @return {Object} persisted value.
  @fetchByKey = secure (client, path, callback) ->
    # for more granular control, specify each sub key as well.
    availableKeys = [ 'countly' ]

    if availableKeys.indexOf(path) is -1
      return callback new Error 'path is forbidden'

    slug = client.context.group
    JGroupData.fetchDataAt slug, path, callback


  @fetchDataAt: (slug, path, callback) ->
    return callback new Error 'slug is required' unless slug

    opts = {}
    opts["payload.#{path}"] = 1 if path

    JGroupData.one { slug }, opts, (err, data) ->
      return callback err  if err

      payload = if data then getAt data.payload, path else null
      return callback null, payload

  # see docs on JGroup::modifyData
  modifyData : (slug, data, callback) ->
    return callback new Error 'slug is required' unless slug

    # it's only allowed to change followings
    whitelist  = ['github.organizationToken', 'test_key__' ]

    # handle $set and $unset cases in one<commit_msg>workers: check if client.context is set<commit_after>{ secure, signature, Model, JsPath:{ getAt } } = require 'bongo'

module.exports = class JGroupData extends Model
  JPermissionSet = require './permissionset'
  { permit }     = JPermissionSet

  @share()

  @set
    indexes       :
      slug        : 'unique'

    sharedEvents  :
      static      : []
      instance    : []

    schema        :
      slug        :
        type      : String
        validate  : require('../name').validateName
        set       : (value) -> value.toLowerCase()
      payload     : Object

    sharedMethods :
      static      :
        fetchByKey: [
          (signature String, Function)
        ]

  @create = (slug, callback) ->
    data = new JGroupData { slug }
    data.save (err) ->
      # this happens if socialapi creates the document in between fetch/create
      # operations.
      if err?.code is 11000 # duplicate key error
        return JGroupData.fetchData slug, callback

      return callback err  if err
      callback null, data

  @fetchData: (slug, callback) ->
    return callback new Error 'slug is required' unless slug

    JGroupData.one { slug }, (err, data) ->
      return callback err  if err
      return callback null, data if data

      JGroupData.create slug, callback

  # fetchByKey fetches given path from GroupData if only they are available
  # within the group.
  #
  # @param {String} path
  #   Provide the path you would like to fetch.
  #
  # @return {Object} persisted value.
  @fetchByKey = secure (client, path, callback) ->
    # for more granular control, specify each sub key as well.
    availableKeys = [ 'countly' ]

    unless path in availableKeys
      return callback new Error 'path is forbidden'

    slug = client?.context?.group
    JGroupData.fetchDataAt slug, path, callback


  @fetchDataAt: (slug, path, callback) ->
    return callback new Error 'slug is required' unless slug

    opts = {}
    opts["payload.#{path}"] = 1 if path

    JGroupData.one { slug }, opts, (err, data) ->
      return callback err  if err

      payload = if data then getAt data.payload, path else null
      return callback null, payload

  # see docs on JGroup::modifyData
  modifyData : (slug, data, callback) ->
    return callback new Error 'slug is required' unless slug

    # it's only allowed to change followings
    whitelist  = ['github.organizationToken', 'test_key__' ]

    # handle $set and $unset cases in one
{'fraction_non_alphanumeric': 0.08950366151342555, 'fraction_numerical': 0.0026444263628966637, 'mean_word_length': 2.374742621825669, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '4260730', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1370, 'n_words': 502}