Skip to content

Commit

Permalink
handle multiple recipient and author record queries
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Aug 15, 2024
1 parent 99964a0 commit 708bb64
Show file tree
Hide file tree
Showing 6 changed files with 455 additions and 18 deletions.
20 changes: 14 additions & 6 deletions json-schemas/interface-methods/records-filter.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,27 @@
"type": "string"
},
"author": {
"type": "array",
"items": {
"oneOf": [{
"$ref": "https://identity.foundation/dwn/json-schemas/defs.json#/$defs/did"
}
},{
"type": "array",
"items": {
"$ref": "https://identity.foundation/dwn/json-schemas/defs.json#/$defs/did"
}
}]
},
"attester": {
"$ref": "https://identity.foundation/dwn/json-schemas/defs.json#/$defs/did"
},
"recipient": {
"type": "array",
"items": {
"oneOf": [{
"$ref": "https://identity.foundation/dwn/json-schemas/defs.json#/$defs/did"
}
},{
"type": "array",
"items": {
"$ref": "https://identity.foundation/dwn/json-schemas/defs.json#/$defs/did"
}
}]
},
"contextId": {
"type": "string"
Expand Down
9 changes: 4 additions & 5 deletions src/handlers/records-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,14 @@ export class RecordsQueryHandler implements MethodHandler {
}

if (Records.filterIncludesUnpublishedRecords(filter)) {
filters.push(RecordsQueryHandler.buildUnpublishedRecordsByQueryAuthorFilter(recordsQuery));

const recipientFilter = recordsQuery.message.descriptor.filter.recipient;
if (recipientFilter === undefined || recipientFilter.length === 1 && recipientFilter.includes(recordsQuery.author!)) {
filters.push(RecordsQueryHandler.buildUnpublishedRecordsForQueryAuthorFilter(recordsQuery));
if (Records.shouldBuildUnpublishedAuthorFilter(filter, recordsQuery.author!)) {
filters.push(RecordsQueryHandler.buildUnpublishedRecordsByQueryAuthorFilter(recordsQuery));
}

if (Records.shouldProtocolAuthorize(recordsQuery.signaturePayload!)) {
filters.push(RecordsQueryHandler.buildUnpublishedProtocolAuthorizedRecordsFilter(recordsQuery));
} else if (Records.shouldBuildUnpublishedRecipientFilter(filter, recordsQuery.author!)) {
filters.push(RecordsQueryHandler.buildUnpublishedRecordsForQueryAuthorFilter(recordsQuery));
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/handlers/records-subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,10 @@ export class RecordsSubscribeHandler implements MethodHandler {
if (Records.filterIncludesUnpublishedRecords(filter)) {
filters.push(RecordsSubscribeHandler.buildUnpublishedRecordsBySubscribeAuthorFilter(recordsSubscribe));

const recipientFilter = recordsSubscribe.message.descriptor.filter.recipient;
if (recipientFilter === undefined || recipientFilter.length === 1 && recipientFilter.includes(recordsSubscribe.author!)) {
filters.push(RecordsSubscribeHandler.buildUnpublishedRecordsForSubscribeAuthorFilter(recordsSubscribe));
}

if (Records.shouldProtocolAuthorize(recordsSubscribe.signaturePayload!)) {
filters.push(RecordsSubscribeHandler.buildUnpublishedProtocolAuthorizedRecordsFilter(recordsSubscribe));
} else if (Records.shouldBuildUnpublishedRecipientFilter(filter, recordsSubscribe.author!)) {
filters.push(RecordsSubscribeHandler.buildUnpublishedRecordsForSubscribeAuthorFilter(recordsSubscribe));
}
}
return filters;
Expand Down
4 changes: 2 additions & 2 deletions src/types/records-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ export type RecordsFilter = {
/**
* The logical author of the record
*/
author?: string[];
author?: string | string[];
attester?: string;
recipient?: string[];
recipient?: string | string[];
protocol?: string;
protocolPath?: string;
published?: boolean;
Expand Down
16 changes: 16 additions & 0 deletions src/utils/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,20 @@ export class Records {
}
return filter.published === false;
}

static shouldBuildUnpublishedRecipientFilter(filter: RecordsFilter, recipient: string): boolean {
const { recipient: recipientFilter } = filter;

return Array.isArray(recipientFilter) ?
recipientFilter.length === 0 || recipientFilter.includes(recipient) :
recipientFilter === undefined || recipientFilter === recipient;
}

static shouldBuildUnpublishedAuthorFilter(filter: RecordsFilter, author: string): boolean {
const { author: authorFilter } = filter;

return Array.isArray(authorFilter) ?
authorFilter.length === 0 || authorFilter.includes(author) :
authorFilter === undefined || authorFilter === author;
}
}
Loading

0 comments on commit 708bb64

Please sign in to comment.