Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Send existing files index to pilot fetch #1089

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/app/domain/manifest/permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { KonnectorsDocument } from 'cozy-client/types/types'

export const hasPermission = (
konnector: KonnectorsDocument,
permission: string
): boolean => {
return Object.values(konnector.permissions ?? []).some(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
perm => perm?.type === permission // Unsafe member access .type on an `any` value
)
}
8 changes: 8 additions & 0 deletions src/libs/ReactNativeLauncher.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
activateKeepAwake,
deactivateKeepAwake
} from '/app/domain/sleep/services/sleep'
import { hasPermission } from '/app/domain/manifest/permissions'

const log = Minilog('ReactNativeLauncher')

Expand Down Expand Up @@ -346,6 +347,13 @@ class ReactNativeLauncher extends Launcher {
job,
sourceAccountIdentifier
}

if (hasPermission(manifest, 'io.cozy.files')) {
const existingFilesIndex = await this.getExistingFilesIndex()
const serializableExistingFilesIndex =
Object.fromEntries(existingFilesIndex)
pilotContext.existingFilesIndex = serializableExistingFilesIndex
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the flagship app will have more and more to check the permissions. We should start creating a new domain (aka folder) to deal with that instead of adding the complexity here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Crash-- @acezard I updated the code to have a manifest domain. I think this is ready.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a wrapTime to that method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A wrapTime to _start or hasPermission ? I'm not sure

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getExistingFilesIndex ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already done :

this.getExistingFilesIndex = wrapTimer(this, 'getExistingFilesIndex')

await this.pilot.call('fetch', pilotContext)
await this.stop()
} catch (err) {
Expand Down
46 changes: 46 additions & 0 deletions src/libs/ReactNativeLauncher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('ReactNativeLauncher', () => {
save: jest.fn(),
create: jest.fn(),
query: jest.fn(),
queryAll: jest.fn(),
destroy: jest.fn(),
getStackClient: () => ({
uri: 'http://cozy.localhost:8080'
Expand Down Expand Up @@ -310,12 +311,57 @@ describe('ReactNativeLauncher', () => {
errorMessage: `getUserDataFromWebsite did not return any sourceAccountIdentifier. Cannot continue the execution.`
})
})
it('should send existingFilesIndex to the konnector if it has files permission', async () => {
const { launcher, client, launch } = setup()
launcher.setStartContext({
client,
account: fixtures.account,
trigger: fixtures.trigger,
manifest: { permissions: { files: { type: 'io.cozy.files' } } },
konnector: {
slug: 'testkonnector',
name: 'Test Konnector'
},
launcherClient: {
setAppMetadata: () => null
}
})
client.query.mockResolvedValue({ data: fixtures.account })
client.queryAll.mockResolvedValueOnce([
{
name: 'file1.txt',
metadata: { fileIdAttributes: 'sourceaccountidentifier1' }
}
])
client.save.mockImplementation(async doc => ({ data: doc }))
launch.mockResolvedValue({ data: fixtures.job })
launcher.pilot.call
.mockResolvedValueOnce(true)
.mockResolvedValueOnce(true)
.mockResolvedValueOnce({
sourceAccountIdentifier: 'testsourceaccountidentifier'
})
.mockResolvedValueOnce(true) // fetch
await launcher.start()
expect(launcher.pilot.call).toHaveBeenLastCalledWith(
'fetch',
expect.objectContaining({
existingFilesIndex: {
sourceaccountidentifier1: {
name: 'file1.txt',
metadata: { fileIdAttributes: 'sourceaccountidentifier1' }
}
}
})
)
})
it('should update job with error message on error', async () => {
const { launcher, client, launch } = setup()
launcher.setStartContext({
client,
account: fixtures.account,
trigger: fixtures.trigger,
manifest: {},
konnector: {
slug: 'testkonnector',
name: 'Test Konnector'
Expand Down
Loading