@@ -8,6 +8,7 @@ import { MessageModel } from '../../models/message';
8
8
import { downloadAttachment , downloadAttachmentSogsV3 } from '../../receiver/attachments' ;
9
9
import { initializeAttachmentLogic , processNewAttachment } from '../../types/MessageAttachment' ;
10
10
import { getAttachmentMetadata } from '../../types/message/initializeAttachmentMetadata' ;
11
+ import { was404Error } from '../apis/snode_api/onions' ;
11
12
12
13
// this may cause issues if we increment that value to > 1, but only having one job will block the whole queue while one attachment is downloading
13
14
const MAX_ATTACHMENT_JOB_PARALLELISM = 3 ;
@@ -175,6 +176,7 @@ async function _runJob(job: any) {
175
176
let downloaded ;
176
177
177
178
try {
179
+ // those two functions throw if they get a 404
178
180
if ( isOpenGroupV2 ) {
179
181
downloaded = await downloadAttachmentSogsV3 ( attachment , openGroupV2Details ) ;
180
182
} else {
@@ -189,14 +191,12 @@ async function _runJob(job: any) {
189
191
} from message ${ found . idForLogging ( ) } as permanent error`
190
192
) ;
191
193
192
- await _finishJob ( found , id ) ;
193
-
194
194
// Make sure to fetch the message from DB here right before writing it.
195
195
// This is to avoid race condition where multiple attachments in a single message get downloaded at the same time,
196
196
// and tries to update the same message.
197
197
found = await Data . getMessageById ( messageId ) ;
198
-
199
198
_addAttachmentToMessage ( found , _markAttachmentAsError ( attachment ) , { type, index } ) ;
199
+ await _finishJob ( found , id ) ;
200
200
201
201
return ;
202
202
}
@@ -232,7 +232,9 @@ async function _runJob(job: any) {
232
232
// tslint:disable: restrict-plus-operands
233
233
const currentAttempt : 1 | 2 | 3 = ( attempts || 0 ) + 1 ;
234
234
235
- if ( currentAttempt >= 3 ) {
235
+ // if we get a 404 error for attachment downloaded, we can safely assume that the attachment expired server-side.
236
+ // so there is no need to continue trying to download it.
237
+ if ( currentAttempt >= 3 || was404Error ( error ) ) {
236
238
logger . error (
237
239
`_runJob: ${ currentAttempt } failed attempts, marking attachment ${ id } from message ${ found ?. idForLogging ( ) } as permament error:` ,
238
240
error && error . stack ? error . stack : error
@@ -321,40 +323,29 @@ function _addAttachmentToMessage(
321
323
return ;
322
324
}
323
325
324
- if ( type === 'preview' ) {
326
+ // for quote and previews, if the attachment cannot be downloaded we just erase it from the message itself, so just the title or body is rendered
327
+ if ( type === 'preview' || type === 'quote' ) {
328
+ if ( type === 'quote' ) {
329
+ const quote = message . get ( 'quote' ) ;
330
+ if ( ! quote ) {
331
+ throw new Error ( "_addAttachmentToMessage: quote didn't exist" ) ;
332
+ }
333
+
334
+ delete message . attributes . quote . attachments ;
335
+
336
+ return ;
337
+ }
325
338
const preview = message . get ( 'preview' ) ;
326
339
if ( ! preview || preview . length <= index ) {
327
340
throw new Error ( `_addAttachmentToMessage: preview didn't exist or ${ index } was too large` ) ;
328
341
}
329
- const item = preview [ index ] ;
330
- if ( ! item ) {
331
- throw new Error ( `_addAttachmentToMessage: preview ${ index } was falsey` ) ;
332
- }
333
- _replaceAttachment ( item , 'image' , attachment , logPrefix ) ;
334
- return ;
335
- }
336
-
337
- if ( type === 'quote' ) {
338
- const quote = message . get ( 'quote' ) ;
339
- if ( ! quote ) {
340
- throw new Error ( "_addAttachmentToMessage: quote didn't exist" ) ;
341
- }
342
- const { attachments } = quote ;
343
- if ( ! attachments || attachments . length <= index ) {
344
- throw new Error (
345
- `_addAttachmentToMessage: quote attachments didn't exist or ${ index } was too large`
346
- ) ;
347
- }
348
-
349
- const item = attachments [ index ] ;
350
- if ( ! item ) {
351
- throw new Error ( `_addAttachmentToMessage: attachment ${ index } was falsey` ) ;
352
- }
353
- _replaceAttachment ( item , 'thumbnail' , attachment , logPrefix ) ;
354
342
343
+ delete message . attributes . preview [ 0 ] . image ;
355
344
return ;
356
345
}
357
346
347
+ // for quote and previews, if the attachment cannot be downloaded we just erase it from the message itself, so just the title or body is rendered
348
+
358
349
throw new Error (
359
350
`_addAttachmentToMessage: Unknown job type ${ type } for message ${ message . idForLogging ( ) } `
360
351
) ;
0 commit comments