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

[stable28] feat(editLocallyAction): Handle possible no local client scenario #46909

Merged
merged 3 commits into from
Jul 31, 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
47 changes: 38 additions & 9 deletions apps/files/src/actions/editLocallyAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,35 @@
import { action } from './editLocallyAction'
import { expect } from '@jest/globals'
import { File, Permission, View, FileAction } from '@nextcloud/files'
import * as ncDialogs from '@nextcloud/dialogs'
import { DialogBuilder, showError } from '@nextcloud/dialogs'
import axios from '@nextcloud/axios'

const dialogBuilder = {
setName: jest.fn().mockReturnThis(),
setText: jest.fn().mockReturnThis(),
setButtons: jest.fn().mockReturnThis(),
build: jest.fn().mockReturnValue({
show: jest.fn().mockResolvedValue(true),
}),
} as unknown as DialogBuilder

jest.mock('@nextcloud/dialogs', () => ({
DialogBuilder: jest.fn(() => dialogBuilder),
showError: jest.fn(),
}))

const view = {
id: 'files',
name: 'Files',
} as View

// Mock webroot variable
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any)._oc_webroot = '';
(window as any).OCA = { Viewer: { open: jest.fn() } }
})

describe('Edit locally action conditions tests', () => {
test('Default values', () => {
expect(action).toBeInstanceOf(FileAction)
Expand All @@ -55,7 +76,7 @@ describe('Edit locally action enabled tests', () => {
expect(action.enabled!([file], view)).toBe(true)
})

test('Disabled for non-dav ressources', () => {
test('Disabled for non-dav resources', () => {
const file = new File({
id: 1,
source: 'https://domain.com/data/foobar.txt',
Expand Down Expand Up @@ -115,8 +136,11 @@ describe('Edit locally action enabled tests', () => {

describe('Edit locally action execute tests', () => {
test('Edit locally opens proper URL', async () => {
jest.spyOn(axios, 'post').mockImplementation(async () => ({ data: { ocs: { data: { token: 'foobar' } } } }))
jest.spyOn(ncDialogs, 'showError')
jest.spyOn(axios, 'post').mockImplementation(async () => ({
data: { ocs: { data: { token: 'foobar' } } }
}))
const mockedShowError = jest.mocked(showError)
const spyDialogBuilder = jest.spyOn(dialogBuilder, 'build')

const file = new File({
id: 1,
Expand All @@ -128,17 +152,20 @@ describe('Edit locally action execute tests', () => {

const exec = await action.exec(file, view, '/')

expect(spyDialogBuilder).toBeCalled()

// Silent action
expect(exec).toBe(null)
expect(axios.post).toBeCalledTimes(1)
expect(axios.post).toBeCalledWith('http://localhost/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' })
expect(ncDialogs.showError).toBeCalledTimes(0)
expect(mockedShowError).toBeCalledTimes(0)
expect(window.location.href).toBe('nc://open/test@localhost/foobar.txt?token=foobar')
})

test('Edit locally fails and show error', async () => {
test('Edit locally fails and shows error', async () => {
jest.spyOn(axios, 'post').mockImplementation(async () => ({}))
jest.spyOn(ncDialogs, 'showError')
const mockedShowError = jest.mocked(showError)
const spyDialogBuilder = jest.spyOn(dialogBuilder, 'build')

const file = new File({
id: 1,
Expand All @@ -150,12 +177,14 @@ describe('Edit locally action execute tests', () => {

const exec = await action.exec(file, view, '/')

expect(spyDialogBuilder).toBeCalled()

// Silent action
expect(exec).toBe(null)
expect(axios.post).toBeCalledTimes(1)
expect(axios.post).toBeCalledWith('http://localhost/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' })
expect(ncDialogs.showError).toBeCalledTimes(1)
expect(ncDialogs.showError).toBeCalledWith('Failed to redirect to client')
expect(mockedShowError).toBeCalledTimes(1)
expect(mockedShowError).toBeCalledWith('Failed to redirect to client')
expect(window.location.href).toBe('http://localhost/')
})
})
57 changes: 54 additions & 3 deletions apps/files/src/actions/editLocallyAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,62 @@ import { encodePath } from '@nextcloud/paths'
import { generateOcsUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { FileAction, Permission, type Node } from '@nextcloud/files'
import { showError } from '@nextcloud/dialogs'
import { showError, DialogBuilder } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import axios from '@nextcloud/axios'

import LaptopSvg from '@mdi/svg/svg/laptop.svg?raw'
import IconCancel from '@mdi/svg/svg/cancel.svg?raw'
import IconCheck from '@mdi/svg/svg/check.svg?raw'

const confirmLocalEditDialog = (
localEditCallback: (openingLocally: boolean) => void = () => {},
) => {
let callbackCalled = false

return (new DialogBuilder())
.setName(t('files', 'Edit file locally'))
.setText(t('files', 'The file should now open locally. If you don\'t see this happening, make sure that the desktop client is installed on your system.'))
.setButtons([
{
label: t('files', 'Retry local edit'),
icon: IconCancel,
callback: () => {
callbackCalled = true
localEditCallback(false)
},
},
{
label: t('files', 'Edit online'),
icon: IconCheck,
type: 'primary',
callback: () => {
callbackCalled = true
localEditCallback(true)
},
},
])
.build()
.show()
.then(() => {
// Ensure the callback is called even if the dialog is dismissed in other ways
if (!callbackCalled) {
localEditCallback(false)
}
})
}

const attemptOpenLocalClient = async (path: string) => {
openLocalClient(path)
confirmLocalEditDialog(
(openLocally: boolean) => {
if (!openLocally) {
window.OCA.Viewer.open({ path })
return
}
openLocalClient(path)
},
)
}

const openLocalClient = async function(path: string) {
const link = generateOcsUrl('apps/files/api/v1') + '/openlocaleditor?format=json'
Expand Down Expand Up @@ -60,7 +111,7 @@ export const action = new FileAction({
},

async exec(node: Node) {
openLocalClient(node.path)
attemptOpenLocalClient(node.path)
return null
},

Expand Down
4 changes: 2 additions & 2 deletions dist/core-common.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-common.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/files-init.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-init.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/files_sharing-init.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files_sharing-init.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/settings-apps-view-4529.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/settings-apps-view-4529.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/settings-users-3239.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/settings-users-3239.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/settings-vue-settings-apps-users-management.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/settings-vue-settings-apps-users-management.js.map

Large diffs are not rendered by default.

Loading