From cf871efc2306a147e857bfac4fe0ea439784edcd Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Thu, 5 Jan 2023 13:13:10 -0800 Subject: [PATCH] Add IsOneOf to websocket message --- types/types.go | 20 +++++++++++++++++++- websocket.go | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/types/types.go b/types/types.go index 4920a1a..c76f74e 100644 --- a/types/types.go +++ b/types/types.go @@ -28,6 +28,24 @@ func (le LemmyError) Error() string { } type LemmyWebSocketMsg struct { - Op UserOperation `json:"op"` + Op string `json:"op"` Data json.RawMessage `json:"data"` } + +// IsOneOf checks if the message is one of the given operation. +// Operations must be of type UserOperation or UserOperationCrud. +func (msg LemmyWebSocketMsg) IsOneOf(ops ...any) bool { + for _, op := range ops { + switch op := op.(type) { + case UserOperation: + if string(op) == msg.Op { + return true + } + case UserOperationCrud: + if string(op) == msg.Op { + return true + } + } + } + return false +} diff --git a/websocket.go b/websocket.go index 9394e1e..c9e4df1 100644 --- a/websocket.go +++ b/websocket.go @@ -99,7 +99,7 @@ func (c *WSClient) Request(op types.UserOperation, data any) error { } return c.conn.WriteJSON(types.LemmyWebSocketMsg{ - Op: op, + Op: string(op), Data: d, }) }