Add IsOneOf to websocket message

This commit is contained in:
Elara 2023-01-05 13:13:10 -08:00
parent a8997d12ee
commit 9ef13bb413
2 changed files with 20 additions and 2 deletions

View File

@ -28,6 +28,24 @@ func (le LemmyError) Error() string {
} }
type LemmyWebSocketMsg struct { type LemmyWebSocketMsg struct {
Op UserOperation `json:"op"` Op string `json:"op"`
Data json.RawMessage `json:"data"` 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
}

View File

@ -99,7 +99,7 @@ func (c *WSClient) Request(op types.UserOperation, data any) error {
} }
return c.conn.WriteJSON(types.LemmyWebSocketMsg{ return c.conn.WriteJSON(types.LemmyWebSocketMsg{
Op: op, Op: string(op),
Data: d, Data: d,
}) })
} }