<commit_before>/* global Notification */

module.exports = window.App = App

var EventEmitter = require('events').EventEmitter

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

var BrowserWindow = remote.require('browser-window')
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 githubCurrentUser = require('github-current-user')
var h = require('virtual-dom/h')
var inherits = require('inherits')
var leveldown = require('leveldown')
var levelup = require('levelup')
var patch = require('virtual-dom/patch')
var path = require('path')
var subleveldown = require('subleveldown')

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

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

var currentWindow = remote.getCurrentWindow()

inherits(App, EventEmitter)

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

  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)
    } else if (/^#/.test(href)) {
      self.emit('addChannel', href)
    }
  })

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

  var swarm = window.swarm = Swarm(subleveldown(db, 'swarm'), {maxPeers: 20})
  var channelsFound = {}
  var usersFound = {}
  var changesOffsets = {}

  // join default channel
  swarm.addChannel('friends')

  githubCurrentUser.verify(function (err, verified, username) {
    if (err || !verified) self.showGitHelp()
    if (err) return console.error(err.message || err)
    if (verified) {
      self.data.username = username
      swarm.username = username

      // Re-create rich messages after we know our username, since we can now do
      // highlights correctly.
      self.data.messages = self.data.messages.map(function (message) {
        return richMessage(message, self.data.username)
      })

      render()
    }
  })

  swarm.process(function (basicMessage, cb) {
    var message = richMessage(basicMessage, self.data.username)
    var channelName = message.channel || 'friends'<commit_msg>clear dock notifications on focus. fixes #79<commit_after>/* global Notification */

module.exports = window.App = App

var EventEmitter = require('events').EventEmitter

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

var BrowserWindow = remote.require('browser-window')
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 githubCurrentUser = require('github-current-user')
var h = require('virtual-dom/h')
var inherits = require('inherits')
var leveldown = require('leveldown')
var levelup = require('levelup')
var patch = require('virtual-dom/patch')
var path = require('path')
var subleveldown = require('subleveldown')

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

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

var currentWindow = remote.getCurrentWindow()

inherits(App, EventEmitter)

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

  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)
    } else if (/^#/.test(href)) {
      self.emit('addChannel', href)
    }
  })

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

  var swarm = window.swarm = Swarm(subleveldown(db, 'swarm'), {maxPeers: 20})
  var channelsFound = {}
  var usersFound = {}
  var changesOffsets = {}

  // join default channel
  swarm.addChannel('friends')

  githubCurrentUser.verify(function (err, verified, username) {
    if (err || !verified) self.showGitHelp()
    if (err) return console.error(err.message || err)
    if (verified) {
      self.data.username = username
      swarm.username = username

      // Re-create rich messages after we know our username, since we can now do
      // highlights correctly.
      self.data.messages = self.data.messages.map(function (message) {
        return richMessage(message, self.data.username)
      })

      render()
    }
  })

  // clear notifications on focus. TODO: only clear notifications in current channel when we have that
  currentWindow.on('focus', function () {
    self.setBadge(false)
  })

  swarm.process(function (basicMessage, cb) {
    var message = richMessage(basicMessage, self.data.username)
    var channelName = message.channel || 'friends'
{'fraction_non_alphanumeric': 0.14994096812278632, 'fraction_numerical': 0.0016866250632484399, 'mean_word_length': 3.7977346278317152, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7066134', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1757, 'n_words': 465}