diff --git a/src/services/talkService.js b/src/services/talkService.js index 4f96d2f0c..08de68be1 100644 --- a/src/services/talkService.js +++ b/src/services/talkService.js @@ -53,7 +53,7 @@ export async function updateTalkParticipants(eventComponent) { logger.debug('Event\'s conference/location is from another host', url) return } - const token = url.match(/\/call\/([a-z0-9]*)$/)[1] + const token = extractCallTokenFromUrl(url) if (!token) { logger.debug('URL ' + url + ' contains no call token') return @@ -129,3 +129,13 @@ export function doesContainTalkLink(text) { function generateURLForToken(token = '') { return window.location.protocol + '//' + window.location.host + generateUrl('/call/' + token) } + +/** + * Extract a spreed call token from the given URL + * + * @param {string} callUrl URL of the spreed call + * @return {string|undefined} Matched token or undefined if URL is invalid + */ +export function extractCallTokenFromUrl(callUrl) { + return callUrl.match(/\/call\/([a-z0-9]*)(\/|#.*)?$/)?.[1] ?? undefined +} diff --git a/tests/javascript/unit/services/talkService.test.js b/tests/javascript/unit/services/talkService.test.js new file mode 100644 index 000000000..b929b5d2e --- /dev/null +++ b/tests/javascript/unit/services/talkService.test.js @@ -0,0 +1,21 @@ +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { extractCallTokenFromUrl } from '../../../../src/services/talkService' + +describe('services/talk test suite', () => { + test.each([ + ['https://foo.bar/call/123abc456', '123abc456'], + ['https://foo.bar/call/123abc456/', '123abc456'], + ['https://foo.bar/call/123abc456/baz', undefined], + ['https://foo.bar/call/123abc456#', '123abc456'], + ['https://foo.bar/call/123abc456#/', '123abc456'], + ['https://foo.bar/call/123abc456#message_3074226', '123abc456'], + ['https://foo.bar/baz', undefined], + ['https://foo.bar/baz/bar', undefined], + ])('should extract a token from call url %s', (url, expected) => { + expect(extractCallTokenFromUrl(url)).toBe(expected) + }) +})