diff --git a/cache.go b/cache.go index 1a3efdc..99d4d68 100644 --- a/cache.go +++ b/cache.go @@ -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 } diff --git a/tokens.go b/tokens.go index ac134bd..1690e49 100644 --- a/tokens.go +++ b/tokens.go @@ -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 -}