fix(album): improve validation consistency and result clarity

Addresses additional PR review suggestions:

1. **Consistent media type validation**
   - Changed from `'image' in m` to `hasNonNullishProperty(m, 'image')`
   - Aligns with validation in generateWAMessageContent
   - Prevents counting items with undefined image/video properties

2. **Explicit interrupted send indication**
   - Added `attemptedItems: number` - how many items were actually tried
   - Added `stoppedEarly: boolean` - true if interrupted by continueOnFailure=false
   - Updated `success` to be false if stoppedEarly (even if no failures in attempted items)
   - Helps automated integrations understand partial sends

Example result when interrupted:
```json
{
  "totalItems": 5,
  "attemptedItems": 3,
  "successCount": 2,
  "failedCount": 1,
  "stoppedEarly": true,
  "success": false
}
```

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Renato Alcara
2026-01-23 15:33:46 -03:00
parent 54549c4fc1
commit 688739239a
2 changed files with 16 additions and 4 deletions
+11 -3
View File
@@ -33,6 +33,7 @@ import {
getStatusCodeForMediaRetry, getStatusCodeForMediaRetry,
getUrlFromDirectPath, getUrlFromDirectPath,
getWAUploadToServer, getWAUploadToServer,
hasNonNullishProperty,
MessageRetryManager, MessageRetryManager,
normalizeMessageContent, normalizeMessageContent,
parseAndInjectE2ESessions, parseAndInjectE2ESessions,
@@ -1345,8 +1346,9 @@ export const makeMessagesSocket = (config: SocketConfig) => {
} }
// Count media types for album root // Count media types for album root
const imageCount = medias.filter(m => 'image' in m).length // Use hasNonNullishProperty for consistency with generateWAMessageContent validation
const videoCount = medias.filter(m => 'video' in m).length const imageCount = medias.filter(m => hasNonNullishProperty(m as AnyMessageContent, 'image')).length
const videoCount = medias.filter(m => hasNonNullishProperty(m as AnyMessageContent, 'video')).length
logger.info( logger.info(
{ jid, totalItems: medias.length, imageCount, videoCount, delayConfig, retryCount }, { jid, totalItems: medias.length, imageCount, videoCount, delayConfig, retryCount },
@@ -1550,6 +1552,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
} }
// Calculate final results // Calculate final results
const attemptedItems = results.length
const stoppedEarly = attemptedItems < medias.length
const successCount = results.filter(r => r.success).length const successCount = results.filter(r => r.success).length
const failedCount = results.filter(r => !r.success).length const failedCount = results.filter(r => !r.success).length
const failedIndices = results.filter(r => !r.success).map(r => r.index) const failedIndices = results.filter(r => !r.success).map(r => r.index)
@@ -1559,10 +1563,12 @@ export const makeMessagesSocket = (config: SocketConfig) => {
albumKey, albumKey,
results, results,
totalItems: medias.length, totalItems: medias.length,
attemptedItems,
successCount, successCount,
failedCount, failedCount,
failedIndices, failedIndices,
success: failedCount === 0, success: failedCount === 0 && !stoppedEarly,
stoppedEarly,
totalLatencyMs totalLatencyMs
} }
@@ -1570,8 +1576,10 @@ export const makeMessagesSocket = (config: SocketConfig) => {
{ {
albumKeyId: albumKey.id, albumKeyId: albumKey.id,
totalItems: medias.length, totalItems: medias.length,
attemptedItems,
successCount, successCount,
failedCount, failedCount,
stoppedEarly,
totalLatencyMs totalLatencyMs
}, },
'Album message send completed' 'Album message send completed'
+5 -1
View File
@@ -363,8 +363,10 @@ export type AlbumSendResult = {
albumKey: WAMessageKey albumKey: WAMessageKey
/** Results for each media item */ /** Results for each media item */
results: AlbumMediaResult[] results: AlbumMediaResult[]
/** Total number of items */ /** Total number of items in the album */
totalItems: number totalItems: number
/** Number of items that were actually attempted (may be < totalItems if stoppedEarly) */
attemptedItems: number
/** Number of successfully sent items */ /** Number of successfully sent items */
successCount: number successCount: number
/** Number of failed items */ /** Number of failed items */
@@ -373,6 +375,8 @@ export type AlbumSendResult = {
failedIndices: number[] failedIndices: number[]
/** Overall success (all items sent) */ /** Overall success (all items sent) */
success: boolean success: boolean
/** Whether the send was interrupted early due to continueOnFailure=false */
stoppedEarly: boolean
/** Total time taken in ms */ /** Total time taken in ms */
totalLatencyMs: number totalLatencyMs: number
} }