diff --git a/add-group-key.js b/add-group-key.js index 9798f84..00ad80e 100644 --- a/add-group-key.js +++ b/add-group-key.js @@ -1,4 +1,6 @@ module.exports = function (ssbSingleton, getGroupKeysFeed) { + const { replicateSubfeeds } = require('./helpers') + return { template: `
@@ -20,7 +22,7 @@ module.exports = function (ssbSingleton, getGroupKeysFeed) { if (err) return console.error(err) getGroupKeysFeed(SSB, async (err, keysFeed) => { - const groupId = this.groupKey.toString('hex') + '.groupies' + const groupId = this.groupKey + '.groupies' const { where, type, toPromise } = SSB.db.operators @@ -33,14 +35,20 @@ module.exports = function (ssbSingleton, getGroupKeysFeed) { SSB.db.publishAs(keysFeed.keys, { type: 'groupkey', - key: this.groupKey.toString('hex'), + key: this.groupKey, id: groupId, recps: [SSB.id] }, (err, msg) => { if (err) return console.error(err) - SSB.box2.addGroupKey(groupId, this.groupKey) - SSB.db.reindexEncrypted() + const groupKey = Buffer.from(this.groupKey, 'hex') + SSB.box2.addGroupKey(groupId, groupKey) + + SSB.db.reindexEncrypted(() => { + // live can have problems with reindex + replicateSubfeeds(false) + }) + alert('Key added!') }) }) diff --git a/helpers.js b/helpers.js index 4a203df..84585b0 100644 --- a/helpers.js +++ b/helpers.js @@ -48,6 +48,59 @@ module.exports = { ) }, + replicateSubfeeds: (isLive, cb) => { + const { where, type, author, live, toPullStream } = SSB.db.operators + + function replicateMetaFeed(metafeed) { + console.log("replicating metafeed", metafeed) + // similar to ack self, we must ack own feeds! + SSB.ebt.request(metafeed, true) + + pull( + SSB.db.query( + where(author(metafeed)), + isLive ? live({ old: true }) : null, + toPullStream() + ), + pull.drain((msg) => { + const { subfeed, feedpurpose } = msg.value.content + if (feedpurpose && feedpurpose !== 'main') { // special + console.log("replicating subfeed", subfeed) + // similar to ack self, we must ack own feeds! + SSB.ebt.request(subfeed, true) + } + }) + ) + } + + pull( + SSB.db.query( + where(type('metafeed/announce')), + toPullStream() + ), + pull.drain((msg) => { + const { metafeed } = msg.value.content + replicateMetaFeed(metafeed) + }, () => { + if (isLive) { + pull( + SSB.db.query( + where(type('metafeed/announce')), + live({ old: false }), + toPullStream() + ), + pull.drain((msg) => { + const { metafeed } = msg.value.content + replicateMetaFeed(metafeed) + }) + ) + } + + if (cb) cb() + }) + ) + }, + dumpDB: () => { const { where, author, toPullStream } = SSB.db.operators @@ -56,7 +109,7 @@ module.exports = { toPullStream() ), pull.drain((msg) => { - console.log(`author ${msg.value.author}, seq: ${msg.value.sequence}, content: ${JSON.stringify(msg, null, 2)}`) + console.log(`author ${msg.value.author}, seq: ${msg.value.sequence}`) // , content: ${JSON.stringify(msg.value.content, null, 2)} }) ) diff --git a/index.js b/index.js index 1481292..a24f35d 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,8 @@ const pull = require('pull-stream') const hkdf = require('futoin-hkdf') const crypto = require('crypto') -const { getGroupKeysFeed, getChatFeed, dumpDB } = require('./helpers') +const { getGroupKeysFeed, getChatFeed, + replicateSubfeeds, dumpDB } = require('./helpers') const { monkeyPatchBox2Libs } = require('./browser-hack') function extraModules(secretStack) { @@ -249,15 +250,6 @@ function setupApp(SSB) { }) ) - // auto connect to room - const roomKey = '@oPnjHuBpFNG+wXC1dzmdzvOO30mVNYmZB778fq3bn3Y=.ed25519' - const room = 'wss:between-two-worlds.dk:444~shs:oPnjHuBpFNG+wXC1dzmdzvOO30mVNYmZB778fq3bn3Y=' - - SSB.conn.connect(room, { - key: roomKey, - type: 'room' - }, () => {}) - // show connection errors pull( SSB.conn.hub().listen(), @@ -275,45 +267,27 @@ function setupApp(SSB) { }) ) - // auto reconnect to room - setInterval(() => { - if (menu.peers.length === 0) { + replicateSubfeeds(true, () => { + // timeout to make sure we get all feeds replicated + setTimeout(() => { + // auto connect to room + const roomKey = '@oPnjHuBpFNG+wXC1dzmdzvOO30mVNYmZB778fq3bn3Y=.ed25519' + const room = 'wss:between-two-worlds.dk:444~shs:oPnjHuBpFNG+wXC1dzmdzvOO30mVNYmZB778fq3bn3Y=' + SSB.conn.connect(room, { key: roomKey, type: 'room' - }) - } - }, 1000) - - // find all meta feeds & children and replicate those - pull( - SSB.db.query( - where(type('metafeed/announce')), - live({ old: true }), - toPullStream() - ), - pull.drain((msg) => { - const { metafeed } = msg.value.content - console.log("replicating metafeed", metafeed) - // similar to ack self, we must ack own feeds! - SSB.ebt.request(metafeed, true) - - // FIXME: this doesn't work on reindex! - pull( - SSB.db.query( - where(author(metafeed)), - live({ old: true }), - toPullStream() - ), - pull.drain((msg) => { - const { subfeed, feedpurpose } = msg.value.content - if (feedpurpose && feedpurpose !== 'main') { // special - console.log("replicating subfeed", subfeed) - // similar to ack self, we must ack own feeds! - SSB.ebt.request(subfeed, true) - } - }) - ) - }) - ) + }, () => {}) + + // auto reconnect on fail + setInterval(() => { + if (menu.peers.length === 0) { + SSB.conn.connect(room, { + key: roomKey, + type: 'room' + }) + } + }, 1000) + }, 1500) + }) }