From 8e9cf13cc3d231525bf274b0a355c0658129415c Mon Sep 17 00:00:00 2001 From: 5elenay <5elenay@protonmail.com> Date: Fri, 27 Aug 2021 11:38:06 +0300 Subject: [PATCH] added channel typing events. --- client.go | 28 ++++++++++++++++++++-------- websocket.go | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index 613e3e7..5582965 100644 --- a/client.go +++ b/client.go @@ -22,14 +22,16 @@ type Client struct { Cache *Cache // Event Functions - OnReadyFunctions []func() - OnMessageFunctions []func(message *Message) - OnMessageUpdateFunctions []func(channel_id, message_id string, payload map[string]interface{}) - OnMessageDeleteFunctions []func(channel_id, message_id string) - OnChannelCreateFunctions []func(channel *Channel) - OnChannelUpdateFunctions []func(channel_id, clear string, payload map[string]interface{}) - OnChannelDeleteFunctions []func(channel_id string) - OnUnknownEventFunctions []func(message string) + OnReadyFunctions []func() + OnMessageFunctions []func(message *Message) + OnMessageUpdateFunctions []func(channel_id, message_id string, payload map[string]interface{}) + OnMessageDeleteFunctions []func(channel_id, message_id string) + OnChannelCreateFunctions []func(channel *Channel) + OnChannelUpdateFunctions []func(channel_id, clear string, payload map[string]interface{}) + OnChannelDeleteFunctions []func(channel_id string) + OnUnknownEventFunctions []func(message string) + OnChannelStartTypingFunctions []func(channel_id, user_id string) + OnChannelStopTypingFunctions []func(channel_id, user_id string) } // Client cache struct. @@ -89,6 +91,16 @@ func (c *Client) OnUnknownEvent(fn func(message string)) { c.OnUnknownEventFunctions = append(c.OnUnknownEventFunctions, fn) } +// On channel start typing will run when someone starts to type a message. +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. +func (c *Client) OnChannelStopTyping(fn func(channel_id, user_id string)) { + c.OnChannelStopTypingFunctions = append(c.OnChannelStopTypingFunctions, 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 3b97efe..475fb9a 100644 --- a/websocket.go +++ b/websocket.go @@ -179,8 +179,40 @@ func (c *Client) handleEvents(rawData *struct { for _, i := range c.OnChannelDeleteFunctions { i(data.ChannelId) } + } else if rawData.Type == "ChannelStartTyping" && c.OnChannelStartTypingFunctions != nil { + // Channel start typing event. + data := &struct { + ChannelId string `json:"id"` + UserId string `json:"user"` + }{} + + err := json.Unmarshal([]byte(message), data) + + if err != nil { + fmt.Printf("Unexcepted Error: %s", err) + } + + for _, i := range c.OnChannelStartTypingFunctions { + i(data.ChannelId, data.UserId) + } + } else if rawData.Type == "ChannelStopTyping" && c.OnChannelStopTypingFunctions != nil { + // Channel stop typing event. + data := &struct { + ChannelId string `json:"id"` + UserId string `json:"user"` + }{} + + err := json.Unmarshal([]byte(message), data) + + if err != nil { + fmt.Printf("Unexcepted Error: %s", err) + } + + for _, i := range c.OnChannelStopTypingFunctions { + i(data.ChannelId, data.UserId) + } } else { - // Unknown Event + // Unknown event. if c.OnUnknownEventFunctions != nil { for _, i := range c.OnUnknownEventFunctions { i(message)