Skip to content

Commit

Permalink
fix: Do not rely on potentially missing datetime
Browse files Browse the repository at this point in the history
Some images such as bitmaps do not have any `metadata` object.
However, we were relying on the existence of a `metadata.datetime` date
in order to build the cursor for fetching HasManyFiles relationship,
which was causing errors in the photos clustering service.
  • Loading branch information
paultranvan committed Feb 21, 2023
1 parent 0a17373 commit e49a1e9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
14 changes: 12 additions & 2 deletions packages/cozy-client/src/associations/HasManyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ const newCursor = ([doctype, id, lastDatetime], startDocId) => {
return [cursorKey, startDocId]
}

/**
* Get the file datetime
*
* @param {import('../types').IOCozyFile} file - io.cozy.files document
* @returns {string} The file datetime
*/
export const getFileDatetime = file => {
// Some files do not have any metadata, e.g. bitmap files.
return file.metadata?.datetime || file.created_at
}

/**
* This class is only used for photos albums relationships.
* Behind the hood, the queries uses a view returning the files sorted
Expand All @@ -48,8 +59,7 @@ export default class HasManyFiles extends HasMany {
lastRelationship._type,
lastRelationship._id
)
// Photos always have a datetime field in metadata
const lastDatetime = lastRelDoc.attributes.metadata.datetime
const lastDatetime = getFileDatetime(lastRelDoc.attributes)
// cursor-based pagination
const cursor = newCursor(
[this.target._type, this.target._id, lastDatetime],
Expand Down
20 changes: 19 additions & 1 deletion packages/cozy-client/src/associations/HasManyFiles.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DOCTYPE_FILES } from '../const'
import HasManyFiles from './HasManyFiles'
import HasManyFiles, { getFileDatetime } from './HasManyFiles'

describe('HasManyFiles', () => {
let originalFile, hydratedFile, originalTodo, hydratedTodo, save, mutate
Expand Down Expand Up @@ -155,3 +155,21 @@ describe('HasManyFiles', () => {
expect(queryDef.ids).toEqual(['1234'])
})
})

describe('getFileDatetime', () => {
it('should get the metadata datetime when it exists', () => {
const file = {
created_at: '2023-01-01',
metadata: {
datetime: '2023-02-01'
}
}
expect(getFileDatetime(file)).toEqual('2023-02-01')
})
it('should get the created_at when there is no datetime metadata', () => {
const file = {
created_at: '2023-01-01'
}
expect(getFileDatetime(file)).toEqual('2023-01-01')
})
})
1 change: 1 addition & 0 deletions packages/cozy-client/types/associations/HasManyFiles.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export function getFileDatetime(file: import('../types').IOCozyFile): string;
/**
* This class is only used for photos albums relationships.
* Behind the hood, the queries uses a view returning the files sorted
Expand Down

0 comments on commit e49a1e9

Please sign in to comment.