package storage import ( "time" "gorm.io/gorm" ) // Auto-generate string names for the various constants //go:generate stringer -type InboundJobSource type InboundJobSource uint8 // TODO: Adjust and expand these constants later, depending on sources const ( InJobSourceAccInbox InboundJobSource = iota InJobSourceServerInbox InJobSourceApiMasto InJobSourceApiLinstrom ) // Store inbound jobs from api and ap in the db until they finished processing // Ensures data consistency in case the server is forced to restart unexpectedly // No DeletedAt field since don't want completed jobs to linger in the db for any longer than necessary type InboundJob struct { ID uint `gorm:"primarykey"` CreatedAt time.Time // Raw data, could be json or gob data, check source for how to interpret RawData []byte `gorm:"->;<-create"` // Where this job is coming from. Important for figuring out how to decode the raw data and what to do with it Source InboundJobSource `gorm:"->;<-create"` // Section: Various data // TODO: Expand based on needs // If from an inbox, include the owner id here InboxOwner *string `gorm:"->;<-create"` } func (s *Storage) AddNewInboundJob(data []byte, source InboundJobSource, inboxOwner *string) { newJob := InboundJob{ RawData: data, Source: source, InboxOwner: inboxOwner, } s.db.Create(&newJob) } // Get the specified amount of jobs, sorted by age (oldest first) func (s *Storage) GetOldestInboundJobs(amount int) ([]InboundJob, error) { jobs := []InboundJob{} switch err := s.db.Order("id asc, created_at asc").Limit(amount).Find(jobs).Error; err { case gorm.ErrRecordNotFound: return nil, ErrEntryNotFound case nil: return jobs, nil default: return nil, err } }