Add Operation interface

This commit is contained in:
Elara 2023-01-05 13:17:10 -08:00
parent cf871efc23
commit 4ecce09b73
2 changed files with 16 additions and 5 deletions

View File

@ -32,9 +32,8 @@ type LemmyWebSocketMsg struct {
Data json.RawMessage `json:"data"` Data json.RawMessage `json:"data"`
} }
// IsOneOf checks if the message is one of the given operation. // IsOneOf checks if the message is one of the given operations.
// Operations must be of type UserOperation or UserOperationCrud. func (msg LemmyWebSocketMsg) IsOneOf(ops ...Operation) bool {
func (msg LemmyWebSocketMsg) IsOneOf(ops ...any) bool {
for _, op := range ops { for _, op := range ops {
switch op := op.(type) { switch op := op.(type) {
case UserOperation: case UserOperation:
@ -49,3 +48,15 @@ func (msg LemmyWebSocketMsg) IsOneOf(ops ...any) bool {
} }
return false 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 // the authentication token will be sent instead. If data
// has an Auth field, it will be set to the authentication // has an Auth field, it will be set to the authentication
// token automatically. // 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 { if data == nil {
data = authData{} data = authData{}
} }
@ -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: string(op), Op: op.Operation(),
Data: d, Data: d,
}) })
} }