Skip to content

Commit

Permalink
sync session status on connect
Browse files Browse the repository at this point in the history
  • Loading branch information
clairton committed Feb 18, 2025
1 parent a803cc7 commit 7e06710
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
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.28.4",
"version": "1.28.5",
"description": "Unoapi Cloud",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/services/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const redisSetAndExpire = async function (key: string, value: any, ttl: number)
}
}

const authKey = (phone: string) => {
export const authKey = (phone: string) => {
return `${BASE_KEY}auth:${phone}`
}

Expand Down
2 changes: 2 additions & 0 deletions src/services/session_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ export abstract class SessionStore {
}

async syncConnections() {}

async syncConnection(_phone: string) {}
}
27 changes: 17 additions & 10 deletions src/services/session_store_redis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SessionStore, sessionStatus } from './session_store'
import { configKey, redisKeys, getSessionStatus, setSessionStatus, sessionStatusKey, redisGet, getConfig, getConnectCount, setConnectCount, delAuth } from './redis'
import { configKey, authKey, redisKeys, getSessionStatus, setSessionStatus, sessionStatusKey, redisGet, getConfig, getConnectCount, setConnectCount, delAuth } from './redis'
import logger from './logger'
import { MAX_CONNECT_RETRY } from '../defaults'

Expand Down Expand Up @@ -49,19 +49,26 @@ export class SessionStoreRedis extends SessionStore {
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const phone = key.replace(toReplaceStatus, '')
if (await redisGet(key) == 'connecting' || !await getConfig(phone) ) {
logger.info(`Sync ${phone} lost connecting!`)
await this.setStatus(phone, 'disconnected')
await delAuth(`${phone}:creds`)
}
if (await redisGet(key) == 'blocked' && await this.getConnectCount(phone) < MAX_CONNECT_RETRY) {
logger.info(`Sync ${phone} blocked!`)
await this.setStatus(phone, 'offline')
}
await this.syncConnection(phone)
}
} catch (error) {
logger.error(error, 'Error on sync lost connecting')
throw error
}
}

async syncConnection(phone: string) {
const aKey = authKey(`${phone}*`)
const keys = await redisKeys(aKey)
if (keys.length == 1 && keys[0] == authKey(`${phone}:creds`)) {
logger.info(`Sync ${phone} lost connecting!`)
await delAuth(phone)
await this.setStatus(phone, 'disconnected')
}
const key = sessionStatusKey(phone)
if (await redisGet(key) == 'blocked' && await this.getConnectCount(phone) < MAX_CONNECT_RETRY) {
logger.info(`Sync ${phone} blocked!`)
await this.setStatus(phone, 'offline')
}
}
}
8 changes: 6 additions & 2 deletions src/services/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export const connect = async ({

const onConnectionUpdate = async (event: Partial<ConnectionState>) => {
logger.debug('onConnectionUpdate ==> %s %s', phone, JSON.stringify(event))
if (event.qr) {
if (event.qr && config.connectionType == 'qrcode') {
if (status.attempt > attempts) {
const message = t('attempts_exceeded', attempts)
logger.debug(message)
Expand Down Expand Up @@ -437,6 +437,7 @@ export const connect = async ({
}

const connect = async () => {
await sessionStore.syncConnection(phone)
if (await sessionStore.isStatusConnecting(phone)) {
logger.warn('Already Connecting %s', phone)
return
Expand Down Expand Up @@ -504,7 +505,7 @@ export const connect = async ({
if (sock) {
dataStore.bind(sock.ev)
event('creds.update', saveCreds)
event('connection.update', onConnectionUpdate)
logger.info('Connection type %s already creds %s', config.connectionType, sock?.authState?.creds?.registered)
if (config.connectionType == 'pairing_code' && !sock?.authState?.creds?.registered) {
logger.info(`Requesting pairing code ${phone}`)
try {
Expand All @@ -514,10 +515,13 @@ export const connect = async ({
const beatyCode = `${code?.match(/.{1,4}/g)?.join('-')}`
const message = t('pairing_code', beatyCode)
await onNotification(message, true)
event('connection.update', onConnectionUpdate)
} catch (error) {
console.error(error)
throw error
}
} else {
event('connection.update', onConnectionUpdate)
}
if (config.wavoipToken) {
useVoiceCallsBaileys(config.wavoipToken, sock as any, 'close', true)
Expand Down

0 comments on commit 7e06710

Please sign in to comment.