Skip to content

Commit 46dec1d

Browse files
authored
Merge pull request #603 from nextcloud/fix/nextcloud-router-patching-with-origin-oc_webroot
fix(patchers): patch router without permanent absolute _oc_webroot
2 parents 104060f + 99666eb commit 46dec1d

5 files changed

Lines changed: 118 additions & 76 deletions

File tree

package-lock.json

Lines changed: 59 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@nextcloud/capabilities": "^1.1.0",
4141
"@nextcloud/event-bus": "^3.1.0",
4242
"@nextcloud/l10n": "^2.2.0",
43+
"@nextcloud/router": "^3.0.0",
4344
"@nextcloud/vue": "^8.11.1",
4445
"core-js": "^3.36.1",
4546
"electron-squirrel-startup": "^1.0.0",

src/patchers/@nextcloud/router.js

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,37 @@
2121

2222
/* eslint-disable jsdoc/require-jsdoc */
2323

24-
import { getRootUrl, generateFilePath as _generateFilePath } from '@desktop-modules--@nextcloud/router'
24+
import {
25+
getRootUrl as _getRootUrl,
26+
getAppRootUrl as _getAppRootUrl,
27+
generateUrl as _generateUrl,
28+
linkTo as _linkTo,
29+
generateRemoteUrl as _generateRemoteUrl,
30+
generateOcsUrl as _generateOcsUrl,
31+
generateFilePath as _generateFilePath,
32+
} from '@desktop-modules--@nextcloud/router'
2533

26-
export { linkTo, getRootUrl, generateUrl } from '@desktop-modules--@nextcloud/router'
34+
// Original @nextcloud/router sometimes relies on window.location which is not correct on desktop
35+
// So, some function must be re-defined or patched
2736

28-
// getBaseUrl added in v3.0.0
29-
// On Talk Desktop same as getRootUrl, because getRootUrl is absolute already
30-
export const getBaseUrl = getRootUrl
37+
// Works as expected originally, does not use location
38+
export const getRootUrl = _getRootUrl
3139

32-
/**
33-
* @param {string} s - String with "{token}" blocks
34-
* @param {{[token: string]: string}} [tokens] - Dict with replacements
35-
* @return {string}
36-
*/
37-
function formattedString(s, tokens = {}) {
38-
return Object.entries(tokens).reduce((acc, [token, replacement]) => acc.replaceAll(`{${token}}`, replacement), s)
39-
}
40-
41-
export function generateOcsUrl(url, params = {}, options = {}) {
42-
// Reason to patch: it uses window.location
43-
const allOptions = { ...options, ocsVersion: 2 }
44-
const version = (allOptions.ocsVersion === 1) ? 1 : 2
45-
return `${getRootUrl()}/ocs/v${version}.php/${formattedString(url, params)}`
46-
}
40+
// Works fine originally with enabled absolute webroot
41+
export const getAppRootUrl = (...args) => window.OCA.Talk.Desktop.runWithAbsoluteWebroot(_getAppRootUrl, ...args)
42+
export const generateUrl = (...args) => window.OCA.Talk.Desktop.runWithAbsoluteWebroot(_generateUrl, ...args)
43+
export const linkTo = (...args) => window.OCA.Talk.Desktop.runWithAbsoluteWebroot(_linkTo, ...args)
4744

48-
const linkToRemoteBase = (service) => getRootUrl() + '/remote.php/' + service
45+
// Original getBaseUrl relies on window.location, create a new one as an absolute version of getRootUrl
46+
export const getBaseUrl = (...args) => window.OCA.Talk.Desktop.runWithAbsoluteWebroot(_getRootUrl, ...args)
4947

50-
export function generateRemoteUrl(service) {
51-
// Reason to patch: it uses window.location
52-
return linkToRemoteBase(service)
53-
}
48+
// Requires changing the default options.baseUrl from original relative getBaseUrl to new absolute getBaseUrl
49+
export const generateRemoteUrl = (service, options = {}) => _generateRemoteUrl(service, { baseURL: getBaseUrl(), ...options })
50+
export const generateOcsUrl = (url, params, options = {}) => _generateOcsUrl(url, params, { baseURL: getBaseUrl(), ...options })
5451

52+
// By default, Talk requests images and sounds as a file from server assets using generateFilePath
53+
// Desktop app should use path to the local file in the bundle
5554
export function generateFilePath(app, type, file) {
56-
/**
57-
* By default, Talk requests images and sounds as a file from server assets using generateFilePath
58-
* Desktop app should use path to the local file in the build
59-
*/
60-
6155
const filename = file.substring(0, file.lastIndexOf('.'))
6256
const ext = file.substring(file.lastIndexOf('.'))
6357

@@ -77,14 +71,14 @@ export function generateFilePath(app, type, file) {
7771
return requiresByExt[ext]()
7872
}
7973
} else if (app === 'notifications' && ext === '.ogg') {
80-
// For now notifications sounds are just a copy of the notifications app sounds
74+
// For now, notifications' sounds are just a copy of the Notifications app sounds
8175
return require(`../../../sounds/${filename}.ogg`)
8276
}
8377

84-
return _generateFilePath(app, type, file)
78+
return window.OCA.Talk.Desktop.runWithAbsoluteWebroot(() => _generateFilePath(app, type, file))
8579
}
8680

87-
// Copy of original function, but using patched generateFilePath
81+
// Copy of original but using patched generateFilePath
8882
export function imagePath(app, file) {
8983
if (file.indexOf('.') === -1) {
9084
return generateFilePath(app, 'img', file + '.svg')

src/shared/globals/globals.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ import { translate, translatePlural } from '@nextcloud/l10n'
2424

2525
import { appData } from '../../app/AppData.js'
2626
import { dialogs } from './OC/dialogs.js'
27+
import { getDesktopMediaSource } from '../../talk/renderer/getDesktopMediaSource.js'
28+
29+
let enabledAbsoluteWebroot = false
30+
31+
/**
32+
* Run a function with an absolute webroot enabled to not rely on window.location
33+
*
34+
* @param {Function} func - the function to run
35+
* @param {...any} args - the arguments to pass to the function
36+
* @return {any} the result of the function's run
37+
*/
38+
function runWithAbsoluteWebroot(func, ...args) {
39+
enabledAbsoluteWebroot = true
40+
const result = func.call(this, ...args)
41+
enabledAbsoluteWebroot = false
42+
return result
43+
}
44+
45+
const getMaybeAbsoluteWebroot = () => enabledAbsoluteWebroot ? appData.serverUrl : new URL(appData.serverUrl).pathname
2746

2847
const OC = {
2948
// Constant from: https://github.com/nextcloud/server/blob/master/core/src/OC/constants.js
@@ -46,9 +65,7 @@ const OC = {
4665
},
4766

4867
get webroot() {
49-
// Original method returns only path, for example, /nextcloud-webroot
50-
// Desktop needs to have full URL: https://nextcloud.host/nextcloud-webroot
51-
return appData.serverUrl
68+
return getMaybeAbsoluteWebroot()
5269
},
5370

5471
config: {
@@ -81,7 +98,15 @@ const OC = {
8198
},
8299
}
83100

84-
const OCA = {}
101+
const OCA = {
102+
Talk: {
103+
Desktop: {
104+
getDesktopMediaSource,
105+
runWithAbsoluteWebroot,
106+
enabledAbsoluteWebroot: false,
107+
},
108+
},
109+
}
85110

86111
const OCP = {
87112
Accessibility: {
@@ -101,10 +126,10 @@ export function initGlobals() {
101126
window.OCP = OCP
102127

103128
Object.defineProperty(window, '_oc_webroot', {
104-
get: () => OC.webroot,
129+
get: () => getMaybeAbsoluteWebroot(),
105130
})
106131

107132
Object.defineProperty(window, '_oc_appswebroots', {
108-
get: () => OC.appswebroots,
133+
get: () => window.OC.appswebroots,
109134
})
110135
}

src/talk/renderer/talk.main.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import './assets/styles.css'
2525
import 'regenerator-runtime' // TODO: Why isn't it added on bundling
2626
import { init, initTalkHashIntegration } from './init.js'
2727
import { setupWebPage } from '../../shared/setupWebPage.js'
28-
import { getDesktopMediaSource } from './getDesktopMediaSource.js'
2928
import { createViewer } from './Viewer/Viewer.js'
3029

3130
// Initially open the welcome page, if not specified
@@ -46,8 +45,4 @@ await import('@talk/src/main.js')
4645

4746
initTalkHashIntegration(OCA.Talk.instance)
4847

49-
window.OCA.Talk.Desktop = {
50-
getDesktopMediaSource,
51-
}
52-
5348
await import('./notifications/notifications.store.js')

0 commit comments

Comments
 (0)