This commit is contained in:
GitProtogen
2026-03-17 19:06:56 +01:00
parent 912136e2bc
commit 84c9fa2b60
2 changed files with 14 additions and 16 deletions
+13 -6
View File
@@ -1,10 +1,13 @@
package main
import "sync"
import (
"errors"
"sync"
)
var (
mu sync.RWMutex
Clients = make(map[uint32]Client)
clients = make(map[uint32]Client)
ChatGroups = make(map[uint32]ChatGroup)
ClientsMap = make(map[uint32]map[uint32]*Client)
)
@@ -23,7 +26,7 @@ func RemoveGroupFromCache(chatGroup *ChatGroup) {
delete(ClientsMap, chatGroup.Id)
}
func AddAuthenticatedClientToCache(client *Client) {
func AddClientConnectionsToCache(client *Client) {
mu.Lock()
defer mu.Unlock()
for _, groupIn := range client.Groups {
@@ -33,7 +36,7 @@ func AddAuthenticatedClientToCache(client *Client) {
}
}
func RemoveClientFromCache(client *Client) {
func RemoveClientConnectionsToCache(client *Client) {
mu.Lock()
defer mu.Unlock()
for _, groupIn := range client.Groups {
@@ -43,6 +46,10 @@ func RemoveClientFromCache(client *Client) {
}
}
func GetClientData(uint32 *Client) {
func GetClientFromId(id uint32) (*Client, error) {
client, ok := &clients[id]
if !ok {
return nil, errors.New("No such user")
}
return client, nil
}
+1 -10
View File
@@ -20,7 +20,7 @@ func GetToken(client *Client) (string, error) {
return token.SignedString(secretKey)
}
func GetDataFromToken(token *string) (uint32, error) {
func GetClientIdFromToken(token *string) (uint32, error) {
parsed, err := jwt.ParseWithClaims(*token, &jwt.RegisteredClaims{}, func(t *jwt.Token) (any, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, jwt.ErrSignatureInvalid
@@ -42,12 +42,3 @@ func GetDataFromToken(token *string) (uint32, error) {
}
return uint32(id), nil
}
func SetClientFromToken(client *Client, token string) error {
id, err := GetDataFromToken(&token)
if err != nil {
return err
}
client.Id = id
return nil
}