From 187da56546b66f3f0f4fa7191371ae4a2a18125d Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Thu, 5 Jan 2023 13:17:10 -0800 Subject: [PATCH] Add Operation interface --- types/types.go | 17 ++++++++++++++--- websocket.go | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/types/types.go b/types/types.go index c76f74e..168f387 100644 --- a/types/types.go +++ b/types/types.go @@ -32,9 +32,8 @@ type LemmyWebSocketMsg struct { 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 { +// IsOneOf checks if the message is one of the given operations. +func (msg LemmyWebSocketMsg) IsOneOf(ops ...Operation) bool { for _, op := range ops { switch op := op.(type) { case UserOperation: @@ -49,3 +48,15 @@ func (msg LemmyWebSocketMsg) IsOneOf(ops ...any) bool { } return false } + +type Operation interface { + Operation() string +} + +func (u UserOperation) Operation() string { + return string(u) +} + +func (u UserOperationCrud) Operation() string { + return string(u) +} diff --git a/websocket.go b/websocket.go index c9e4df1..fa2bffa 100644 --- a/websocket.go +++ b/websocket.go @@ -86,7 +86,7 @@ func (c *WSClient) ClientLogin(ctx context.Context, l types.Login) error { // the authentication token will be sent instead. If data // has an Auth field, it will be set to the authentication // token automatically. -func (c *WSClient) Request(op types.UserOperation, data any) error { +func (c *WSClient) Request(op types.Operation, data any) error { if data == nil { data = authData{} } @@ -99,7 +99,7 @@ func (c *WSClient) Request(op types.UserOperation, data any) error { } return c.conn.WriteJSON(types.LemmyWebSocketMsg{ - Op: string(op), + Op: op.Operation(), Data: d, }) }