From 8ed2527a8879e01f4f3f741feebaf86fee2bf18d Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Wed, 27 Nov 2024 07:47:30 +0200 Subject: Started working on refactoring packet types and some changes to the SQL schema --- internal/client/api/api.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'internal/client') diff --git a/internal/client/api/api.go b/internal/client/api/api.go index d91de46..cfc4ce9 100644 --- a/internal/client/api/api.go +++ b/internal/client/api/api.go @@ -92,3 +92,39 @@ func GetUserById(id snowflake.ID) tea.Cmd { } } +func CreateServer( + name string, + icon string, + bgHexColor string, + fgHexColor string, + isPublic bool, +) tea.Cmd { + return func() tea.Msg { + requestName := "CreateServer" + log.Println("request", requestName, "sent") + request := packet.CreateNetwork{ + Name: name, + Icon: icon, + BgHexColor: bgHexColor, + FgHexColor: fgHexColor, + IsPublic: isPublic, + } + response, ok := <-gateway.Send(&request) + if !ok { + return fmt.Errorf("request %s timeout", requestName) + } + log.Println("request", requestName, "received response") + + switch response := response.(type) { + case *packet.ErrorMessage: + return errors.New(response.Error) + case *packet.Users: + if len(response.Users) == 0 { + return fmt.Errorf("requested user id %v not found", id) + } + assert.Assert(len(response.Users) == 1, "server must return only one user with the matching id") + return UserProfileUpdate(response.Users[0]) + } + return fmt.Errorf("received invalid response from server: %v", response.Type()) + } +} -- cgit v1.3.1