2022-12-10 17:17:16 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2022-12-13 02:11:57 +00:00
|
|
|
"encoding/json"
|
2022-12-10 17:17:16 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-01-05 21:42:24 +00:00
|
|
|
"time"
|
2022-12-10 17:17:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LemmyResponse struct {
|
2022-12-10 23:33:08 +00:00
|
|
|
Error Optional[string] `json:"error" url:"error,omitempty"`
|
2022-12-10 17:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPError struct {
|
|
|
|
Code int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (he HTTPError) Error() string {
|
|
|
|
return fmt.Sprintf("%d %s", he.Code, http.StatusText(he.Code))
|
|
|
|
}
|
|
|
|
|
|
|
|
type LemmyError struct {
|
|
|
|
ErrStr string
|
|
|
|
Code int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (le LemmyError) Error() string {
|
|
|
|
return fmt.Sprintf("%d %s: %s", le.Code, http.StatusText(le.Code), le.ErrStr)
|
|
|
|
}
|
2022-12-13 01:35:41 +00:00
|
|
|
|
2023-01-05 21:42:24 +00:00
|
|
|
type LemmyTime struct {
|
|
|
|
time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lt *LemmyTime) UnmarshalJSON(b []byte) error {
|
|
|
|
var timeStr string
|
|
|
|
err := json.Unmarshal(b, &timeStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if timeStr == "" {
|
|
|
|
lt.Time = time.Unix(0, 0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := time.Parse("2006-01-02T15:04:05", timeStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lt.Time = t
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-13 01:35:41 +00:00
|
|
|
type LemmyWebSocketMsg struct {
|
2023-01-05 21:13:10 +00:00
|
|
|
Op string `json:"op"`
|
2022-12-13 02:11:57 +00:00
|
|
|
Data json.RawMessage `json:"data"`
|
2022-12-13 01:35:41 +00:00
|
|
|
}
|
2023-01-05 21:13:10 +00:00
|
|
|
|
2023-01-05 21:17:10 +00:00
|
|
|
// IsOneOf checks if the message is one of the given operations.
|
|
|
|
func (msg LemmyWebSocketMsg) IsOneOf(ops ...Operation) bool {
|
2023-01-05 21:13:10 +00:00
|
|
|
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
|
|
|
|
}
|
2023-01-05 21:17:10 +00:00
|
|
|
|
|
|
|
type Operation interface {
|
|
|
|
Operation() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u UserOperation) Operation() string {
|
|
|
|
return string(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u UserOperationCrud) Operation() string {
|
|
|
|
return string(u)
|
|
|
|
}
|