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

module.exports = class JApp extends jraphical.Module

  JAccount  = require './account'
  JReview   = require './messages/review'
  JTag      = require './tag'

  @trait __dirname, '../traits/filterable'       # brings only static methods
  @trait __dirname, '../traits/followable'
  @trait __dirname, '../traits/taggable'
  @trait __dirname, '../traits/likeable'
  @trait __dirname, '../traits/slugifiable'
  @trait __dirname, '../traits/protected'

  {permit}   = require './group/permissionset'
  Validators = require './group/validators'

  #
  {Inflector,JsPath,secure,daisy,ObjectId,ObjectRef} = require 'bongo'
  {Relationship} = jraphical

  @share()

  @set

    softDelete          : yes
    # slugifyFrom         : 'name'
    # slugTemplate        : -> "#{@authorNick}/\#{slug}"

    indexes             :
      slug              : 'unique'
      name              : 'sparse'

    sharedEvents        :
      instance          : [
        { name: 'ReviewIsAdded' }
      ]

    sharedMethods       :
      instance          : [
        'approve', 'fetchRelativeReviews'
        # 'approve', 'review', 'delete', 'install'
        # 'follow', 'unfollow', 'delete', 'review',
        # 'like', 'checkIfLikedBefore', 'fetchLikedByes',
        # 'fetchFollowersWithRelationship', 'install',
        # 'fetchFollowingWithRelationship', 'fetchCreator',
        # 'fetchRelativeReviews', 'approve'
      ]
      static            : [
        'create', 'someWithRelationship', 'one'
        # , 'someWithRelationship', 'updateAllSlugs'
        # 'some', 'each', 'fetchAllAppsData'
      ]

    permissions         :

      'list apps'       : ['member']
      'create apps'     : ['member']
      'update own apps' : ['member']
      'delete own apps' : ['member']
      'list reviews'    : ['member']
      'create review'   : ['member']
      'approve apps'    : []
      'delete apps'     : []
      'update apps'     : []
      'list all apps'   : []

    schema              :

      authorNick        : String
      identifier        :
        type            : String
        set             : (value)-> value?.trim()
        required        : yes
      name              :
        type            : String
        set             : (value)-> value?.trim()
        required        : yes
      url               :
        type            : String
        set             : (value)-> value?.trim()
        required        : yes
      counts            :
        followers       :
          type          : Number
          default       : 0
        installed       :
          type          : Number
          default       : 0
      versions          : [String]

      meta              : require "bongo/bundles/meta"
      manifest          : Object

      type              :
        type            : String
        enum            : ["Wrong type specified!",["web-app", "add-on", "server-stack", "framework"]]
        default         : "web-app"

      approved          :
        type            : Boolean
        default         : false

      repliesCount      :
        type            : Number
        default         : 0

      slug              :
        type            : String
        default         : (value)-> Inflector.slugify @name.toLowerCase()
      slug_             : String

    relationships       :
      creator           :
        targetType      : JAccount
        as              : "creator"
      review            :
        targetType      : JReview
        as              : "review"
      follower          :
        targetType      : JAccount
        as              : 'follower'
      likedBy           :
        targetType      : JAccount
        as              : 'like'
      participant       :
        targetType      : JAccount
        as              : ['author','reviewer','user']
      tag               :
        targetType      : JTag
        as              : 'tag'

  @getAuthorType =-> JAccount

  @create = permit 'create apps',

    success: (client, data, callback)->

      console.log "creating the JApp"

      {connection:{delegate}} = client
      {profile} = delegate

      if not data.name or not data.url
        return callback new KodingError 'Name and Url is required!'

      data.manifest           ?= {}

      # Overwrite the user information in manifest
      data.manifest.authorNick = profile.nickname
      data.manifest.author     = "#{profile.firstName} #{profile.lastName}"

      # Optionals
      data.manifest.version   ?= "1.0"
      data.identifier         ?= "com.koding.apps.#{data.name}"

      app           = new JApp
        url         : data.url
        name        : data.name
        type        : data.type or 'web-app'
        manifest    : data.manifest
        identifier  : data.identifier
        version     : data.manifest.version
        authorNick  : data.manifest.authorNick

      app.save (err)->
        return callback err  if err
        app.addCreator delegate, (err)->
          return callback err  if err<commit_msg>JApp: Create slug manually since the current workflow of slugifable is not fit with JApp<commit_after>jraphical   = require 'jraphical'
KodingError = require '../error'

module.exports = class JApp extends jraphical.Module

  JAccount  = require './account'
  JReview   = require './messages/review'
  JTag      = require './tag'

  @trait __dirname, '../traits/filterable'       # brings only static methods
  @trait __dirname, '../traits/followable'
  @trait __dirname, '../traits/taggable'
  @trait __dirname, '../traits/likeable'
  @trait __dirname, '../traits/slugifiable'
  @trait __dirname, '../traits/protected'

  {permit}   = require './group/permissionset'
  Validators = require './group/validators'

  #
  {Inflector,JsPath,secure,daisy,ObjectId,ObjectRef} = require 'bongo'
  {Relationship} = jraphical

  @share()

  @set

    softDelete          : yes

    indexes             :
      slug              : 'unique'
      name              : 'sparse'

    sharedEvents        :
      instance          : [
        { name: 'ReviewIsAdded' }
      ]

    sharedMethods       :
      instance          : [
        'approve', 'fetchRelativeReviews'
        # 'approve', 'review', 'delete', 'install'
        # 'follow', 'unfollow', 'delete', 'review',
        # 'like', 'checkIfLikedBefore', 'fetchLikedByes',
        # 'fetchFollowersWithRelationship', 'install',
        # 'fetchFollowingWithRelationship', 'fetchCreator',
        # 'fetchRelativeReviews', 'approve'
      ]
      static            : [
        'create', 'someWithRelationship', 'one'
        # , 'someWithRelationship', 'updateAllSlugs'
        # 'some', 'each', 'fetchAllAppsData'
      ]

    permissions         :

      'list apps'       : ['member']
      'create apps'     : ['member']
      'update own apps' : ['member']
      'delete own apps' : ['member']
      'list reviews'    : ['member']
      'create review'   : ['member']
      'approve apps'    : []
      'delete apps'     : []
      'update apps'     : []
      'list all apps'   : []

    schema              :

      authorNick        : String
      identifier        :
        type            : String
        set             : (value)-> value?.trim()
        required        : yes
      name              :
        type            : String
        set             : (value)-> value?.trim()
        required        : yes
      url               :
        type            : String
        set             : (value)-> value?.trim()
        required        : yes
      counts            :
        followers       :
          type          : Number
          default       : 0
        installed       :
          type          : Number
          default       : 0
      versions          : [String]

      meta              : require "bongo/bundles/meta"
      manifest          : Object

      type              :
        type            : String
        enum            : ["Wrong type specified!",["web-app", "add-on", "server-stack", "framework"]]
        default         : "web-app"

      approved          :
        type            : Boolean
        default         : false

      repliesCount      :
        type            : Number
        default         : 0

      slug              :
        type            : String
        default         : (value)-> Inflector.slugify @name.toLowerCase()
      slug_             : String

    relationships       :
      creator           :
        targetType      : JAccount
        as              : "creator"
      review            :
        targetType      : JReview
        as              : "review"
      follower          :
        targetType      : JAccount
        as              : 'follower'
      likedBy           :
        targetType      : JAccount
        as              : 'like'
      participant       :
        targetType      : JAccount
        as              : ['author','reviewer','user']
      tag               :
        targetType      : JTag
        as              : 'tag'

  @getAuthorType =-> JAccount

  @create = permit 'create apps',

    success: (client, data, callback)->

      console.log "creating the JApp"

      {connection:{delegate}} = client
      {profile} = delegate

      if not data.name or not data.url
        return callback new KodingError 'Name and Url is required!'

      data.manifest           ?= {}

      # Overwrite the user information in manifest
      data.manifest.authorNick = profile.nickname
      data.manifest.author     = "#{profile.firstName} #{profile.lastName}"

      # Optionals
      data.manifest.version   ?= "1.0"
      data.identifier         ?= "com.koding.apps.#{data.name}"

      app           = new JApp
        url         : data.url
        name        : data.name
        type        : data.type or 'web-app'
        manifest    : data.manifest
        identifier  : data.identifier
        version     : data.manifest.version
        authorNick  : data.manifest.authorNick

      app.save (err)->
        return callback err  if err
        slug = "Apps/#{app.manifest.authorNick}/#{app.name}"
        app.useSlug slug, (err, slugobj)->
          if err then return app.remove -> callback err
          slug = slugobj.slug
          app.update {$set: {slug, slug_: slug}}, (err)->
            console.warn "Slug update failed for #{slug}", err  if err
          app.addCreator delegate, (err)->
            return callback err  if err
            callback null, app
          return callback err  if err
{'fraction_non_alphanumeric': 0.10649326170954669, 'fraction_numerical': 0.0009424182452172274, 'mean_word_length': 1.349867139061116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '2001667', 'n_tokens_mistral': 2893, 'n_tokens_neox': 2813, 'n_words': 689}