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
+110 -59
View File
@@ -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)
}