-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* change comment * add ws event converters * remove pointer
- Loading branch information
Showing
2 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters