Skip to content

Commit

Permalink
wip pms
Browse files Browse the repository at this point in the history
  • Loading branch information
hackergrrl committed Mar 20, 2020
1 parent c29c84c commit 0e00e78
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var createMessagesView = require('./views/messages')
var createTopicsView = require('./views/topics')
var createUsersView = require('./views/users')
var createModerationView = require('./views/moderation')
var createPrivateMessagesView = require('./views/private-messages')
var swarm = require('./swarm')

var DATABASE_VERSION = 1
Expand All @@ -24,6 +25,7 @@ var MESSAGES = 'm'
var TOPICS = 't'
var USERS = 'u'
var MODERATION = 'x'
var PRIVATE_MESSAGES = 'p'

module.exports = Cabal
module.exports.databaseVersion = DATABASE_VERSION
Expand Down Expand Up @@ -93,6 +95,11 @@ function Cabal (storage, key, opts) {
this, this.modKey,
sublevel(this.db, MODERATION, { valueEncoding: json }))
)
this.feed(function (feed) {
self.kcore.use('privateMessages', createPrivateMessagesView(
feed.secretKey,
sublevel(self.db, PRIVATE_MESSAGES, { valueEncoding: json })))
})

this.messages = this.kcore.api.messages
this.channels = this.kcore.api.channels
Expand Down
104 changes: 104 additions & 0 deletions views/private-messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const {unbox} = require('../lib/crypto')
var EventEmitter = require('events').EventEmitter

module.exports = function (mySecretKey, lvl) {
var events = new EventEmitter()

return {
maxBatch: 100,

map: function (msgs, next) {
const self = this
var ops = []
var seen = {}
var pending = 0
msgs.forEach(function (msg) {
if (msg.value.type !== 'encrypted') return

var jsonBuffer, res
try {
jsonBuffer = unbox(Buffer.from(msg.value.content, 'base64'), mySecretKey)
if (!jsonBuffer) return // undecryptable
res = JSON.parse(jsonBuffer.toString())
} catch (e) {
// skip unparseable messages
return
}

if (res.type !== 'private/text') return
if (typeof res.timestamp !== 'number') return null
if (!Array.isArray(res.content.recipients)) return null
if (res.content.recipients.length <= 0) return null
if (typeof res.content.text !== 'string') return null

const senderHexKey = msg.key
const recipientHexKey = res.content.recipients[0]

// TODO: how to figure out my local feed key?
// const otherPersonHexKey =
console.log(self)

pending++

// add private convo recipient to list
lvl.get('pm!' + recipientHexKey, function (err) {
if (err && err.notFound) {
if (!seen[recipient]) events.emit('add', channel)
seen[recipient] = true

ops.push({
type: 'put',
key: 'pm!' + recipient,
value: 1
})
}
if (!--pending) done()
})
})
if (!pending) done()

function done () {
lvl.batch(ops, next)
}
},

api: {
get: function (core, cb) {
this.ready(function () {
var channels = []
lvl.createKeyStream({
gt: 'channel!!',
lt: 'channel!~'
})
.on('data', function (channel) {
channels.push(channel.replace('channel!', ''))
})
.once('end', function () {
cb(null, channels)
})
.once('error', cb)
})
},

events: events
},

storeState: function (state, cb) {
state = state.toString('base64')
lvl.put('state', state, cb)
},

fetchState: function (cb) {
lvl.get('state', function (err, state) {
if (err && err.notFound) cb()
else if (err) cb(err)
else cb(null, Buffer.from(state, 'base64'))
})
},
}
}

// Either returns a well-formed chat message, or null.
function sanitize (msg) {
return msg
}

1 comment on commit 0e00e78

@dwblair
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh exciting!!

Please sign in to comment.