<commit_before>/* global Notification */

module.exports = window.App = App

var fs = require('fs')
var EventEmitter = require('events').EventEmitter

var shell = require('shell')
var remote = require('remote')
var app = remote.require('app')

var catNames = require('cat-names')
var createElement = require('virtual-dom/create-element')
var delegate = require('delegate-dom')
var diff = require('virtual-dom/diff')
var eos = require('end-of-stream')
var h = require('virtual-dom/h')
var inherits = require('inherits')
var patch = require('virtual-dom/patch')
var raf = require('raf')
var user = require('github-current-user')
var ghsign = require('ghsign')
var request = require('request')
var levelup = require('levelup')
var leveldown = require('leveldown')
var subleveldown = require('subleveldown')

var richMessage = require('./lib/rich-message.js')
var Swarm = require('./lib/swarm.js')
var util = require('./lib/util.js')

var Channels = require('./lib/elements/channels.js')
var Composer = require('./lib/elements/composer.js')
var Messages = require('./lib/elements/messages.js')
var Status = require('./lib/elements/status.js')
var Users = require('./lib/elements/users.js')
var Peers = require('./lib/elements/peers.js')

var currentWindow = remote.getCurrentWindow()

ghsign = ghsign(function (username, cb) {
  fs.readFile('./public-keys/' + username + '.keys', 'utf-8', function (err, keys) {
    if (err) return cb(err)
    if (keys) return cb(null, keys)
    request('https://github.com/' + username + '.keys', function (err, response) {
      if (err) return cb(err)
      var keys = response.statusCode === 200 && response.body
      if (!keys) return cb(new Error('Could not find public keys for ' + username))
      fs.mkdir('./public-keys', function () {
        fs.writeFile('./public-keys/' + username + '.keys', keys, function () {
          cb(null, keys)
        })
      })
    })
  })
})

delegate.on(document.body, 'a', 'click', function (e) {
  var href = e.target.getAttribute('href')
  if (/^https?:/.test(href)) {
    e.preventDefault()
    shell.openExternal(href)
  }
})

inherits(App, EventEmitter)

function App (el) {
  var self = this
  if (!(self instanceof App)) return new App(el)

  var db = levelup('./friendsdb', {db: leveldown})

  db.channels = subleveldown(db, 'channels', {valueEncoding: 'json'})

  // The mock data model
  self.data = {
    peers: 0,
    username: 'Anonymous (' + catNames.random() + ')',
    channels: [],
    messages: [],
    users: []
  }

  var swarm = window.swarm = Swarm(subleveldown(db, 'swarm'))
  user.verify(function (err, verified, username) {
    if (err) return console.error(err.message || err)
    if (verified) {
      self.data.username = username
      swarm.username = username
      ghsign.verifier(username)
    } else {
      self.data.username = 'Anonymous (' + catNames.random() + ')'
    }
  })

  var channelsFound = {}
  var usersFound = {}
  var verifiers = {}

  swarm.process(function (entry, cb) {
    var basicMessage = JSON.parse(entry.value)
    var userVerify = verifiers[basicMessage.username]

    var onverify = function () {
      var message = richMessage(basicMessage)
      var channelName = message.channel || 'friends'
      var channel = channelsFound[channelName]

      if (!channel) {
        channel = channelsFound[channelName] = {
          id: self.data.channels.length,
          name: channelName,
          active: false,
          messages: []
        }
        self.data.channels.push(channel)
        self.data.activeChannel = channel
      }<commit_msg>cleanup<commit_after>/* global Notification */

module.exports = window.App = App

var fs = require('fs')
var EventEmitter = require('events').EventEmitter

var shell = require('shell')
var remote = require('remote')
var app = remote.require('app')

var catNames = require('cat-names')
var createElement = require('virtual-dom/create-element')
var delegate = require('delegate-dom')
var diff = require('virtual-dom/diff')
var eos = require('end-of-stream')
var h = require('virtual-dom/h')
var inherits = require('inherits')
var patch = require('virtual-dom/patch')
var raf = require('raf')
var githubCurrentUser = require('github-current-user')
var ghsign = require('ghsign')
var request = require('request')
var levelup = require('levelup')
var leveldown = require('leveldown')
var subleveldown = require('subleveldown')

var richMessage = require('./lib/rich-message.js')
var Swarm = require('./lib/swarm.js')
var util = require('./lib/util.js')

var Channels = require('./lib/elements/channels.js')
var Composer = require('./lib/elements/composer.js')
var Messages = require('./lib/elements/messages.js')
var Status = require('./lib/elements/status.js')
var Users = require('./lib/elements/users.js')
var Peers = require('./lib/elements/peers.js')

var currentWindow = remote.getCurrentWindow()

ghsign = ghsign(function (username, cb) {
  fs.readFile('./public-keys/' + username + '.keys', 'utf-8', function (err, keys) {
    if (err) return cb(err)
    if (keys) return cb(null, keys)
    request('https://github.com/' + username + '.keys', function (err, response) {
      if (err) return cb(err)
      var keys = response.statusCode === 200 && response.body
      if (!keys) return cb(new Error('Could not find public keys for ' + username))
      fs.mkdir('./public-keys', function () {
        fs.writeFile('./public-keys/' + username + '.keys', keys, function () {
          cb(null, keys)
        })
      })
    })
  })
})

inherits(App, EventEmitter)

function App (el) {
  var self = this
  if (!(self instanceof App)) return new App(el)

  var db = levelup('./friendsdb', {db: leveldown})

  db.channels = subleveldown(db, 'channels', {valueEncoding: 'json'})

  // Open links in user's default browser
  delegate.on(el, 'a', 'click', function (e) {
    var href = e.target.getAttribute('href')
    if (/^https?:/.test(href)) {
      e.preventDefault()
      shell.openExternal(href)
    }
  })

  // The mock data model
  self.data = {
    peers: 0,
    username: 'Anonymous (' + catNames.random() + ')',
    channels: [],
    messages: [],
    users: []
  }

  var swarm = window.swarm = Swarm(subleveldown(db, 'swarm'))
  githubCurrentUser.verify(function (err, verified, username) {
    if (err) return console.error(err.message || err)
    if (verified) {
      self.data.username = username
      swarm.username = username
      ghsign.verifier(username)
    }
  })

  var channelsFound = {}
  var usersFound = {}
  var verifiers = {}

  swarm.process(function (entry, cb) {
    var basicMessage = JSON.parse(entry.value)
    var userVerify = verifiers[basicMessage.username]

    var onverify = function () {
      var message = richMessage(basicMessage)
      var channelName = message.channel || 'friends'
      var channel = channelsFound[channelName]

      if (!channel) {
        channel = channelsFound[channelName] = {
          id: self.data.channels.length,
          name: channelName,
          active: false,
          messages: []
        }
        self.data.channels.push(channel)
        self.data.activeChannel = channel
      }
{'fraction_non_alphanumeric': 0.15885890085302753, 'fraction_numerical': 0.0013984058173682002, 'mean_word_length': 3.3371740448756824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6083963', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2165, 'n_words': 538}