X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/48d7bce28b18ecef4c52c98821675605464cecb9..3cdbd017da9ae7a7045f4272f16d4b262fd01c79:/whatsapp_state.go?ds=sidebyside diff --git a/whatsapp_state.go b/whatsapp_state.go index 41bd2f0..b1b5935 100644 --- a/whatsapp_state.go +++ b/whatsapp_state.go @@ -16,12 +16,12 @@ const ( // the batches that are not in the log yet and the records of the sends. whatsappStateFileName = "state.json" - // whatsappBatchQuiet is how long a chat is quiet before its batch + // whatsappBatchIdleTimeout is how long a chat is quiet before its batch // closes. - whatsappBatchQuiet = 20 * time.Second + whatsappBatchIdleTimeout = 20 * time.Second - // whatsappBatchWindow is the longest time of a batch. - whatsappBatchWindow = 2 * time.Minute + // whatsappBatchTimeout is the longest time of a batch. + whatsappBatchTimeout = 2 * time.Minute // whatsappBatchMessages is the most messages of a batch. whatsappBatchMessages = 50 @@ -32,8 +32,9 @@ const ( ) // openWhatsAppState reads the batches and the send records of the state file at -// path. A missing file holds none. A batch closes after whatsappBatchQuiet with -// no message, or after whatsappBatchWindow at the latest. +// path. A missing file holds none. A batch closes after +// whatsappBatchIdleTimeout with no message, or after whatsappBatchTimeout at +// the latest. func openWhatsAppState(path string) (*WhatsAppState, error) { s := &WhatsAppState{ path: path, @@ -59,12 +60,13 @@ func openWhatsAppState(path string) (*WhatsAppState, error) { // 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 whatsappBatchQuiet apart, at most -// whatsappBatchWindow after the first, and the batch has fewer than +// times of the messages are at most whatsappBatchIdleTimeout apart, at most +// whatsappBatchTimeout 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. +// closes when whatsappBatchIdleTimeout passes after the arrival of its last +// message, or whatsappBatchTimeout 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 @@ -99,7 +101,7 @@ func (s *WhatsAppState) Add(chat WhatsAppChat, message WhatsAppMessage, arrival return false, nil } - if message.Time.Sub(batch.Messages[len(batch.Messages)-1].Time) > whatsappBatchQuiet || message.Time.Sub(batch.Messages[0].Time) > whatsappBatchWindow { + if message.Time.Sub(batch.Messages[len(batch.Messages)-1].Time) > whatsappBatchIdleTimeout || message.Time.Sub(batch.Messages[0].Time) > whatsappBatchTimeout { batch.Closed = true closed = true index = -1 @@ -258,8 +260,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(whatsappBatchQuiet) - window := batch.First.Add(whatsappBatchWindow) + quiet := batch.Last.Add(whatsappBatchIdleTimeout) + window := batch.First.Add(whatsappBatchTimeout) if window.Before(quiet) { return window }