-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract BW diviner logic to in-memory helper
- Loading branch information
1 parent
57efca1
commit f593e1c
Showing
2 changed files
with
53 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...diviner/packages/boundwitness/packages/memory/src/applyBoundWitnessDivinerQueryPayload.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { containsAll } from '@xylabs/array' | ||
import { assertEx } from '@xylabs/assert' | ||
import { exists } from '@xylabs/exists' | ||
import { hexFromHexString } from '@xylabs/hex' | ||
import type { BoundWitness } from '@xyo-network/boundwitness-model' | ||
import { isBoundWitness } from '@xyo-network/boundwitness-model' | ||
import type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model' | ||
import type { Payload, WithMeta } from '@xyo-network/payload-model' | ||
|
||
type WithTimestamp = BoundWitness & { timestamp: number } | ||
const hasTimestamp = (bw: BoundWitness): bw is WithTimestamp => bw.timestamp !== undefined | ||
|
||
// eslint-disable-next-line complexity | ||
export const applyBoundWitnessDivinerQueryPayload = (filter?: BoundWitnessDivinerQueryPayload, payloads: Payload[] = []) => { | ||
if (!filter) return [] | ||
const { | ||
addresses, payload_hashes, payload_schemas, limit, offset, order = 'desc', sourceQuery, destination, timestamp, | ||
} = filter | ||
|
||
let bws = payloads.filter(isBoundWitness) as WithMeta<BoundWitness>[] | ||
if (order === 'desc') bws = bws.reverse() | ||
const allAddresses = addresses?.map(address => hexFromHexString(address)).filter(exists) | ||
if (allAddresses?.length) bws = bws.filter(bw => containsAll(bw.addresses, allAddresses)) | ||
if (payload_hashes?.length) bws = bws.filter(bw => containsAll(bw.payload_hashes, payload_hashes)) | ||
if (payload_schemas?.length) bws = bws.filter(bw => containsAll(bw.payload_schemas, payload_schemas)) | ||
if (sourceQuery) bws = bws.filter(bw => (bw?.$meta as { sourceQuery?: string })?.sourceQuery === sourceQuery) | ||
// If there's a destination filter of the right kind | ||
if (destination && Array.isArray(destination) && destination?.length > 0) { | ||
const targetFilter = assertEx(destination, () => 'Missing destination') | ||
// Find all BWs that satisfy the destination constraint | ||
bws = bws.filter((bw) => { | ||
const targetDestinationField = (bw?.$meta as { destination?: string[] })?.destination | ||
// If the destination field is an array and contains at least one element | ||
return targetDestinationField !== undefined && Array.isArray(targetDestinationField) && targetDestinationField.length > 0 | ||
// Check that the targetDestinationField contains all the elements in the targetFilter | ||
? containsAll(targetFilter, targetDestinationField ?? []) | ||
// Otherwise, filter it out | ||
: false | ||
}) | ||
} | ||
if (timestamp !== undefined) { | ||
bws | ||
= order === 'desc' | ||
? bws.filter(hasTimestamp).filter(bw => bw.timestamp <= timestamp) | ||
: bws.filter(hasTimestamp).filter(bw => bw.timestamp >= timestamp) | ||
} | ||
const parsedLimit = limit ?? bws.length | ||
const parsedOffset = offset ?? 0 | ||
return bws.slice(parsedOffset, parsedLimit) | ||
} |