X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/80e348724041a694002fc38f57e8868b9b9a2be9..2a528fe2981639cf099c713a6be16b8ea0e2cc67:/whatsapp_state.go?ds=sidebyside diff --git a/whatsapp_state.go b/whatsapp_state.go index 825fa18..6114b28 100644 --- a/whatsapp_state.go +++ b/whatsapp_state.go @@ -139,6 +139,7 @@ func (s *WhatsAppState) Add(chat WhatsAppChat, message WhatsAppMessage, arrival Chat: chat, First: arrival, }) + index = len(s.file.Batches) - 1 } @@ -153,6 +154,33 @@ func (s *WhatsAppState) Add(chat WhatsAppChat, message WhatsAppMessage, arrival return closed, s.save() } +// open returns the index of the open batch of chat, or -1. The caller holds the +// mutex. +func (s *WhatsAppState) open(chat string) int { + return slices.IndexFunc(s.file.Batches, func(batch WhatsAppBatch) bool { + return !batch.Closed && batch.Chat.ID == chat + }) +} + +// save writes the state file whole. The caller holds the mutex. +func (s *WhatsAppState) save() error { + data, err := json.Marshal(s.file) + if err != nil { + return fmt.Errorf("encode WhatsApp state: %w", err) + } + + err = os.MkdirAll(filepath.Dir(s.path), 0o700) + if err == nil { + err = writeFileAtomic(s.path, append(data, '\n')) + } + + if err != nil { + return fmt.Errorf("save WhatsApp state: %w", err) + } + + return nil +} + // CloseDue closes each open batch whose time is up at now and tells if one // closed. func (s *WhatsAppState) CloseDue(now time.Time) (bool, error) { @@ -175,6 +203,17 @@ func (s *WhatsAppState) CloseDue(now time.Time) (bool, error) { return true, s.save() } +// deadline returns when batch closes in real time. +func (s *WhatsAppState) deadline(batch WhatsAppBatch) time.Time { + quiet := batch.Last.Add(whatsappBatchIdleTimeout) + window := batch.First.Add(whatsappBatchTimeout) + if window.Before(quiet) { + return window + } + + return quiet +} + // CloseAll closes every open batch. func (s *WhatsAppState) CloseAll() error { s.mutex.Lock() @@ -273,41 +312,3 @@ func (s *WhatsAppState) PutSend(record WhatsAppSend) error { return s.save() } - -// open returns the index of the open batch of chat, or -1. The caller holds the -// mutex. -func (s *WhatsAppState) open(chat string) int { - return slices.IndexFunc(s.file.Batches, func(batch WhatsAppBatch) bool { - return !batch.Closed && batch.Chat.ID == chat - }) -} - -// deadline returns when batch closes in real time. -func (s *WhatsAppState) deadline(batch WhatsAppBatch) time.Time { - quiet := batch.Last.Add(whatsappBatchIdleTimeout) - window := batch.First.Add(whatsappBatchTimeout) - if window.Before(quiet) { - return window - } - - return quiet -} - -// save writes the state file whole. The caller holds the mutex. -func (s *WhatsAppState) save() error { - data, err := json.Marshal(s.file) - if err != nil { - return fmt.Errorf("encode WhatsApp state: %w", err) - } - - err = os.MkdirAll(filepath.Dir(s.path), 0o700) - if err == nil { - err = writeFileAtomic(s.path, append(data, '\n')) - } - - if err != nil { - return fmt.Errorf("save WhatsApp state: %w", err) - } - - return nil -}