From 274721dce794286098973b5d60058b0f153818ce Mon Sep 17 00:00:00 2001 From: decanus <7621705+decanus@users.noreply.github.com> Date: Wed, 30 Sep 2020 13:57:17 +0200 Subject: [PATCH] started --- pkg/pubsub/events.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkg/pubsub/events.go b/pkg/pubsub/events.go index a9694d48..05689a53 100644 --- a/pkg/pubsub/events.go +++ b/pkg/pubsub/events.go @@ -1,5 +1,9 @@ package pubsub +import ( + "fmt" +) + type EventType int const ( @@ -19,6 +23,11 @@ const ( Private RoomVisibility = "private" ) +const ( + roomIDKey = "roomID" + userIDKey = "userID" +) + type Event struct { Type EventType `json:"type"` Params map[string]interface{} `json:"params"` @@ -72,3 +81,21 @@ func NewRoomLeftEvent(room, user int) Event { Params: map[string]interface{}{"id": room, "creator": user}, } } + +func (e Event) GetUserID() (int, error) { + id, ok := e.Params[userIDKey].(float64) + if !ok { + return 0, fmt.Errorf("invalid field \"%s\"", userIDKey) + } + + return int(id), nil +} + +func (e Event) GetRoomID() (int, error) { + id, ok := e.Params[roomIDKey].(float64) + if !ok { + return 0, fmt.Errorf("invalid field \"%s\"", roomIDKey) + } + + return int(id), nil +}