store only honks in the honkMap we only need the message id from TelegramResponse, so no need to store everything else inside a struct inside a map. on a way to encapsulate and isolate the honkMap!
la-ninpre leobrekalini@gmail.com
Mon, 17 Oct 2022 13:12:23 +0300
2 files changed,
9 insertions(+),
14 deletions(-)
M
changelog.txt
→
changelog.txt
@@ -2,4 +2,5 @@ changelog
=== next ++ removed TelegramResponse from the honkMap + make gethonks retry 5 times before giving up
M
main.go
→
main.go
@@ -98,7 +98,7 @@ hi, ok := honkMap[h.RID]
if !ok { return fmt.Errorf("cannot reply to nonexisting telebonk") } - h.replyToID = hi.t.Result.MessageID + h.replyToID = hi.messID default: return fmt.Errorf("unsupported honk type: %s", h.What) }@@ -118,19 +118,19 @@
func (h *Honk) Decide() honkAction { hi, ok := honkMap[h.XID] if ok { - if hi.t == nil || hi.h.Date.Equal(h.Date) { - h.save(hi.t) // is this wrong? + if hi.messID == 0 || hi.Date.Equal(h.Date) { + h.save(hi.messID) return honkIgnore } - h.messID = hi.t.Result.MessageID return honkEdit } return honkSend } // save records a Honk to a honkMap -func (h *Honk) save(t *TelegramResponse) { - honkMap[h.XID] = honkInfo{h: h, t: t} +func (h *Honk) save(messID int) { + h.messID = messID + honkMap[h.XID] = h } // forget removes a Honk from a honkMap@@ -313,12 +313,6 @@ tgSendPhoto = "sendPhoto"
tgSendDocument = "sendDocument" ) -// A honkInfo stores data for honk that is sent to Telegram. -type honkInfo struct { - h *Honk - t *TelegramResponse -} - // getHonks receives and unmarshals some honks from a Honk instance. func getHonks(after int) ([]*Honk, error) { query := url.Values{}@@ -403,7 +397,7 @@ }
} var client = http.DefaultClient -var honkMap = make(map[string]honkInfo) // FIXME: not safe for use by multiple goroutines! +var honkMap = make(map[string]*Honk) // FIXME: not safe for use by multiple goroutines! var now = time.Now() func main() {@@ -442,7 +436,7 @@ honk.forget() // retry
continue } // remember only the first mess' response - honk.save(resp) + honk.save(resp.Result.MessageID) for _, mess := range messes[1:] { if _, err := mess.Send(); err != nil { log.Print(err) // error