Skip to content

Commit c69dba7

Browse files
making follow up request active
1 parent 66b83aa commit c69dba7

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

src/Permissions.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,6 @@ function pushToMapped <K, V> (map: Map<K, V[]>, key: K, value: V): number {
1818
return list.push(value)
1919
}
2020

21-
function deleteFromMapped <K, V> (map: Map<K, V[]>, key: K, value: V): boolean {
22-
const list = map.get(key)
23-
if (list === undefined) {
24-
return false
25-
}
26-
const index = list.indexOf(value)
27-
if (index === -1) {
28-
return false
29-
}
30-
if (list.length === 1) {
31-
map.delete(key)
32-
return true
33-
}
34-
list.splice(index, 1)
35-
return true
36-
}
37-
3821
export class Permissions {
3922
readonly members = new States<MemberState>()
4023
readonly requests = new States<RequestState>()
@@ -114,6 +97,15 @@ export class Permissions {
11497
const amountMembers = this.members.byState.added?.size ?? 0
11598
// The signature of the member that created the request is not necessary
11699
const neededSignatures = amountMembers - 1
100+
if (
101+
// Remove operations are okay with having one less
102+
request.operation === 'remove' &&
103+
// Two members can form a majority, to remove one of two members
104+
// unilaterally is impossible
105+
amountMembers > 2
106+
) {
107+
return neededSignatures - 1
108+
}
117109
return neededSignatures
118110
}
119111

@@ -136,7 +128,19 @@ export class Permissions {
136128
this.requests.set(request.id, 'finished')
137129
this.openRequests.delete(request.id)
138130
this.signatures.delete(request.id)
139-
deleteFromMapped(this.openRequestsByMember, request.from, request)
131+
const list = this.openRequestsByMember.get(request.from)
132+
if (list === undefined) {
133+
throw new Error('This may never occur')
134+
}
135+
if (list.shift() !== request) {
136+
throw new Error('This may also never occur')
137+
}
138+
const entry = list[0]
139+
if (entry === undefined) {
140+
this.openRequestsByMember.delete(request.from)
141+
} else {
142+
this.requests.set(entry.id, 'active')
143+
}
140144
return
141145
}
142146
throw new Error('todo')

test/Permissions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,16 @@ test('Two members need to confirm the add-request', t => {
213213
t.deepEquals(p.members.byState.added, new Set([memberA, memberB, memberC, memberD]))
214214
t.end()
215215
})
216+
217+
test('Previously pending requests need to become active', t => {
218+
const p = new Permissions()
219+
p.add(request({ operation: 'add', who: memberA, id: '1', from: memberA }))
220+
p.add(request({ operation: 'add', who: memberB, from: memberA }))
221+
p.add(request({ operation: 'add', id: '1', who: memberC, from: memberA }))
222+
p.add(request({ operation: 'add', id: '2', who: memberD, from: memberA }))
223+
p.add(request({ operation: 'add', id: '3', who: memberB, from: memberA }))
224+
p.add(response({ response: 'accept', id: '1', from: memberB }))
225+
t.equals(p.requests.get('2'), 'active')
226+
t.equals(p.requests.get('3'), 'pending')
227+
t.end()
228+
})

0 commit comments

Comments
 (0)