-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add utils for public link shares
Signed-off-by: Ferdinand Thiessen <[email protected]>
- Loading branch information
Showing
6 changed files
with
214 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
# @nextcloud/sharing | ||
# `@nextcloud/sharing` | ||
|
||
[](https://www.npmjs.com/package/@nextcloud/sharing) | ||
|
||
Common sharing code for the front-end | ||
Common front-end utils for files sharing on Nextcloud and Nextcloud apps. | ||
|
||
## Installation | ||
|
||
``` | ||
npm i -S @nextcloud/sharing | ||
``` | ||
|
||
## Usage | ||
|
||
There are two entry points provided: | ||
|
||
- The main entry point `@nextcloud/sharing` provides general utils for file sharing | ||
- The _public_ entry point `@nextcloud/sharing/public` provides utils for handling public file shares |
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,122 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
* @vitest-environment happy-dom | ||
*/ | ||
|
||
import { beforeEach, describe, expect, test, vi } from 'vitest' | ||
|
||
const initialState = vi.hoisted(() => ({ loadState: vi.fn() })) | ||
vi.mock('@nextcloud/initial-state', () => initialState) | ||
|
||
const mockPublicShare = () => { | ||
initialState.loadState.mockImplementation((app, key) => { | ||
if (key === 'isPublic') { | ||
return true | ||
} else if (key === 'sharingToken') { | ||
return 'modern-token' | ||
} | ||
throw new Error('Unexpected loadState') | ||
}) | ||
} | ||
|
||
const mockLegacyPublicShare = () => { | ||
initialState.loadState.mockImplementationOnce(() => null) | ||
|
||
const input = document.createElement('input') | ||
input.id = 'isPublic' | ||
input.name = 'isPublic' | ||
input.type = 'hidden' | ||
input.value = '1' | ||
document.body.appendChild(input) | ||
|
||
const token = document.createElement('input') | ||
token.id = 'sharingToken' | ||
token.type = 'hidden' | ||
token.value = 'legacy-token' | ||
document.body.appendChild(token) | ||
} | ||
|
||
describe('isPublicShare', () => { | ||
beforeEach(() => { | ||
vi.resetModules() | ||
vi.resetAllMocks() | ||
// reset JSDom | ||
document.body.innerHTML = '' | ||
}) | ||
|
||
const isPublicShare = async () => { | ||
const { isPublicShare: publicShare } = await import('./publicShare') | ||
return publicShare() | ||
} | ||
|
||
test('no public share', async () => { | ||
initialState.loadState.mockImplementation(() => null) | ||
|
||
expect(await isPublicShare()).toBe(false) | ||
expect(initialState.loadState).toBeCalledWith( | ||
'files_sharing', | ||
'isPublic', | ||
null, | ||
) | ||
}) | ||
|
||
test('public share', async () => { | ||
mockPublicShare() | ||
|
||
expect(await isPublicShare()).toBe(true) | ||
expect(initialState.loadState).toBeCalledWith( | ||
'files_sharing', | ||
'isPublic', | ||
null, | ||
) | ||
}) | ||
|
||
test('legacy public share', async () => { | ||
mockLegacyPublicShare() | ||
|
||
expect(await isPublicShare()).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('getSharingToken', () => { | ||
beforeEach(() => { | ||
vi.resetModules() | ||
vi.resetAllMocks() | ||
// reset happy-dom | ||
document.body.innerHTML = '' | ||
}) | ||
|
||
const getSharingToken = async () => { | ||
const { getSharingToken: sharingToken } = await import('./publicShare') | ||
return sharingToken() | ||
} | ||
|
||
test('no public share', async () => { | ||
initialState.loadState.mockImplementation(() => null) | ||
|
||
expect(await getSharingToken()).toBe(null) | ||
expect(initialState.loadState).toBeCalledWith( | ||
'files_sharing', | ||
'sharingToken', | ||
null, | ||
) | ||
}) | ||
|
||
test('public share', async () => { | ||
mockPublicShare() | ||
|
||
expect(await getSharingToken()).toBe('modern-token') | ||
expect(initialState.loadState).toBeCalledWith( | ||
'files_sharing', | ||
'sharingToken', | ||
null, | ||
) | ||
}) | ||
|
||
test('legacy public share', async () => { | ||
mockLegacyPublicShare() | ||
|
||
expect(await getSharingToken()).toBe('legacy-token') | ||
}) | ||
}) |
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 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
import { loadState } from '@nextcloud/initial-state' | ||
|
||
/** | ||
* Check if the current page is on a public share | ||
*/ | ||
export function isPublicShare(): boolean { | ||
// check both the new initial state version and fallback to legacy input | ||
return ( | ||
loadState<boolean | null>('files_sharing', 'isPublic', null) ?? | ||
document.querySelector( | ||
'input#isPublic[type="hidden"][name="isPublic"][value="1"]', | ||
) !== null | ||
) | ||
} | ||
|
||
/** | ||
* Get the sharing token for the current public share | ||
*/ | ||
export function getSharingToken(): string | null { | ||
return ( | ||
loadState<string | null>('files_sharing', 'sharingToken', null) ?? | ||
document.querySelector<HTMLInputElement>('input#sharingToken[type="hidden"]') | ||
?.value ?? | ||
null | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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