<commit_before>'use strict'

// https://www.rethinkdb.com/docs/sql-to-reql/python/

// import * as db from './db'
import * as r from 'rethinkdb'
import request from 'request'

// db goodness
const db = r.db('main')
const table = db.table('batons')

let dbConn = null
r.connect((err, conn) => {
  if (err) throw err;
  dbConn = conn
})

// create a new baton
export function pass(baton: Object, notify: Boolean = false) {
  return new Promise((resolve, reject) => {
    table.insert(baton).run(dbConn, (err, body) => {
      if (!err) {
        const dbBaton = Object.assign(baton, body)

        console.log('Passed baton', dbBaton)

        // TODO - poke link to validate it before saving (also match against url regex)

        resolve(dbBaton)
      } else {
        console.log(`[baton] Failed to pass baton: ${err}`)

        reject(err)
      }
    })
  })
}

// find a baton by its id
export function byId(id: String, notify: Boolean = false) {
  return new Promise((resolve, reject) => {
    table.filter({id}).run(dbConn, (err, baton) => {
      if (!err) {
        console.log('Fetched baton', id, baton)
        resolve(baton)
      } else {
        console.log(`[baton] Failed to acquire baton ${id}: ${err}`)
        reject(err)
      }
    })
  })
}

// delete a baton
export function drop(id: String, notify: Boolean = false) {
  return new Promise((resolve, reject) => {
    table.filter({id}).delete().run(err, body) => {
      if (!err) {
        console.log('Dropped baton', id, body)
        resolve(body)
      } else {
        console.log(`[baton] Failed to drop baton ${id}: ${err}`)
        reject(err)
      }
    })
  })
}

// determines if a baton's link is still active, deleting it and optionally notifying the team if not
export function poke(id: Baton, notify: Boolean = false) {
  byId(id)
    .then(body => {
      if (!body.link) {
        console.log(`[baton] No link present ${id}`)
        return false
      }

      return request.get(body.link)
        .on('success', () => { return true  })
        .on('error',   () => { return false })
    })
    .catch(err => {
      // TODO - maybe resp with slack err
      return false
    })
}<commit_msg>:sparkles: url validation for baton pass<commit_after>'use strict'

// https://www.rethinkdb.com/docs/sql-to-reql/python/

// import * as db from './db'
import * as r from 'rethinkdb'
import request from 'request'

// db goodness
const db = r.db('main')
const table = db.table('batons')

let dbConn = null
r.connect((err, conn) => {
  if (err) throw err;
  dbConn = conn
})

const url = /^(http(?:s)?\:\/\/[a-zA-Z0-9]+(?:(?:\.|\-)[a-zA-Z0-9]+)+(?:\:\d+)?(?:\/[\w\-]+)*(?:\/?|\/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$/

// create a new baton
export function pass(baton: Object, notify: Boolean = false) {
  return new Promise((resolve, reject) => {
    if (!baton.link || !url.test(baton.link)) {
      reject(`Baton link must be a valid URL ${baton.link}`)
    }

    table.insert(baton).run(dbConn, (err, body) => {
      if (!err) {
        const dbBaton = Object.assign(baton, body)

        console.log('Passed baton', dbBaton)

        // TODO - poke link to validate it before saving (also match against url regex)

        resolve(dbBaton)
      } else {
        console.log(`[baton] Failed to pass baton: ${err}`)

        reject(err)
      }
    })
  })
}

// find a baton by its id
export function byId(id: String, notify: Boolean = false) {
  return new Promise((resolve, reject) => {
    table.filter({id}).run(dbConn, (err, baton) => {
      if (!err) {
        console.log('Fetched baton', id, baton)
        resolve(baton)
      } else {
        console.log(`[baton] Failed to acquire baton ${id}: ${err}`)
        reject(err)
      }
    })
  })
}

// delete a baton
export function drop(id: String, notify: Boolean = false) {
  return new Promise((resolve, reject) => {
    table.filter({id}).delete().run((err, body) => {
      if (!err) {
        console.log('Dropped baton', id, body)
        resolve(body)
      } else {
        console.log(`[baton] Failed to drop baton ${id}: ${err}`)
        reject(err)
      }
    })
  })
}

// determines if a baton's link is still active, deleting it and optionally notifying the team if not
export function poke(id: Baton, notify: Boolean = false) {
  byId(id)
    .then(body => {
      if (!body.link) {
        console.log(`[baton] No link present ${id}`)
        return false
      }

      return request.get(body.link)
        .on('success', () => { return true  })
        .on('error',   () => { return false })
    })
    .catch(err => {
      // TODO - maybe resp with slack err
      return false
    })
}
{'fraction_non_alphanumeric': 0.17701002345915973, 'fraction_numerical': 0.0012795905310300703, 'mean_word_length': 2.415877640203933, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1950781', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1575, 'n_words': 429}