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:
@@ -33,6 +33,7 @@ import {
|
||||
getStatusCodeForMediaRetry,
|
||||
getUrlFromDirectPath,
|
||||
getWAUploadToServer,
|
||||
hasNonNullishProperty,
|
||||
MessageRetryManager,
|
||||
normalizeMessageContent,
|
||||
parseAndInjectE2ESessions,
|
||||
@@ -1345,8 +1346,9 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
}
|
||||
|
||||
// Count media types for album root
|
||||
const imageCount = medias.filter(m => 'image' in m).length
|
||||
const videoCount = medias.filter(m => 'video' in m).length
|
||||
// Use hasNonNullishProperty for consistency with generateWAMessageContent validation
|
||||
const imageCount = medias.filter(m => hasNonNullishProperty(m as AnyMessageContent, 'image')).length
|
||||
const videoCount = medias.filter(m => hasNonNullishProperty(m as AnyMessageContent, 'video')).length
|
||||
|
||||
logger.info(
|
||||
{ jid, totalItems: medias.length, imageCount, videoCount, delayConfig, retryCount },
|
||||
@@ -1550,6 +1552,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
}
|
||||
|
||||
// Calculate final results
|
||||
const attemptedItems = results.length
|
||||
const stoppedEarly = attemptedItems < medias.length
|
||||
const successCount = 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)
|
||||
@@ -1559,10 +1563,12 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
albumKey,
|
||||
results,
|
||||
totalItems: medias.length,
|
||||
attemptedItems,
|
||||
successCount,
|
||||
failedCount,
|
||||
failedIndices,
|
||||
success: failedCount === 0,
|
||||
success: failedCount === 0 && !stoppedEarly,
|
||||
stoppedEarly,
|
||||
totalLatencyMs
|
||||
}
|
||||
|
||||
@@ -1570,8 +1576,10 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
{
|
||||
albumKeyId: albumKey.id,
|
||||
totalItems: medias.length,
|
||||
attemptedItems,
|
||||
successCount,
|
||||
failedCount,
|
||||
stoppedEarly,
|
||||
totalLatencyMs
|
||||
},
|
||||
'Album message send completed'
|
||||
|
||||
@@ -363,8 +363,10 @@ export type AlbumSendResult = {
|
||||
albumKey: WAMessageKey
|
||||
/** Results for each media item */
|
||||
results: AlbumMediaResult[]
|
||||
/** Total number of items */
|
||||
/** Total number of items in the album */
|
||||
totalItems: number
|
||||
/** Number of items that were actually attempted (may be < totalItems if stoppedEarly) */
|
||||
attemptedItems: number
|
||||
/** Number of successfully sent items */
|
||||
successCount: number
|
||||
/** Number of failed items */
|
||||
@@ -373,6 +375,8 @@ export type AlbumSendResult = {
|
||||
failedIndices: number[]
|
||||
/** Overall success (all items sent) */
|
||||
success: boolean
|
||||
/** Whether the send was interrupted early due to continueOnFailure=false */
|
||||
stoppedEarly: boolean
|
||||
/** Total time taken in ms */
|
||||
totalLatencyMs: number
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user