Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Socket/chats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeCache from '@cacheable/node-cache'

Check failure on line 1 in src/Socket/chats.ts

View workflow job for this annotation

GitHub Actions / check-lint

Run autofix to sort these imports!

Check failure on line 1 in src/Socket/chats.ts

View workflow job for this annotation

GitHub Actions / check-lint

Run autofix to sort these imports!
import { Boom } from '@hapi/boom'
import { proto } from '../../WAProto/index.js'
import { DEFAULT_CACHE_TTLS, PROCESSABLE_HISTORY_TYPES } from '../Defaults'
Expand Down Expand Up @@ -28,7 +28,7 @@
import { ALL_WA_PATCH_NAMES } from '../Types'
import type { QuickReplyAction } from '../Types/Bussines.js'
import type { LabelActionBody } from '../Types/Label'
import { SyncState } from '../Types/State'
import { SyncState } from '../Types'
import {
chatModificationToAppPatch,
type ChatMutationMap,
Expand All @@ -41,7 +41,7 @@
newLTHashState,
processSyncAction
} from '../Utils'
import { makeMutex } from '../Utils/make-mutex'
import { makeMutex , makeKeyedMutex} from '../Utils/make-mutex'

Check failure on line 44 in src/Socket/chats.ts

View workflow job for this annotation

GitHub Actions / check-lint

Replace `·,·makeKeyedMutex` with `,·makeKeyedMutex·`

Check failure on line 44 in src/Socket/chats.ts

View workflow job for this annotation

GitHub Actions / check-lint

Replace `·,·makeKeyedMutex` with `,·makeKeyedMutex·`
import processMessage from '../Utils/process-message'
import {
type BinaryNode,
Expand Down Expand Up @@ -74,16 +74,16 @@
let syncState: SyncState = SyncState.Connecting

/** this mutex ensures that messages are processed in order */
const messageMutex = makeMutex()
const messageMutex = makeKeyedMutex()

/** this mutex ensures that receipts are processed in order */
const receiptMutex = makeMutex()
const receiptMutex = makeKeyedMutex()

/** this mutex ensures that app state patches are processed in order */
const appStatePatchMutex = makeMutex()

/** this mutex ensures that notifications are processed in order */
const notificationMutex = makeMutex()
const notificationMutex = makeKeyedMutex()

// Timeout for AwaitingInitialSync state
let awaitingSyncTimeout: NodeJS.Timeout | undefined
Expand Down Expand Up @@ -575,7 +575,7 @@
// collection is done with sync
collectionsToHandle.delete(name)
}
} catch (error: any) {

Check warning on line 578 in src/Socket/chats.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type

Check warning on line 578 in src/Socket/chats.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type
// if retry attempts overshoot
// or key not found
const isIrrecoverableError =
Expand Down
23 changes: 13 additions & 10 deletions src/Socket/messages-recv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Boom } from '@hapi/boom'
import { randomBytes } from 'crypto'
import Long from 'long'
import { proto } from '../../WAProto/index.js'
import { proto } from '../../WAProto'
import { DEFAULT_CACHE_TTLS, KEY_BUNDLE_TYPE, MIN_PREKEY_COUNT } from '../Defaults'
import type {
GroupParticipant,
Expand Down Expand Up @@ -41,7 +41,7 @@
xmppPreKey,
xmppSignedPreKey
} from '../Utils'
import { makeMutex } from '../Utils/make-mutex'
import { makeKeyedMutex } from '../Utils/make-mutex'
import {
areJidsSameUser,
type BinaryNode,
Expand Down Expand Up @@ -89,7 +89,7 @@
} = sock

/** this mutex ensures that each retryRequest will wait for the previous one to finish */
const retryMutex = makeMutex()
const retryMutex = makeKeyedMutex();

Check failure on line 92 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Delete `;`

Check failure on line 92 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Delete `;`

const msgRetryCache =
config.msgRetryCounterCache ||
Expand Down Expand Up @@ -185,7 +185,7 @@
return
}

let data: any

Check warning on line 188 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type

Check warning on line 188 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type
try {
data = JSON.parse(mexNode.content.toString())
} catch (error) {
Expand Down Expand Up @@ -281,7 +281,7 @@
case 'update':
const settingsNode = getBinaryNodeChild(child, 'settings')
if (settingsNode) {
const update: Record<string, any> = {}

Check warning on line 284 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type

Check warning on line 284 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type
const nameNode = getBinaryNodeChild(settingsNode, 'name')
if (nameNode?.content) update.name = nameNode.content.toString()

Expand Down Expand Up @@ -1045,7 +1045,7 @@
attrs.participant || attrs.from,
isLid ? authState.creds.me?.lid : authState.creds.me?.id
)
const remoteJid = !isNodeFromMe || isJidGroup(attrs.from) ? attrs.from : attrs.recipient
const remoteJid = !isNodeFromMe || isJidGroup(attrs.from) ? attrs.from : attrs.recipient;

Check failure on line 1048 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Delete `;`

Check failure on line 1048 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Delete `;`
const fromMe = !attrs.recipient || ((attrs.type === 'retry' || attrs.type === 'sender') && isNodeFromMe)

const key: proto.IMessageKey = {
Expand All @@ -1069,7 +1069,7 @@

try {
await Promise.all([
receiptMutex.mutex(async () => {
receiptMutex.mutex(remoteJid!,async () => {

Check failure on line 1072 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Insert `·`

Check failure on line 1072 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Insert `·`
const status = getStatusFromReceiptType(attrs.type)
if (
typeof status !== 'undefined' &&
Expand Down Expand Up @@ -1143,7 +1143,7 @@

try {
await Promise.all([
notificationMutex.mutex(async () => {
notificationMutex.mutex(remoteJid!,async () => {

Check failure on line 1146 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Insert `·`

Check failure on line 1146 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Insert `·`
const msg = await processNotification(node)
if (msg) {
const fromMe = areJidsSameUser(node.attrs.participant || remoteJid, authState.creds.me!.id)
Expand Down Expand Up @@ -1208,8 +1208,11 @@
}
}

if (msg.key?.remoteJid && msg.key?.id && messageRetryManager) {
messageRetryManager.addRecentMessage(msg.key.remoteJid, msg.key.id, msg.message!)
const remoteJid = msg.key.remoteJid!;

Check failure on line 1211 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Delete `;`

Check failure on line 1211 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Delete `;`
const msgId = msg.key.id!;

Check failure on line 1212 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Delete `;`

Check failure on line 1212 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Delete `;`

if (messageRetryManager && remoteJid && msgId ) {

Check failure on line 1214 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Delete `·`

Check failure on line 1214 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Delete `·`
messageRetryManager.addRecentMessage(remoteJid, msgId, msg.message!)
logger.debug(
{
jid: msg.key.remoteJid,
Expand All @@ -1220,7 +1223,7 @@
}

try {
await messageMutex.mutex(async () => {
await messageMutex.mutex(remoteJid,async () => {

Check failure on line 1226 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Insert `·`

Check failure on line 1226 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Insert `·`
await decrypt()
// message failed to decrypt
if (msg.messageStubType === proto.WebMessageInfo.StubType.CIPHERTEXT && msg.category !== 'peer') {
Expand All @@ -1237,7 +1240,7 @@
logger.debug(`[handleMessage] Attempting retry request for failed decryption`)

// Handle both pre-key and normal retries in single mutex
await retryMutex.mutex(async () => {
await retryMutex.mutex(msgId,async () => {
try {
if (!ws.isOpen) {
logger.debug({ node }, 'Connection closed, skipping retry')
Expand Down
2 changes: 1 addition & 1 deletion src/Socket/messages-send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,8 @@
const meLid = authState.creds.me?.lid
const meLidUser = meLid ? jidDecode(meLid)?.user : null

const encryptionPromises = (patchedMessages as any).map(

Check warning on line 511 in src/Socket/messages-send.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type

Check warning on line 511 in src/Socket/messages-send.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type
async ({ recipientJid: jid, message: patchedMessage }: any) => {

Check warning on line 512 in src/Socket/messages-send.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type

Check warning on line 512 in src/Socket/messages-send.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type
if (!jid) return null
let msgToEncrypt = patchedMessage
if (dsmMessage) {
Expand Down Expand Up @@ -1098,7 +1098,7 @@
content.url = getUrlFromDirectPath(content.directPath!)

logger.debug({ directPath: media.directPath, key: result.key }, 'media update successful')
} catch (err: any) {

Check warning on line 1101 in src/Socket/messages-send.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type

Check warning on line 1101 in src/Socket/messages-send.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type
error = err
}
}
Expand Down Expand Up @@ -1199,7 +1199,7 @@
})
if (config.emitOwnEvents) {
process.nextTick(async () => {
await messageMutex.mutex(() => upsertMessage(fullMsg, 'append'))
await messageMutex.mutex(jid,() => upsertMessage(fullMsg, 'append'))
})
}

Expand Down
Loading