-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: When photos are trashed, delete the reference to albums
- Loading branch information
Showing
7 changed files
with
229 additions
and
18 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
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,51 @@ | ||
import { getReferencedBy } from 'cozy-client' | ||
import log from 'cozy-logger' | ||
import pLimit from 'p-limit' | ||
|
||
import { buildPhotosTrashedWithReferencedBy } from 'photos/queries/queries' | ||
import { DOCTYPE_ALBUMS, DOCTYPE_FILES } from 'drive/lib/doctypes' | ||
|
||
const onPhotoTrashed = async client => { | ||
const photosTrashedQuery = buildPhotosTrashedWithReferencedBy() | ||
try { | ||
const photos = await client.queryAll( | ||
photosTrashedQuery.definition, | ||
photosTrashedQuery.options | ||
) | ||
|
||
if (photos.length > 0) { | ||
log( | ||
'info', | ||
`Start deleting album references on ${photos.length} trashed photos` | ||
) | ||
const fileCollection = client.collection(DOCTYPE_FILES) | ||
const limit = pLimit(20) | ||
|
||
const removePromises = [] | ||
photos.forEach(photo => { | ||
const ablumsReferenced = getReferencedBy(photo, DOCTYPE_ALBUMS) | ||
if (ablumsReferenced.length > 0) { | ||
const albumsReferencedNormalized = ablumsReferenced.map(c => ({ | ||
_id: c.id, | ||
_type: c.type | ||
})) | ||
removePromises.push( | ||
limit(() => | ||
fileCollection.removeReferencedBy( | ||
photo, | ||
albumsReferencedNormalized | ||
) | ||
) | ||
) | ||
} | ||
}) | ||
|
||
await Promise.all(removePromises) | ||
} | ||
} catch (e) { | ||
log('error', 'Failure to delete references with albums on trashed photos') | ||
log('error', e) | ||
} | ||
} | ||
|
||
export { onPhotoTrashed } |
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,117 @@ | ||
import { getReferencedBy } from 'cozy-client' | ||
import log from 'cozy-logger' | ||
|
||
import { onPhotoTrashed } from 'photos/lib/onPhotoTrashed' | ||
|
||
jest.mock('cozy-logger') | ||
jest.mock('cozy-client', () => ({ | ||
...jest.requireActual('cozy-client'), | ||
getReferencedBy: jest.fn() | ||
})) | ||
|
||
describe('onPhotoTrashed', () => { | ||
beforeEach(() => { | ||
getReferencedBy.mockReturnValue([]) | ||
}) | ||
|
||
it('should not remove album references if there are no trashed photos', async () => { | ||
const removeSpy = jest.fn() | ||
const mockClient = { | ||
queryAll: () => [], | ||
collection: () => ({ removeReferencedBy: removeSpy }) | ||
} | ||
|
||
await onPhotoTrashed(mockClient) | ||
|
||
expect(removeSpy).toHaveBeenCalledTimes(0) | ||
expect(log).toBeCalledTimes(0) | ||
}) | ||
|
||
it('should remove album references from trashed photos', async () => { | ||
const removeSpy = jest.fn() | ||
const mockClient = { | ||
queryAll: () => [ | ||
{ | ||
_id: 'photo_1' | ||
}, | ||
{ | ||
_id: 'photo_2' | ||
} | ||
], | ||
collection: () => ({ removeReferencedBy: removeSpy }) | ||
} | ||
getReferencedBy | ||
.mockReturnValueOnce([ | ||
{ | ||
id: '123', | ||
type: 'albums' | ||
}, | ||
{ | ||
id: '456', | ||
type: 'albums' | ||
} | ||
]) | ||
.mockReturnValueOnce([]) | ||
|
||
await onPhotoTrashed(mockClient) | ||
|
||
expect(removeSpy).toBeCalledTimes(1) | ||
expect(removeSpy).toBeCalledWith({ _id: 'photo_1' }, [ | ||
{ | ||
_id: '123', | ||
_type: 'albums' | ||
}, | ||
{ | ||
_id: '456', | ||
_type: 'albums' | ||
} | ||
]) | ||
}) | ||
|
||
it('should not remove album references if there are no references', async () => { | ||
const removeSpy = jest.fn() | ||
const mockClient = { | ||
queryAll: [ | ||
{ | ||
_id: 'photo_1' | ||
} | ||
], | ||
collection: () => ({ removeReferencedBy: removeSpy }) | ||
} | ||
|
||
await onPhotoTrashed(mockClient) | ||
|
||
expect(removeSpy).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
it('should handle errors when removing album references', async () => { | ||
const error = new Error('Failed to remove album references') | ||
const removeSpy = jest.fn(() => { | ||
throw new Error('Failed to remove album references') | ||
}) | ||
const mockClient = { | ||
queryAll: () => [ | ||
{ | ||
_id: 'photo_1' | ||
} | ||
], | ||
collection: () => ({ removeReferencedBy: removeSpy }) | ||
} | ||
getReferencedBy.mockReturnValue([ | ||
{ | ||
id: '123', | ||
type: 'albums' | ||
} | ||
]) | ||
|
||
await onPhotoTrashed(mockClient) | ||
|
||
expect(removeSpy).toBeCalledWith({ _id: 'photo_1' }, [ | ||
{ | ||
_id: '123', | ||
_type: 'albums' | ||
} | ||
]) | ||
expect(log).toHaveBeenLastCalledWith('error', error) | ||
}) | ||
}) |
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
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
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,30 @@ | ||
import fetch from 'node-fetch' | ||
global.fetch = fetch | ||
|
||
import CozyClient from 'cozy-client' | ||
import { Document } from 'cozy-doctypes' | ||
import log from 'cozy-logger' | ||
|
||
import { onPhotoTrashed } from 'photos/lib/onPhotoTrashed' | ||
|
||
const attachProcessEventHandlers = () => { | ||
process.on('uncaughtException', err => { | ||
log('warn', JSON.stringify(err.stack)) | ||
}) | ||
|
||
process.on('unhandledRejection', err => { | ||
log('warn', JSON.stringify(err.stack)) | ||
}) | ||
} | ||
|
||
const main = async () => { | ||
attachProcessEventHandlers() | ||
const client = CozyClient.fromEnv(process.env) | ||
Document.registerClient(client) | ||
await onPhotoTrashed(client) | ||
} | ||
|
||
main().catch(e => { | ||
log('critical', e) | ||
process.exit(1) | ||
}) |
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