messages

package
v1.26.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2025 License: Apache-2.0 Imports: 26 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 Module = fx.Module(
	"messages",
	fx.Decorate(func(log *zap.Logger) *zap.Logger {
		return log.Named("messages")
	}),
	fx.Provide(func(p ServiceParams) FxResult {
		svc := NewService(p)
		return FxResult{
			Service:   svc,
			AsCleaner: svc,
		}
	}),
	fx.Provide(newRepository),
	fx.Provide(NewHashingTask, fx.Private),
)

Functions

func Migrate added in v1.24.0

func Migrate(db *gorm.DB) error

Types

type Config

type Config struct {
	ProcessedLifetime 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 FxResult

type FxResult struct {
	fx.Out

	Service   *Service
	AsCleaner cleaner.Cleanable `group:"cleaners"`
}

type HashingTask

type HashingTask struct {
	Messages *repository
	Config   HashingTaskConfig
	Logger   *zap.Logger
	// contains filtered or unexported fields
}

func NewHashingTask

func NewHashingTask(params HashingTaskParams) *HashingTask

func (*HashingTask) Enqueue added in v1.20.0

func (t *HashingTask) Enqueue(id uint64)

Enqueue adds a message ID to the processing queue to be hashed in the next batch

func (*HashingTask) Run

func (t *HashingTask) Run(ctx context.Context)

type HashingTaskConfig

type HashingTaskConfig struct {
	Interval time.Duration
}

type HashingTaskParams

type HashingTaskParams struct {
	fx.In

	Messages *repository
	Config   HashingTaskConfig
	Logger   *zap.Logger
}

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 {
	// Message ID
	ID string
	// State
	State ProcessingState
	// Recipients states
	Recipients []smsgateway.RecipientState
	// History of states
	States map[string]time.Time
}

type MessageStateOut added in v1.25.0

type MessageStateOut struct {
	// Device ID
	DeviceID string
	// Hashed
	IsHashed bool
	// Encrypted
	IsEncrypted bool

	MessageStateIn
}

type MessageType added in v1.24.0

type MessageType string

type MessagesSelectFilter

type MessagesSelectFilter struct {
	DeviceID string
}

type MessagesSelectOptions

type MessagesSelectOptions struct {
	WithRecipients bool
	WithDevice     bool
	WithStates     bool
}

type ProcessingState added in v1.24.0

type ProcessingState string

type Service

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

func NewService

func NewService(params ServiceParams) *Service

func (*Service) Clean

func (s *Service) Clean(ctx context.Context) error

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) ([]MessageOut, error)

func (*Service) UpdateState

func (s *Service) UpdateState(deviceID string, message MessageStateIn) error

type ServiceParams

type ServiceParams struct {
	fx.In

	IDGen db.IDGen

	Config Config

	Messages    *repository
	HashingTask *HashingTask

	EventsSvc *events.Service

	Logger *zap.Logger
}

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