messages

package
v1.35.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2026 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProcessingStatePending   ProcessingState = "Pending"
	ProcessingStateProcessed ProcessingState = "Processed"
	ProcessingStateSent      ProcessingState = "Sent"
	ProcessingStateDelivered ProcessingState = "Delivered"
	ProcessingStateFailed    ProcessingState = "Failed"

	MessageTypeText MessageType = "Text"
	MessageTypeData MessageType = "Data"
)

Variables

View Source
var ErrMessageAlreadyExists = errors.New("duplicate id")
View Source
var ErrMessageNotFound = errors.New("message not found")
View Source
var ErrMultipleMessagesFound = errors.New("multiple messages found")
View Source
var (
	ErrNoContent = errors.New("no text or data content")
)

Functions

func Migrate added in v1.24.0

func Migrate(db *gorm.DB) error

func Module

func Module() fx.Option

Types

type Config

type Config struct {
	HashingInterval time.Duration
	CacheTTL        time.Duration
}

type DataMessageContent added in v1.24.0

type DataMessageContent struct {
	Data string `json:"data"`
	Port uint16 `json:"port"`
}

type EnqueueOptions

type EnqueueOptions struct {
	SkipPhoneValidation bool
}

type Message added in v1.24.0

type Message struct {
	models.SoftDeletableModel

	ID                 uint64          `gorm:"primaryKey;type:BIGINT UNSIGNED;autoIncrement"`
	DeviceID           string          `gorm:"not null;type:char(21);uniqueIndex:unq_messages_id_device,priority:2;index:idx_messages_device_state"`
	ExtID              string          `gorm:"not null;type:varchar(36);uniqueIndex:unq_messages_id_device,priority:1"`
	Type               MessageType     `gorm:"not null;type:enum('Text','Data');default:Text"`
	Content            string          `gorm:"not null;type:text"`
	State              ProcessingState `gorm:"not null;type:enum('Pending','Sent','Processed','Delivered','Failed');default:Pending;index:idx_messages_device_state"`
	ValidUntil         *time.Time      `gorm:"type:datetime"`
	SimNumber          *uint8          `gorm:"type:tinyint(1) unsigned"`
	WithDeliveryReport bool            `gorm:"not null;type:tinyint(1) unsigned"`
	Priority           int8            `gorm:"not null;type:tinyint;default:0"`

	IsHashed    bool `gorm:"not null;type:tinyint(1) unsigned;default:0"`
	IsEncrypted bool `gorm:"not null;type:tinyint(1) unsigned;default:0"`

	Device     models.Device      `gorm:"foreignKey:DeviceID;constraint:OnDelete:CASCADE"`
	Recipients []MessageRecipient `gorm:"foreignKey:MessageID;constraint:OnDelete:CASCADE"`
	States     []MessageState     `gorm:"foreignKey:MessageID;constraint:OnDelete:CASCADE"`
}

func NewMessage added in v1.34.3

func NewMessage(
	extID string,
	deviceID string,
	phoneNumbers []string,
	priority int8,
	simNumber *uint8,
	validUntil *time.Time,
	withDeliveryReport bool,
	isEncrypted bool,
) *Message

func (*Message) GetDataContent added in v1.24.0

func (m *Message) GetDataContent() (*DataMessageContent, error)

func (*Message) GetTextContent added in v1.24.0

func (m *Message) GetTextContent() (*TextMessageContent, error)

func (*Message) SetDataContent added in v1.24.0

func (m *Message) SetDataContent(content DataMessageContent) error

func (*Message) SetTextContent added in v1.24.0

func (m *Message) SetTextContent(content TextMessageContent) error

type MessageIn added in v1.20.0

type MessageIn struct {
	ID string

	TextContent *TextMessageContent
	DataContent *DataMessageContent

	PhoneNumbers []string
	IsEncrypted  bool

	SimNumber          *uint8
	WithDeliveryReport *bool
	TTL                *uint64
	ValidUntil         *time.Time
	Priority           smsgateway.MessagePriority
}

type MessageOut added in v1.20.0

type MessageOut struct {
	MessageIn

	CreatedAt time.Time
}

type MessageRecipient added in v1.24.0

type MessageRecipient struct {
	ID          uint64          `gorm:"primaryKey;type:BIGINT UNSIGNED;autoIncrement"`
	MessageID   uint64          `gorm:"uniqueIndex:unq_message_recipients_message_id_phone_number,priority:1;type:BIGINT UNSIGNED"`
	PhoneNumber string          `gorm:"uniqueIndex:unq_message_recipients_message_id_phone_number,priority:2;type:varchar(128)"`
	State       ProcessingState `gorm:"not null;type:enum('Pending','Sent','Processed','Delivered','Failed');default:Pending"`
	Error       *string         `gorm:"type:varchar(256)"`
}

type MessageState added in v1.24.0

type MessageState struct {
	ID        uint64          `gorm:"primaryKey;type:BIGINT UNSIGNED;autoIncrement"`
	MessageID uint64          `gorm:"not null;type:BIGINT UNSIGNED;uniqueIndex:unq_message_states_message_id_state,priority:1"`
	State     ProcessingState `` /* 135-byte string literal not displayed */
	UpdatedAt time.Time       `gorm:"<-:create;not null;autoupdatetime:false"`
}

type MessageStateIn added in v1.25.0

type MessageStateIn struct {
	ID         string                      `json:"id"`         // Message ID
	State      ProcessingState             `json:"state"`      // State
	Recipients []smsgateway.RecipientState `json:"recipients"` // Recipients states
	States     map[string]time.Time        `json:"states"`     // History of states
}

type MessageStateOut added in v1.25.0

type MessageStateOut struct {
	MessageStateIn

	DeviceID    string `json:"device_id"`    // Device ID
	IsHashed    bool   `json:"is_hashed"`    // Hashed
	IsEncrypted bool   `json:"is_encrypted"` // Encrypted
}

type MessageType added in v1.24.0

type MessageType string

type Order added in v1.34.3

type Order string

Order defines supported ordering for message selection. Valid values: "lifo" (default), "fifo".

const (
	// MessagesOrderLIFO orders messages newest-first within the same priority (default).
	MessagesOrderLIFO Order = "lifo"
	// MessagesOrderFIFO orders messages oldest-first within the same priority.
	MessagesOrderFIFO Order = "fifo"
)

type ProcessingState added in v1.24.0

type ProcessingState string

type Repository added in v1.34.0

type Repository struct {
	// contains filtered or unexported fields
}

func NewRepository added in v1.34.0

func NewRepository(db *gorm.DB) *Repository

func (*Repository) Cleanup added in v1.34.0

func (r *Repository) Cleanup(ctx context.Context, until time.Time) (int64, error)

func (*Repository) Get added in v1.34.0

func (r *Repository) Get(filter SelectFilter, options SelectOptions) (Message, error)

func (*Repository) HashProcessed added in v1.34.0

func (r *Repository) HashProcessed(ctx context.Context, ids []uint64) (int64, error)

func (*Repository) Insert added in v1.34.0

func (r *Repository) Insert(message *Message) error

func (*Repository) Select added in v1.34.0

func (r *Repository) Select(filter SelectFilter, options SelectOptions) ([]Message, int64, error)

func (*Repository) SelectPending added in v1.34.0

func (r *Repository) SelectPending(deviceID string, order Order) ([]Message, error)

func (*Repository) UpdateState added in v1.34.0

func (r *Repository) UpdateState(message *Message) error

type SelectFilter added in v1.34.3

type SelectFilter struct {
	ExtID     string
	UserID    string
	DeviceID  string
	StartDate time.Time
	EndDate   time.Time
	State     ProcessingState
}

func (*SelectFilter) WithDateRange added in v1.34.3

func (f *SelectFilter) WithDateRange(start, end time.Time) *SelectFilter

func (*SelectFilter) WithDeviceID added in v1.34.3

func (f *SelectFilter) WithDeviceID(deviceID string) *SelectFilter

func (*SelectFilter) WithExtID added in v1.34.3

func (f *SelectFilter) WithExtID(extID string) *SelectFilter

func (*SelectFilter) WithState added in v1.34.3

func (f *SelectFilter) WithState(state ProcessingState) *SelectFilter

func (*SelectFilter) WithUserID added in v1.34.3

func (f *SelectFilter) WithUserID(userID string) *SelectFilter

type SelectOptions added in v1.34.3

type SelectOptions struct {
	WithRecipients bool
	WithDevice     bool
	WithStates     bool

	// OrderBy sets the retrieval order for pending messages.
	// Empty (zero) value defaults to "lifo".
	OrderBy Order

	Limit  int
	Offset int
}

func (*SelectOptions) IncludeDevice added in v1.34.3

func (o *SelectOptions) IncludeDevice() *SelectOptions

func (*SelectOptions) IncludeRecipients added in v1.34.3

func (o *SelectOptions) IncludeRecipients() *SelectOptions

func (*SelectOptions) IncludeStates added in v1.34.3

func (o *SelectOptions) IncludeStates() *SelectOptions

func (*SelectOptions) WithLimit added in v1.34.3

func (o *SelectOptions) WithLimit(limit int) *SelectOptions

func (*SelectOptions) WithOffset added in v1.34.3

func (o *SelectOptions) WithOffset(offset int) *SelectOptions

func (*SelectOptions) WithOrderBy added in v1.34.3

func (o *SelectOptions) WithOrderBy(order Order) *SelectOptions

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(
	config Config,
	metrics *metrics,
	cache *cache,
	messages *Repository,
	eventsSvc *events.Service,
	hashingTask *hashingWorker,
	logger *zap.Logger,
	idgen db.IDGen,
) *Service

func (*Service) Enqueue added in v1.20.0

func (s *Service) Enqueue(device models.Device, message MessageIn, opts EnqueueOptions) (*MessageStateOut, error)

func (*Service) ExportInbox added in v1.17.0

func (s *Service) ExportInbox(device models.Device, since, until time.Time) error

func (*Service) GetState

func (s *Service) GetState(userID string, id string) (*MessageStateOut, error)

func (*Service) RunBackgroundTasks

func (s *Service) RunBackgroundTasks(ctx context.Context, wg *sync.WaitGroup)

func (*Service) SelectPending

func (s *Service) SelectPending(deviceID string, order Order) ([]MessageOut, error)

func (*Service) SelectStates added in v1.27.0

func (s *Service) SelectStates(
	userID string,
	filter SelectFilter,
	options SelectOptions,
) ([]MessageStateOut, int64, error)

func (*Service) UpdateState

func (s *Service) UpdateState(device *models.Device, message MessageStateIn) error

type TextMessageContent added in v1.24.0

type TextMessageContent struct {
	Text string `json:"text"`
}

type ValidationError added in v1.34.3

type ValidationError string

func (ValidationError) Error added in v1.34.3

func (e ValidationError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL