go-lemmy/websocket.go

131 lines
2.3 KiB
Go
Raw Normal View History

2022-12-13 01:35:41 +00:00
package lemmy
import (
2022-12-13 02:12:22 +00:00
"context"
"encoding/json"
"net/http"
2022-12-13 01:35:41 +00:00
"net/url"
2022-12-13 02:12:22 +00:00
"reflect"
2022-12-13 01:35:41 +00:00
"github.com/gorilla/websocket"
2022-12-13 01:35:41 +00:00
"go.arsenm.dev/go-lemmy/types"
)
2022-12-13 02:12:22 +00:00
type authData struct {
Auth string `json:"auth"`
}
2022-12-13 01:35:41 +00:00
type WSClient struct {
conn *websocket.Conn
2022-12-13 02:12:22 +00:00
baseURL *url.URL
respCh chan types.LemmyWebSocketMsg
errCh chan error
token string
2022-12-13 01:35:41 +00:00
}
func NewWebSocket(baseURL string) (*WSClient, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
u = u.JoinPath("/api/v3")
conn, _, err := websocket.DefaultDialer.Dial(u.JoinPath("ws").String(), nil)
if err != nil {
return nil, err
}
2022-12-13 01:35:41 +00:00
out := &WSClient{
conn: conn,
2022-12-13 02:12:22 +00:00
baseURL: u,
respCh: make(chan types.LemmyWebSocketMsg, 10),
errCh: make(chan error, 10),
2022-12-13 01:35:41 +00:00
}
go func() {
for {
var msg types.LemmyWebSocketMsg
err = conn.ReadJSON(&msg)
2022-12-13 01:35:41 +00:00
if err != nil {
out.errCh <- err
continue
}
out.respCh <- msg
}
}()
return out, nil
}
2022-12-13 02:12:22 +00:00
func (c *WSClient) Login(ctx context.Context, l types.Login) error {
u := &url.URL{}
*u = *c.baseURL
if u.Scheme == "ws" {
u.Scheme = "http"
} else if u.Scheme == "wss" {
u.Scheme = "https"
}
hc := &Client{baseURL: u, client: http.DefaultClient}
err := hc.Login(ctx, l)
if err != nil {
return err
}
c.token = hc.token
return nil
}
2022-12-13 01:35:41 +00:00
func (c *WSClient) Request(op types.UserOperation, data any) error {
2022-12-13 02:12:22 +00:00
if data == nil {
data = authData{}
}
data = c.setAuth(data)
d, err := json.Marshal(data)
if err != nil {
return err
}
2022-12-13 01:35:41 +00:00
return c.conn.WriteJSON(types.LemmyWebSocketMsg{
Op: op,
2022-12-13 02:12:22 +00:00
Data: d,
2022-12-13 01:35:41 +00:00
})
}
func (c *WSClient) Responses() <-chan types.LemmyWebSocketMsg {
return c.respCh
}
func (c *WSClient) Errors() <-chan error {
return c.errCh
}
2022-12-13 02:12:22 +00:00
func (c *WSClient) setAuth(data any) any {
val := reflect.New(reflect.TypeOf(data))
val.Elem().Set(reflect.ValueOf(data))
authField := val.Elem().FieldByName("Auth")
if !authField.IsValid() {
return data
}
switch authField.Type().String() {
case "string":
authField.SetString(c.token)
case "types.Optional[string]":
setMtd := authField.MethodByName("Set")
out := setMtd.Call([]reflect.Value{reflect.ValueOf(c.token)})
authField.Set(out[0])
default:
return data
2022-12-13 01:35:41 +00:00
}
2022-12-13 02:12:22 +00:00
return val.Elem().Interface()
}
func DecodeResponse(data json.RawMessage, out any) error {
return json.Unmarshal(data, out)
2022-12-13 01:35:41 +00:00
}