Оптимизирован код вложений, исправлена установка аватарки на диалог, а не у отправителя

This commit is contained in:
RoyceDa
2026-02-20 17:43:56 +02:00
parent db72246e5a
commit c64a7005d3
6 changed files with 22 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
import { useContext, useEffect, useState } from "react";
import { useEffect, useState } from "react";
import { useDownloadStatus } from "../TransportProvider/useDownloadStatus";
import { useUploadStatus } from "../TransportProvider/useUploadStatus";
import { useFileStorage } from "../../hooks/useFileStorage";
@@ -10,11 +10,11 @@ import { useDialogsCache } from "../DialogProvider/useDialogsCache";
import { useConsoleLogger } from "../../hooks/useConsoleLogger";
import { Attachment, AttachmentType } from "../ProtocolProvider/protocol/packets/packet.message";
import { useMemory } from "../MemoryProvider/useMemory";
import { DialogContext } from "../DialogProvider/DialogProvider";
import { useSaveAvatar } from "../AvatarProvider/useSaveAvatar";
import { AVATAR_PASSWORD_TO_ENCODE } from "@/app/constants";
import { useDialog } from "../DialogProvider/useDialog";
import { useCore } from "@/app/hooks/useCore";
import { MessageProps } from "@/app/components/Messages/Message";
export enum DownloadStatus {
DOWNLOADED,
@@ -25,7 +25,7 @@ export enum DownloadStatus {
ERROR
}
export function useAttachment(attachment: Attachment, keyPlain: string) {
export function useAttachment(attachment: Attachment, parentMessage: MessageProps) {
const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
const uploadedPercentage = useUploadStatus(attachment.id);
const downloadPercentage = useDownloadStatus(attachment.id);
@@ -39,13 +39,7 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
const {info} = useConsoleLogger('useAttachment');
const {updateAttachmentsInMessagesByAttachmentId} = useDialog();
const {getDownloadsPath} = useCore();
const context = useContext(DialogContext);
if(!context) {
throw new Error("useAttachment must be used within a DialogProvider");
}
const {dialog} = context;
const saveAvatar = useSaveAvatar();
useEffect(() => {
@@ -155,7 +149,7 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
}
setDownloadStatus(DownloadStatus.DECRYPTING);
//console.info("Decrypted attachment ", Buffer.from(keyPlain, 'binary').toString('hex'));
const decrypted = await decodeWithPassword(keyPlain, downloadedBlob);
const decrypted = await decodeWithPassword(parentMessage.chacha_key_plain, downloadedBlob);
setDownloadTag("");
if(attachment.type == AttachmentType.FILE) {
/**
@@ -189,7 +183,10 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
await writeFile(avatarPath,
Buffer.from(await encodeWithPassword(AVATAR_PASSWORD_TO_ENCODE, decrypted)));
setDownloadStatus(DownloadStatus.DOWNLOADED);
saveAvatar(dialog, avatarPath, decrypted);
/**
* Устанавливаем аватарку тому, кто ее прислал.
*/
saveAvatar(parentMessage.from, avatarPath, decrypted);
return;
}
/**