Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Single image shown multiple times #5966

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/containers/message/hooks/useMediaAutoDownload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getMediaCache,
isDownloadActive,
MediaTypes,
persistMessageWithCacheFile,
TDownloadState
} from '../../../lib/methods/handleMediaDownload';
import { emitter } from '../../../lib/methods/helpers';
Expand Down Expand Up @@ -135,6 +136,10 @@ export const useMediaAutoDownload = ({
urlToCache: url
});
if (result?.exists && !isEncrypted) {
if (!currentFile?.title_link) {
await persistMessageWithCacheFile(id, result.uri, file.encryption, file.image_url || url);
}

updateCurrentFile(result.uri);
}
return result?.exists;
Expand Down
22 changes: 14 additions & 8 deletions app/lib/methods/handleMediaDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,42 +202,44 @@ export async function cancelDownload(messageUrl: string): Promise<void> {
const mapAttachments = ({
attachments,
uri,
encryption
encryption,
downloadUrl
}: {
attachments?: IAttachment[];
uri: string;
encryption: boolean;
downloadUrl: string;
}): TMessageModel['attachments'] =>
attachments?.map(att => ({
...att,
title_link: uri,
title_link: att.image_url && !downloadUrl.startsWith("file://") && downloadUrl.includes(att.image_url) ? uri : att.title_link,
e2e: encryption ? 'done' : undefined
}));

const persistMessage = async (messageId: string, uri: string, encryption: boolean) => {
const persistMessage = async (messageId: string, uri: string, encryption: boolean, downloadUrl: string) => {
const db = database.active;
const batch: Model[] = [];
const messageRecord = await getMessageById(messageId);
if (messageRecord) {
batch.push(
messageRecord.prepareUpdate(m => {
m.attachments = mapAttachments({ attachments: m.attachments, uri, encryption });
m.attachments = mapAttachments({ attachments: m.attachments, uri, encryption, downloadUrl });
})
);
}
const threadRecord = await getThreadById(messageId);
if (threadRecord) {
batch.push(
threadRecord.prepareUpdate(m => {
m.attachments = mapAttachments({ attachments: m.attachments, uri, encryption });
m.attachments = mapAttachments({ attachments: m.attachments, uri, encryption, downloadUrl });
})
);
}
const threadMessageRecord = await getThreadMessageById(messageId);
if (threadMessageRecord) {
batch.push(
threadMessageRecord.prepareUpdate(m => {
m.attachments = mapAttachments({ attachments: m.attachments, uri, encryption });
m.attachments = mapAttachments({ attachments: m.attachments, uri, encryption, downloadUrl });
})
);
}
Expand Down Expand Up @@ -270,7 +272,7 @@ export function downloadMediaFile({
if (!path) {
return reject();
}
downloadKey = mediaDownloadKey(downloadUrl);
downloadKey = messageId + mediaDownloadKey(downloadUrl);
downloadQueue[downloadKey] = FileSystem.createDownloadResumable(downloadUrl, path);
const result = await downloadQueue[downloadKey].downloadAsync();

Expand All @@ -282,7 +284,7 @@ export function downloadMediaFile({
await Encryption.addFileToDecryptFileQueue(messageId, result.uri, encryption, originalChecksum);
}

await persistMessage(messageId, result.uri, !!encryption);
await persistMessage(messageId, result.uri, !!encryption, downloadUrl);

emitter.emit(`downloadMedia${downloadUrl}`, result.uri);
return resolve(result.uri);
Expand All @@ -294,3 +296,7 @@ export function downloadMediaFile({
}
});
}

export const persistMessageWithCacheFile = async (messageId: string, uri: string, encryption: TAttachmentEncryption | undefined, downloadUrl: string) => {
await persistMessage(messageId, uri, !!encryption, downloadUrl);
}