SimpleCloudNotifier/scnserver/db/cursortoken/token.go

146 lines
2.8 KiB
Go
Raw Permalink Normal View History

2022-11-20 00:19:41 +01:00
package cursortoken
import (
"encoding/base32"
"encoding/json"
"errors"
"strings"
"time"
)
2023-04-21 21:45:16 +02:00
type Mode string //@enum:type
2022-11-20 00:19:41 +01:00
const (
CTMStart = "START"
CTMNormal = "NORMAL"
CTMEnd = "END"
)
type CursorToken struct {
2022-12-10 03:38:48 +01:00
Mode Mode
Timestamp int64
Id string
2022-12-10 03:38:48 +01:00
Direction string
FilterHash string
2022-11-20 00:19:41 +01:00
}
type cursorTokenSerialize struct {
2022-12-10 03:38:48 +01:00
Timestamp *int64 `json:"ts,omitempty"`
Id *string `json:"id,omitempty"`
2022-12-10 03:38:48 +01:00
Direction *string `json:"dir,omitempty"`
FilterHash *string `json:"f,omitempty"`
2022-11-20 00:19:41 +01:00
}
func Start() CursorToken {
return CursorToken{
2022-12-10 03:38:48 +01:00
Mode: CTMStart,
Timestamp: 0,
Id: "",
2022-12-10 03:38:48 +01:00
Direction: "",
FilterHash: "",
2022-11-20 00:19:41 +01:00
}
}
func End() CursorToken {
return CursorToken{
2022-12-10 03:38:48 +01:00
Mode: CTMEnd,
Timestamp: 0,
Id: "",
2022-12-10 03:38:48 +01:00
Direction: "",
FilterHash: "",
2022-11-20 00:19:41 +01:00
}
}
func Normal(ts time.Time, id string, dir string, filter string) CursorToken {
2022-11-20 00:19:41 +01:00
return CursorToken{
2022-12-10 03:38:48 +01:00
Mode: CTMNormal,
Timestamp: ts.UnixMilli(),
Id: id,
Direction: dir,
FilterHash: filter,
2022-11-20 00:19:41 +01:00
}
}
func (c *CursorToken) Token() string {
if c.Mode == CTMStart {
return "@start"
}
if c.Mode == CTMEnd {
return "@end"
}
// We kinda manually implement omitempty for the CursorToken here
// because omitempty does not work for time.Time and otherwise we would always
// get weird time values when decoding a token that initially didn't have an Timestamp set
// For this usecase we treat Unix=0 as an empty timestamp
sertok := cursorTokenSerialize{}
if c.Id != "" {
2022-11-20 00:19:41 +01:00
sertok.Id = &c.Id
}
if c.Timestamp != 0 {
sertok.Timestamp = &c.Timestamp
}
if c.Direction != "" {
sertok.Direction = &c.Direction
}
2022-12-10 03:38:48 +01:00
if c.FilterHash != "" {
sertok.FilterHash = &c.FilterHash
}
2022-11-20 00:19:41 +01:00
body, err := json.Marshal(sertok)
if err != nil {
panic(err)
}
return "tok_" + base32.StdEncoding.EncodeToString(body)
}
func Decode(tok string) (CursorToken, error) {
if tok == "" {
return Start(), nil
}
if strings.ToLower(tok) == "@start" {
return Start(), nil
}
if strings.ToLower(tok) == "@end" {
return End(), nil
}
if !strings.HasPrefix(tok, "tok_") {
return CursorToken{}, errors.New("could not decode token, missing prefix")
}
body, err := base32.StdEncoding.DecodeString(tok[len("tok_"):])
if err != nil {
return CursorToken{}, err
}
var tokenDeserialize cursorTokenSerialize
err = json.Unmarshal(body, &tokenDeserialize)
if err != nil {
return CursorToken{}, err
}
token := CursorToken{Mode: CTMNormal}
if tokenDeserialize.Timestamp != nil {
token.Timestamp = *tokenDeserialize.Timestamp
}
if tokenDeserialize.Id != nil {
token.Id = *tokenDeserialize.Id
}
if tokenDeserialize.Direction != nil {
token.Direction = *tokenDeserialize.Direction
}
2022-12-10 03:38:48 +01:00
if tokenDeserialize.FilterHash != nil {
token.FilterHash = *tokenDeserialize.FilterHash
}
2022-11-20 00:19:41 +01:00
return token, nil
}