Open History to see the last commit
1import { createAttachmentEnvironmentAtoms } from "@t3tools/client-runtime/state/attachments";
2import type { AtomCommand } from "@t3tools/client-runtime/state/runtime";
3import {
4 AuthOrchestrationOperateScope,
5 EnvironmentAuthorizationError,
6 type EnvironmentId,
7} from "@t3tools/contracts";
8import * as Cause from "effect/Cause";
9import { msg } from "gt-react-native";
10import { m } from "../i18n/messages";
11import { AsyncResult } from "effect/reactivity";
12
13import { connectionAtomRuntime } from "../connection/runtime";
14import { readEnvironmentScope } from "./session";
15
16function requireAttachmentWriteAccess<W extends { readonly environmentId: EnvironmentId }, A, E>(
17 command: AtomCommand<W, A, E>,
18): AtomCommand<W, A, E | EnvironmentAuthorizationError> {
19 return {
20 ...command,
21 run: async (registry, input) => {
22 if (!readEnvironmentScope(input.environmentId, AuthOrchestrationOperateScope)) {
23 return AsyncResult.failure(
24 Cause.fail(
25 new EnvironmentAuthorizationError({
26 message: m(msg("This connection cannot change attachments.")),
27 requiredScope: AuthOrchestrationOperateScope,
28 }),
29 ),
30 );
31 }
32 return command.run(registry, input);
33 },
34 };
35}
36
37const commands = createAttachmentEnvironmentAtoms(connectionAtomRuntime);
38export const attachmentEnvironment = {
39 createUploadUrl: requireAttachmentWriteAccess(commands.createUploadUrl),
40 remove: requireAttachmentWriteAccess(commands.remove),
41};