messages

package
v1.34.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: Apache-2.0 Imports: 27 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"
)
View Source
const (
	ErrorTTLExpired = "TTL expired"
)

Variables

View Source
var ErrMessageAlreadyExists = errors.New("duplicate id")
View Source
var ErrMessageNotFound = gorm.ErrRecordNotFound
View Source
var ErrMultipleMessagesFound = errors.New("multiple messages found")

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 ErrValidation

type ErrValidation string

func (ErrValidation) Error

func (e ErrValidation) Error() string

type Message added in v1.24.0

type Message struct {
	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"`

	models.SoftDeletableModel
}

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 MessagesOrder added in v1.28.0

type MessagesOrder string

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

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

type MessagesSelectFilter

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

type MessagesSelectOptions

type MessagesSelectOptions struct {
	WithRecipients bool
	WithDevice     bool
	WithStates     bool

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

	Limit  int
	Offset int
}

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 (*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 MessagesSelectFilter, options MessagesSelectOptions) ([]Message, int64, error)

func (*Repository) SelectPending added in v1.34.0

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

func (*Repository) UpdateState added in v1.34.0

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

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(user models.User, 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 MessagesOrder) ([]MessageOut, error)

func (*Service) SelectStates added in v1.27.0

func (s *Service) SelectStates(user models.User, filter MessagesSelectFilter, options MessagesSelectOptions) ([]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"`
}

Jump to

Keyboard shortcuts

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