Add Operation interface

This commit is contained in:
Elara 2023-01-05 13:17:10 -08:00
parent 9ef13bb413
commit 187da56546
2 changed files with 16 additions and 5 deletions

View File

@ -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)
}

View File

@ -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,
})
}