add helper functions to http, add group handlers, add json group
This commit is contained in:
@@ -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,17 +245,10 @@ 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
|
if err != nil {
|
||||||
groupPtr, err := CacheGetGroup(affectedGroupId)
|
http.Error(response, "no such group", http.StatusUnauthorized)
|
||||||
if err == nil {
|
return
|
||||||
group = *groupPtr
|
|
||||||
} else {
|
|
||||||
err = DbSetGroupByIdWithoutClients(ctx, &group)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(response, "no such group", http.StatusUnauthorized)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if group.OwnerId != clientId {
|
if group.OwnerId != clientId {
|
||||||
@@ -328,46 +344,81 @@ func HttpHandleGroupsGeWithoutMembers(response http.ResponseWriter, request *htt
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token := request.FormValue("token")
|
ctx := request.Context()
|
||||||
if token == "" {
|
|
||||||
|
client, err := getClient(ctx, request.FormValue("token"))
|
||||||
|
if err != nil {
|
||||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||||
return
|
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 {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := request.Context()
|
ctx := request.Context()
|
||||||
|
client, err := getClient(ctx, request.FormValue("token"))
|
||||||
client, err := CacheGetClientById(clientId)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
client = &Client{Id: clientId}
|
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||||
err = DbSetClientById(ctx, client)
|
return
|
||||||
if err != nil {
|
|
||||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
CacheSaveClient(client)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var groups [MaxGroupsForClient]*Group
|
groupStr := request.FormValue("group")
|
||||||
var i uint32
|
groupId, err := ConvertStringUint32(groupStr)
|
||||||
|
if err != nil {
|
||||||
for groupId := range client.Groups {
|
http.Error(response, "invalid group", http.StatusBadRequest)
|
||||||
group, err := CacheGetGroup(groupId)
|
return
|
||||||
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++
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, 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
@@ -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"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user