|
3 | 3 | * SPDX-License-Identifier: AGPL-3.0-or-later |
4 | 4 | */ |
5 | 5 |
|
6 | | -import { defineAsyncComponent } from 'vue' |
7 | 6 | import { getBuilder } from '@nextcloud/browser-storage' |
8 | | -import { getGuestNickname, setGuestNickname } from '@nextcloud/auth' |
| 7 | +import { getGuestNickname, type NextcloudUser } from '@nextcloud/auth' |
9 | 8 | import { getUploader } from '@nextcloud/upload' |
10 | | -import { spawnDialog } from '@nextcloud/dialogs' |
| 9 | +import { loadState } from '@nextcloud/initial-state' |
| 10 | +import { showGuestUserPrompt } from '@nextcloud/dialogs' |
| 11 | +import { t } from '@nextcloud/l10n' |
11 | 12 |
|
12 | 13 | import logger from './services/logger' |
| 14 | +import { subscribe } from '@nextcloud/event-bus' |
13 | 15 |
|
14 | 16 | const storage = getBuilder('files_sharing').build() |
15 | 17 |
|
16 | | -/** |
17 | | - * Setup file-request nickname header for the uploader |
18 | | - * @param nickname The nickname |
19 | | - */ |
20 | | -function registerFileRequestHeader(nickname: string) { |
| 18 | +// Setup file-request nickname header for the uploader |
| 19 | +const registerFileRequestHeader = (nickname: string) => { |
21 | 20 | const uploader = getUploader() |
22 | 21 | uploader.setCustomHeader('X-NC-Nickname', encodeURIComponent(nickname)) |
23 | 22 | logger.debug('Nickname header registered for uploader', { headers: uploader.customHeaders }) |
24 | 23 | } |
25 | 24 |
|
26 | | -/** |
27 | | - * Callback when a nickname was chosen |
28 | | - * @param nickname The chosen nickname |
29 | | - */ |
30 | | -function onSetNickname(nickname: string): void { |
31 | | - // Set the nickname |
32 | | - setGuestNickname(nickname) |
33 | | - // Set the dialog as shown |
34 | | - storage.setItem('public-auth-prompt-shown', 'true') |
35 | | - // Register header for uploader |
36 | | - registerFileRequestHeader(nickname) |
| 25 | +// Callback when a nickname was chosen |
| 26 | +const onUserInfoChanged = (guest: NextcloudUser) => { |
| 27 | + logger.debug('User info changed', { guest }) |
| 28 | + registerFileRequestHeader(guest.displayName ?? '') |
37 | 29 | } |
38 | 30 |
|
| 31 | +// Monitor nickname changes |
| 32 | +subscribe('user:info:changed', onUserInfoChanged) |
| 33 | + |
39 | 34 | window.addEventListener('DOMContentLoaded', () => { |
40 | 35 | const nickname = getGuestNickname() ?? '' |
41 | 36 | const dialogShown = storage.getItem('public-auth-prompt-shown') !== null |
42 | 37 |
|
43 | | - // If we don't have a nickname or the public auth prompt hasn't been shown yet, show it |
44 | | - // We still show the prompt if the user has a nickname to double check |
45 | | - if (!nickname || !dialogShown) { |
46 | | - spawnDialog( |
47 | | - defineAsyncComponent(() => import('./views/PublicAuthPrompt.vue')), |
48 | | - { |
49 | | - nickname, |
50 | | - }, |
51 | | - onSetNickname as (...rest: unknown[]) => void, |
52 | | - ) |
53 | | - } else { |
54 | | - logger.debug('Public auth prompt already shown.', { nickname }) |
55 | | - registerFileRequestHeader(nickname) |
| 38 | + // Check if a nickname is mandatory |
| 39 | + const isFileRequest = loadState('files_sharing', 'isFileRequest', false) |
| 40 | + |
| 41 | + const owner = loadState('files_sharing', 'owner', '') |
| 42 | + const ownerDisplayName = loadState('files_sharing', 'ownerDisplayName', '') |
| 43 | + const label = loadState('files_sharing', 'label', '') |
| 44 | + const filename = loadState('files_sharing', 'filename', '') |
| 45 | + |
| 46 | + // If the owner provided a custom label, use it instead of the filename |
| 47 | + const folder = label || filename |
| 48 | + |
| 49 | + const options = { |
| 50 | + nickname, |
| 51 | + notice: t('files_sharing', 'To upload files to {folder}, you need to provide your name first.', { folder }), |
| 52 | + subtitle: undefined as string | undefined, |
| 53 | + title: t('files_sharing', 'Upload files to {folder}', { folder }), |
56 | 54 | } |
| 55 | + |
| 56 | + // If the guest already has a nickname, we just make them double check |
| 57 | + if (nickname) { |
| 58 | + options.notice = t('files_sharing', 'Please confirm your name to upload files to {folder}', { folder }) |
| 59 | + } |
| 60 | + |
| 61 | + // If the account owner set their name as public, |
| 62 | + // we show it in the subtitle |
| 63 | + if (owner) { |
| 64 | + options.subtitle = t('files_sharing', '{ownerDisplayName} shared a folder with you.', { ownerDisplayName }) |
| 65 | + } |
| 66 | + |
| 67 | + // If this is a file request, then we need a nickname |
| 68 | + if (isFileRequest) { |
| 69 | + // If we don't have a nickname or the public auth prompt hasn't been shown yet, show it |
| 70 | + // We still show the prompt if the user has a nickname to double check |
| 71 | + if (!nickname || !dialogShown) { |
| 72 | + logger.debug('Showing public auth prompt.', { nickname }) |
| 73 | + showGuestUserPrompt(options) |
| 74 | + } |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + if (!dialogShown && !nickname) { |
| 79 | + logger.debug('Public auth prompt not shown yet but nickname is not mandatory.', { nickname }) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + // Else, we just register the nickname header if any. |
| 84 | + logger.debug('Public auth prompt already shown.', { nickname }) |
| 85 | + registerFileRequestHeader(nickname) |
57 | 86 | }) |
0 commit comments