<commit_before>Mongo = require('./mongo').Mongo
http = require('./http')
sys = require 'sys'
crypto = require 'crypto'
require '../public/javascripts/Math.uuid'
nko = {}

md5 = (str) ->
  hash = crypto.createHash 'md5'
  hash.update str
  hash.digest 'hex'

validEmail = (email) ->
  /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test email

escapeURL = require('querystring').escape

class Team
  build: (options) ->
    @name = options?.name or ''
    @createdAt = new Date()
    @application = options?.application or ''
    @description = options?.description or ''
    @colophon = options?.colophon or ''
    @link = options?.link or ''
    @url = options?.url or ''
    @joyentCode = options?.joyentCode or ''
    @lastDeployedTo = options?.lastDeployedTo or ''
    @lastDeployedAt = options?.lastDeployedAt
    @deployHeads = options?.deployHeads or []

  constructor: (options, fn) ->
    @build options
    @token = Math.uuid()
    @setMembers options?.emails, fn

  # TODO DRY
  authKey: ->
    @id() + ':' + @token

  hasMember: (member) ->
    return false unless member?
    _.include _.invoke(@members, 'id'), member.id()

  emails: ->
    _.pluck @members, 'email'

  validate: ->
    errors = []
    errors.push 'Must have team name' unless @name
    errors.push 'Team needs at least one member' unless @members?.length
    errors.concat _.compact _.flatten [member.validate() for member in @members]

  beforeSave: (fn) ->
    @generateSlug =>
      @generateDeploySlugs()
      threads = @members.length
      return fn() unless threads
      for member in @members
        member.save (error, res) ->
          fn() if --threads is 0

  beforeInstantiate: (fn) ->
    query = { _id: { $in: _.pluck @members, '_id' }}
    Person.all query, (error, members) =>
      @members = members
      @invited = _.select @members, (m) -> m.name == ''
      fn()

  setMembers: (emails, fn) ->
    emails = _.compact emails or []
    @members = or []
    oldEmails = @emails()

    keepEmails = _.intersect emails, oldEmails
    @members = _.select @members, (m) ->
      _.include keepEmails, m.email

    newEmails = _.without emails, oldEmails...
    threads = newEmails.length
    return process.nextTick fn unless threads

    for email in newEmails
      Person.firstOrNew { email: email }, (error, member) =>
        @members.push member
        member.type = 'Participant'
        member.inviteTo this, ->
          fn() if --threads is 0

  generateSlug: (fn, attempt) ->
    @slug = attempt || @name.toLowerCase().replace(/\W+/g, '-').replace(/^-|-$/, '')
    Team.fromParam @slug, (error, existing) =>
      if !existing? or existing.id() == @id()
        fn()  # no conflicts
      else
        @generateSlug fn, @slug + '-'  # try with another -

  generateDeploySlugs: ->
    @joyentSlug or= @slug.replace(/^(\d)/, 'ko-$1').replace(/_/g, '-').substring(0, 30)
    @herokuSlug or= 'nko-' + @slug.replace(/_/g, '-').substring(0, 26)

nko.Team = Team

class Person
  constructor: (options) ->
    @name = options?.name or ''
    @email = options?.email or ''

    @link = options?.link or ''
    @github = options?.github or ''
    @heroku = options?.heroku or ''
    @joyent = options?.joyent or ''

    @password = options?.password or @randomPassword()
    @new_password = !options?.password?
    @confirmed = options?.confirmed or false<commit_msg>if a person has a type, don't override it<commit_after>Mongo = require('./mongo').Mongo
http = require('./http')
sys = require 'sys'
crypto = require 'crypto'
require '../public/javascripts/Math.uuid'
nko = {}

md5 = (str) ->
  hash = crypto.createHash 'md5'
  hash.update str
  hash.digest 'hex'

validEmail = (email) ->
  /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test email

escapeURL = require('querystring').escape

class Team
  build: (options) ->
    @name = options?.name or ''
    @createdAt = new Date()
    @application = options?.application or ''
    @description = options?.description or ''
    @colophon = options?.colophon or ''
    @link = options?.link or ''
    @url = options?.url or ''
    @joyentCode = options?.joyentCode or ''
    @lastDeployedTo = options?.lastDeployedTo or ''
    @lastDeployedAt = options?.lastDeployedAt
    @deployHeads = options?.deployHeads or []

  constructor: (options, fn) ->
    @build options
    @token = Math.uuid()
    @setMembers options?.emails, fn

  # TODO DRY
  authKey: ->
    @id() + ':' + @token

  hasMember: (member) ->
    return false unless member?
    _.include _.invoke(@members, 'id'), member.id()

  emails: ->
    _.pluck @members, 'email'

  validate: ->
    errors = []
    errors.push 'Must have team name' unless @name
    errors.push 'Team needs at least one member' unless @members?.length
    errors.concat _.compact _.flatten [member.validate() for member in @members]

  beforeSave: (fn) ->
    @generateSlug =>
      @generateDeploySlugs()
      threads = @members.length
      return fn() unless threads
      for member in @members
        member.save (error, res) ->
          fn() if --threads is 0

  beforeInstantiate: (fn) ->
    query = { _id: { $in: _.pluck @members, '_id' }}
    Person.all query, (error, members) =>
      @members = members
      @invited = _.select @members, (m) -> m.name == ''
      fn()

  setMembers: (emails, fn) ->
    emails = _.compact emails or []
    @members = or []
    oldEmails = @emails()

    keepEmails = _.intersect emails, oldEmails
    @members = _.select @members, (m) ->
      _.include keepEmails, m.email

    newEmails = _.without emails, oldEmails...
    threads = newEmails.length
    return process.nextTick fn unless threads

    for email in newEmails
      Person.firstOrNew { email: email }, (error, member) =>
        @members.push member
        member.type or= 'Participant'
        member.inviteTo this, ->
          fn() if --threads is 0

  generateSlug: (fn, attempt) ->
    @slug = attempt || @name.toLowerCase().replace(/\W+/g, '-').replace(/^-|-$/, '')
    Team.fromParam @slug, (error, existing) =>
      if !existing? or existing.id() == @id()
        fn()  # no conflicts
      else
        @generateSlug fn, @slug + '-'  # try with another -

  generateDeploySlugs: ->
    @joyentSlug or= @slug.replace(/^(\d)/, 'ko-$1').replace(/_/g, '-').substring(0, 30)
    @herokuSlug or= 'nko-' + @slug.replace(/_/g, '-').substring(0, 26)

nko.Team = Team

class Person
  constructor: (options) ->
    @name = options?.name or ''
    @email = options?.email or ''

    @link = options?.link or ''
    @github = options?.github or ''
    @heroku = options?.heroku or ''
    @joyent = options?.joyent or ''

    @password = options?.password or @randomPassword()
    @new_password = !options?.password?
    @confirmed = options?.confirmed or false
{'fraction_non_alphanumeric': 0.1640117994100295, 'fraction_numerical': 0.005014749262536873, 'mean_word_length': 2.7074904319300166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, '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': '5732148', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2193, 'n_words': 582}