forked from mtusi2026/mtusi2026lab3
35 lines
876 B
Go
35 lines
876 B
Go
package handler
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
"uuid"
|
|
|
|
"messenger/internal/domain"
|
|
)
|
|
|
|
func encodeCursor(c domain.Cursor) string {
|
|
raw := c.Scope.String() + "." + strconv.FormatInt(c.CreatedAt.UnixMicro(), 10) + "." + c.ID.String()
|
|
return base64.RawURLEncoding.EncodeToString([]byte(raw))
|
|
}
|
|
|
|
func decodeCursor(s string) (domain.Cursor, bool) {
|
|
raw, err := base64.RawURLEncoding.DecodeString(s)
|
|
if err != nil {
|
|
return domain.Cursor{}, false
|
|
}
|
|
parts := strings.Split(string(raw), ".")
|
|
if len(parts) != 3 {
|
|
return domain.Cursor{}, false
|
|
}
|
|
scope, err1 := uuid.Parse(parts[0])
|
|
micros, err2 := strconv.ParseInt(parts[1], 10, 64)
|
|
id, err3 := uuid.Parse(parts[2])
|
|
if err1 != nil || err2 != nil || err3 != nil {
|
|
return domain.Cursor{}, false
|
|
}
|
|
return domain.Cursor{Scope: scope, CreatedAt: time.UnixMicro(micros).UTC(), ID: id}, true
|
|
}
|