Finish unification of attachment sending

This commit is contained in:
N-Pex 2025-06-11 18:04:56 +02:00
parent fd82fd8840
commit a92d767fc2
11 changed files with 246 additions and 1100 deletions

View file

@ -62,6 +62,8 @@ export type AttachmentBatch = {
attachmentsSent: ComputedRef<Attachment[]>;
addAttachment: (attachment: Attachment) => void;
removeAttachment: (attachment: Attachment) => void;
isTooLarge: (attachment: Attachment) => boolean;
canSend: ComputedRef<boolean>;
send: (message: string) => Promise<any>;
cancel: () => void;
cancelSendAttachment: (attachment: Attachment) => void;

View file

@ -35,7 +35,7 @@ export class AttachmentManager {
}
public createUpload(room: Room) {
return createUploadBatch(this.matrixClient, room);
return createUploadBatch(this.matrixClient, room, this.maxSizeUploads);
}
public createAttachment(file: File): Attachment {
@ -298,7 +298,11 @@ export class AttachmentManager {
}
}
const createUploadBatch = (matrixClient: MatrixClient, room: Room): AttachmentBatch => {
export const createUploadBatch = (
matrixClient: MatrixClient | null,
room: Room | null,
maxSizeUploads: number
): AttachmentBatch => {
const sendingStatus: Ref<"initial" | "sending" | "sent" | "canceled" | "failed"> = ref("initial");
const sendingRootEventId: Ref<string | undefined> = ref(undefined);
const sendingPromise: Ref<Promise<any> | undefined> = ref(undefined);
@ -333,8 +337,17 @@ const createUploadBatch = (matrixClient: MatrixClient, room: Room): AttachmentBa
}
};
const isTooLarge = (attachment: Attachment) => {
const file = attachment.scaledFile && attachment.useScaled ? attachment.scaledFile : attachment.file;
return file.size > maxSizeUploads;
};
const canSend = computed(() => {
return attachments.value.length > 0 && !attachments.value.some((a: Attachment) => isTooLarge(a));
});
const cancel = () => {
if (sendingStatus.value !== "initial") {
if (sendingStatus.value !== "initial" && matrixClient && room) {
attachments.value.toReversed().forEach((attachment) => {
cancelSendAttachment(attachment);
});
@ -379,6 +392,7 @@ const createUploadBatch = (matrixClient: MatrixClient, room: Room): AttachmentBa
};
const send = (message: string): Promise<any> => {
if (!matrixClient || !room) return Promise.reject("Not configured");
sendingStatus.value = "sending";
attachments.value.forEach((attachment) => {
let sendInfo: AttachmentSendInfo = {
@ -494,6 +508,8 @@ const createUploadBatch = (matrixClient: MatrixClient, room: Room): AttachmentBa
attachmentsSent,
addAttachment,
removeAttachment,
isTooLarge,
canSend,
send,
cancel,
cancelSendAttachment,