package v1 import ( "encoding/base64" "encoding/binary" "errors" "time" "github.com/google/uuid" "lincodex/backend/internal/port/outbound" ) const ( notificationCursorVersion = 1 notificationCursorBytes = 61 // maxNotificationCursorYear keeps the decoded time inside the range that // PostgreSQL stores and that the contract's RFC 3339 timestamps print. maxNotificationCursorYear = 9999 ) // encodeNotificationCursor binds a page position to the account and the // organization context of the listing, so a cursor of another account or // another cabinet fails as malformed instead of continuing a foreign list. // A nil organization is the candidate cabinet. func encodeNotificationCursor(recipientID uuid.UUID, organizationID *uuid.UUID, position outbound.NotificationInboxPosition) (string, error) { at := position.OccurredAt.UTC() if recipientID == uuid.Nil || position.EventID == uuid.Nil || at.IsZero() || at.Year() > maxNotificationCursorYear { return "", errors.New("v1.encodeNotificationCursor - a recipient and a valid page position are required") } seconds, nanos := at.Unix(), at.Nanosecond() if seconds < 0 { return "", errors.New("v1.encodeNotificationCursor - the page position predates the epoch") } if nanos < 0 || nanos >= 1_000_000_000 || nanos%1_000 != 0 { return "", errors.New("v1.encodeNotificationCursor - the page position has no microsecond precision") } var value [notificationCursorBytes]byte value[0] = notificationCursorVersion copy(value[1:17], recipientID[:]) if organizationID != nil { copy(value[17:33], organizationID[:]) } binary.BigEndian.PutUint64(value[33:41], uint64(seconds)) binary.BigEndian.PutUint32(value[41:45], uint32(nanos)) copy(value[45:61], position.EventID[:]) return base64.RawURLEncoding.EncodeToString(value[:]), nil } // decodeNotificationCursor reads a cursor made for the same account and // cabinet by encodeNotificationCursor. func decodeNotificationCursor(cursor string, recipientID uuid.UUID, organizationID *uuid.UUID) (outbound.NotificationInboxPosition, error) { const location = "v1.decodeNotificationCursor" value, err := base64.RawURLEncoding.DecodeString(cursor) if err != nil || len(value) != notificationCursorBytes || recipientID == uuid.Nil || base64.RawURLEncoding.EncodeToString(value) != cursor || value[0] != notificationCursorVersion || uuid.UUID(value[1:17]) != recipientID { return outbound.NotificationInboxPosition{}, errors.New(location + " - the cursor is invalid") } var scope uuid.UUID if organizationID != nil { scope = *organizationID } if uuid.UUID(value[17:33]) != scope { return outbound.NotificationInboxPosition{}, errors.New(location + " - the cursor belongs to another cabinet") } seconds := binary.BigEndian.Uint64(value[33:41]) nanos := binary.BigEndian.Uint32(value[41:45]) eventID := uuid.UUID(value[45:61]) if seconds > uint64(1<<62) || nanos >= 1_000_000_000 || nanos%1_000 != 0 || eventID == uuid.Nil { return outbound.NotificationInboxPosition{}, errors.New(location + " - the cursor position is invalid") } at := time.Unix(int64(seconds), int64(nanos)).UTC() if at.IsZero() || at.Year() > maxNotificationCursorYear { return outbound.NotificationInboxPosition{}, errors.New(location + " - the cursor timestamp is invalid") } return outbound.NotificationInboxPosition{OccurredAt: at, EventID: eventID}, nil }