@sgintokic/baileys 0.0.2
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.
Potentially problematic release.
This version of @sgintokic/baileys might be problematic. Click here for more details.
- package/LICENSE +22 -0
- package/README.md +1097 -0
- package/WAProto/index.js +2 -0
- package/engine-requirements.js +1 -0
- package/lib/Defaults/index.js +155 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.js +41 -0
- package/lib/Signal/Group/group_cipher.js +108 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.js +14 -0
- package/lib/Signal/Group/sender-chain-key.js +31 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +79 -0
- package/lib/Signal/Group/sender-key-name.js +49 -0
- package/lib/Signal/Group/sender-key-record.js +46 -0
- package/lib/Signal/Group/sender-key-state.js +104 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/libsignal.js +485 -0
- package/lib/Signal/lid-mapping.js +291 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +64 -0
- package/lib/Socket/business.js +293 -0
- package/lib/Socket/chats.js +1068 -0
- package/lib/Socket/communities.js +476 -0
- package/lib/Socket/groups.js +383 -0
- package/lib/Socket/index.js +8 -0
- package/lib/Socket/messages-recv.js +1830 -0
- package/lib/Socket/messages-send.js +1462 -0
- package/lib/Socket/mex.js +55 -0
- package/lib/Socket/newsletter.js +277 -0
- package/lib/Socket/socket.js +1087 -0
- package/lib/Store/index.js +3 -0
- package/lib/Store/make-in-memory-store.js +517 -0
- package/lib/Store/make-ordered-dictionary.js +75 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.js +18 -0
- package/lib/Types/Newsletter.js +33 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +15 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.js +31 -0
- package/lib/Utils/auth-utils.js +293 -0
- package/lib/Utils/browser-utils.js +32 -0
- package/lib/Utils/business.js +245 -0
- package/lib/Utils/chat-utils.js +959 -0
- package/lib/Utils/crypto.js +133 -0
- package/lib/Utils/decode-wa-message.js +376 -0
- package/lib/Utils/event-buffer.js +620 -0
- package/lib/Utils/generics.js +417 -0
- package/lib/Utils/history.js +150 -0
- package/lib/Utils/identity-change-handler.js +63 -0
- package/lib/Utils/index.js +21 -0
- package/lib/Utils/link-preview.js +91 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.js +6 -0
- package/lib/Utils/make-mutex.js +31 -0
- package/lib/Utils/message-retry-manager.js +240 -0
- package/lib/Utils/messages-media.js +901 -0
- package/lib/Utils/messages.js +2052 -0
- package/lib/Utils/noise-handler.js +229 -0
- package/lib/Utils/offline-node-processor.js +50 -0
- package/lib/Utils/pre-key-manager.js +119 -0
- package/lib/Utils/process-message.js +641 -0
- package/lib/Utils/reporting-utils.js +346 -0
- package/lib/Utils/signal.js +188 -0
- package/lib/Utils/stanza-ack.js +33 -0
- package/lib/Utils/sync-action-utils.js +53 -0
- package/lib/Utils/tc-token-utils.js +15 -0
- package/lib/Utils/use-multi-file-auth-state.js +116 -0
- package/lib/Utils/use-single-file-auth-state.js +94 -0
- package/lib/Utils/validate-connection.js +235 -0
- package/lib/WABinary/constants.js +1300 -0
- package/lib/WABinary/decode.js +258 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.js +203 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.js +93 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.js +20669 -0
- package/lib/WAM/encode.js +151 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +21 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +50 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +20 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +29 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +59 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +21 -0
- package/lib/WAUSync/Protocols/index.js +4 -0
- package/lib/WAUSync/USyncQuery.js +103 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +11 -0
- package/package.json +58 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { prepareWAMessageMedia } from "./messages.js";
|
|
2
|
+
import { extractImageThumb, getHttpStream } from "./messages-media.js";
|
|
3
|
+
const THUMBNAIL_WIDTH_PX = 192;
|
|
4
|
+
/** Fetches an image and generates a thumbnail for it */ const getCompressedJpegThumbnail =
|
|
5
|
+
async (url, { thumbnailWidth, fetchOpts }) => {
|
|
6
|
+
const stream = await getHttpStream(url, fetchOpts);
|
|
7
|
+
const result = await extractImageThumb(stream, thumbnailWidth);
|
|
8
|
+
return result;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Given a piece of text, checks for any URL present, generates link preview for the same and returns it
|
|
12
|
+
* Return undefined if the fetch failed or no URL was found
|
|
13
|
+
* @param text first matched URL in text
|
|
14
|
+
* @returns the URL info required to generate link preview
|
|
15
|
+
*/ export const getUrlInfo = async (
|
|
16
|
+
text,
|
|
17
|
+
opts = { thumbnailWidth: THUMBNAIL_WIDTH_PX, fetchOpts: { timeout: 3e3 } },
|
|
18
|
+
) => {
|
|
19
|
+
try {
|
|
20
|
+
// retries
|
|
21
|
+
const retries = 0;
|
|
22
|
+
const maxRetry = 5;
|
|
23
|
+
const { getLinkPreview } = await import("link-preview-js");
|
|
24
|
+
let previewLink = text;
|
|
25
|
+
if (!text.startsWith("https://") && !text.startsWith("http://")) {
|
|
26
|
+
previewLink = "https://" + previewLink;
|
|
27
|
+
}
|
|
28
|
+
const info = await getLinkPreview(previewLink, {
|
|
29
|
+
...opts.fetchOpts,
|
|
30
|
+
followRedirects: "follow",
|
|
31
|
+
handleRedirects: (baseURL, forwardedURL) => {
|
|
32
|
+
const urlObj = new URL(baseURL);
|
|
33
|
+
const forwardedURLObj = new URL(forwardedURL);
|
|
34
|
+
if (retries >= maxRetry) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
if (
|
|
38
|
+
forwardedURLObj.hostname === urlObj.hostname ||
|
|
39
|
+
forwardedURLObj.hostname === "www." + urlObj.hostname ||
|
|
40
|
+
"www." + forwardedURLObj.hostname === urlObj.hostname
|
|
41
|
+
) {
|
|
42
|
+
retries + 1;
|
|
43
|
+
return true;
|
|
44
|
+
} else {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
headers: opts.fetchOpts?.headers,
|
|
49
|
+
});
|
|
50
|
+
if (info && "title" in info && info.title) {
|
|
51
|
+
const [image] = info.images;
|
|
52
|
+
const urlInfo = {
|
|
53
|
+
"canonical-url": info.url,
|
|
54
|
+
"matched-text": text,
|
|
55
|
+
title: info.title,
|
|
56
|
+
description: info.description,
|
|
57
|
+
originalThumbnailUrl: image,
|
|
58
|
+
};
|
|
59
|
+
if (opts.uploadImage) {
|
|
60
|
+
const { imageMessage } = await prepareWAMessageMedia(
|
|
61
|
+
{ image: { url: image } },
|
|
62
|
+
{
|
|
63
|
+
upload: opts.uploadImage,
|
|
64
|
+
mediaTypeOverride: "thumbnail-link",
|
|
65
|
+
options: opts.fetchOpts,
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail
|
|
69
|
+
? Buffer.from(imageMessage.jpegThumbnail)
|
|
70
|
+
: undefined;
|
|
71
|
+
urlInfo.highQualityThumbnail = imageMessage || undefined;
|
|
72
|
+
} else {
|
|
73
|
+
try {
|
|
74
|
+
urlInfo.jpegThumbnail = image
|
|
75
|
+
? (await getCompressedJpegThumbnail(image, opts)).buffer
|
|
76
|
+
: undefined;
|
|
77
|
+
} catch (error) {
|
|
78
|
+
opts.logger?.debug(
|
|
79
|
+
{ err: error.stack, url: previewLink },
|
|
80
|
+
"error in generating thumbnail",
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return urlInfo;
|
|
85
|
+
}
|
|
86
|
+
} catch (error) {
|
|
87
|
+
if (!error.message.includes("receive a valid")) {
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { LTHashAntiTampering } from "whatsapp-rust-bridge";
|
|
2
|
+
/**
|
|
3
|
+
* LT Hash is a summation based hash algorithm that maintains the integrity of a piece of data
|
|
4
|
+
* over a series of mutations. You can add/remove mutations and it'll return a hash equal to
|
|
5
|
+
* if the same series of mutations was made sequentially.
|
|
6
|
+
*/ export const LT_HASH_ANTI_TAMPERING = new LTHashAntiTampering();
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Mutex as AsyncMutex } from "async-mutex";
|
|
2
|
+
export const makeMutex = () => {
|
|
3
|
+
const mutex = new AsyncMutex();
|
|
4
|
+
return {
|
|
5
|
+
mutex(code) {
|
|
6
|
+
return mutex.runExclusive(code);
|
|
7
|
+
},
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export const makeKeyedMutex = () => {
|
|
11
|
+
const map = new Map();
|
|
12
|
+
return {
|
|
13
|
+
async mutex(key, task) {
|
|
14
|
+
let entry = map.get(key);
|
|
15
|
+
if (!entry) {
|
|
16
|
+
entry = { mutex: new AsyncMutex(), refCount: 0 };
|
|
17
|
+
map.set(key, entry);
|
|
18
|
+
}
|
|
19
|
+
entry.refCount++;
|
|
20
|
+
try {
|
|
21
|
+
return await entry.mutex.runExclusive(task);
|
|
22
|
+
} finally {
|
|
23
|
+
entry.refCount--;
|
|
24
|
+
// only delete it if this is still the current entry
|
|
25
|
+
if (entry.refCount === 0 && map.get(key) === entry) {
|
|
26
|
+
map.delete(key);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
};
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { LRUCache } from "lru-cache";
|
|
2
|
+
/** Number of sent messages to cache in memory for handling retry receipts */ const RECENT_MESSAGES_SIZE = 512;
|
|
3
|
+
const MESSAGE_KEY_SEPARATOR = "\0";
|
|
4
|
+
/** Timeout for session recreation - 1 hour */ const RECREATE_SESSION_TIMEOUT =
|
|
5
|
+
60 * 60 * 1e3; // 1 hour in milliseconds
|
|
6
|
+
const PHONE_REQUEST_DELAY = 3e3;
|
|
7
|
+
// Retry reason codes matching WhatsApp Web's Signal error codes.
|
|
8
|
+
export var RetryReason;
|
|
9
|
+
(function (RetryReason) {
|
|
10
|
+
RetryReason[(RetryReason["UnknownError"] = 0)] = "UnknownError";
|
|
11
|
+
RetryReason[(RetryReason["SignalErrorNoSession"] = 1)] =
|
|
12
|
+
"SignalErrorNoSession";
|
|
13
|
+
RetryReason[(RetryReason["SignalErrorInvalidKey"] = 2)] =
|
|
14
|
+
"SignalErrorInvalidKey";
|
|
15
|
+
RetryReason[(RetryReason["SignalErrorInvalidKeyId"] = 3)] =
|
|
16
|
+
"SignalErrorInvalidKeyId";
|
|
17
|
+
/** MAC verification failed - most common cause of decryption failures */ RetryReason[
|
|
18
|
+
(RetryReason["SignalErrorInvalidMessage"] = 4)
|
|
19
|
+
] = "SignalErrorInvalidMessage";
|
|
20
|
+
RetryReason[(RetryReason["SignalErrorInvalidSignature"] = 5)] =
|
|
21
|
+
"SignalErrorInvalidSignature";
|
|
22
|
+
RetryReason[(RetryReason["SignalErrorFutureMessage"] = 6)] =
|
|
23
|
+
"SignalErrorFutureMessage";
|
|
24
|
+
/** Explicit MAC failure - session is definitely out of sync */ RetryReason[
|
|
25
|
+
(RetryReason["SignalErrorBadMac"] = 7)
|
|
26
|
+
] = "SignalErrorBadMac";
|
|
27
|
+
RetryReason[(RetryReason["SignalErrorInvalidSession"] = 8)] =
|
|
28
|
+
"SignalErrorInvalidSession";
|
|
29
|
+
RetryReason[(RetryReason["SignalErrorInvalidMsgKey"] = 9)] =
|
|
30
|
+
"SignalErrorInvalidMsgKey";
|
|
31
|
+
RetryReason[(RetryReason["BadBroadcastEphemeralSetting"] = 10)] =
|
|
32
|
+
"BadBroadcastEphemeralSetting";
|
|
33
|
+
RetryReason[(RetryReason["UnknownCompanionNoPrekey"] = 11)] =
|
|
34
|
+
"UnknownCompanionNoPrekey";
|
|
35
|
+
RetryReason[(RetryReason["AdvFailure"] = 12)] = "AdvFailure";
|
|
36
|
+
RetryReason[(RetryReason["StatusRevokeDelay"] = 13)] = "StatusRevokeDelay";
|
|
37
|
+
})(RetryReason || (RetryReason = {}));
|
|
38
|
+
/** Error codes that indicate a MAC failure and require immediate session recreation */ const MAC_ERROR_CODES =
|
|
39
|
+
new Set([
|
|
40
|
+
RetryReason.SignalErrorInvalidMessage,
|
|
41
|
+
RetryReason.SignalErrorBadMac,
|
|
42
|
+
]);
|
|
43
|
+
export class MessageRetryManager {
|
|
44
|
+
constructor(logger, maxMsgRetryCount) {
|
|
45
|
+
this.logger = logger;
|
|
46
|
+
this.recentMessagesMap = new LRUCache({
|
|
47
|
+
max: RECENT_MESSAGES_SIZE,
|
|
48
|
+
ttl: 5 * 60 * 1e3,
|
|
49
|
+
ttlAutopurge: true,
|
|
50
|
+
dispose: (_value, key) => {
|
|
51
|
+
const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR);
|
|
52
|
+
if (separatorIndex > -1) {
|
|
53
|
+
const messageId = key.slice(
|
|
54
|
+
separatorIndex + MESSAGE_KEY_SEPARATOR.length,
|
|
55
|
+
);
|
|
56
|
+
this.messageKeyIndex.delete(messageId);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
this.messageKeyIndex = new Map();
|
|
61
|
+
this.sessionRecreateHistory = new LRUCache({
|
|
62
|
+
ttl: RECREATE_SESSION_TIMEOUT * 2,
|
|
63
|
+
ttlAutopurge: true,
|
|
64
|
+
});
|
|
65
|
+
this.retryCounters = new LRUCache({
|
|
66
|
+
ttl: 15 * 60 * 1e3,
|
|
67
|
+
ttlAutopurge: true,
|
|
68
|
+
updateAgeOnGet: true,
|
|
69
|
+
}); // 15 minutes TTL
|
|
70
|
+
this.pendingPhoneRequests = {};
|
|
71
|
+
this.maxMsgRetryCount = 5;
|
|
72
|
+
this.statistics = {
|
|
73
|
+
totalRetries: 0,
|
|
74
|
+
successfulRetries: 0,
|
|
75
|
+
failedRetries: 0,
|
|
76
|
+
mediaRetries: 0,
|
|
77
|
+
sessionRecreations: 0,
|
|
78
|
+
phoneRequests: 0,
|
|
79
|
+
};
|
|
80
|
+
this.maxMsgRetryCount = maxMsgRetryCount;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Add a recent message to the cache for retry handling
|
|
84
|
+
*/ addRecentMessage(to, id, message) {
|
|
85
|
+
const key = { to: to, id: id };
|
|
86
|
+
const keyStr = this.keyToString(key);
|
|
87
|
+
// Add new message
|
|
88
|
+
this.recentMessagesMap.set(keyStr, {
|
|
89
|
+
message: message,
|
|
90
|
+
timestamp: Date.now(),
|
|
91
|
+
});
|
|
92
|
+
this.messageKeyIndex.set(id, keyStr);
|
|
93
|
+
this.logger.debug(`Added message to retry cache: ${to}/${id}`);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get a recent message from the cache
|
|
97
|
+
*/ getRecentMessage(to, id) {
|
|
98
|
+
const key = { to: to, id: id };
|
|
99
|
+
const keyStr = this.keyToString(key);
|
|
100
|
+
return this.recentMessagesMap.get(keyStr);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Check if a session should be recreated based on retry count, history, and error code.
|
|
104
|
+
* MAC errors (codes 4 and 7) trigger immediate session recreation regardless of timeout.
|
|
105
|
+
*/ shouldRecreateSession(jid, hasSession, errorCode) {
|
|
106
|
+
// If we don't have a session, always recreate
|
|
107
|
+
if (!hasSession) {
|
|
108
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
109
|
+
this.statistics.sessionRecreations++;
|
|
110
|
+
return {
|
|
111
|
+
reason: "we don't have a Signal session with them",
|
|
112
|
+
recreate: true,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
// IMMEDIATE recreation for MAC errors - session is definitely out of sync
|
|
116
|
+
if (errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)) {
|
|
117
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
118
|
+
this.statistics.sessionRecreations++;
|
|
119
|
+
this.logger.warn(
|
|
120
|
+
{ jid: jid, errorCode: RetryReason[errorCode] },
|
|
121
|
+
"MAC error detected, forcing immediate session recreation",
|
|
122
|
+
);
|
|
123
|
+
return {
|
|
124
|
+
reason: `MAC error (code ${errorCode}: ${RetryReason[errorCode]}), immediate session recreation`,
|
|
125
|
+
recreate: true,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
const now = Date.now();
|
|
129
|
+
const prevTime = this.sessionRecreateHistory.get(jid);
|
|
130
|
+
// If no previous recreation or it's been more than an hour
|
|
131
|
+
if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
|
|
132
|
+
this.sessionRecreateHistory.set(jid, now);
|
|
133
|
+
this.statistics.sessionRecreations++;
|
|
134
|
+
return {
|
|
135
|
+
reason: "retry count > 1 and over an hour since last recreation",
|
|
136
|
+
recreate: true,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
return { reason: "", recreate: false };
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Parse error code from retry receipt's retry node.
|
|
143
|
+
* Returns undefined if no error code is present.
|
|
144
|
+
*/ parseRetryErrorCode(errorAttr) {
|
|
145
|
+
if (errorAttr === undefined || errorAttr === "") {
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
const code = parseInt(errorAttr, 10);
|
|
149
|
+
if (Number.isNaN(code)) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
// Validate it's a known RetryReason
|
|
153
|
+
if (
|
|
154
|
+
code >= RetryReason.UnknownError &&
|
|
155
|
+
code <= RetryReason.StatusRevokeDelay
|
|
156
|
+
) {
|
|
157
|
+
return code;
|
|
158
|
+
}
|
|
159
|
+
return RetryReason.UnknownError;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Check if an error code indicates a MAC failure
|
|
163
|
+
*/ isMacError(errorCode) {
|
|
164
|
+
return errorCode !== undefined && MAC_ERROR_CODES.has(errorCode);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Increment retry counter for a message
|
|
168
|
+
*/ incrementRetryCount(messageId) {
|
|
169
|
+
this.retryCounters.set(
|
|
170
|
+
messageId,
|
|
171
|
+
(this.retryCounters.get(messageId) || 0) + 1,
|
|
172
|
+
);
|
|
173
|
+
this.statistics.totalRetries++;
|
|
174
|
+
return this.retryCounters.get(messageId);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Get retry count for a message
|
|
178
|
+
*/ getRetryCount(messageId) {
|
|
179
|
+
return this.retryCounters.get(messageId) || 0;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Check if message has exceeded maximum retry attempts
|
|
183
|
+
*/ hasExceededMaxRetries(messageId) {
|
|
184
|
+
return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Mark retry as successful
|
|
188
|
+
*/ markRetrySuccess(messageId) {
|
|
189
|
+
this.statistics.successfulRetries++;
|
|
190
|
+
// Clean up retry counter for successful message
|
|
191
|
+
this.retryCounters.delete(messageId);
|
|
192
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
193
|
+
this.removeRecentMessage(messageId);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Mark retry as failed
|
|
197
|
+
*/ markRetryFailed(messageId) {
|
|
198
|
+
this.statistics.failedRetries++;
|
|
199
|
+
this.retryCounters.delete(messageId);
|
|
200
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
201
|
+
this.removeRecentMessage(messageId);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Schedule a phone request with delay
|
|
205
|
+
*/ schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
|
|
206
|
+
// Cancel any existing request for this message
|
|
207
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
208
|
+
this.pendingPhoneRequests[messageId] = setTimeout(() => {
|
|
209
|
+
delete this.pendingPhoneRequests[messageId];
|
|
210
|
+
this.statistics.phoneRequests++;
|
|
211
|
+
callback();
|
|
212
|
+
}, delay);
|
|
213
|
+
this.logger.debug(
|
|
214
|
+
`Scheduled phone request for message ${messageId} with ${delay}ms delay`,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Cancel pending phone request
|
|
219
|
+
*/ cancelPendingPhoneRequest(messageId) {
|
|
220
|
+
const timeout = this.pendingPhoneRequests[messageId];
|
|
221
|
+
if (timeout) {
|
|
222
|
+
clearTimeout(timeout);
|
|
223
|
+
delete this.pendingPhoneRequests[messageId];
|
|
224
|
+
this.logger.debug(
|
|
225
|
+
`Cancelled pending phone request for message ${messageId}`,
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
keyToString(key) {
|
|
230
|
+
return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`;
|
|
231
|
+
}
|
|
232
|
+
removeRecentMessage(messageId) {
|
|
233
|
+
const keyStr = this.messageKeyIndex.get(messageId);
|
|
234
|
+
if (!keyStr) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
this.recentMessagesMap.delete(keyStr);
|
|
238
|
+
this.messageKeyIndex.delete(messageId);
|
|
239
|
+
}
|
|
240
|
+
}
|