X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/6e7c7f3d47f086b9d59fe40b91fb071e1528684e..48d7bce28b18ecef4c52c98821675605464cecb9:/whatsapp_state.go diff --git a/whatsapp_state.go b/whatsapp_state.go index b8929f0..41bd2f0 100644 --- a/whatsapp_state.go +++ b/whatsapp_state.go @@ -32,13 +32,11 @@ const ( ) // openWhatsAppState reads the batches and the send records of the state file at -// path. A missing file holds none. A batch closes after quiet with no message, -// or after window at the latest. -func openWhatsAppState(path string, quiet, window time.Duration) (*WhatsAppState, error) { +// path. A missing file holds none. A batch closes after whatsappBatchQuiet with +// no message, or after whatsappBatchWindow at the latest. +func openWhatsAppState(path string) (*WhatsAppState, error) { s := &WhatsAppState{ - path: path, - quiet: quiet, - window: window, + path: path, } data, err := os.ReadFile(path) @@ -61,11 +59,12 @@ func openWhatsAppState(path string, quiet, window time.Duration) (*WhatsAppState // WhatsAppState holds the state file: the send records, and the batches of the // WhatsApp messages that are not in the log yet, open and closed, in the order // of their first message. The messages of one chat form one batch while the -// times of the messages are at most quiet apart, at most window after the -// first, and the batch has fewer than whatsappBatchMessages messages. The times -// are the WhatsApp times, so a catch-up after a stop gives the same batches as -// live messages. A batch also closes when quiet passes after the arrival of its -// last message, or window after the arrival of its first message, in real time. +// times of the messages are at most whatsappBatchQuiet apart, at most +// whatsappBatchWindow after the first, and the batch has fewer than +// whatsappBatchMessages messages. The times are the WhatsApp times, so a +// catch-up after a stop gives the same batches as live messages. A batch also +// closes when whatsappBatchQuiet passes after the arrival of its last message, +// or whatsappBatchWindow after the arrival of its first message, in real time. // // A send record keeps the result of a send by its ID, so that a repeated call // of the send tool does not send again. The state file keeps the last @@ -76,9 +75,7 @@ func openWhatsAppState(path string, quiet, window time.Duration) (*WhatsAppState // library adds messages while Run closes and removes batches, and the turns // send. type WhatsAppState struct { - path string - quiet time.Duration - window time.Duration + path string mutex sync.Mutex file WhatsAppStateFile @@ -102,7 +99,7 @@ func (s *WhatsAppState) Add(chat WhatsAppChat, message WhatsAppMessage, arrival return false, nil } - if message.Time.Sub(batch.Messages[len(batch.Messages)-1].Time) > s.quiet || message.Time.Sub(batch.Messages[0].Time) > s.window { + if message.Time.Sub(batch.Messages[len(batch.Messages)-1].Time) > whatsappBatchQuiet || message.Time.Sub(batch.Messages[0].Time) > whatsappBatchWindow { batch.Closed = true closed = true index = -1 @@ -170,6 +167,7 @@ func (s *WhatsAppState) Closed() []WhatsAppBatch { defer s.mutex.Unlock() var closed []WhatsAppBatch + for _, batch := range s.file.Batches { if batch.Closed { closed = append(closed, batch) @@ -198,6 +196,7 @@ func (s *WhatsAppState) Wait(now time.Time) (time.Duration, bool) { defer s.mutex.Unlock() var first time.Time + for _, batch := range s.file.Batches { deadline := s.deadline(batch) if !batch.Closed && (first.IsZero() || deadline.Before(first)) { @@ -259,8 +258,8 @@ func (s *WhatsAppState) open(chat string) int { // deadline returns when batch closes in real time. func (s *WhatsAppState) deadline(batch WhatsAppBatch) time.Time { - quiet := batch.Last.Add(s.quiet) - window := batch.First.Add(s.window) + quiet := batch.Last.Add(whatsappBatchQuiet) + window := batch.First.Add(whatsappBatchWindow) if window.Before(quiet) { return window }