Skip to content

Commit 7e06710

Browse files
committed
sync session status on connect
1 parent a803cc7 commit 7e06710

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "unoapi-cloud",
3-
"version": "1.28.4",
3+
"version": "1.28.5",
44
"description": "Unoapi Cloud",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/services/redis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ const redisSetAndExpire = async function (key: string, value: any, ttl: number)
131131
}
132132
}
133133

134-
const authKey = (phone: string) => {
134+
export const authKey = (phone: string) => {
135135
return `${BASE_KEY}auth:${phone}`
136136
}
137137

src/services/session_store.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ export abstract class SessionStore {
6565
}
6666

6767
async syncConnections() {}
68+
69+
async syncConnection(_phone: string) {}
6870
}

src/services/session_store_redis.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SessionStore, sessionStatus } from './session_store'
2-
import { configKey, redisKeys, getSessionStatus, setSessionStatus, sessionStatusKey, redisGet, getConfig, getConnectCount, setConnectCount, delAuth } from './redis'
2+
import { configKey, authKey, redisKeys, getSessionStatus, setSessionStatus, sessionStatusKey, redisGet, getConfig, getConnectCount, setConnectCount, delAuth } from './redis'
33
import logger from './logger'
44
import { MAX_CONNECT_RETRY } from '../defaults'
55

@@ -49,19 +49,26 @@ export class SessionStoreRedis extends SessionStore {
4949
for (let i = 0; i < keys.length; i++) {
5050
const key = keys[i];
5151
const phone = key.replace(toReplaceStatus, '')
52-
if (await redisGet(key) == 'connecting' || !await getConfig(phone) ) {
53-
logger.info(`Sync ${phone} lost connecting!`)
54-
await this.setStatus(phone, 'disconnected')
55-
await delAuth(`${phone}:creds`)
56-
}
57-
if (await redisGet(key) == 'blocked' && await this.getConnectCount(phone) < MAX_CONNECT_RETRY) {
58-
logger.info(`Sync ${phone} blocked!`)
59-
await this.setStatus(phone, 'offline')
60-
}
52+
await this.syncConnection(phone)
6153
}
6254
} catch (error) {
6355
logger.error(error, 'Error on sync lost connecting')
6456
throw error
6557
}
6658
}
59+
60+
async syncConnection(phone: string) {
61+
const aKey = authKey(`${phone}*`)
62+
const keys = await redisKeys(aKey)
63+
if (keys.length == 1 && keys[0] == authKey(`${phone}:creds`)) {
64+
logger.info(`Sync ${phone} lost connecting!`)
65+
await delAuth(phone)
66+
await this.setStatus(phone, 'disconnected')
67+
}
68+
const key = sessionStatusKey(phone)
69+
if (await redisGet(key) == 'blocked' && await this.getConnectCount(phone) < MAX_CONNECT_RETRY) {
70+
logger.info(`Sync ${phone} blocked!`)
71+
await this.setStatus(phone, 'offline')
72+
}
73+
}
6774
}

src/services/socket.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export const connect = async ({
142142

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

439439
const connect = async () => {
440+
await sessionStore.syncConnection(phone)
440441
if (await sessionStore.isStatusConnecting(phone)) {
441442
logger.warn('Already Connecting %s', phone)
442443
return
@@ -504,7 +505,7 @@ export const connect = async ({
504505
if (sock) {
505506
dataStore.bind(sock.ev)
506507
event('creds.update', saveCreds)
507-
event('connection.update', onConnectionUpdate)
508+
logger.info('Connection type %s already creds %s', config.connectionType, sock?.authState?.creds?.registered)
508509
if (config.connectionType == 'pairing_code' && !sock?.authState?.creds?.registered) {
509510
logger.info(`Requesting pairing code ${phone}`)
510511
try {
@@ -514,10 +515,13 @@ export const connect = async ({
514515
const beatyCode = `${code?.match(/.{1,4}/g)?.join('-')}`
515516
const message = t('pairing_code', beatyCode)
516517
await onNotification(message, true)
518+
event('connection.update', onConnectionUpdate)
517519
} catch (error) {
518520
console.error(error)
519521
throw error
520522
}
523+
} else {
524+
event('connection.update', onConnectionUpdate)
521525
}
522526
if (config.wavoipToken) {
523527
useVoiceCallsBaileys(config.wavoipToken, sock as any, 'close', true)

0 commit comments

Comments
 (0)