<commit_before>class JInvitation extends jraphical.Module
  
  crypto = require 'crypto'
  {uniq} = require 'underscore'
  
  @isEnabledGlobally = yes
  
  {ObjectRef, dash, daisy} = bongo
  
  @share()
  
  @set
    indexes         :
      code          : 'unique'
    sharedMethods   :
      static        : ['create','byCode','__grantInvitations']#,'__sendBetaInvites',,'__createBetaInvites']
    schema          :
      code          : String
      inviteeEmail  : String
      customMessage :
        subject     : String
        body        : String
      uses          :
        type        : Number
        default     : 0
      maxUses       :
        type        : Number
        default     : 0
      type          :
        type        : String
        enum        : ['invalid invitation type', ['personal','multiuse','launchrock']]
        default     : 'personal'
      status        : 
        type        : String 
        enum        : ['invalid status type', ['active','blocked','redeemed']]
        default     : 'active' # 'unconfirmed'
      origin        : ObjectRef
    relationships   :
      invitedBy     :
        targetType  : JAccount
        as          : 'inviter'
      redeemer      :
        targetType  : JAccount
        as          : 'redeemer'
  
  @__grantInvitations =(options, callback)->
    [callback, options] = [options, callback] unless callback
    options or= {}
    {numInvitations} = options
    numInvitations ?= 3
    @grant {}, numInvitations, callback
  # @__attemptToFixChrisFuckup =(callback)->
  #   i = 0
  #   JAccount.one {'profile.nickname': 'devrim'}, (err, devrim)=>
  #     if err
  #       callback err
  #     else
  #       @all { 'origin.constructorName': $exists: no }, (err, invites)->
  #         invites.forEach (invite)->
  #           if err
  #             callback err
  #           else
  #             invite.update {$set: origin: ObjectRef devrim}, {multi: yes}, callback
  #             # invite.addInvitedBy devrim, (err)->
  #             #   console.log err
  #             #   console.log 'finished', i++
  #             #   callback 'ok'
  # 
  @__sendBetaInvites =do->
    betaTestersEmails = fs.readFileSync 'invitee-emails.txt', 'utf-8'
    # betaTestersEmails = '<EMAIL>'
    betaTestersHTML   = fs.readFileSync 'email/beta-testers-invite.html', 'utf-8'
    protocol = 'https://'
    (callback)->
      i = 0
      recipients = []
      {host, port} = server
      # host = 'localhost:3000'
      # protocol = 'http://'
      uniq(betaTestersEmails.split '\n').slice(7000, 8000).forEach (email)=>
        recipients.push =>
          @one {inviteeEmail: email}, (err, invite)=>
            if err
              console.log err
            else if invite?
              url = "#{protocol}#{host}/invitation/#{invite.code}"
              # bitly.shorten url, (err, response)=>
              #   shortenedUrl = response.data.url
              #   if shortenedUrl?
                  # shortenedUrl = url
              console.log 'hello there ---<<<'
              personalizedMail = betaTestersHTML.replace '#{url}', url#shortenedUrl
              Emailer.send
                From      : @getInviteEmail()
                To        : email
                Subject   : '[Koding] Here is your beta invite!'
                HtmlBody  : personalizedMail
              , (err)-> 
                console.log 'finished', i++, err
                recipients.fin(err)
                # else console.log email
            else
              console.log "no invitation was found for #{email}"
              recipients.fin null
      dash recipients, callback
        
  
  @__createBetaInvites =do ->
    betaTestersEmails = '<EMAIL>'
    # betaTestersEmails = fs.readFileSync('./invitee-emails.txt', 'utf-8')
    #betaTestersEmails = '<EMAIL>'
    (callback)->
      JAccount.one {'profile.nickname': 'devrim'}, (err, devrim)=>
        i = 0
        recipients = []
        uniq(betaTestersEmails.split '\n').forEach (email)->
          code = crypto
            .createHmac('sha1', 'kodingsecret')
            .update(email)
            .digest('hex')
          # console.log email, code
          recipients.push ->
            invite = new JInvitation {
              code
              inviteeEmail  : email
              maxUses       : 1
              origin        : ObjectRef(devrim)
              type          : 'launchrock'
            }
            invite.save (err)-> 
              recipients.next(err)
        recipients.push -> callback(recipients)
        daisy recipients
  
  @grant =(selector, quota, callback)->
    unless quota > 0
      callback new KodingError "Quota must be positive."
    else
      batch = []
      i = 0
      console.log arguments
      JAccount.all selector, (err, accounts)->
        accounts.forEach (account)->
          batch.push ->
            account.fetchLimit 'invite', (err, limit)->
              if err then batch.fin(err)
              else if limit
                limit.update $inc: {quota}, (err)-> batch.fin(err)
              else
                limit = new JLimit {quota}
                limit.save (err)-><commit_msg>cleanup<commit_after>class JInvitation extends jraphical.Module
  
  crypto = require 'crypto'
  {uniq} = require 'underscore'
  
  @isEnabledGlobally = yes
  
  {ObjectRef, dash, daisy} = bongo
  
  @share()
  
  @set
    indexes         :
      code          : 'unique'
    sharedMethods   :
      static        : ['create','byCode','__grantInvitations']#,'__sendBetaInvites',,'__createBetaInvites']
    schema          :
      code          : String
      inviteeEmail  : String
      customMessage :
        subject     : String
        body        : String
      uses          :
        type        : Number
        default     : 0
      maxUses       :
        type        : Number
        default     : 0
      type          :
        type        : String
        enum        : ['invalid invitation type', ['personal','multiuse','launchrock']]
        default     : 'personal'
      status        : 
        type        : String 
        enum        : ['invalid status type', ['active','blocked','redeemed']]
        default     : 'active'
      origin        : ObjectRef
    relationships   :
      invitedBy     :
        targetType  : JAccount
        as          : 'inviter'
      redeemer      :
        targetType  : JAccount
        as          : 'redeemer'
  
  @__grantInvitations =(options, callback)->
    [callback, options] = [options, callback] unless callback
    options or= {}
    {numInvitations} = options
    numInvitations ?= 3
    @grant {}, numInvitations, callback
  # @__attemptToFixChrisFuckup =(callback)->
  #   i = 0
  #   JAccount.one {'profile.nickname': 'devrim'}, (err, devrim)=>
  #     if err
  #       callback err
  #     else
  #       @all { 'origin.constructorName': $exists: no }, (err, invites)->
  #         invites.forEach (invite)->
  #           if err
  #             callback err
  #           else
  #             invite.update {$set: origin: ObjectRef devrim}, {multi: yes}, callback
  #             # invite.addInvitedBy devrim, (err)->
  #             #   console.log err
  #             #   console.log 'finished', i++
  #             #   callback 'ok'
  # 
  @__sendBetaInvites =do->
    betaTestersEmails = fs.readFileSync 'invitee-emails.txt', 'utf-8'
    # betaTestersEmails = '<EMAIL>'
    betaTestersHTML   = fs.readFileSync 'email/beta-testers-invite.html', 'utf-8'
    protocol = 'https://'
    (callback)->
      i = 0
      recipients = []
      {host, port} = server
      # host = 'localhost:3000'
      # protocol = 'http://'
      uniq(betaTestersEmails.split '\n').slice(7000, 8000).forEach (email)=>
        recipients.push =>
          @one {inviteeEmail: email}, (err, invite)=>
            if err
              console.log err
            else if invite?
              url = "#{protocol}#{host}/invitation/#{invite.code}"
              # bitly.shorten url, (err, response)=>
              #   shortenedUrl = response.data.url
              #   if shortenedUrl?
                  # shortenedUrl = url
              console.log 'hello there ---<<<'
              personalizedMail = betaTestersHTML.replace '#{url}', url#shortenedUrl
              Emailer.send
                From      : @getInviteEmail()
                To        : email
                Subject   : '[Koding] Here is your beta invite!'
                HtmlBody  : personalizedMail
              , (err)-> 
                console.log 'finished', i++, err
                recipients.fin(err)
                # else console.log email
            else
              console.log "no invitation was found for #{email}"
              recipients.fin null
      dash recipients, callback
        
  
  @__createBetaInvites =do ->
    # betaTestersEmails = '<EMAIL>'
    # betaTestersEmails = fs.readFileSync('./invitee-emails.txt', 'utf-8')
    #betaTestersEmails = '<EMAIL>'
    (callback)->
      JAccount.one {'profile.nickname': 'devrim'}, (err, devrim)=>
        i = 0
        recipients = []
        uniq(betaTestersEmails.split '\n').forEach (email)->
          code = crypto
            .createHmac('sha1', 'kodingsecret')
            .update(email)
            .digest('hex')
          recipients.push ->
            invite = new JInvitation {
              code
              inviteeEmail  : email
              maxUses       : 1
              origin        : ObjectRef(devrim)
              type          : 'launchrock'
            }
            invite.save (err)-> 
              recipients.next(err)
        recipients.push -> callback(recipients)
        daisy recipients
  
  @grant =(selector, quota, options, callback)->
    [callback, options] = [options, callback] unless callback
    unless quota > 0
      callback new KodingError "Quota must be positive."
    else
      batch = []
      i = 0
      console.log arguments
      JAccount.all selector, options, (err, accounts)->
        accounts.forEach (account)->
          batch.push ->
            account.fetchLimit 'invite', (err, limit)->
              if err then batch.fin(err)
              else if limit
                limit.update $inc: {quota}, (err)-> batch.fin(err)
              else
                limit = new JLimit {quota}
                limit.save (err)->
{'fraction_non_alphanumeric': 0.11400966183574879, 'fraction_numerical': 0.004830917874396135, 'mean_word_length': 1.5332843857072933, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 65, 'https://': 2, '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': '5204828', 'n_tokens_mistral': 3010, 'n_tokens_neox': 2888, 'n_words': 679}