Skip to content

Commit

Permalink
Fix problems with add group key
Browse files Browse the repository at this point in the history
  • Loading branch information
arj03 committed Nov 22, 2021
1 parent a906502 commit fea3e8c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 53 deletions.
16 changes: 12 additions & 4 deletions add-group-key.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = function (ssbSingleton, getGroupKeysFeed) {
const { replicateSubfeeds } = require('./helpers')

return {
template: `
<div id="app">
Expand All @@ -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

Expand All @@ -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!')
})
})
Expand Down
55 changes: 54 additions & 1 deletion helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)}
})
)
Expand Down
70 changes: 22 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(),
Expand All @@ -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)
})
}

0 comments on commit fea3e8c

Please sign in to comment.