From 43459b8ea978529183712e242a732e52d4c950e8 Mon Sep 17 00:00:00 2001 From: zubiden <19638254+zubiden@users.noreply.github.com> Date: Tue, 6 Aug 2024 20:05:45 +0200 Subject: [PATCH] Album: Attach caption to the last file on send (#4766) --- src/global/actions/api/messages.ts | 53 ++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/src/global/actions/api/messages.ts b/src/global/actions/api/messages.ts index df4761344..d12009567 100644 --- a/src/global/actions/api/messages.ts +++ b/src/global/actions/api/messages.ts @@ -363,30 +363,55 @@ addActionHandler('sendMessage', (global, actions, payload): ActionReturnType => } = params; const byType = splitAttachmentsByType(attachments!); + let hasSentCaption = false; byType.forEach((group, groupIndex) => { const groupedAttachments = split(group as ApiAttachment[], MAX_MEDIA_FILES_FOR_ALBUM); for (let i = 0; i < groupedAttachments.length; i++) { - const [firstAttachment, ...restAttachments] = groupedAttachments[i]; const groupedId = `${Date.now()}${groupIndex}${i}`; const isFirst = i === 0 && groupIndex === 0; + const isLast = i === groupedAttachments.length - 1 && groupIndex === byType.length - 1; - sendMessage(global, { - ...commonParams, - text: isFirst ? text : undefined, - entities: isFirst ? entities : undefined, - attachment: firstAttachment, - groupedId: restAttachments.length > 0 ? groupedId : undefined, - wasDrafted: Boolean(draft), - }); - - restAttachments.forEach((attachment: ApiAttachment) => { + if (group[0].quick && !group[0].shouldSendAsFile) { + const [firstAttachment, ...restAttachments] = groupedAttachments[i]; sendMessage(global, { ...commonParams, - attachment, - groupedId, + text: isFirst && !hasSentCaption ? text : undefined, + entities: isFirst && !hasSentCaption ? entities : undefined, + attachment: firstAttachment, + groupedId: restAttachments.length > 0 ? groupedId : undefined, + wasDrafted: Boolean(draft), }); - }); + hasSentCaption = true; + + restAttachments.forEach((attachment: ApiAttachment) => { + sendMessage(global, { + ...commonParams, + attachment, + groupedId, + }); + }); + } else { + const firstAttachments = groupedAttachments[i].slice(0, -1); + const lastAttachment = groupedAttachments[i][groupedAttachments[i].length - 1]; + firstAttachments.forEach((attachment: ApiAttachment) => { + sendMessage(global, { + ...commonParams, + attachment, + groupedId, + }); + }); + + sendMessage(global, { + ...commonParams, + text: isLast && !hasSentCaption ? text : undefined, + entities: isLast && !hasSentCaption ? entities : undefined, + attachment: lastAttachment, + groupedId: firstAttachments.length > 0 ? groupedId : undefined, + wasDrafted: Boolean(draft), + }); + hasSentCaption = true; + } } }); } else {