From c6b9d70f5935159ebf9a34e2199a3542d51c6d98 Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Wed, 5 Apr 2023 14:14:58 +0200 Subject: [PATCH 1/3] Publish readd messages on the additions feed --- index.js | 123 +++++++++++++++++++++++-------------------------------- 1 file changed, 51 insertions(+), 72 deletions(-) diff --git a/index.js b/index.js index 1770ca1..015328e 100644 --- a/index.js +++ b/index.js @@ -177,91 +177,70 @@ module.exports = { // prettier-ignore if (err) return cb(clarify(err, "Couldn't get own root when excluding members")) - get(groupId, (err, { writeKey: oldWriteKey } = {}) => { + const excludeContent = { + type: 'group/exclude', + excludes: feedIds, + recps: [groupId], + } + const excludeOpts = { + tangles: ['members'], + isValid: isExclude, + } + publish(excludeContent, excludeOpts, (err) => { // prettier-ignore - if (err) return cb(clarify(err, "Couldn't get old key when excluding members")) - - findOrCreateGroupFeed(oldWriteKey.key, (err, oldGroupFeed) => { - // prettier-ignore - if (err) return cb(clarify(err, "Couldn't get the old group feed when excluding members")) + if (err) return cb(clarify(err, 'Failed to publish exclude msg')) - const excludeContent = { - type: 'group/exclude', - excludes: feedIds, - recps: [groupId], - } - const excludeOpts = { - tangles: ['members'], - isValid: isExclude, - } - publish(excludeContent, excludeOpts, (err) => { + pull( + listMembers(groupId), + pull.collect((err, beforeMembers) => { // prettier-ignore - if (err) return cb(clarify(err, 'Failed to publish exclude msg')) + if (err) return cb(clarify(err, "Couldn't get old member list when excluding members")) - pull( - listMembers(groupId), - pull.collect((err, beforeMembers) => { - // prettier-ignore - if (err) return cb(clarify(err, "Couldn't get old member list when excluding members")) + const remainingMembers = beforeMembers.filter( + (member) => !feedIds.includes(member) + ) + const newGroupKey = new SecretKey() + const addInfo = { key: newGroupKey.toBuffer() } - const remainingMembers = beforeMembers.filter( - (member) => !feedIds.includes(member) - ) - const newGroupKey = new SecretKey() - const addInfo = { key: newGroupKey.toBuffer() } + ssb.box2.addGroupInfo(groupId, addInfo, (err) => { + // prettier-ignore + if (err) return cb(clarify(err, "Couldn't store new key when excluding members")) - ssb.box2.addGroupInfo(groupId, addInfo, (err) => { + const newKey = { + key: newGroupKey.toBuffer(), + scheme: keySchemes.private_group, + } + ssb.box2.pickGroupWriteKey(groupId, newKey, (err) => { + // prettier-ignore + if (err) return cb(clarify(err, "Couldn't switch to new key for writing when excluding members")) + + const newEpochContent = { + type: 'group/init', + version: 'v2', + groupKey: newGroupKey.toString('base64'), + tangles: { + members: { root: null, previous: null }, + }, + recps: [groupId, myRoot.id], + } + const newTangleOpts = { + tangles: ['epoch'], + isValid: isInitEpoch, + } + publish(newEpochContent, newTangleOpts, (err) => { // prettier-ignore - if (err) return cb(clarify(err, "Couldn't store new key when excluding members")) + if (err) return cb(clarify(err, "Couldn't post init msg on new epoch when excluding members")) - const newKey = { - key: newGroupKey.toBuffer(), - scheme: keySchemes.private_group, - } - ssb.box2.pickGroupWriteKey(groupId, newKey, (err) => { + addMembers(groupId, remainingMembers, {}, (err) => { // prettier-ignore - if (err) return cb(clarify(err, "Couldn't switch to new key for writing when excluding members")) - - const newEpochContent = { - type: 'group/init', - version: 'v2', - groupKey: newGroupKey.toString('base64'), - tangles: { - members: { root: null, previous: null }, - }, - recps: [groupId, myRoot.id], - } - const newTangleOpts = { - tangles: ['epoch'], - isValid: isInitEpoch, - } - publish(newEpochContent, newTangleOpts, (err) => { - // prettier-ignore - if (err) return cb(clarify(err, "Couldn't post init msg on new epoch when excluding members")) - - const reAddOpts = { - // the re-adding needs to be published on the old - // feed so that the additions feed is not spammed, - // while people need to still be able to find it - _feedKeys: oldGroupFeed.keys, - } - addMembers( - groupId, - remainingMembers, - reAddOpts, - (err) => { - // prettier-ignore - if (err) return cb(clarify(err, "Couldn't re-add remaining members when excluding members")) - return cb() - } - ) - }) + if (err) return cb(clarify(err, "Couldn't re-add remaining members when excluding members")) + return cb() }) }) }) - ) + }) }) - }) + ) }) }) } From 155fc60b1db958fa279a4cf301d3231f381b57fd Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Wed, 5 Apr 2023 14:33:01 +0200 Subject: [PATCH 2/3] Adapt test for readds on additions feed --- test/exclude-members.test.js | 45 +++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/test/exclude-members.test.js b/test/exclude-members.test.js index 6f743f6..e7d2001 100644 --- a/test/exclude-members.test.js +++ b/test/exclude-members.test.js @@ -13,9 +13,9 @@ const countGroupFeeds = require('./helpers/count-group-feeds') test('add and remove a person, post on the new feed', async (t) => { // Alice's feeds should look like - // first: initGroup->excludeBob->reAddAlice + // first: initGroup->excludeBob // second: initEpoch->post - // additions: addAlice->addBob (not checking this here) + // additions: addAlice->addBob->reAddAlice const alice = Testbot({ keys: ssbKeys.generate(null, 'alice'), mfSeed: Buffer.from( @@ -103,7 +103,7 @@ test('add and remove a person, post on the new feed', async (t) => { const firstContents = msgsFromFirst.map((msg) => msg.value.content) - t.equal(firstContents.length, 3, '3 messages on first feed') + t.equal(firstContents.length, 2, '2 messages on first feed') const firstInit = firstContents[0] @@ -120,26 +120,11 @@ test('add and remove a person, post on the new feed', async (t) => { previous: [fromMessageSigil(addBobMsg.key)], }) - const reinviteMsg = firstContents[2] - - t.equal(reinviteMsg.type, 'group/add-member') - t.deepEqual(reinviteMsg.recps, [groupId, aliceRoot.id]) - const msgsFromSecond = await alice.db.query( where(author(secondFeedId)), toPromise() ) - const secondInitKey = fromMessageSigil(msgsFromSecond[0].key) - t.deepEqual( - reinviteMsg.tangles.members, - { - root: secondInitKey, - previous: [secondInitKey], - }, - 'members tangle resets after new epoch' - ) - const secondContents = msgsFromSecond.map((msg) => msg.value.content) t.equal(secondContents.length, 2, '2 messages on second (new) feed') @@ -160,6 +145,30 @@ test('add and remove a person, post on the new feed', async (t) => { t.equal(post.text, 'post', 'found post on second feed') + const aliceAdditions = await p(alice.metafeeds.findOrCreate)({ + purpose: 'group/additions', + }) + const msgsFromAdditions = await alice.db.query( + where(author(aliceAdditions.id)), + toPromise() + ) + const additionsContents = msgsFromAdditions.map((msg) => msg.value.content) + + const reinviteMsg = additionsContents[2] + + t.equal(reinviteMsg.type, 'group/add-member') + t.deepEqual(reinviteMsg.recps, [groupId, aliceRoot.id]) + + const secondInitKey = fromMessageSigil(msgsFromSecond[0].key) + t.deepEqual( + reinviteMsg.tangles.members, + { + root: secondInitKey, + previous: [secondInitKey], + }, + 'members tangle resets after new epoch' + ) + await p(alice.close)(true) await p(bob.close)(true) }) From 5dc0ec96c229abf1c11a0a307c72e5788bd15b4a Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Wed, 5 Apr 2023 14:37:00 +0200 Subject: [PATCH 3/3] Count msgs on additions feed --- test/exclude-members.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/exclude-members.test.js b/test/exclude-members.test.js index e7d2001..93ab60c 100644 --- a/test/exclude-members.test.js +++ b/test/exclude-members.test.js @@ -154,6 +154,8 @@ test('add and remove a person, post on the new feed', async (t) => { ) const additionsContents = msgsFromAdditions.map((msg) => msg.value.content) + t.equal(additionsContents.length, 3, '3 messages on additions feed') + const reinviteMsg = additionsContents[2] t.equal(reinviteMsg.type, 'group/add-member')