diff --git a/database.go b/database.go index 014bccd..44bc12b 100644 --- a/database.go +++ b/database.go @@ -125,6 +125,15 @@ func DbSaveGroupWithoutClients(ctx context.Context, group *Group) error { return err } +// DbDeleteGroup deletes given group by id +// return: error if not successful +func DbDeleteGroup(ctx context.Context, group *Group) error { + _, err := dbConn.Exec(ctx, ` + DELETE FROM chat_groups WHERE id = $1 + `, group.Id) + return err +} + // DbSetGroupByIdWithoutClients sets all fields of given struct with database's data using id, populates Clients map with member ids but not their data // return: error if not successful func DbSetGroupByIdWithoutClients(ctx context.Context, group *Group) error { diff --git a/enums.go b/enums.go index 1401a4d..aaf8639 100644 --- a/enums.go +++ b/enums.go @@ -6,10 +6,3 @@ const ( BadMessage WsServerResponse = iota InvalidCredentials ) - -var Colors = map[string][3]uint8{ - "red": {255, 0, 0}, - "green": {0, 255, 0}, - "blue": {0, 0, 255}, - "default": {255, 255, 255}, -} diff --git a/go-socket b/go-socket index 14cb3ed..0dd408e 100755 Binary files a/go-socket and b/go-socket differ diff --git a/http.go b/http.go index 0fe2b1a..12cb662 100644 --- a/http.go +++ b/http.go @@ -187,7 +187,7 @@ func HttpHandleNewToken(response http.ResponseWriter, request *http.Request) { response.Write([]byte(token)) } -func HttpHandeNewGroup(response http.ResponseWriter, request *http.Request) { +func HttpHandeGroupCreate(response http.ResponseWriter, request *http.Request) { if !isMethodAllowed(&response, request) { return } @@ -207,13 +207,6 @@ func HttpHandeNewGroup(response http.ResponseWriter, request *http.Request) { colorString := request.FormValue("color") color, err := ConvertStringToRgb(colorString) - if err != nil { - var ok bool - color, ok = Colors[colorString] - if !ok { - color = Colors["default"] - } - } group := Group{ Name: name, @@ -238,6 +231,26 @@ func HttpHandeNewGroup(response http.ResponseWriter, request *http.Request) { fmt.Fprintf(response, "%d", group.Id) } +func HttpHandleGroupRemove(response http.ResponseWriter, request *http.Request) { + if !isMethodAllowed(&response, request) { + return + } + + ctx := request.Context() + _, group, err := getIfOwnerClientAndGroup(ctx, &response, request) + if err != nil { + return + } + + err = DbDeleteGroup(ctx, group) + if err != nil { + http.Error(response, "internal server error", http.StatusInternalServerError) + return + } + + response.WriteHeader(http.StatusAccepted) +} + func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Request) { if !isMethodAllowed(&response, request) { return @@ -292,7 +305,6 @@ func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Reques } response.WriteHeader(http.StatusAccepted) - response.Write([]byte("ok")) } func HttpHandleGroupRemoveClient(response http.ResponseWriter, request *http.Request) { @@ -410,7 +422,6 @@ func HttpHandleGroupChangeOwner(response http.ResponseWriter, request *http.Requ } response.WriteHeader(http.StatusAccepted) - response.Write([]byte("changed")) } func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) { @@ -448,7 +459,6 @@ func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) { return } response.WriteHeader(http.StatusAccepted) - response.Write([]byte("sent")) } func HttpHandleGroupsGetWithoutMembers(response http.ResponseWriter, request *http.Request) { diff --git a/machine-client/index.html b/machine-client/index.html index 2518623..3686ecb 100644 --- a/machine-client/index.html +++ b/machine-client/index.html @@ -76,6 +76,7 @@ + @@ -176,6 +177,14 @@ ], submit: () => httpPost('/get/group/members', { token:'gm-token', group:'gm-group' }) }, + 'del-group': { + title: 'POST /del/group — delete group (owner only)', + fields: [ + { id: 'dg-token', label: 'token', ph: '' }, + { id: 'dg-groupid', label: 'groupid', ph: 'uint32' }, + ], + submit: () => httpPost('/del/group', { token:'dg-token', groupid:'dg-groupid' }) + }, 'websocket': { title: 'WS /ws — WebSocket connection', renderCustom: () => { diff --git a/main.go b/main.go index 23f1065..d260a83 100644 --- a/main.go +++ b/main.go @@ -19,14 +19,15 @@ func main() { http.HandleFunc("/new/client", withCORS(HttpHandleNewClient)) http.HandleFunc("/new/token", withCORS(HttpHandleNewToken)) - http.HandleFunc("/new/group", withCORS(HttpHandeNewGroup)) + http.HandleFunc("/new/group", withCORS(HttpHandeGroupCreate)) + http.HandleFunc("/new/message", withCORS(HttpHandleNewMessage)) http.HandleFunc("/mod/group/addclients", withCORS(HttpHandleGroupAddClient)) http.HandleFunc("/mod/group/removeclients", withCORS(HttpHandleGroupRemoveClient)) http.HandleFunc("/mod/group/color", withCORS(HttpHandleGroupChangeColor)) http.HandleFunc("/mod/group/owner", withCORS(HttpHandleGroupChangeOwner)) - http.HandleFunc("/new/message", withCORS(HttpHandleNewMessage)) http.HandleFunc("/get/groups", withCORS(HttpHandleGroupsGetWithoutMembers)) http.HandleFunc("/get/group/members", withCORS(HttpHandleGroupMembersGet)) + http.HandleFunc("/del/group", withCORS(HttpHandleGroupRemove)) http.HandleFunc("/ws", ServeWsConnection) log.Println("listening on :8080") diff --git a/todo.txt b/todo.txt index 2fb63eb..a5b7e06 100644 --- a/todo.txt +++ b/todo.txt @@ -1,2 +1,7 @@ -debug gui -chat history \ No newline at end of file +delete client +change client color +make client work with string color names + +chat history + +base new tokens on hash from previous one \ No newline at end of file