From 055bc5dcf29cff6847de3106b70f484731c37564 Mon Sep 17 00:00:00 2001 From: gitGnome Date: Wed, 1 Apr 2026 09:40:05 +0200 Subject: [PATCH] add helper functions to http, add group handlers, add json group --- http.go | 169 ++++++++++++++++++++++++++++++++++------------------- structs.go | 14 ++--- 2 files changed, 117 insertions(+), 66 deletions(-) diff --git a/http.go b/http.go index 619f463..7c40044 100644 --- a/http.go +++ b/http.go @@ -1,9 +1,13 @@ package main import ( + "context" "encoding/binary" + json2 "encoding/json" "fmt" + "maps" "net/http" + "slices" "strconv" "strings" "time" @@ -140,8 +144,9 @@ func HttpHandeNewGroup(response http.ResponseWriter, request *http.Request) { return } - token := request.FormValue("token") - clientId, err := TokenValidateGetId(token) + ctx := request.Context() + + client, err := getClient(ctx, request.FormValue("token")) if err != nil { http.Error(response, "invalid token", http.StatusUnauthorized) return @@ -162,27 +167,13 @@ func HttpHandeNewGroup(response http.ResponseWriter, request *http.Request) { } } - ctx := request.Context() - - client := Client{Id: clientId} - cacheClient, err := CacheGetClientById(clientId) - if err == nil { - client = *cacheClient - } else { - err = DbSetClientByIdWithoutGroups(ctx, &client) - if err != nil { - http.Error(response, "internal server error", http.StatusInternalServerError) - return - } - } - group := Group{ Name: name, CreatedAt: time.Now(), - OwnerId: clientId, - CreatorId: clientId, + OwnerId: client.Id, + CreatorId: client.Id, Color: color, - Clients: map[uint32]struct{}{clientId: {}}, + Clients: map[uint32]struct{}{client.Id: {}}, } enableClientColors := request.FormValue("enableClientColors") @@ -202,6 +193,38 @@ func HttpHandeNewGroup(response http.ResponseWriter, request *http.Request) { response.Write(groupIdBytes) } +func getClient(ctx context.Context, token string) (*Client, error) { + clientId, err := TokenValidateGetId(token) + if err != nil { + return nil, err + } + + client, err := CacheGetClientById(clientId) + if err != nil { + client = &Client{Id: clientId} + err = DbSetClientById(ctx, client) + if err != nil { + return nil, err + } + CacheSaveClient(client) + } + + return client, nil +} + +func getGroup(ctx context.Context, groupId uint32) (*Group, error) { + group, err := CacheGetGroup(groupId) + if err != nil { + group = &Group{Id: groupId} + err = DbSetGroupById(ctx, group) + if err != nil { + return nil, err + } + CacheSaveGroup(group) + } + return group, nil +} + func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Request) { if !isMethodAllowed(&response, request) { return @@ -222,17 +245,10 @@ func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Reques ctx := request.Context() - var group Group - group.Id = affectedGroupId - groupPtr, err := CacheGetGroup(affectedGroupId) - if err == nil { - group = *groupPtr - } else { - err = DbSetGroupByIdWithoutClients(ctx, &group) - if err != nil { - http.Error(response, "no such group", http.StatusUnauthorized) - return - } + group, err := getGroup(ctx, affectedGroupId) + if err != nil { + http.Error(response, "no such group", http.StatusUnauthorized) + return } if group.OwnerId != clientId { @@ -328,46 +344,81 @@ func HttpHandleGroupsGeWithoutMembers(response http.ResponseWriter, request *htt return } - token := request.FormValue("token") - if token == "" { + ctx := request.Context() + + client, err := getClient(ctx, request.FormValue("token")) + if err != nil { http.Error(response, "invalid token", http.StatusUnauthorized) return } - clientId, err := TokenValidateGetId(token) + groups := make([]GroupNoMembers, 0, len(client.Groups)) + + for groupId := range client.Groups { + group, err := getGroup(ctx, groupId) + if err != nil { + continue + } + + groups = append(groups, GroupNoMembers{ + Id: groupId, + Name: group.Name, + CreatedAt: group.CreatedAt, + CreatorId: group.CreatorId, + OwnerId: group.OwnerId, + Color: group.Color, + EnableClientsColors: group.EnableClientColors, + }) + } + + json, err := json2.Marshal(groups) if err != nil { - http.Error(response, "invalid token", http.StatusUnauthorized) + http.Error(response, "internal server error", http.StatusInternalServerError) + return + } + + response.WriteHeader(http.StatusAccepted) + response.Write(json) +} + +func HttpHandleGroupMembersGet(response http.ResponseWriter, request *http.Request) { + if !isMethodAllowed(&response, request) { return } ctx := request.Context() - - client, err := CacheGetClientById(clientId) + client, err := getClient(ctx, request.FormValue("token")) if err != nil { - client = &Client{Id: clientId} - err = DbSetClientById(ctx, client) - if err != nil { - http.Error(response, "invalid token", http.StatusUnauthorized) - return - } - CacheSaveClient(client) + http.Error(response, "invalid token", http.StatusUnauthorized) + return } - var groups [MaxGroupsForClient]*Group - var i uint32 - - for groupId := range client.Groups { - group, err := CacheGetGroup(groupId) - if err != nil { - group = &Group{Id: groupId} - err = DbSetClientByIdWithoutGroups(ctx, client) - if err != nil { - http.Error(response, "internal server error", http.StatusInternalServerError) - return - } - groups[i] = group - i++ - } - + groupStr := request.FormValue("group") + groupId, err := ConvertStringUint32(groupStr) + if err != nil { + http.Error(response, "invalid group", http.StatusBadRequest) + return } + + _, ok := client.Groups[groupId] + if !ok { + http.Error(response, "no such group", http.StatusUnauthorized) + return + } + + group, err := getGroup(ctx, groupId) + if err != nil { + http.Error(response, "no such group", http.StatusUnauthorized) + return + } + + groupMembers := slices.Collect(maps.Keys(group.Clients)) + + json, err := json2.Marshal(groupMembers) + if err != nil { + http.Error(response, "internal server error", http.StatusInternalServerError) + } + + response.WriteHeader(http.StatusAccepted) + response.Write(json) } diff --git a/structs.go b/structs.go index 8bde1ca..b792c4e 100644 --- a/structs.go +++ b/structs.go @@ -29,11 +29,11 @@ type Group struct { } type GroupNoMembers struct { - Name string - CreatedAt time.Time - Id uint32 - CreatorId uint32 - OwnerId uint32 - Color [3]uint8 - EnableClientsColors bool + Name string `json:"name"` + CreatedAt time.Time `json:"createdAt"` + Id uint32 `json:"id"` + CreatorId uint32 `json:"creatorId"` + OwnerId uint32 `json:"ownerId"` + Color [3]uint8 `json:"color"` + EnableClientsColors bool `json:"enableClientsColors"` }