Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(app): refresh attachment limits from instance discovery (#3700)

Hampus39a8c1524bda
diff --git a/fluxer_app/src/features/app/state/RuntimeConfig.ts b/fluxer_app/src/features/app/state/RuntimeConfig.ts
index feb1e13c5..39e42b76d 100644
--- a/fluxer_app/src/features/app/state/RuntimeConfig.ts
+++ b/fluxer_app/src/features/app/state/RuntimeConfig.ts
@@ -684,6 +684,7 @@ class RuntimeConfig {
appPublic: resolution.snapshot.appPublic,
notice: resolution.snapshot.notice,
guildBadges: resolution.snapshot.guildBadges,
+ limits: resolution.snapshot.limits,
});
this.activeRuntime = {...runtime, snapshot: usable};
this.lifecycleGeneration = nextLifecycleGeneration(this.lifecycleGeneration);
diff --git a/fluxer_app/src/features/channel/components/UploadManager.tsx b/fluxer_app/src/features/channel/components/UploadManager.tsx
index fd711bda1..0370196d1 100644
--- a/fluxer_app/src/features/channel/components/UploadManager.tsx
+++ b/fluxer_app/src/features/channel/components/UploadManager.tsx
@@ -10,6 +10,7 @@ import {Message} from '@app/features/messaging/models/MessagingMessage';
import {UploadingAttachment} from '@app/features/messaging/models/UploadingAttachment';
import MessageQueue from '@app/features/messaging/state/MessageQueue';
import {CloudUpload} from '@app/features/messaging/upload/CloudUpload';
+import {countOversizedAttachments} from '@app/features/messaging/utils/AttachmentUtils';
import {isDialogPasteTarget} from '@app/features/messaging/utils/TextInputEditUtils';
import {formatUploadingAttachmentSummary} from '@app/features/messaging/utils/UploadingAttachmentLabelUtils';
import {ComponentBus} from '@app/features/platform/utils/ComponentBus';
@@ -84,8 +85,7 @@ export const UploadManager = observer(({channel, canAttachFiles, canSendMessages
);
return;
}
- const maxFileSize = Users.getCurrentUser()?.maxAttachmentFileSize ?? 25 * 1024 * 1024;
- const oversizedFileCount = files.filter((file) => file.size > maxFileSize).length;
+ const oversizedFileCount = await countOversizedAttachments(files);
if (oversizedFileCount > 0) {
ModalCommands.push(
modal(() => (
@@ -140,8 +140,7 @@ export const UploadManager = observer(({channel, canAttachFiles, canSendMessages
);
return;
}
- const maxFileSize = Users.getCurrentUser()?.maxAttachmentFileSize ?? 25 * 1024 * 1024;
- const oversizedFileCount = files.filter((file) => file.size > maxFileSize).length;
+ const oversizedFileCount = await countOversizedAttachments(files);
if (oversizedFileCount > 0) {
ModalCommands.push(
modal(() => (
diff --git a/fluxer_app/src/features/messaging/utils/AttachmentUtils.ts b/fluxer_app/src/features/messaging/utils/AttachmentUtils.ts
index d4b19aefa..cbc2766e2 100644
--- a/fluxer_app/src/features/messaging/utils/AttachmentUtils.ts
+++ b/fluxer_app/src/features/messaging/utils/AttachmentUtils.ts
@@ -1,11 +1,32 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
+import RuntimeConfig from '@app/features/app/state/RuntimeConfig';
import {LimitResolver} from '@app/features/app/utils/LimitResolverAdapter';
+import {Logger} from '@app/features/platform/utils/AppLogger';
import {ATTACHMENT_MAX_SIZE_NON_PREMIUM} from '@fluxer/constants/src/LimitConstants';
-export function getMaxAttachmentFileSize(): number {
+const logger = new Logger('AttachmentUtils');
+
+function getMaxAttachmentFileSize(): number {
return LimitResolver.resolve({
key: 'max_attachment_file_size',
fallback: ATTACHMENT_MAX_SIZE_NON_PREMIUM,
});
}
+
+function countFilesAboveLimit(files: ReadonlyArray<File>): number {
+ const maxFileSize = getMaxAttachmentFileSize();
+ return files.filter((file) => file.size > maxFileSize).length;
+}
+
+export async function countOversizedAttachments(files: ReadonlyArray<File>): Promise<number> {
+ if (countFilesAboveLimit(files) === 0) {
+ return 0;
+ }
+ try {
+ await RuntimeConfig.revalidateDiscovery();
+ } catch (error) {
+ logger.warn('Failed to refresh the attachment size limit:', error);
+ }
+ return countFilesAboveLimit(files);
+}
diff --git a/fluxer_app/src/features/messaging/utils/FileUploadUtils.ts b/fluxer_app/src/features/messaging/utils/FileUploadUtils.ts
index 83e184fb6..137a6f92e 100644
--- a/fluxer_app/src/features/messaging/utils/FileUploadUtils.ts
+++ b/fluxer_app/src/features/messaging/utils/FileUploadUtils.ts
@@ -2,7 +2,7 @@
import MessageQueue from '@app/features/messaging/state/MessageQueue';
import {CloudUpload} from '@app/features/messaging/upload/CloudUpload';
-import {getMaxAttachmentFileSize} from '@app/features/messaging/utils/AttachmentUtils';
+import {countOversizedAttachments} from '@app/features/messaging/utils/AttachmentUtils';
export interface FileUploadResult {
success: boolean;
@@ -24,8 +24,7 @@ export async function handleFileUpload(
if (currentAttachmentCount + fileArray.length > maxAttachments) {
return {success: false, error: 'too_many_attachments'};
}
- const maxFileSize = getMaxAttachmentFileSize();
- const oversizedFileCount = fileArray.filter((file) => file.size > maxFileSize).length;
+ const oversizedFileCount = await countOversizedAttachments(fileArray);
if (oversizedFileCount > 0) {
return {success: false, error: 'file_size_too_large', oversizedFileCount};
}
diff --git a/fluxer_desktop/src/main/DesktopRuntimeTransactionRegistry.ts b/fluxer_desktop/src/main/DesktopRuntimeTransactionRegistry.ts
index 184837fef..b554c51b1 100644
--- a/fluxer_desktop/src/main/DesktopRuntimeTransactionRegistry.ts
+++ b/fluxer_desktop/src/main/DesktopRuntimeTransactionRegistry.ts
@@ -213,9 +213,8 @@ export class DesktopRuntimeTransactionRegistry {
this.pending.set(preparationId, pending);
try {
const protocol = getDesktopLocalAppProtocol();
- const cached = protocol.findPlanForRoute(instanceKey);
const signal = AbortSignal.any([controller.signal, protocol.getShutdownSignal()]);
- const plan = cached ?? (await discoverPlanForInstanceKey(instanceKey, signal));
+ const plan = await discoverPlanForInstanceKey(instanceKey, signal, protocol.findPlanForRoute(instanceKey));
owner.requireCurrent('Desktop runtime config prepare');
if (this.pending.get(preparationId) !== pending) {
throw new DesktopRuntimePreparationNotFoundError(preparationId);
@@ -508,12 +507,24 @@ function buildRuntimePreparation(preparation: RuntimePreparationRecord): Desktop
};
}
-async function discoverPlanForInstanceKey(instanceKey: string, signal: AbortSignal): Promise<LocalAppRuntimePlan> {
- const plan = await resolveDesktopRuntimePlan({input: instanceKey, signal});
- if (plan.instanceKey !== instanceKey) {
- throw new DesktopRuntimeInstanceMismatchError(instanceKey, plan.instanceKey);
+async function discoverPlanForInstanceKey(
+ instanceKey: string,
+ signal: AbortSignal,
+ cached: LocalAppRuntimePlan | null,
+): Promise<LocalAppRuntimePlan> {
+ try {
+ const plan = await resolveDesktopRuntimePlan({input: instanceKey, signal});
+ if (plan.instanceKey !== instanceKey) {
+ throw new DesktopRuntimeInstanceMismatchError(instanceKey, plan.instanceKey);
+ }
+ return plan;
+ } catch (error) {
+ if (cached === null || signal.aborted) {
+ throw error;
+ }
+ log.warn('Instance discovery failed, preparing the cached runtime plan', {instanceKey, error});
+ return cached;
}
- return plan;
}
function requireTransactionIdentity(request: unknown): RuntimeTransactionIdentity {