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

Fix 'previous' in the members tangle #88

Merged
merged 19 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
28 changes: 18 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,26 @@ function init (ssb, config) {
if (!isGroup(content.recps[0])) return publish.apply(this, args)

onKeystoreReady(() => {
getGroupTangle(content.recps[0], (err, tangle) => {
// NOTE there are two ways an err can occur in getGroupTangle
// 1. recps is not a groupId
// 2. unknown groupId,

// Rather than cb(err) here we we pass it on to boxers to see if an err is needed
getGroupTangle(content.recps[0], 'group', (err, groupTangle) => {
if (err) return publish.apply(this, args)
getGroupTangle(content.recps[0], 'members', (err, membersTangle) => {
// NOTE there are two ways an err can occur in getGroupTangle
// 1. recps is not a groupId
// 2. unknown groupId,

// Rather than cb(err) here we we pass it on to boxers to see if an err is needed
// TODO: why do we skip adding the tangles if we err? we should skip publishing in that case
if (err) return publish.apply(this, args)

set(content, 'tangles.group', groupTangle)
if (content.type === 'group/add-member' || content.type === 'group/exclude-member') {
set(content, 'tangles.members', membersTangle)
}
tanglePrune(content) // prune the group tangle down if needed
// TODO: prune members tangle too?

set(content, 'tangles.group', tangle)
tanglePrune(content) // prune the group tangle down if needed

publish.call(this, content, cb)
publish.call(this, content, cb)
})
Copy link
Member

Choose a reason for hiding this comment

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

the maybeMembersTangle is not something I'm fond of. Prefer early return?

getGroupTangle(content.recps[0], (err, groupTangle) => {
  if (err) return cb(Error("Couldn't get group tangle", { cause: err }))

  set(content, 'tangles.group', groupTangle)
  tanglePrune(content, 'group')

  if (!memberType(content.type)) {
    return publish.call(this, content, cb)
  }

  getMembersTangle(content.recps[0], (err, membersTangle) => {
    if (err) return cb(Error("Couldn't get members tangle", { cause: err }))

    set(content, 'tangles.members', membersTangle)
    tanglePrune(content, 'members')
    publish.call(this, content, cb)
  })

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yeah that's smart

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

})
})
})
Expand Down
11 changes: 7 additions & 4 deletions lib/get-group-tangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const Strategy = require('@tangle/strategy')

// for figuring out what "previous" should be for the group

const TANGLE = 'group'
const strategy = new Strategy({})

module.exports = function GetGroupTangle (server, keystore) {
Expand Down Expand Up @@ -42,7 +41,11 @@ module.exports = function GetGroupTangle (server, keystore) {
}
}

return function getGroupTangle (groupId, cb) {
return function getGroupTangle (groupId, tangle, cb) {
if (typeof tangle === 'function') {
cb = tangle
tangle = 'group'
}
if (!isGroup(groupId)) return cb(new Error(`get-group-tangle expects valid groupId, got: ${groupId}`))

const info = keystore.group.get(groupId)
Expand All @@ -64,7 +67,7 @@ module.exports = function GetGroupTangle (server, keystore) {
value: {
content: {
tangles: {
[TANGLE]: { root: info.root }
[tangle]: { root: info.root }
}
}
}
Expand All @@ -73,7 +76,7 @@ module.exports = function GetGroupTangle (server, keystore) {
{
$map: {
key: ['key'],
previous: ['value', 'content', 'tangles', TANGLE, 'previous']
previous: ['value', 'content', 'tangles', tangle, 'previous']
}
}
]
Expand Down
8 changes: 2 additions & 6 deletions method/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,9 @@ module.exports = function GroupMethods (ssb, keystore, state) {
type: 'group/exclude-member',
excludes: authorIds,
tangles: {
members: {
root,
previous: [root] // TODO calculate previous for members tangle
},

members: { root, previous: [root] },
group: { root, previous: [root] }
// NOTE: this is a dummy entry which is over-written in publish hook
// NOTE: these are dummy entries which are over-written in the publish hook
},
recps: [groupId]
}
Expand Down
2 changes: 1 addition & 1 deletion test/api/exclude-members.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test('tribes.excludeMembers', async t => {
excludes: authorIds,

tangles: {
members: { root: groupInitMsg.key, previous: [groupInitMsg.key] },
members: { root: groupInitMsg.key, previous: [...exclude.content.tangles.members.previous] },
group: { root: groupInitMsg.key, previous: [...exclude.content.tangles.group.previous] }
},
recps: [groupId]
Expand Down
34 changes: 34 additions & 0 deletions test/lib/get-group-tangle.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { promisify: p } = require('util')
const test = require('tape')
const { Server, replicate } = require('../helpers')
const pull = require('pull-stream')
Expand Down Expand Up @@ -286,3 +287,36 @@ test('get-group-tangle with branch', t => {
})
}
})

test('members tangle', async t => {
const alice = Server()
const bob = Server()

const { groupId, root } = await p(alice.tribes.create)({})
const bobInvite = await p(alice.tribes.invite)(groupId, [bob.id], {})

const keystore = { group: { get: () => ({ root }) } }

const getGroupTangle = p(GetGroupTangle(alice, keystore))

const firstGroup = await getGroupTangle(groupId, 'group')
const firstMembers = await getGroupTangle(groupId, 'members')

t.deepEqual(firstGroup, { root, previous: [bobInvite.key] }, 'group tangle generated after add msg is correct')
t.deepEqual(firstMembers, { root, previous: [bobInvite.key] }, 'members tangle generated after add msg is correct')

const { key: bobExcludeKey } = await p(alice.tribes.excludeMembers)(groupId, [bob.id])
const bobExclude = await p(alice.get)({ id: bobExcludeKey, private: true })

t.deepEqual(bobExclude.content.tangles, { group: firstGroup, members: firstMembers }, 'exclude message gets tangles')

const secondGroup = await getGroupTangle(groupId, 'group')
const secondMembers = await getGroupTangle(groupId, 'members')

t.deepEqual(secondGroup, { root, previous: [bobExcludeKey] }, 'group tangle generated after exclude msg is correct')
t.deepEqual(secondMembers, { root, previous: [bobExcludeKey] }, 'members tangle generated after exclude msg is correct')

await Promise.all([p(alice.close)(), p(bob.close)()])

t.end()
})