@@ -69,6 +69,12 @@ export async function markAttachmentAsCorrupted(
6969 } ) ;
7070}
7171
72+ export class AttachmentNotNeededForMessageError extends Error {
73+ constructor ( public readonly attachment : AttachmentType ) {
74+ super ( 'AttachmentNotNeededForMessageError' ) ;
75+ }
76+ }
77+
7278export async function addAttachmentToMessage (
7379 messageId : string ,
7480 attachment : AttachmentType ,
@@ -167,14 +173,15 @@ export async function addAttachmentToMessage(
167173 await deleteAttachmentData ( attachment . path ) ;
168174 }
169175 if ( ! handledAnywhere ) {
170- log . warn (
171- `${ logPrefix } : Long message attachment found no matching place to apply`
172- ) ;
176+ // eslint-disable-next-line no-unsafe-finally
177+ throw new AttachmentNotNeededForMessageError ( attachment ) ;
173178 }
174179 }
175180 return ;
176181 }
177182
183+ let foundPlaceForAttachment = false ;
184+
178185 const maybeReplaceAttachment = ( existing : AttachmentType ) : AttachmentType => {
179186 if ( isDownloaded ( existing ) ) {
180187 return existing ;
@@ -184,13 +191,13 @@ export async function addAttachmentToMessage(
184191 return existing ;
185192 }
186193
194+ foundPlaceForAttachment = true ;
187195 return attachment ;
188196 } ;
189197
190198 if ( type === 'attachment' ) {
191199 const attachments = message . get ( 'attachments' ) ;
192200
193- let handledAnywhere = false ;
194201 let handledInEditHistory = false ;
195202
196203 const editHistory = message . get ( 'editHistory' ) ;
@@ -207,7 +214,6 @@ export async function addAttachmentToMessage(
207214 attachments : edit . attachments . map ( item => {
208215 const newItem = maybeReplaceAttachment ( item ) ;
209216 handledInEditHistory ||= item !== newItem ;
210- handledAnywhere ||= handledInEditHistory ;
211217 return newItem ;
212218 } ) ,
213219 } ;
@@ -220,18 +226,12 @@ export async function addAttachmentToMessage(
220226
221227 if ( attachments ) {
222228 message . set ( {
223- attachments : attachments . map ( item => {
224- const newItem = maybeReplaceAttachment ( item ) ;
225- handledAnywhere ||= item !== newItem ;
226- return newItem ;
227- } ) ,
229+ attachments : attachments . map ( maybeReplaceAttachment ) ,
228230 } ) ;
229231 }
230232
231- if ( ! handledAnywhere ) {
232- log . warn (
233- `${ logPrefix } : 'attachment' type found no matching place to apply`
234- ) ;
233+ if ( ! foundPlaceForAttachment ) {
234+ throw new AttachmentNotNeededForMessageError ( attachment ) ;
235235 }
236236
237237 return ;
@@ -243,7 +243,7 @@ export async function addAttachmentToMessage(
243243 let handledInEditHistory = false ;
244244
245245 const editHistory = message . get ( 'editHistory' ) ;
246- if ( preview && editHistory ) {
246+ if ( editHistory ) {
247247 const newEditHistory = editHistory . map ( edit => {
248248 if ( ! edit . preview ) {
249249 return edit ;
@@ -282,6 +282,10 @@ export async function addAttachmentToMessage(
282282 } ) ;
283283 }
284284
285+ if ( ! foundPlaceForAttachment ) {
286+ throw new AttachmentNotNeededForMessageError ( attachment ) ;
287+ }
288+
285289 return ;
286290 }
287291
@@ -290,7 +294,6 @@ export async function addAttachmentToMessage(
290294 if ( ! contacts ?. length ) {
291295 throw new Error ( `${ logPrefix } : no contacts, cannot add attachment!` ) ;
292296 }
293- let handled = false ;
294297
295298 const newContacts = contacts . map ( contact => {
296299 if ( ! contact . avatar ?. avatar ) {
@@ -301,7 +304,6 @@ export async function addAttachmentToMessage(
301304
302305 const newAttachment = maybeReplaceAttachment ( existingAttachment ) ;
303306 if ( existingAttachment !== newAttachment ) {
304- handled = true ;
305307 return {
306308 ...contact ,
307309 avatar : { ...contact . avatar , avatar : newAttachment } ,
@@ -310,10 +312,8 @@ export async function addAttachmentToMessage(
310312 return contact ;
311313 } ) ;
312314
313- if ( ! handled ) {
314- throw new Error (
315- `${ logPrefix } : Couldn't find matching contact with avatar attachment for message`
316- ) ;
315+ if ( ! foundPlaceForAttachment ) {
316+ throw new AttachmentNotNeededForMessageError ( attachment ) ;
317317 }
318318
319319 message . set ( { contact : newContacts } ) ;
@@ -374,6 +374,10 @@ export async function addAttachmentToMessage(
374374 message . set ( { quote : newQuote } ) ;
375375 }
376376
377+ if ( ! foundPlaceForAttachment ) {
378+ throw new AttachmentNotNeededForMessageError ( attachment ) ;
379+ }
380+
377381 return ;
378382 }
379383
@@ -389,6 +393,11 @@ export async function addAttachmentToMessage(
389393 data : sticker . data ? maybeReplaceAttachment ( sticker . data ) : attachment ,
390394 } ,
391395 } ) ;
396+
397+ if ( ! foundPlaceForAttachment ) {
398+ throw new AttachmentNotNeededForMessageError ( attachment ) ;
399+ }
400+
392401 return ;
393402 }
394403
0 commit comments