Skip to content

Commit

Permalink
Return BW as part of sources
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Oct 5, 2023
1 parent 887d36c commit b5f15cd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,39 +75,44 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
})
const batch = await boundWitnessDiviner.divine([query])
if (batch.length === 0) return
const imageThumbnailTimestampTuples = batch
.filter(isBoundWitness)
.map((bw) => {
const imageThumbnailIndexes = bw.payload_schemas?.map((schema, index) => (schema === ImageThumbnailSchema ? index : undefined)).filter(exists)
const timestampIndex = bw.payload_schemas?.findIndex((schema) => schema === TimestampSchema)
if (!imageThumbnailIndexes.length || timestampIndex === -1) return undefined
const imageThumbnails = bw.payload_hashes.map((hash, index) => (imageThumbnailIndexes.includes(index) ? hash : undefined)).filter(exists)
const timestamp = bw.payload_hashes?.[timestampIndex]
return imageThumbnails.map((imageThumbnail) => [imageThumbnail, timestamp] as const)
})
const imageThumbnailTimestampTuples = (
await Promise.all(
batch.filter(isBoundWitness).map(async (bw) => {
const imageThumbnailIndexes = bw.payload_schemas
?.map((schema, index) => (schema === ImageThumbnailSchema ? index : undefined))
.filter(exists)
const timestampIndex = bw.payload_schemas?.findIndex((schema) => schema === TimestampSchema)
if (!imageThumbnailIndexes.length || timestampIndex === -1) return undefined
const imageThumbnails = bw.payload_hashes.map((hash, index) => (imageThumbnailIndexes.includes(index) ? hash : undefined)).filter(exists)
const timestamp = bw.payload_hashes?.[timestampIndex]
const boundWitnessHash = await PayloadHasher.hashAsync(bw)
return imageThumbnails.map((imageThumbnail) => [boundWitnessHash, imageThumbnail, timestamp] as const)
}),
)
)
.flat()
.filter(exists)
const archivist = await this.getArchivistForStore('thumbnailStore')
const payloadTuples = (
await Promise.all(
imageThumbnailTimestampTuples.map(async ([imageThumbnailHash, timestampHash]) => {
imageThumbnailTimestampTuples.map(async ([boundWitnessHash, imageThumbnailHash, timestampHash]) => {
const results = await archivist.get([imageThumbnailHash, timestampHash])
const imageThumbnailPayload = results.find(isImageThumbnail)
const timestampPayload = results.find(isTimestamp)
if (!imageThumbnailPayload || !timestampPayload) return undefined
const calculatedImageThumbnailHash = await PayloadHasher.hashAsync(imageThumbnailPayload)
const calculatedTimestampHash = await PayloadHasher.hashAsync(timestampPayload)
if (imageThumbnailHash !== calculatedImageThumbnailHash || timestampHash !== calculatedTimestampHash) return undefined
return [imageThumbnailHash, imageThumbnailPayload, timestampHash, timestampPayload] as const
return [boundWitnessHash, imageThumbnailHash, imageThumbnailPayload, timestampHash, timestampPayload] as const
}),
)
).filter(exists)
// Build index results
const indexedResults = payloadTuples.map(([thumbnailHash, thumbnailPayload, timestampHash, timestampPayload]) => {
const indexedResults = payloadTuples.map(([boundWitnessHash, thumbnailHash, thumbnailPayload, timestampHash, timestampPayload]) => {
const { sourceUrl: url } = thumbnailPayload
const { timestamp } = timestampPayload
const status = thumbnailPayload.http?.status ?? -1
const sources = [thumbnailHash, timestampHash]
const sources = [boundWitnessHash, thumbnailHash, timestampHash]
const result = new PayloadBuilder<ImageThumbnailResult>({ schema: ImageThumbnailResultIndexSchema })
.fields({ sources, status, timestamp, url })
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,8 @@ describe('ImageThumbnailDiviner', () => {
const payload: UrlPayload = { schema: UrlSchema, url: successfulThumbnail.sourceUrl }
const result = await sut.divine([payload])
expect(result).toBeArrayOfSize(1)
const actual = result[0]?.sources?.[0]
const expected = await PayloadHasher.hashAsync(successfulThumbnail)
expect(actual).toEqual(expected)
expect(result[0]?.sources).toContain(expected)
})
})
})

0 comments on commit b5f15cd

Please sign in to comment.