Files
wuzapi/clients.go
Felipe Aquino 8b80b70471
Some checks failed
Build and Test / Build Go Application (push) Has been cancelled
Publish Docker image / build-and-push (push) Has been cancelled
Update Contributors / update-contributors (push) Has been cancelled
upload
2026-03-04 10:54:04 -03:00

87 lines
2.0 KiB
Go

package main
import (
"sync"
"github.com/go-resty/resty/v2"
"go.mau.fi/whatsmeow"
)
type ClientManager struct {
sync.RWMutex
whatsmeowClients map[string]*whatsmeow.Client
httpClients map[string]*resty.Client
myClients map[string]*MyClient
}
func NewClientManager() *ClientManager {
return &ClientManager{
whatsmeowClients: make(map[string]*whatsmeow.Client),
httpClients: make(map[string]*resty.Client),
myClients: make(map[string]*MyClient),
}
}
func (cm *ClientManager) SetWhatsmeowClient(userID string, client *whatsmeow.Client) {
cm.Lock()
defer cm.Unlock()
cm.whatsmeowClients[userID] = client
}
func (cm *ClientManager) GetWhatsmeowClient(userID string) *whatsmeow.Client {
cm.RLock()
defer cm.RUnlock()
return cm.whatsmeowClients[userID]
}
func (cm *ClientManager) DeleteWhatsmeowClient(userID string) {
cm.Lock()
defer cm.Unlock()
delete(cm.whatsmeowClients, userID)
}
func (cm *ClientManager) SetHTTPClient(userID string, client *resty.Client) {
cm.Lock()
defer cm.Unlock()
cm.httpClients[userID] = client
}
func (cm *ClientManager) GetHTTPClient(userID string) *resty.Client {
cm.RLock()
defer cm.RUnlock()
return cm.httpClients[userID]
}
func (cm *ClientManager) DeleteHTTPClient(userID string) {
cm.Lock()
defer cm.Unlock()
delete(cm.httpClients, userID)
}
func (cm *ClientManager) SetMyClient(userID string, client *MyClient) {
cm.Lock()
defer cm.Unlock()
cm.myClients[userID] = client
}
func (cm *ClientManager) GetMyClient(userID string) *MyClient {
cm.RLock()
defer cm.RUnlock()
return cm.myClients[userID]
}
func (cm *ClientManager) DeleteMyClient(userID string) {
cm.Lock()
defer cm.Unlock()
delete(cm.myClients, userID)
}
// UpdateMyClientSubscriptions updates the event subscriptions of a client without reconnecting
func (cm *ClientManager) UpdateMyClientSubscriptions(userID string, subscriptions []string) {
cm.Lock()
defer cm.Unlock()
if client, exists := cm.myClients[userID]; exists {
client.subscriptions = subscriptions
}
}