new start
This commit is contained in:
@@ -1,92 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
var (
|
||||
mu sync.RWMutex
|
||||
clients = make(map[uint32]*Client)
|
||||
chatGroups = make(map[uint32]ChatGroup)
|
||||
CacheClients = make(map[uint32]*Client)
|
||||
mu sync.RWMutex
|
||||
Groups = make(map[uint32]*Group)
|
||||
)
|
||||
|
||||
func CreateGroup(ctx context.Context, chatGroup *ChatGroup) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
chatGroups[chatGroup.Id] = *chatGroup
|
||||
}
|
||||
func CacheGetClientById(id uint32) (*Client, error) {
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
|
||||
func DeleteGroup(ctx context.Context, chatGroup *ChatGroup) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
delete(chatGroups, chatGroup.Id)
|
||||
}
|
||||
|
||||
func CreateClient(ctx context.Context, client *Client) error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
clients[client.Id] = client
|
||||
|
||||
hashed, err := bcrypt.GenerateFromPassword([]byte(client.PasswordHash), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client.PasswordHash = string(hashed)
|
||||
|
||||
//err := DbSaveClientWithoutGroups(ctx, client)
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CheckPassword(ctx context.Context, id uint32, password string) error {
|
||||
client, err := GetClientFromId(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = bcrypt.CompareHashAndPassword([]byte(client.PasswordHash), []byte(password))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetIdFromClientName(ctx context.Context, name string) (uint32, error) {
|
||||
for _, client := range clients {
|
||||
if client.Name == name {
|
||||
return client.Id, nil
|
||||
}
|
||||
}
|
||||
return 0, errors.New("client not found")
|
||||
}
|
||||
|
||||
func GetClientFromId(id uint32) (*Client, error) {
|
||||
client, ok := clients[id]
|
||||
client, ok := CacheClients[id]
|
||||
if !ok {
|
||||
return nil, errors.New("no such user")
|
||||
return nil, fmt.Errorf("client %d not found", id)
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func ConnectClientToGroups(ctx context.Context, client *Client) {
|
||||
func CacheSetClient(id uint32, client *Client) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
for _, groupIn := range client.Groups {
|
||||
chatGroups[groupIn.Id].Members[client.Id] = client
|
||||
}
|
||||
|
||||
CacheClients[id] = client
|
||||
}
|
||||
|
||||
func DisconnectClientFromGroups(ctx context.Context, client *Client) {
|
||||
func CacheDeleteClient(id uint32) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
for _, groupIn := range client.Groups {
|
||||
delete(chatGroups[groupIn.Id].Members, client.Id)
|
||||
}
|
||||
|
||||
delete(CacheClients, id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user