Skip to content

Commit

Permalink
Update ws (#5)
Browse files Browse the repository at this point in the history
* change comment

* add ws event converters

* remove pointer
  • Loading branch information
Sagleft authored Feb 15, 2023
1 parent 1cdba57 commit 7f3b72a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions v2/pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package helpers

import (
"encoding/json"
"errors"

"github.com/Sagleft/utopialib-go/v2/pkg/structs"
"github.com/Sagleft/utopialib-go/v2/pkg/websocket"
)

// GetChannelMessageFromEvent - get the event data converted to ChannelMessage.
// actual only for `newPrivateChannelMessage` and `newChannelMessage` events
func GetChannelMessageFromEvent(ws websocket.WsEvent) (structs.WsChannelMessage, error) {
result := structs.WsChannelMessage{}
eventBytes, err := json.Marshal(ws.Data)
if err != nil {
return result, errors.New("failed to encode channel message: " + err.Error())
}

err = json.Unmarshal(eventBytes, &result)
if err != nil {
return result, errors.New("failed to decode event data as channel message: " + err.Error())
}
return result, nil
}

// GetInstantMessageFromEvent - get the event data converted to InstantMessage.
// actual only for `newInstantMessage` event
func GetInstantMessageFromEvent(ws websocket.WsEvent) (structs.InstantMessage, error) {
result := structs.InstantMessage{}
eventBytes, err := json.Marshal(ws.Data)
if err != nil {
return result, errors.New("failed to encode contact message: " + err.Error())
}

err = json.Unmarshal(eventBytes, &result)
if err != nil {
return result, errors.New("failed to decode event data as contact message: " + err.Error())
}
return result, nil
}
2 changes: 1 addition & 1 deletion v2/pkg/websocket/structs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package websocket

// WsEvent - websocket event from Utopia Client
// WsEvent - websocket event
type WsEvent struct {
Type string `json:"type"`
Data map[string]interface{} `json:"data"`
Expand Down

0 comments on commit 7f3b72a

Please sign in to comment.