|
| 1 | +const {unbox} = require('../lib/crypto') |
| 2 | +var EventEmitter = require('events').EventEmitter |
| 3 | + |
| 4 | +module.exports = function (mySecretKey, lvl) { |
| 5 | + var events = new EventEmitter() |
| 6 | + |
| 7 | + return { |
| 8 | + maxBatch: 100, |
| 9 | + |
| 10 | + map: function (msgs, next) { |
| 11 | + const self = this |
| 12 | + var ops = [] |
| 13 | + var seen = {} |
| 14 | + var pending = 0 |
| 15 | + msgs.forEach(function (msg) { |
| 16 | + if (msg.value.type !== 'encrypted') return |
| 17 | + |
| 18 | + var jsonBuffer, res |
| 19 | + try { |
| 20 | + jsonBuffer = unbox(Buffer.from(msg.value.content, 'base64'), mySecretKey) |
| 21 | + if (!jsonBuffer) return // undecryptable |
| 22 | + res = JSON.parse(jsonBuffer.toString()) |
| 23 | + } catch (e) { |
| 24 | + // skip unparseable messages |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + if (res.type !== 'private/text') return |
| 29 | + if (typeof res.timestamp !== 'number') return null |
| 30 | + if (!Array.isArray(res.content.recipients)) return null |
| 31 | + if (res.content.recipients.length <= 0) return null |
| 32 | + if (typeof res.content.text !== 'string') return null |
| 33 | + |
| 34 | + const senderHexKey = msg.key |
| 35 | + const recipientHexKey = res.content.recipients[0] |
| 36 | + |
| 37 | + // TODO: how to figure out my local feed key? |
| 38 | + // const otherPersonHexKey = |
| 39 | + console.log(self) |
| 40 | + |
| 41 | + pending++ |
| 42 | + |
| 43 | + // add private convo recipient to list |
| 44 | + lvl.get('pm!' + recipientHexKey, function (err) { |
| 45 | + if (err && err.notFound) { |
| 46 | + if (!seen[recipient]) events.emit('add', channel) |
| 47 | + seen[recipient] = true |
| 48 | + |
| 49 | + ops.push({ |
| 50 | + type: 'put', |
| 51 | + key: 'pm!' + recipient, |
| 52 | + value: 1 |
| 53 | + }) |
| 54 | + } |
| 55 | + if (!--pending) done() |
| 56 | + }) |
| 57 | + }) |
| 58 | + if (!pending) done() |
| 59 | + |
| 60 | + function done () { |
| 61 | + lvl.batch(ops, next) |
| 62 | + } |
| 63 | + }, |
| 64 | + |
| 65 | + api: { |
| 66 | + get: function (core, cb) { |
| 67 | + this.ready(function () { |
| 68 | + var channels = [] |
| 69 | + lvl.createKeyStream({ |
| 70 | + gt: 'channel!!', |
| 71 | + lt: 'channel!~' |
| 72 | + }) |
| 73 | + .on('data', function (channel) { |
| 74 | + channels.push(channel.replace('channel!', '')) |
| 75 | + }) |
| 76 | + .once('end', function () { |
| 77 | + cb(null, channels) |
| 78 | + }) |
| 79 | + .once('error', cb) |
| 80 | + }) |
| 81 | + }, |
| 82 | + |
| 83 | + events: events |
| 84 | + }, |
| 85 | + |
| 86 | + storeState: function (state, cb) { |
| 87 | + state = state.toString('base64') |
| 88 | + lvl.put('state', state, cb) |
| 89 | + }, |
| 90 | + |
| 91 | + fetchState: function (cb) { |
| 92 | + lvl.get('state', function (err, state) { |
| 93 | + if (err && err.notFound) cb() |
| 94 | + else if (err) cb(err) |
| 95 | + else cb(null, Buffer.from(state, 'base64')) |
| 96 | + }) |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// Either returns a well-formed chat message, or null. |
| 102 | +function sanitize (msg) { |
| 103 | + return msg |
| 104 | +} |
0 commit comments