keanu-weblite/src/models/attachment.ts

70 lines
1.9 KiB
TypeScript
Raw Normal View History

import { ComputedRef, Ref } from "vue";
2025-06-09 09:44:37 +02:00
export class UploadPromise<Type> {
wrappedPromise: Promise<Type>;
aborted: boolean = false;
onAbort: (() => void) | undefined = undefined;
constructor(wrappedPromise: Promise<Type>) {
this.wrappedPromise = wrappedPromise;
}
abort() {
this.aborted = true;
if (this.onAbort) {
this.onAbort();
}
}
then(resolve: any, reject: any) {
this.wrappedPromise = this.wrappedPromise.then(resolve, reject);
return this;
}
catch(handler: any) {
this.wrappedPromise = this.wrappedPromise.catch(handler);
return this;
}
}
export type AttachmentSendStatus = "initial" | "sending" | "sent" | "canceled" | "failed";
export type AttachmentSendInfo = {
status: AttachmentSendStatus;
statusDate: number; //ms
mediaEventId: string | undefined;
progress: number;
promise: UploadPromise<string> | undefined;
randomRotation: number; // For UI effects
randomTranslationX: number; // For UI effects
randomTranslationY: number; // For UI effects
};
export type Attachment = {
status: "loading" | "loaded";
file: File;
dimensions?: { width: number; height: number };
scaledFile?: File;
scaledDimensions?: { width: number; height: number };
useScaled: boolean;
src?: string;
proof?: any;
sendInfo?: AttachmentSendInfo;
};
export type AttachmentBatch = {
sendingStatus: Ref<"initial" | "sending" | "sent" | "canceled" | "failed">;
sendingRootEventId: Ref<string | undefined>;
sendingPromise: Ref<Promise<any> | undefined>;
attachments: Ref<Attachment[]>;
attachmentsSentCount: ComputedRef<number>;
attachmentsSending: ComputedRef<Attachment[]>;
attachmentsSent: ComputedRef<Attachment[]>;
addAttachment: (attachment: Attachment) => void;
removeAttachment: (attachment: Attachment) => void;
send: (message: string) => Promise<any>;
cancel: () => void;
cancelSendAttachment: (attachment: Attachment) => void;
};