Skip to content

Commit

Permalink
Merge pull request #85 from ssbc/readd-additions
Browse files Browse the repository at this point in the history
Publish re-ad messages on the additions feed
  • Loading branch information
Powersource authored Apr 6, 2023
2 parents d8148a5 + 5dc0ec9 commit 9a47576
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 90 deletions.
123 changes: 51 additions & 72 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})
)
})
})
})
)
})
})
}
Expand Down
47 changes: 29 additions & 18 deletions test/exclude-members.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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]

Expand All @@ -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')
Expand All @@ -160,6 +145,32 @@ 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)

t.equal(additionsContents.length, 3, '3 messages on additions feed')

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)
})

0 comments on commit 9a47576

Please sign in to comment.