120 lines
3.3 KiB
Go
120 lines
3.3 KiB
Go
// Package cursor implements the opaque keyset cursor of the contract. A cursor
|
|
// carries the last item already returned; the next page asks PostgreSQL for
|
|
// rows strictly after (created_at, id). Cursors are bound to the endpoint and,
|
|
// for message history, to the chat that issued them.
|
|
package cursor
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"time"
|
|
|
|
"mtusi.ru/messenger-lab3/internal/model"
|
|
"mtusi.ru/messenger-lab3/internal/uuid"
|
|
)
|
|
|
|
const (
|
|
ScopeChats = "chats"
|
|
ScopeMessages = "messages"
|
|
|
|
version = 1
|
|
maxCursorSize = 512
|
|
)
|
|
|
|
// ErrInvalid is returned for malformed, tampered or out-of-scope cursors. The
|
|
// HTTP layer maps it to 400 invalid_request.
|
|
var ErrInvalid = errors.New("invalid cursor")
|
|
|
|
// Cursor is the decoded keyset position.
|
|
type Cursor struct {
|
|
At time.Time
|
|
ID string
|
|
ChatID string
|
|
}
|
|
|
|
// Encoding of the wire form. Time is an RFC 3339 timestamp with UTC Z and
|
|
// microsecond precision, matching PostgreSQL timestamptz. It is never used as
|
|
// an identifier on its own.
|
|
type wire struct {
|
|
Version int `json:"v"`
|
|
Scope string `json:"s"`
|
|
ChatID string `json:"c,omitempty"`
|
|
Time string `json:"t"`
|
|
ID string `json:"i"`
|
|
}
|
|
|
|
// ForChat encodes the position of the last returned chat.
|
|
func ForChat(chat model.Chat) string {
|
|
return encode(wire{Scope: ScopeChats, Time: chat.CreatedAt.UTC().Format(time.RFC3339Nano), ID: chat.ID})
|
|
}
|
|
|
|
// ForMessage encodes the position of the last returned message.
|
|
func ForMessage(message model.Message) string {
|
|
return encode(wire{
|
|
Scope: ScopeMessages,
|
|
ChatID: message.ChatID,
|
|
Time: message.CreatedAt.UTC().Format(time.RFC3339Nano),
|
|
ID: message.ID,
|
|
})
|
|
}
|
|
|
|
// Decode validates a cursor for the expected scope. chatID is required for the
|
|
// messages scope and must be empty for the chats scope.
|
|
func Decode(raw, scope, chatID string) (Cursor, error) {
|
|
if raw == "" || len(raw) > maxCursorSize {
|
|
return Cursor{}, ErrInvalid
|
|
}
|
|
data, err := base64.RawURLEncoding.DecodeString(raw)
|
|
if err != nil {
|
|
return Cursor{}, ErrInvalid
|
|
}
|
|
var value wire
|
|
decoder := json.NewDecoder(bytes.NewReader(data))
|
|
decoder.DisallowUnknownFields()
|
|
if err := decoder.Decode(&value); err != nil {
|
|
return Cursor{}, ErrInvalid
|
|
}
|
|
var extra any
|
|
if err := decoder.Decode(&extra); !errors.Is(err, io.EOF) {
|
|
return Cursor{}, ErrInvalid
|
|
}
|
|
if value.Version != version || value.Scope != scope {
|
|
return Cursor{}, ErrInvalid
|
|
}
|
|
at, err := time.Parse(time.RFC3339Nano, value.Time)
|
|
if err != nil || at.IsZero() || at.Location() != time.UTC {
|
|
return Cursor{}, ErrInvalid
|
|
}
|
|
normalizedID, ok := uuid.Normalize(value.ID)
|
|
if !ok || normalizedID != value.ID {
|
|
return Cursor{}, ErrInvalid
|
|
}
|
|
switch scope {
|
|
case ScopeChats:
|
|
if value.ChatID != "" {
|
|
return Cursor{}, ErrInvalid
|
|
}
|
|
return Cursor{At: at, ID: normalizedID}, nil
|
|
case ScopeMessages:
|
|
normalizedChat, ok := uuid.Normalize(value.ChatID)
|
|
if !ok || normalizedChat != value.ChatID || normalizedChat != chatID {
|
|
return Cursor{}, ErrInvalid
|
|
}
|
|
return Cursor{At: at, ID: normalizedID, ChatID: normalizedChat}, nil
|
|
default:
|
|
return Cursor{}, ErrInvalid
|
|
}
|
|
}
|
|
|
|
func encode(value wire) string {
|
|
value.Version = version
|
|
data, err := json.Marshal(value)
|
|
if err != nil {
|
|
panic("encode cursor: " + err.Error())
|
|
}
|
|
return base64.RawURLEncoding.EncodeToString(data)
|
|
}
|