Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable checking of add-member spec #58

Merged
merged 14 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 32 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const {
} = require('ssb-db2/operators')
const {
validator: {
group: { content: contentSpec },
group: { addMember: isAddMember, content: isContent },
},
} = require('private-group-spec')
const { fromMessageSigil, isBendyButtV1FeedSSBURI } = require('ssb-uri2')
Expand All @@ -43,6 +43,7 @@ module.exports = {
findOrCreateAdditionsFeed,
findOrCreateGroupFeed,
findOrCreateGroupWithoutMembers,
getRootFeedIdFromMsgId,
} = MetaFeedHelpers(ssb)

function create(opts = {}, cb) {
Expand Down Expand Up @@ -119,41 +120,40 @@ module.exports = {
// prettier-ignore
if (err) return cb(clarify(err, `Failed to get group details when adding members`))

const content = {
type: 'group/add-member',
version: 'v2',
secret: secret.toString('base64'),
root,
tangles: {
members: {
root,
previous: [root], // TODO calculate previous for members tangle
},
// likely incorrect group tangle and this will be overwritten by
// publish(), we just add it here to make the spec pass
group: {
root,
previous: [root],
getRootFeedIdFromMsgId(root, (err, rootAuthorId) => {
// prettier-ignore
if (err) return cb(clarify(err, "couldn't get root id of author of root msg"))

const content = {
type: 'group/add-member',
version: 'v2',
groupKey: secret.toString('base64'),
root,
creator: rootAuthorId,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have missed this (creator) being added to the spec? I dislike that it's like author. Why do we need it?

creator is the root metafeed id of the creator of the group

Nope, I was involved! I think we should call it groupCreatorRootFeedId or initAuthorRootrId .. if it's not too late :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too late! But this PR just adds the checking against the spec. Changing the field name would IMO be a different issue/pr.

tangles: {
members: {
root,
previous: [root], // TODO calculate previous for members tangle
},
},
},
recps: [groupId, ...feedIds],
}
recps: [groupId, ...feedIds],
}

if (opts.text) content.text = opts.text
if (opts.text) content.text = opts.text

// TODO: this should accept bendybutt-v1 feed IDs
// if (!addMemberSpec(content))
// return cb(new Error(addMemberSpec.errorsString))
findOrCreateAdditionsFeed((err, additionsFeed) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to find or create additions feed when adding members'))

findOrCreateAdditionsFeed((err, additionsFeed) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to find or create additions feed when adding members'))
addGroupTangle(content, (err, content) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to add group tangle when adding members'))

addGroupTangle(content, (err, content) => {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to add group tangle when adding members'))
if (!isAddMember(content))
return cb(new Error(isAddMember.errorsString))

publishAndPrune(ssb, content, additionsFeed.keys, cb)
publishAndPrune(ssb, content, additionsFeed.keys, cb)
})
})
})
})
Expand All @@ -174,8 +174,7 @@ module.exports = {
// prettier-ignore
if (err) return cb(clarify(err, 'Failed to add group tangle when publishing to a group'))

if (!contentSpec(content))
return cb(new Error(contentSpec.errorsString))
if (!isContent(content)) return cb(new Error(isContent.errorsString))

get(groupId, (err, { secret }) => {
// prettier-ignore
Expand Down Expand Up @@ -242,7 +241,7 @@ module.exports = {
return {
id: lodashGet(msg, 'value.content.recps[0]'),
secret: Buffer.from(
lodashGet(msg, 'value.content.secret'),
lodashGet(msg, 'value.content.groupKey'),
'base64'
),
root: lodashGet(msg, 'value.content.root'),
Expand Down
19 changes: 17 additions & 2 deletions lib/meta-feed-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const clarify = require('clarify-error')
const {
keySchemes,
validator: {
group: { init: initSpec },
group: { init: isInit },
},
} = require('private-group-spec')
const { SecretKey } = require('ssb-private-group-keys')
Expand Down Expand Up @@ -148,7 +148,7 @@ module.exports = (ssb) => {
group: { root: null, previous: null },
},
}
if (!initSpec(content)) return cb(new Error(initSpec.errorsString))
if (!isInit(content)) return cb(new Error(isInit.errorsString))

ssb.db.create(
{
Expand Down Expand Up @@ -237,6 +237,20 @@ module.exports = (ssb) => {
})
}

function getRootFeedIdFromMsgId(groupRootMsgId, cb) {
ssb.db.get(groupRootMsgId, (err, rootMsg) => {
// prettier-ignore
if (err) return cb(clarify(err, "couldn't get root msg for finding root feed"))

ssb.metafeeds.findRootFeedId(rootMsg.author, (err, rootFeedId) => {
// prettier-ignore
if (err) return cb(clarify(err, "couldn't find root feed id from root msg author"))

return cb(null, rootFeedId)
})
})
}

return {
secretKeyFromString,
findOrCreateAdditionsFeed,
Expand All @@ -245,5 +259,6 @@ module.exports = (ssb) => {
findOrCreateGroupFeed,
createGroupWithoutMembers,
findOrCreateGroupWithoutMembers,
getRootFeedIdFromMsgId,
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"ssb-box2": "^5.0.0",
"ssb-crut": "^4.6.1",
"ssb-db2": "^6.3.0",
"ssb-meta-feeds": "^0.38.2",
"ssb-meta-feeds": "^0.39.0",
"ssb-private-group-keys": "^1.1.1",
"ssb-ref": "^2.16.0",
"ssb-uri2": "^2.4.1"
Expand Down
14 changes: 11 additions & 3 deletions test/add-member.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ test('get added to a group', async (t) => {
id: groupId,
secret,
root,
} = await alice.tribes2.create().catch(t.fail)
} = await alice.tribes2.create().catch((err) => {
console.error("couldn't create group", err)
t.fail(err)
})
t.pass('alice created a group')

await alice.tribes2.addMembers(groupId, [bobRoot.id]).catch((err) => {
Expand All @@ -51,7 +54,10 @@ test('get added to a group', async (t) => {
await replicate(alice, bob).catch(t.fail)
t.pass('alice and bob replicate')

await bob.tribes2.acceptInvite(groupId)
await bob.tribes2.acceptInvite(groupId).catch((err) => {
console.error('failed to accept invite', err)
t.fail(err)
})

t.pass('bob accepted invite')

Expand Down Expand Up @@ -92,6 +98,7 @@ test('add member', async (t) => {
newPerson.tribes2.start()
t.pass('they start up tribes2')

const kaitiakiRoot = await p(kaitiaki.metafeeds.findOrCreate)()
const newPersonRoot = await p(newPerson.metafeeds.findOrCreate)()

await replicate(kaitiaki, newPerson)
Expand All @@ -116,8 +123,9 @@ test('add member', async (t) => {
const expected = {
type: 'group/add-member',
version: 'v2',
secret: group.secret.toString('base64'),
groupKey: group.secret.toString('base64'),
root: group.root,
creator: kaitiakiRoot.id,

text: 'welcome friends',
recps: [group.id, ...newMembers],
Expand Down
5 changes: 4 additions & 1 deletion test/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ test('create more', async (t) => {

const group = await ssb.tribes2.create().catch(t.fail)

const rootFeed = await p(ssb.metafeeds.findOrCreate)().catch(t.fail)

t.true(isIdentityGroupSSBURI(group.id), 'returns group identifier - groupId')
t.true(
Buffer.isBuffer(group.secret) && group.secret.length === 32,
Expand Down Expand Up @@ -65,7 +67,8 @@ test('create more', async (t) => {
{
type: 'group/add-member',
version: 'v2',
secret: group.secret.toString('base64'),
groupKey: group.secret.toString('base64'),
creator: rootFeed.id,
root: group.root,
recps: [group.id, root.id], // me being added to the group
tangles: {
Expand Down