From dc094ceb03734a9d1a98028784ef8d3d25cb0ef1 Mon Sep 17 00:00:00 2001 From: 5elenay <5elenay@protonmail.com> Date: Fri, 27 Aug 2021 11:44:24 +0300 Subject: [PATCH] added update, delete server events. --- client.go | 14 +++++++++++++- websocket.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 5582965..80e8309 100644 --- a/client.go +++ b/client.go @@ -32,6 +32,8 @@ type Client struct { OnUnknownEventFunctions []func(message string) OnChannelStartTypingFunctions []func(channel_id, user_id string) OnChannelStopTypingFunctions []func(channel_id, user_id string) + OnServerUpdateFunctions []func(server_id, clear string, payload map[string]interface{}) + OnServerDeleteFunctions []func(server_id string) } // Client cache struct. @@ -96,11 +98,21 @@ func (c *Client) OnChannelStartTyping(fn func(channel_id, user_id string)) { c.OnChannelStartTypingFunctions = append(c.OnChannelStartTypingFunctions, fn) } -// On channel stıp typing will run when someone stops the typing status. +// On channel stop typing will run when someone stops the typing status. func (c *Client) OnChannelStopTyping(fn func(channel_id, user_id string)) { c.OnChannelStopTypingFunctions = append(c.OnChannelStopTypingFunctions, fn) } +// On server update will run when someone updates a server. +func (c *Client) OnServerUpdate(fn func(server_id, clear string, payload map[string]interface{})) { + c.OnServerUpdateFunctions = append(c.OnServerUpdateFunctions, fn) +} + +// On server delete will run when someone deletes a server. +func (c *Client) OnServerDelete(fn func(server_id string)) { + c.OnServerDeleteFunctions = append(c.OnServerDeleteFunctions, fn) +} + // Fetch a channel by Id. func (c *Client) FetchChannel(id string) (*Channel, error) { channel := &Channel{} diff --git a/websocket.go b/websocket.go index 475fb9a..8405298 100644 --- a/websocket.go +++ b/websocket.go @@ -211,6 +211,38 @@ func (c *Client) handleEvents(rawData *struct { for _, i := range c.OnChannelStopTypingFunctions { i(data.ChannelId, data.UserId) } + } else if rawData.Type == "ServerUpdate" && c.OnServerUpdateFunctions != nil { + // Server update event. + data := &struct { + ServerId string `json:"id"` + Clear string `json:"clear"` + Payload map[string]interface{} `json:"data"` + }{} + + err := json.Unmarshal([]byte(message), data) + + if err != nil { + fmt.Printf("Unexcepted Error: %s", err) + } + + for _, i := range c.OnServerUpdateFunctions { + i(data.ServerId, data.Clear, data.Payload) + } + } else if rawData.Type == "ServerDelete" && c.OnServerDeleteFunctions != nil { + // Server delete event. + data := &struct { + ServerId string `json:"id"` + }{} + + err := json.Unmarshal([]byte(message), data) + + if err != nil { + fmt.Printf("Unexcepted Error: %s", err) + } + + for _, i := range c.OnServerDeleteFunctions { + i(data.ServerId) + } } else { // Unknown event. if c.OnUnknownEventFunctions != nil {