Add saving user, add nonpersistant register login handling

This commit is contained in:
2026-03-17 21:40:48 +01:00
parent e496cb0017
commit d3fc2a65d9
9 changed files with 160 additions and 25 deletions
+45 -10
View File
@@ -1,8 +1,11 @@
package main
import (
"context"
"errors"
"sync"
"golang.org/x/crypto/bcrypt"
)
var (
@@ -11,32 +14,48 @@ var (
chatGroups = make(map[uint32]ChatGroup)
)
func AddGroupToCache(chatGroup *ChatGroup) {
func CreateGroup(ctx context.Context, chatGroup *ChatGroup) {
mu.Lock()
defer mu.Unlock()
chatGroups[chatGroup.Id] = *chatGroup
}
func RemoveGroupFromCache(chatGroup *ChatGroup) {
func DeleteGroup(ctx context.Context, chatGroup *ChatGroup) {
mu.Lock()
defer mu.Unlock()
delete(chatGroups, chatGroup.Id)
}
func AddClientConnectionsToCache(client *Client) {
func CreateClient(ctx context.Context, client *Client) error {
mu.Lock()
defer mu.Unlock()
for _, groupIn := range client.Groups {
chatGroups[groupIn.Id].Members[client.Id] = client
err := SaveClientWithoutGroups(ctx, client)
if err != nil {
return err
}
return nil
}
func RemoveClientConnectionsToCache(client *Client) {
mu.Lock()
defer mu.Unlock()
for _, groupIn := range client.Groups {
delete(chatGroups[groupIn.Id].Members, client.Id)
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) {
@@ -46,3 +65,19 @@ func GetClientFromId(id uint32) (*Client, error) {
}
return client, nil
}
func ConnectClientToGroups(ctx context.Context, client *Client) {
mu.Lock()
defer mu.Unlock()
for _, groupIn := range client.Groups {
chatGroups[groupIn.Id].Members[client.Id] = client
}
}
func DisconnectClientFromGroups(ctx context.Context, client *Client) {
mu.Lock()
defer mu.Unlock()
for _, groupIn := range client.Groups {
delete(chatGroups[groupIn.Id].Members, client.Id)
}
}