Оптимизирован код вложений, исправлена установка аватарки на диалог, а не у отправителя
This commit is contained in:
@@ -19,7 +19,7 @@ export function MessageAvatar(props: AttachmentProps) {
|
|||||||
download,
|
download,
|
||||||
downloadStatus,
|
downloadStatus,
|
||||||
getBlob,
|
getBlob,
|
||||||
getPreview} = useAttachment(props.attachment, props.chacha_key_plain);
|
getPreview} = useAttachment(props.attachment, props.parent);
|
||||||
const mainRef = useRef<HTMLDivElement>(null);
|
const mainRef = useRef<HTMLDivElement>(null);
|
||||||
const { open } = useImageViewer();
|
const { open } = useImageViewer();
|
||||||
const preview = getPreview();
|
const preview = getPreview();
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export function MessageFile(props : AttachmentProps) {
|
|||||||
} =
|
} =
|
||||||
useAttachment(
|
useAttachment(
|
||||||
props.attachment,
|
props.attachment,
|
||||||
props.chacha_key_plain,
|
props.parent,
|
||||||
);
|
);
|
||||||
const preview = getPreview();
|
const preview = getPreview();
|
||||||
const error = downloadStatus == DownloadStatus.ERROR;
|
const error = downloadStatus == DownloadStatus.ERROR;
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export function MessageImage(props: AttachmentProps) {
|
|||||||
download,
|
download,
|
||||||
downloadStatus,
|
downloadStatus,
|
||||||
getBlob,
|
getBlob,
|
||||||
getPreview } = useAttachment(props.attachment, props.chacha_key_plain);
|
getPreview } = useAttachment(props.attachment, props.parent);
|
||||||
const mainRef = useRef<HTMLDivElement>(null);
|
const mainRef = useRef<HTMLDivElement>(null);
|
||||||
const error = downloadStatus == DownloadStatus.ERROR;
|
const error = downloadStatus == DownloadStatus.ERROR;
|
||||||
const { open } = useImageViewer();
|
const { open } = useImageViewer();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useContext, useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useDownloadStatus } from "../TransportProvider/useDownloadStatus";
|
import { useDownloadStatus } from "../TransportProvider/useDownloadStatus";
|
||||||
import { useUploadStatus } from "../TransportProvider/useUploadStatus";
|
import { useUploadStatus } from "../TransportProvider/useUploadStatus";
|
||||||
import { useFileStorage } from "../../hooks/useFileStorage";
|
import { useFileStorage } from "../../hooks/useFileStorage";
|
||||||
@@ -10,11 +10,11 @@ import { useDialogsCache } from "../DialogProvider/useDialogsCache";
|
|||||||
import { useConsoleLogger } from "../../hooks/useConsoleLogger";
|
import { useConsoleLogger } from "../../hooks/useConsoleLogger";
|
||||||
import { Attachment, AttachmentType } from "../ProtocolProvider/protocol/packets/packet.message";
|
import { Attachment, AttachmentType } from "../ProtocolProvider/protocol/packets/packet.message";
|
||||||
import { useMemory } from "../MemoryProvider/useMemory";
|
import { useMemory } from "../MemoryProvider/useMemory";
|
||||||
import { DialogContext } from "../DialogProvider/DialogProvider";
|
|
||||||
import { useSaveAvatar } from "../AvatarProvider/useSaveAvatar";
|
import { useSaveAvatar } from "../AvatarProvider/useSaveAvatar";
|
||||||
import { AVATAR_PASSWORD_TO_ENCODE } from "@/app/constants";
|
import { AVATAR_PASSWORD_TO_ENCODE } from "@/app/constants";
|
||||||
import { useDialog } from "../DialogProvider/useDialog";
|
import { useDialog } from "../DialogProvider/useDialog";
|
||||||
import { useCore } from "@/app/hooks/useCore";
|
import { useCore } from "@/app/hooks/useCore";
|
||||||
|
import { MessageProps } from "@/app/components/Messages/Message";
|
||||||
|
|
||||||
export enum DownloadStatus {
|
export enum DownloadStatus {
|
||||||
DOWNLOADED,
|
DOWNLOADED,
|
||||||
@@ -25,7 +25,7 @@ export enum DownloadStatus {
|
|||||||
ERROR
|
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 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 uploadedPercentage = useUploadStatus(attachment.id);
|
||||||
const downloadPercentage = useDownloadStatus(attachment.id);
|
const downloadPercentage = useDownloadStatus(attachment.id);
|
||||||
@@ -39,13 +39,7 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
|
|||||||
const {info} = useConsoleLogger('useAttachment');
|
const {info} = useConsoleLogger('useAttachment');
|
||||||
const {updateAttachmentsInMessagesByAttachmentId} = useDialog();
|
const {updateAttachmentsInMessagesByAttachmentId} = useDialog();
|
||||||
const {getDownloadsPath} = useCore();
|
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();
|
const saveAvatar = useSaveAvatar();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -155,7 +149,7 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
|
|||||||
}
|
}
|
||||||
setDownloadStatus(DownloadStatus.DECRYPTING);
|
setDownloadStatus(DownloadStatus.DECRYPTING);
|
||||||
//console.info("Decrypted attachment ", Buffer.from(keyPlain, 'binary').toString('hex'));
|
//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("");
|
setDownloadTag("");
|
||||||
if(attachment.type == AttachmentType.FILE) {
|
if(attachment.type == AttachmentType.FILE) {
|
||||||
/**
|
/**
|
||||||
@@ -189,7 +183,10 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
|
|||||||
await writeFile(avatarPath,
|
await writeFile(avatarPath,
|
||||||
Buffer.from(await encodeWithPassword(AVATAR_PASSWORD_TO_ENCODE, decrypted)));
|
Buffer.from(await encodeWithPassword(AVATAR_PASSWORD_TO_ENCODE, decrypted)));
|
||||||
setDownloadStatus(DownloadStatus.DOWNLOADED);
|
setDownloadStatus(DownloadStatus.DOWNLOADED);
|
||||||
saveAvatar(dialog, avatarPath, decrypted);
|
/**
|
||||||
|
* Устанавливаем аватарку тому, кто ее прислал.
|
||||||
|
*/
|
||||||
|
saveAvatar(parentMessage.from, avatarPath, decrypted);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -7,7 +7,15 @@ import { useConsoleLogger } from "@/app/hooks/useConsoleLogger";
|
|||||||
import { useSystemAccounts } from "../SystemAccountsProvider/useSystemAccounts";
|
import { useSystemAccounts } from "../SystemAccountsProvider/useSystemAccounts";
|
||||||
import { AVATAR_PASSWORD_TO_ENCODE } from "@/app/constants";
|
import { AVATAR_PASSWORD_TO_ENCODE } from "@/app/constants";
|
||||||
|
|
||||||
export const AvatarContext = createContext({});
|
export interface AvatarProviderContextValue {
|
||||||
|
deliveredAvatars: string[];
|
||||||
|
saveAvatar: (fromPublicKey: string, path : string, decryptedContent : string) => Promise<void>;
|
||||||
|
loadAvatarsFromCacheByPublicKey: (publicKey : string, allDecode? : boolean) => Promise<void>;
|
||||||
|
changeAvatar: (base64Image : string, entity : string) => Promise<void>;
|
||||||
|
decodedAvatarsCache: AvatarCacheEntry[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const AvatarContext = createContext<AvatarProviderContextValue | null>(null);
|
||||||
|
|
||||||
interface AvatarProviderProps {
|
interface AvatarProviderProps {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { useContext } from "react";
|
import { useContext } from "react";
|
||||||
import { AvatarContext } from "./AvatarProvider";
|
import { AvatarContext } from "./AvatarProvider";
|
||||||
|
|
||||||
export function useSaveAvatar() {
|
export function useSaveAvatar() : (fromPublicKey: string, path : string, decryptedContent : string) => Promise<void> {
|
||||||
const context : any = useContext(AvatarContext);
|
const context : any = useContext(AvatarContext);
|
||||||
if(!context){
|
if(!context){
|
||||||
throw new Error("useSaveAvatar must be used within an AvatarProvider");
|
throw new Error("useSaveAvatar must be used within an AvatarProvider");
|
||||||
}
|
}
|
||||||
return context.saveAvatar;
|
return context.saveAvatar;
|
||||||
|
|||||||
Reference in New Issue
Block a user