82 lines
2.9 KiB
Go
82 lines
2.9 KiB
Go
package store
|
|
|
|
import (
|
|
"time"
|
|
|
|
adapter "github.com/tinode/chat/server/db"
|
|
"github.com/tinode/chat/server/store/types"
|
|
)
|
|
|
|
type HTTPInfrastructurePersistenceInterface interface {
|
|
BeginIdempotency(types.TenantID, *types.HTTPIdempotencyRecord) (*types.HTTPIdempotencyRecord, bool, error)
|
|
CompleteIdempotency(types.TenantID, *types.HTTPIdempotencyRecord) error
|
|
AbortIdempotency(types.TenantID, *types.HTTPIdempotencyRecord) error
|
|
CreateEvent(types.TenantID, *types.BusinessEvent) error
|
|
ListPendingEvents(types.TenantID, int, time.Time) ([]types.BusinessEvent, error)
|
|
MarkEventDelivered(types.TenantID, string, time.Time) error
|
|
}
|
|
|
|
type httpInfrastructureMapper struct{}
|
|
|
|
var HTTPInfrastructure HTTPInfrastructurePersistenceInterface = httpInfrastructureMapper{}
|
|
|
|
func tenantHTTPInfrastructureAdapter(tenantID types.TenantID) (adapter.TenantHTTPInfrastructureAdapter, error) {
|
|
business, err := tenantBusinessAdapter(tenantID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
infrastructure, ok := business.(adapter.TenantHTTPInfrastructureAdapter)
|
|
if !ok {
|
|
return nil, types.ErrUnsupported
|
|
}
|
|
return infrastructure, nil
|
|
}
|
|
|
|
func (httpInfrastructureMapper) BeginIdempotency(tenantID types.TenantID, record *types.HTTPIdempotencyRecord) (*types.HTTPIdempotencyRecord, bool, error) {
|
|
infrastructure, err := tenantHTTPInfrastructureAdapter(tenantID)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return infrastructure.TenantHTTPIdempotencyBegin(tenantID, record)
|
|
}
|
|
|
|
func (httpInfrastructureMapper) CompleteIdempotency(tenantID types.TenantID, record *types.HTTPIdempotencyRecord) error {
|
|
infrastructure, err := tenantHTTPInfrastructureAdapter(tenantID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return infrastructure.TenantHTTPIdempotencyComplete(tenantID, record)
|
|
}
|
|
|
|
func (httpInfrastructureMapper) AbortIdempotency(tenantID types.TenantID, record *types.HTTPIdempotencyRecord) error {
|
|
infrastructure, err := tenantHTTPInfrastructureAdapter(tenantID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return infrastructure.TenantHTTPIdempotencyAbort(tenantID, record)
|
|
}
|
|
|
|
func (httpInfrastructureMapper) CreateEvent(tenantID types.TenantID, event *types.BusinessEvent) error {
|
|
infrastructure, err := tenantHTTPInfrastructureAdapter(tenantID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return infrastructure.TenantBusinessEventCreate(tenantID, event)
|
|
}
|
|
|
|
func (httpInfrastructureMapper) ListPendingEvents(tenantID types.TenantID, limit int, now time.Time) ([]types.BusinessEvent, error) {
|
|
infrastructure, err := tenantHTTPInfrastructureAdapter(tenantID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return infrastructure.TenantBusinessEventListPending(tenantID, limit, now)
|
|
}
|
|
|
|
func (httpInfrastructureMapper) MarkEventDelivered(tenantID types.TenantID, eventID string, now time.Time) error {
|
|
infrastructure, err := tenantHTTPInfrastructureAdapter(tenantID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return infrastructure.TenantBusinessEventMarkDelivered(tenantID, eventID, now)
|
|
}
|