Skip to content

Commit

Permalink
refactor validate status
Browse files Browse the repository at this point in the history
  • Loading branch information
clairton committed Dec 11, 2024
1 parent aec8ded commit aea7038
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ with:
* 9 - connection lost
* 10 - Invalid token value
* 11 - Http Head test link not return success
* 12 - offline number, connecting....

## Up for development

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "unoapi-cloud",
"version": "1.21.16",
"version": "1.21.17",
"description": "Unoapi Cloud",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
6 changes: 2 additions & 4 deletions src/services/client_baileys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,6 @@ export class ClientBaileys implements Client {
}
const r: Response = { ok }
return r
} else {
logger.error('Response on sent to baileys is empty.....')
throw new SendError(5, 'Wait a moment, connecting process')
}
} else {
throw new Error(`Unknow message type ${type}`)
Expand All @@ -495,7 +492,8 @@ export class ClientBaileys implements Client {
const code = e.code
const title = e.title
await this.onNotification(title, true)
if ([3, '3'].includes(code)) {
if ([3, '3', 12, '12'].includes(code)) {
await this.close()
await this.connect(1)
}
const id = uuid()
Expand Down
53 changes: 35 additions & 18 deletions src/services/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export const connect = async ({
let sock: WASocket | undefined = undefined
const msgRetryCounterCache = new NodeCache()
const { dataStore, state, saveCreds, sessionStore } = store
let connectingTimeout

const status: Status = {
attempt: time,
Expand Down Expand Up @@ -154,19 +155,31 @@ export const connect = async ({
}
}

const verifyConnectingTimeout = async () => {
if (connectingTimeout) {
return
}
logger.info(`Connecting ${phone} set timeout to ${CONNECTING_TIMEOUT_MS} ms`)
if (await sessionStore.isStatusConnecting(phone)) {
connectingTimeout = setTimeout(async () => {
if (await sessionStore.isStatusConnecting(phone)) {
connectingTimeout = null
const message = `Connecting ${phone} timed out ${CONNECTING_TIMEOUT_MS} ms, change to disconnect`
await onNotification(message, false)
logger.warn(message)
await onDisconnected(phone, {})
}
}, CONNECTING_TIMEOUT_MS)
} else {
connectingTimeout = null
}
}

const onConnecting = async () => {
await sessionStore.setStatus(phone, 'connecting')
await onNotification(`Connnecting...`, false)
logger.info(`Connecting ${phone} set timeout to ${CONNECTING_TIMEOUT_MS} ms`)
setTimeout(async () => {
if (await sessionStore.isStatusConnecting(phone)) {
const message = `Connecting ${phone} timed out ${CONNECTING_TIMEOUT_MS} ms, change to disconnect`
await onNotification(message, false)
logger.info(message)
await onDisconnected(phone, {})
await close()
}
}, CONNECTING_TIMEOUT_MS)
return verifyConnectingTimeout()
}

const onOpen = async () => {
Expand Down Expand Up @@ -314,15 +327,22 @@ export const connect = async ({

const exists: exists = async (phone: string) => {
await validateStatus()
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

return dataStore.loadJid(phone, sock!)
}

const validateStatus = async () => {
if (await sessionStore.isStatusConnecting(phone)) {
await verifyConnectingTimeout()
throw new SendError(5, 'Wait a moment, connecting process')
} else if (!(await sessionStore.isStatusOnline(phone))) {
} else if (await sessionStore.isStatusIsDisconnect(phone) || !sock) {
throw new SendError(3, 'Disconnected number, please read qr code')
} else if (await sessionStore.isStatusOffline(phone)) {
throw new SendError(12, 'offline number, connecting....')
}
if (connectingTimeout) {
clearTimeout(connectingTimeout)
connectingTimeout = null
}
}

Expand All @@ -331,12 +351,7 @@ export const connect = async ({
message: AnyMessageContent,
options: { composing: boolean; quoted: boolean | undefined } = { composing: false, quoted: undefined },
) => {
if (!(await sessionStore.isStatusOnline(phone))) {
if (!(await sessionStore.isStatusConnecting(phone))) {
reconnect()
}
return
}
await validateStatus()

const id = isJidGroup(to) ? to : await exists(to)
if (id) {
Expand Down Expand Up @@ -364,7 +379,7 @@ export const connect = async ({
}

const read: readMessages = async (keys: WAMessageKey[]) => {
if (!(await sessionStore.isStatusOnline(phone))) return false
await validateStatus()

await sock?.readMessages(keys)
return true
Expand All @@ -376,6 +391,8 @@ export const connect = async ({
}

const rejectCall: rejectCall = async (callId: string, callFrom: string) => {
await validateStatus()

return sock?.rejectCall(callId, callFrom)
}

Expand Down

0 comments on commit aea7038

Please sign in to comment.