add helper functions to http, add group handlers, add json group

This commit is contained in:
gitGnome
2026-04-01 09:40:05 +02:00
parent caab2e22bc
commit 055bc5dcf2
2 changed files with 117 additions and 66 deletions
+103 -52
View File
@@ -1,9 +1,13 @@
package main package main
import ( import (
"context"
"encoding/binary" "encoding/binary"
json2 "encoding/json"
"fmt" "fmt"
"maps"
"net/http" "net/http"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -140,8 +144,9 @@ func HttpHandeNewGroup(response http.ResponseWriter, request *http.Request) {
return return
} }
token := request.FormValue("token") ctx := request.Context()
clientId, err := TokenValidateGetId(token)
client, err := getClient(ctx, request.FormValue("token"))
if err != nil { if err != nil {
http.Error(response, "invalid token", http.StatusUnauthorized) http.Error(response, "invalid token", http.StatusUnauthorized)
return 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{ group := Group{
Name: name, Name: name,
CreatedAt: time.Now(), CreatedAt: time.Now(),
OwnerId: clientId, OwnerId: client.Id,
CreatorId: clientId, CreatorId: client.Id,
Color: color, Color: color,
Clients: map[uint32]struct{}{clientId: {}}, Clients: map[uint32]struct{}{client.Id: {}},
} }
enableClientColors := request.FormValue("enableClientColors") enableClientColors := request.FormValue("enableClientColors")
@@ -202,6 +193,38 @@ func HttpHandeNewGroup(response http.ResponseWriter, request *http.Request) {
response.Write(groupIdBytes) 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) { func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Request) {
if !isMethodAllowed(&response, request) { if !isMethodAllowed(&response, request) {
return return
@@ -222,18 +245,11 @@ func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Reques
ctx := request.Context() ctx := request.Context()
var group Group group, err := getGroup(ctx, affectedGroupId)
group.Id = affectedGroupId
groupPtr, err := CacheGetGroup(affectedGroupId)
if err == nil {
group = *groupPtr
} else {
err = DbSetGroupByIdWithoutClients(ctx, &group)
if err != nil { if err != nil {
http.Error(response, "no such group", http.StatusUnauthorized) http.Error(response, "no such group", http.StatusUnauthorized)
return return
} }
}
if group.OwnerId != clientId { if group.OwnerId != clientId {
http.Error(response, "no such group", http.StatusUnauthorized) http.Error(response, "no such group", http.StatusUnauthorized)
@@ -328,46 +344,81 @@ func HttpHandleGroupsGeWithoutMembers(response http.ResponseWriter, request *htt
return return
} }
token := request.FormValue("token")
if token == "" {
http.Error(response, "invalid token", http.StatusUnauthorized)
return
}
clientId, err := TokenValidateGetId(token)
if err != nil {
http.Error(response, "invalid token", http.StatusUnauthorized)
return
}
ctx := request.Context() 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 { if err != nil {
http.Error(response, "invalid token", http.StatusUnauthorized) http.Error(response, "invalid token", http.StatusUnauthorized)
return return
} }
CacheSaveClient(client)
}
var groups [MaxGroupsForClient]*Group groups := make([]GroupNoMembers, 0, len(client.Groups))
var i uint32
for groupId := range client.Groups { for groupId := range client.Groups {
group, err := CacheGetGroup(groupId) group, err := getGroup(ctx, groupId)
if err != nil { if err != nil {
group = &Group{Id: groupId} continue
err = DbSetClientByIdWithoutGroups(ctx, client) }
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 { if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError) http.Error(response, "internal server error", http.StatusInternalServerError)
return return
} }
groups[i] = group
i++ 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 := getClient(ctx, request.FormValue("token"))
if err != nil {
http.Error(response, "invalid token", http.StatusUnauthorized)
return
}
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)
} }
+7 -7
View File
@@ -29,11 +29,11 @@ type Group struct {
} }
type GroupNoMembers struct { type GroupNoMembers struct {
Name string Name string `json:"name"`
CreatedAt time.Time CreatedAt time.Time `json:"createdAt"`
Id uint32 Id uint32 `json:"id"`
CreatorId uint32 CreatorId uint32 `json:"creatorId"`
OwnerId uint32 OwnerId uint32 `json:"ownerId"`
Color [3]uint8 Color [3]uint8 `json:"color"`
EnableClientsColors bool EnableClientsColors bool `json:"enableClientsColors"`
} }