<commit_before>jraphical   = require "jraphical"
KodingError = require "../../error"

module.exports = class JProxyFilter extends jraphical.Module

  {secure, ObjectId, signature} = require "bongo"

  @trait __dirname, "../../traits/protected"

  @share()

  @set
    sharedEvents      :
      static          : []
      instance        : []
    schema            :
      name            :
        type          : String
        required      : yes
      enabled         : Boolean
        defaultValue  : yes
      rules           : Array
      owner           : ObjectId
      createdAt       :
        type          : Date
        default       : -> new Date
      modifiedAt      :
        type          : Date
        default       : -> new Date
    sharedMethods     :
      static          :
        create        : (signature Object, Function)
        fetch         : (signature Object, Function)

  @create: secure (client, data, callback = noop) ->
    {delegate}    = client.connection
    {name, rules} = data
    ruleTypes     = [ "ip", "country", "request.minute", "request.second" ]
    actionTypes   = [ "allow", "block", "securepage" ]

    unless name and rules?.length
      return callback new KodingError { message: "Missing arguments" }

    for rule in rules
      {enabled, type, match, action} = rule
      hasAllFields       = enabled and type and match and action
      hasValidRuleType   = ruleTypes.indexOf(type)     isnt -1
      hasValidActionType = actionTypes.indexOf(action) isnt -1

      unless hasAllFields and hasValidRuleType and hasValidActionType
        hasInvalidRule = yes

    if hasInvalidRule
      return callback new KodingError { message: "One or more rules are invalid" }

    data.owner = delegate.getId()
    filter     = new JProxyFilter data

    filter.save (err) ->
      return callback err, null  if err
      callback null, filter

  @fetch: secure (client, query, callback = noop) ->
    query.owner = client.connection.delegate.getId()

    JProxyFilter.some query, {}, (err, filters) ->
      return callback err, null  if err
      callback null, filters
<commit_msg>JProxyFilter: Added remove static method.<commit_after>jraphical   = require "jraphical"
KodingError = require "../../error"

module.exports = class JProxyFilter extends jraphical.Module

  {secure, ObjectId, signature} = require "bongo"

  @trait __dirname, "../../traits/protected"

  @share()

  @set
    sharedEvents      :
      static          : []
      instance        : []
    schema            :
      name            :
        type          : String
        required      : yes
      enabled         : Boolean
        defaultValue  : yes
      rules           : Array
      owner           : ObjectId
      createdAt       :
        type          : Date
        default       : -> new Date
      modifiedAt      :
        type          : Date
        default       : -> new Date
    sharedMethods     :
      static          :
        create        : (signature Object, Function)
        fetch         : (signature Object, Function)
      instance        :
        remove        : (signature Function)

  @create: secure (client, data, callback = noop) ->
    {delegate}    = client.connection
    {name, rules} = data
    ruleTypes     = [ "ip", "country", "request.minute", "request.second" ]
    actionTypes   = [ "allow", "block", "securepage" ]

    unless name and rules?.length
      return callback new KodingError { message: "Missing arguments" }

    for rule in rules
      {enabled, type, match, action} = rule
      hasAllFields       = enabled and type and match and action
      hasValidRuleType   = ruleTypes.indexOf(type)     isnt -1
      hasValidActionType = actionTypes.indexOf(action) isnt -1

      unless hasAllFields and hasValidRuleType and hasValidActionType
        hasInvalidRule = yes

    if hasInvalidRule
      return callback new KodingError { message: "One or more rules are invalid" }

    data.owner = delegate.getId()
    filter     = new JProxyFilter data

    filter.save (err) ->
      return callback err, null  if err
      callback null, filter

  @fetch: secure (client, query, callback = noop) ->
    query.owner = client.connection.delegate.getId()

    JProxyFilter.some query, {}, (err, filters) ->
      return callback err, null  if err
      callback null, filters

  remove$: secure (client, callback = noop) ->
    {delegate} = client.connection
    if @owner.toString() is delegate.getId().toString()
      @remove (err) -> callback err

{'fraction_non_alphanumeric': 0.0911297440423654, 'fraction_numerical': 0.00088261253309797, 'mean_word_length': 1.9094993581514763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '2397629', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1182, 'n_words': 373}