Skip to content

Commit 0a3f319

Browse files
committed
added the element as a parameter to authorizers, just make it optional
1 parent 2ef95c9 commit 0a3f319

File tree

3 files changed

+52
-24
lines changed

3 files changed

+52
-24
lines changed

src/data/collections/causal/CausalArray.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,25 @@ class CausalArray<T> extends BaseCausalCollection<T> implements CausalCollection
204204
this._ordinals = [];
205205
}
206206

207-
canInsert(author?: Identity, extraAuth?: Authorizer): Promise<boolean> {
208-
return Authorization.chain(this.createInsertAuthorizer(author), extraAuth).attempt();
207+
// canInsert: if a parameter is absent, interpret it as if insertion is allowed for any possible value
208+
209+
// e.g.: element === undefined, can add independently of what is being added,
210+
// author === undefined, can add independently of who is doing it, etc.
211+
212+
// same goes for canDelete, canDeleteByHash.
213+
214+
canInsert(element?: T, _idx?: number, author?: Identity, extraAuth?: Authorizer): Promise<boolean> {
215+
216+
// TODO: translate the idx into an actual ordinal and actually pass it on!
217+
218+
return Authorization.chain(this.createInsertAtAuthorizer(element, undefined, author), extraAuth).attempt();
209219
}
210220

211-
canDelete(author?: Identity, extraAuth?: Authorizer): Promise<boolean> {
212-
return Authorization.chain(this.createDeleteAuthorizer(author), extraAuth).attempt();
221+
canDelete(author?: Identity, _idx?: number, extraAuth?: Authorizer): Promise<boolean> {
222+
223+
// TODO: find the actual ops that should be deleted and actually pass them on!
224+
225+
return Authorization.chain(this.createDeleteAuthorizer(undefined, author), extraAuth).attempt();
213226
}
214227

215228
async insertAt(element: T, idx: number, author?: Identity, extraAuth?: Authorizer): Promise<boolean> {
@@ -268,7 +281,7 @@ class CausalArray<T> extends BaseCausalCollection<T> implements CausalCollection
268281

269282
const insertOp = new InsertOp(this, element, ordinal, author);
270283

271-
const auth = Authorization.chain(this.createInsertAuthorizer(insertOp.getAuthor()), extraAuth);
284+
const auth = Authorization.chain(this.createInsertAtAuthorizer(element, ordinal, author), extraAuth);
272285

273286
this.setCurrentPrevOpsTo(insertOp);
274287

@@ -420,7 +433,7 @@ class CausalArray<T> extends BaseCausalCollection<T> implements CausalCollection
420433

421434
const deleteOp = new DeleteOp(insertOp, author);
422435

423-
const auth = Authorization.chain(this.createDeleteAuthorizer(deleteOp.getAuthor()), extraAuth);
436+
const auth = Authorization.chain(this.createDeleteAuthorizer(insertOp, author), extraAuth);
424437

425438
this.setCurrentPrevOpsTo(deleteOp);
426439

@@ -616,9 +629,9 @@ class CausalArray<T> extends BaseCausalCollection<T> implements CausalCollection
616629
const author = op.getAuthor();
617630

618631
const auth = (op instanceof InsertOp) ?
619-
this.createInsertAuthorizer(author)
632+
this.createInsertAtAuthorizer(op.element, op.ordinal, author)
620633
:
621-
this.createDeleteAuthorizer(author);
634+
this.createDeleteAuthorizer(op.getInsertOp(), author);
622635

623636
const usedKeys = new Set<string>();
624637

@@ -723,11 +736,11 @@ class CausalArray<T> extends BaseCausalCollection<T> implements CausalCollection
723736

724737
}
725738

726-
protected createInsertAuthorizer(author?: Identity): Authorizer {
739+
protected createInsertAtAuthorizer(_element?: T, _ordinal?: Ordinal, author?: Identity): Authorizer {
727740
return this.createWriteAuthorizer(author);
728741
}
729742

730-
protected createDeleteAuthorizer(author?: Identity): Authorizer {
743+
protected createDeleteAuthorizer(_insertOp?: InsertOp<T>, author?: Identity): Authorizer {
731744
return this.createWriteAuthorizer(author);
732745
}
733746

src/data/collections/causal/CausalSet.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,23 @@ class CausalSet<T> extends BaseCausalCollection<T> implements CausalCollection<T
218218
return CausalSet.className;
219219
}
220220

221-
canAdd(author?: Identity, extraAuth?: Authorizer): Promise<boolean> {
222-
return Authorization.chain(this.createAddAuthorizer(author), extraAuth).attempt();
221+
// canAdd: if a parameter is absent, interpret it as if addition is allowed for any possible value
222+
223+
// e.g.: element === undefined, can add independently of what is being added,
224+
// author === undefined, can add independently of who is doing it, etc.
225+
226+
// same goes for canDelete, canDeleteByHash.
227+
228+
canAdd(element?: T, author?: Identity, extraAuth?: Authorizer): Promise<boolean> {
229+
return Authorization.chain(this.createAddAuthorizer(element, author), extraAuth).attempt();
230+
}
231+
232+
canDelete(element?: T, author?: Identity, extraAuth?: Authorizer): Promise<boolean> {
233+
return Authorization.chain(this.createDeleteAuthorizer(element, author), extraAuth).attempt();
223234
}
224235

225-
canDelete(author?: Identity, extraAuth?: Authorizer): Promise<boolean> {
226-
return Authorization.chain(this.createDeleteAuthorizer(author), extraAuth).attempt();
236+
canDeleteByHash(elementHash?: Hash, author?: Identity, extraAuth?: Authorizer): Promise<boolean> {
237+
return Authorization.chain(this.createDeleteAuthorizerByHash(elementHash, author), extraAuth).attempt();
227238
}
228239

229240

@@ -239,7 +250,7 @@ class CausalSet<T> extends BaseCausalCollection<T> implements CausalCollection<T
239250

240251
const addOp = new AddOp(this, elmt, author);
241252

242-
const auth = Authorization.chain(this.createAddAuthorizer(addOp.getAuthor()), extraAuth);
253+
const auth = Authorization.chain(this.createAddAuthorizer(elmt, addOp.getAuthor()), extraAuth);
243254

244255
this.setCurrentPrevOpsTo(addOp);
245256

@@ -266,7 +277,7 @@ class CausalSet<T> extends BaseCausalCollection<T> implements CausalCollection<T
266277
const addOp = this._currentAddOps.get(addOpHash) as AddOp<T>;
267278
const deleteOp = new DeleteOp(addOp, author);
268279

269-
const auth = Authorization.chain(this.createDeleteAuthorizer(deleteOp.getAuthor()), extraAuth);
280+
const auth = Authorization.chain(this.createDeleteAuthorizerByHash(hash, deleteOp.getAuthor()), extraAuth);
270281

271282
this.setCurrentPrevOpsTo(deleteOp);
272283

@@ -395,9 +406,9 @@ class CausalSet<T> extends BaseCausalCollection<T> implements CausalCollection<T
395406
const author = op.getAuthor();
396407

397408
const auth = (op instanceof AddOp) ?
398-
this.createAddAuthorizer(author)
409+
this.createAddAuthorizer(op.element, author)
399410
:
400-
this.createDeleteAuthorizer(author);;
411+
this.createDeleteAuthorizer(op.getAddOp().element, author);;
401412

402413
const usedKeys = new Set<string>();
403414

@@ -537,11 +548,15 @@ class CausalSet<T> extends BaseCausalCollection<T> implements CausalCollection<T
537548
return found;
538549
}
539550

540-
protected createAddAuthorizer(author?: Identity): Authorizer {
551+
protected createAddAuthorizer(_element?: T, author?: Identity): Authorizer {
552+
return this.createWriteAuthorizer(author);
553+
}
554+
555+
protected createDeleteAuthorizer(_element?: T, author?: Identity): Authorizer {
541556
return this.createWriteAuthorizer(author);
542557
}
543558

544-
protected createDeleteAuthorizer(author?: Identity): Authorizer {
559+
protected createDeleteAuthorizerByHash(_elementHash?: Hash, author?: Identity): Authorizer {
545560
return this.createWriteAuthorizer(author);
546561
}
547562

src/data/collections/causal/SingleAuthorCausalSet.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ class SingleAuthorCausalSet<T> extends CausalSet<T> {
5858
return this.getAuthor() !== undefined;
5959
}
6060

61-
protected createAddAuthorizer(author: Identity): Authorizer {
61+
protected createAddAuthorizer(_element?: T, author?: Identity): Authorizer {
6262

63-
if (author.equals(this.getAuthor())) {
63+
if (author !== undefined && author.equals(this.getAuthor())) {
6464
return Authorization.always;
6565
} else {
6666
return Authorization.never;
6767
}
6868
}
6969

70-
protected createDeleteAuthorizerByHash(_elmtHash: Hash, author: Identity): Authorizer {
70+
protected createDeleteAuthorizerByHash(_elmtHash?: Hash, author?: Identity): Authorizer {
7171

72-
if (author.equals(this.getAuthor())) {
72+
if (author !== undefined && author.equals(this.getAuthor())) {
7373
return Authorization.always;
7474
} else {
7575
return Authorization.never;

0 commit comments

Comments
 (0)