add brief for functions in db, implemet full group manipulation (except owner,color), some fixes
This commit is contained in:
@@ -256,38 +256,47 @@ func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
usersToAddString := request.FormValue("users")
|
||||
clientsString := request.FormValue("clients")
|
||||
var remainingUsersCount = int(MaxClientsInGroup) - len(group.Clients)
|
||||
if remainingUsersCount < 1 {
|
||||
http.Error(response, "max users", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
userIdStrings := strings.SplitN(usersToAddString, ",", remainingUsersCount+1)
|
||||
if len(userIdStrings) == 0 {
|
||||
clientsStringSlice := strings.SplitN(clientsString, ",", remainingUsersCount+1)
|
||||
if len(clientsStringSlice) == 0 {
|
||||
http.Error(response, "no users to add", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
clientIds := make([]uint32, 0, len(userIdStrings))
|
||||
for _, s := range userIdStrings {
|
||||
var ids [MaxClientsInGroup]uint32
|
||||
var idx uint32 = 0
|
||||
for _, s := range clientsStringSlice {
|
||||
if idx >= MaxClientsInGroup {
|
||||
break
|
||||
}
|
||||
id, err := ConvertStringUint32(strings.TrimSpace(s))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
clientIds = append(clientIds, id)
|
||||
ids[idx] = id
|
||||
idx++
|
||||
}
|
||||
if len(clientIds) == 0 {
|
||||
if idx == 0 {
|
||||
http.Error(response, "no valid users", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = DbAddClientsToGroup(ctx, group.Id, clientIds)
|
||||
err = DbAddClientsToGroup(ctx, group.Id, &ids)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
for i := uint32(0); i < idx; i++ {
|
||||
group.Clients[ids[i]] = struct{}{}
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
_, err = response.Write([]byte("ok"))
|
||||
if err != nil {
|
||||
@@ -296,6 +305,73 @@ func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Reques
|
||||
}
|
||||
}
|
||||
|
||||
func HttpHandleGroupRemoveClient(response http.ResponseWriter, request *http.Request) {
|
||||
if !isMethodAllowed(&response, request) {
|
||||
return
|
||||
}
|
||||
|
||||
token := request.FormValue("token")
|
||||
clientId, err := TokenValidateGetId(token)
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
affectedGroupId, err := ConvertStringUint32(request.FormValue("groupid"))
|
||||
if err != nil {
|
||||
http.Error(response, "no such group", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := request.Context()
|
||||
|
||||
group, err := getGroup(ctx, affectedGroupId)
|
||||
if err != nil {
|
||||
http.Error(response, "no such group", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
if group.OwnerId != clientId {
|
||||
http.Error(response, "no such group", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
clientsString := request.FormValue("clients")
|
||||
|
||||
clientsStringSlice := strings.SplitN(clientsString, ",", int(MaxClientsInGroup)+1)
|
||||
|
||||
var ids [MaxClientsInGroup]uint32
|
||||
var idx uint32 = 0
|
||||
for _, s := range clientsStringSlice {
|
||||
if idx >= MaxClientsInGroup {
|
||||
break
|
||||
}
|
||||
id, err := ConvertStringUint32(strings.TrimSpace(s))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
ids[idx] = id
|
||||
idx++
|
||||
}
|
||||
if idx == 0 {
|
||||
http.Error(response, "no valid users", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
count, err := DbRemoveClientsFromGroup(ctx, group.Id, &ids)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
for i := uint32(0); i < idx; i++ {
|
||||
delete(group.Clients, ids[i])
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
response.Write([]byte(strconv.Itoa(count)))
|
||||
}
|
||||
|
||||
func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) {
|
||||
if !isMethodAllowed(&response, request) {
|
||||
return
|
||||
@@ -417,6 +493,7 @@ func HttpHandleGroupMembersGet(response http.ResponseWriter, request *http.Reque
|
||||
json, err := json2.Marshal(groupMembers)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
|
||||
Reference in New Issue
Block a user