Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pkg/pubsub/events.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package pubsub

import (
"fmt"
)

type EventType int

const (
Expand All @@ -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"`
Expand Down Expand Up @@ -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
}