Merge branch 'feat/rbf-wording' into 'main'
[ProtonMail-WebClient.git] / packages / drive-store / store / _shares / usePublicShare.ts
blob1e425c4f29c6334c78e2247548204f2297ae6749
1 import { useApi } from '@proton/components';
2 import { querySharedURLInformation, querySubmitAbuseReport } from '@proton/shared/lib/api/drive/sharing';
3 import type { SharedURLInfo } from '@proton/shared/lib/interfaces/drive/sharing';
5 import { usePublicSession } from '../_api';
6 import { useLink } from '../_links';
7 import { useDecryptPublicShareLink } from './useDecryptPublicShareLink';
9 /**
10  * usePublicShare loads shared share with link to the store and decrypts them.
11  */
12 export default function usePublicShare() {
13     const api = useApi();
14     const { user, request, getSessionInfo } = usePublicSession();
15     const { decryptPublicShareLink } = useDecryptPublicShareLink();
16     const { getLinkPassphraseAndSessionKey } = useLink();
18     const loadPublicShare = async (abortSignal: AbortSignal) => {
19         const sessionInfo = getSessionInfo();
20         if (!sessionInfo) {
21             throw new Error('Unauthenticated session');
22         }
24         const { Token } = await request<{ Token: SharedURLInfo }>({
25             ...querySharedURLInformation(sessionInfo.token),
26             silence: true,
27         });
29         const link = await decryptPublicShareLink(abortSignal, {
30             token: sessionInfo.token,
31             urlPassword: sessionInfo.password,
32             shareUrlInfo: Token,
33         });
35         return {
36             token: sessionInfo.token,
37             link,
38             permissions: Token.Permissions,
39         };
40     };
42     const submitAbuseReport = async (params: {
43         linkId: string;
44         abuseCategory: string;
45         reporterEmail?: string;
46         reporterMessage?: string;
47     }): Promise<void> => {
48         const sessionInfo = getSessionInfo();
49         if (!sessionInfo) {
50             throw new Error('Unauthenticated session');
51         }
52         const { token, password } = sessionInfo;
53         const ac = new AbortController();
54         const { passphrase } = await getLinkPassphraseAndSessionKey(ac.signal, token, params.linkId);
56         return api(
57             querySubmitAbuseReport({
58                 ShareURL: window.location.href,
59                 Password: password,
60                 AbuseCategory: params.abuseCategory,
61                 ReporterEmail: params.reporterEmail,
62                 ReporterMessage: params.reporterMessage,
63                 ResourcePassphrase: passphrase,
64             })
65         );
66     };
68     return {
69         loadPublicShare,
70         submitAbuseReport,
71         user,
72     };