@xmanrui/dsh-im 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -313,12 +313,31 @@ function waitWithSignal(promise, signal) {
313
313
 
314
314
  function uncertainArtifactDelivery(error) {
315
315
  if (error?.code === 'artifact-delivery-uncertain') return error;
316
- const uncertain = new Error('WhatsApp could not confirm result file delivery.');
316
+ const uncertain = new Error('WhatsApp could not confirm artifact delivery.');
317
317
  uncertain.code = 'artifact-delivery-uncertain';
318
318
  uncertain.cause = error;
319
319
  return uncertain;
320
320
  }
321
321
 
322
+ function whatsappArtifactError(error) {
323
+ if (error?.code?.startsWith?.('artifact-')) return error;
324
+ const status = error?.output?.statusCode
325
+ ?? error?.data?.statusCode
326
+ ?? error?.statusCode;
327
+ let code;
328
+ if (status === 401 || status === 403) code = 'artifact-permission-required';
329
+ else if (status === 413) code = 'artifact-too-large';
330
+ else if (status === 429) code = 'artifact-rate-limited';
331
+ else if ([400, 404, 405, 406, 410, 415, 422].includes(status)) {
332
+ code = 'artifact-provider-rejected';
333
+ }
334
+ if (!code) return uncertainArtifactDelivery(error);
335
+ const wrapped = new Error('WhatsApp rejected artifact delivery.');
336
+ wrapped.code = code;
337
+ wrapped.cause = error;
338
+ return wrapped;
339
+ }
340
+
322
341
  export class WhatsappBotClient {
323
342
  #socket;
324
343
  #outboundIds;
@@ -354,14 +373,32 @@ export class WhatsappBotClient {
354
373
  }
355
374
 
356
375
  async sendFile(target, file) {
376
+ return this.#sendArtifact(target, file, {
377
+ document: file.bytes,
378
+ mimetype: file.mediaType ?? 'application/octet-stream',
379
+ fileName: file.fileName,
380
+ }, 'file');
381
+ }
382
+
383
+ async sendImage(target, file) {
384
+ return this.#sendArtifact(target, file, {
385
+ image: file.bytes,
386
+ mimetype: file.mediaType ?? 'image/jpeg',
387
+ }, 'image');
388
+ }
389
+
390
+ async #sendArtifact(target, file, content, presentation) {
357
391
  this.#signal?.throwIfAborted();
358
392
  await this.#stopTyping(target.jid);
359
393
  this.#signal?.throwIfAborted();
360
394
  const deliverySeed = typeof file.deliveryKey === 'string' && file.deliveryKey
361
395
  ? file.deliveryKey
362
396
  : file.artifactId;
363
- const messageId = typeof deliverySeed === 'string' && deliverySeed
364
- ? createHash('sha256').update(deliverySeed).digest('hex').slice(0, 20).toUpperCase()
397
+ const messageIdSeed = presentation === 'image'
398
+ ? `${deliverySeed}:image`
399
+ : deliverySeed;
400
+ const messageId = typeof messageIdSeed === 'string' && messageIdSeed
401
+ ? createHash('sha256').update(messageIdSeed).digest('hex').slice(0, 20).toUpperCase()
365
402
  : undefined;
366
403
  const options = {
367
404
  ...(target.quoted ? { quoted: target.quoted } : {}),
@@ -375,11 +412,7 @@ export class WhatsappBotClient {
375
412
  try {
376
413
  const pending = this.#socket.sendMessage(
377
414
  target.jid,
378
- {
379
- document: file.bytes,
380
- mimetype: file.mediaType ?? 'application/octet-stream',
381
- fileName: file.fileName,
382
- },
415
+ content,
383
416
  options,
384
417
  );
385
418
  trackOutboundArtifactProviderPromise(file, pending);
@@ -390,7 +423,7 @@ export class WhatsappBotClient {
390
423
  result = await waitWithSignal(pending, waitSignal);
391
424
  } catch (error) {
392
425
  if (this.#signal?.aborted) throw abortReason(this.#signal);
393
- throw uncertainArtifactDelivery(error);
426
+ throw whatsappArtifactError(error);
394
427
  }
395
428
  this.#signal?.throwIfAborted();
396
429
  this.#outboundIds.remember(result?.key?.id);