Передача транспортного сервера в контекст, в базу, и в кэш
This commit is contained in:
@@ -53,6 +53,7 @@ export function useAttachment(attachment: Attachment, parentMessage: MessageProp
|
||||
}
|
||||
|
||||
const calcDownloadStatus = async () => {
|
||||
console.info("ds", attachment);
|
||||
if (downloadStatus == DownloadStatus.DOWNLOADED) {
|
||||
return;
|
||||
}
|
||||
@@ -142,6 +143,7 @@ export function useAttachment(attachment: Attachment, parentMessage: MessageProp
|
||||
return;
|
||||
}
|
||||
setDownloadStatus(DownloadStatus.DECRYPTING);
|
||||
console.info("decoding with key " + parentMessage.chacha_key_plain);
|
||||
//console.info("Decrypted attachment ", Buffer.from(keyPlain, 'binary').toString('hex'));
|
||||
const decrypted = await decodeWithPassword(parentMessage.chacha_key_plain, downloadedBlob);
|
||||
setDownloadTag("");
|
||||
|
||||
@@ -9,7 +9,7 @@ import { useDialogsList } from "../DialogListProvider/useDialogsList";
|
||||
import { useDatabase } from "../DatabaseProvider/useDatabase";
|
||||
import { useConsoleLogger } from "@/app/hooks/useConsoleLogger";
|
||||
import { useDialogsCache } from "../DialogProvider/useDialogsCache";
|
||||
import { DialogContext } from "../DialogProvider/DialogProvider";
|
||||
import { AttachmentMeta, DialogContext } from "../DialogProvider/DialogProvider";
|
||||
import { useTransportServer } from "../TransportProvider/useTransportServer";
|
||||
import { usePublicKey } from "../AccountProvider/usePublicKey";
|
||||
|
||||
@@ -17,7 +17,7 @@ export function usePrepareAttachment() {
|
||||
const intervalsRef = useRef<NodeJS.Timeout>(null);
|
||||
const {uploadFile} = useTransport();
|
||||
const {updateDialog} = useDialogsList();
|
||||
const {runQuery} = useDatabase();
|
||||
const {runQuery, getQuery} = useDatabase();
|
||||
const {info} = useConsoleLogger('usePrepareAttachment');
|
||||
const {getDialogCache} = useDialogsCache();
|
||||
const context = useContext(DialogContext);
|
||||
@@ -37,6 +37,79 @@ export function usePrepareAttachment() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновляет транспортный сервер в кэше, чтобы поддерживать его в актуальном состоянии после загрузки
|
||||
*/
|
||||
const updateAttachmentTransportInCache = (dialog: string, message_id : string, attachment: Attachment) => {
|
||||
const dialogCache = getDialogCache(dialog);
|
||||
if(dialogCache == null){
|
||||
return;
|
||||
}
|
||||
for(let i = 0; i < dialogCache.length; i++){
|
||||
if(dialogCache[i].message_id == message_id){
|
||||
for(let j = 0; j < dialogCache[i].attachments.length; j++){
|
||||
if(dialogCache[i].attachments[j].id == attachment.id){
|
||||
dialogCache[i].attachments[j].transport = attachment.transport;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновляет транспорт в базе после загрузки вложения (нам нужно сохранить транспорт)
|
||||
*/
|
||||
const updateAttachmentTransportInDatabase = async (message_id : string, attachment: Attachment) => {
|
||||
let message = await getQuery(`SELECT attachments FROM messages WHERE message_id = ?`, [message_id]);
|
||||
console.info(message)
|
||||
if(!message){
|
||||
return;
|
||||
}
|
||||
if(message.attachments == '[]'){
|
||||
return;
|
||||
}
|
||||
let meta : AttachmentMeta[] = JSON.parse(message.attachments);
|
||||
for(let i = 0; i < meta.length; i++){
|
||||
if(meta[i].id == attachment.id){
|
||||
meta[i].transport = attachment.transport;
|
||||
}
|
||||
}
|
||||
await runQuery(`UPDATE messages SET attachments = ? WHERE message_id = ?`, [JSON.stringify(meta), message_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновляет вложение в стейте сообщений
|
||||
*/
|
||||
const updateAttachmentTransportInContext = (message_id: string, attachment : Attachment) => {
|
||||
if(context == null || !context){
|
||||
/**
|
||||
* Если этот диалог сейчас не открыт
|
||||
*/
|
||||
return;
|
||||
}
|
||||
context.setMessages((prev) => {
|
||||
return prev.map((value) => {
|
||||
if(value.message_id != message_id){
|
||||
return value;
|
||||
}
|
||||
for(let i = 0; i < value.attachments.length; i++){
|
||||
if(value.attachments[i].id != attachment.id){
|
||||
return value;
|
||||
}
|
||||
value.attachments[i].transport = attachment.transport;
|
||||
return value;
|
||||
}
|
||||
return value;
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
const updateTransportAfterUploading = async (dialog: string, message_id : string, attachment: Attachment) => {
|
||||
updateAttachmentTransportInCache(dialog, message_id, attachment);
|
||||
updateAttachmentTransportInDatabase(message_id, attachment);
|
||||
updateAttachmentTransportInContext(message_id, attachment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновляет временную метку в сообщении, пока вложения отправляются,
|
||||
* потому что если этого не делать, то сообщение может быть помечено как
|
||||
@@ -103,7 +176,9 @@ export function usePrepareAttachment() {
|
||||
if(attachment.type == AttachmentType.MESSAGES){
|
||||
let reply : MessageReply[] = JSON.parse(attachment.blob)
|
||||
for(let j = 0; j < reply.length; j++){
|
||||
reply[j].attachments = await prepareAttachmentsToSend(message_id, dialog, password, reply[j].attachments, true);
|
||||
for(let k = 0; k < reply[j].attachments.length; k++){
|
||||
reply[j].attachments[k].blob = "";
|
||||
}
|
||||
}
|
||||
prepared.push({
|
||||
...attachment,
|
||||
@@ -119,16 +194,16 @@ export function usePrepareAttachment() {
|
||||
const blurhash = await base64ImageToBlurhash(attachment.blob);
|
||||
attachment.preview = blurhash;
|
||||
}
|
||||
if(rePrepared && (attachment.encoding.encoded_for == dialog || attachment.encoding.encoder == dialog)){
|
||||
/**
|
||||
* Это пересланное сообщение и оно уже закодировано для этого диалога, или закодировано отправителем, значит не нужно его кодировать и загружать заново
|
||||
*/
|
||||
prepared.push({
|
||||
...attachment,
|
||||
blob: ""
|
||||
});
|
||||
continue;
|
||||
}
|
||||
// if(rePrepared && (attachment.encoding.encoded_for == dialog || attachment.encoding.encoder == dialog)){
|
||||
// /**
|
||||
// * Это пересланное сообщение и оно уже закодировано для этого диалога, или закодировано отправителем, значит не нужно его кодировать и загружать заново
|
||||
// */
|
||||
// prepared.push({
|
||||
// ...attachment,
|
||||
// blob: ""
|
||||
// });
|
||||
// continue;
|
||||
// }
|
||||
doTimestampUpdateImMessageWhileAttachmentsSend(message_id, dialog);
|
||||
const content = await encodeWithPassword(password, attachment.blob);
|
||||
const upid = attachment.id;
|
||||
@@ -139,7 +214,7 @@ export function usePrepareAttachment() {
|
||||
if(intervalsRef.current != null){
|
||||
clearInterval(intervalsRef.current);
|
||||
}
|
||||
prepared.push({
|
||||
const preparedAttachment : Attachment = {
|
||||
...attachment,
|
||||
transport: {
|
||||
transport_server: transportServer || "",
|
||||
@@ -151,7 +226,9 @@ export function usePrepareAttachment() {
|
||||
},
|
||||
preview: attachment.preview,
|
||||
blob: ""
|
||||
});
|
||||
};
|
||||
await updateTransportAfterUploading(dialog, message_id, preparedAttachment);
|
||||
prepared.push(preparedAttachment);
|
||||
}
|
||||
return prepared;
|
||||
}catch(e){
|
||||
|
||||
Reference in New Issue
Block a user