From 7815009318e7857dd5040582cf07c05dd7f2c530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Sun, 10 Mar 2024 21:15:51 +0100 Subject: [PATCH] (hopefully) fix not all chatIds being used --- filters.go | 2 +- filters_test.go | 10 +++++----- main.go | 16 ++++++++-------- message_contents.go | 3 --- sender_worker.go | 12 ------------ templates_test.go | 2 +- 6 files changed, 15 insertions(+), 30 deletions(-) diff --git a/filters.go b/filters.go index 8910d0e..a262852 100644 --- a/filters.go +++ b/filters.go @@ -89,7 +89,7 @@ func FilterByLastSeen(ev <-chan satel.Event, wg *sync.WaitGroup, dataStore *Data func appendToGenericMessage(msg *GenericMessage, new *GenericMessage) *GenericMessage { if msg == nil { - msg = &GenericMessage{new.ChatIds, make([]satel.BasicEventElement, 0)} + msg = &GenericMessage{make([]satel.BasicEventElement, 0)} } throughNewMessages: diff --git a/filters_test.go b/filters_test.go index 370f144..8580c0c 100644 --- a/filters_test.go +++ b/filters_test.go @@ -251,12 +251,12 @@ func TestThrottle(t *testing.T) { wg.Done() }() - testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest1}} - testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest2}} - testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest3}} + testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest1}} + testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest2}} + testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest3}} *mockSleeper.ch <- nil - testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest4}} + testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest4}} close(testEvents) wg.Wait() @@ -272,7 +272,7 @@ func TestThrottle(t *testing.T) { } func makeMassiveEvent(element satel.BasicEventElement, numElements int) GenericMessage { - retval := GenericMessage{TgChatId{123}, make([]satel.BasicEventElement, 0)} + retval := GenericMessage{make([]satel.BasicEventElement, 0)} for i := 0; i < numElements; i++ { retval.Messages = append(retval.Messages, element) diff --git a/main.go b/main.go index f0da7a7..c7a8359 100644 --- a/main.go +++ b/main.go @@ -22,18 +22,18 @@ const ( ) type TgSender struct { - bot *tgbotapi.BotAPI - s SatelNameGetter - logger *log.Logger + bot *tgbotapi.BotAPI + s SatelNameGetter + logger *log.Logger + chatIds []int64 } func (self TgSender) Send(msg GenericMessage, tpl *template.Template) error { - chatIds := msg.ChatIds.GetTgIds() - if chatIds == nil { + if len(self.chatIds) == 0 { return nil } message := msg.Format(tpl, self.s, self.logger) - for _, chatId := range *chatIds { + for _, chatId := range self.chatIds { toSend := tgbotapi.NewMessage(chatId, message) toSend.ParseMode = "HTML" _, err := self.bot.Send(toSend) @@ -45,7 +45,7 @@ func (self TgSender) Send(msg GenericMessage, tpl *template.Template) error { } func sendTgMessage(tgEvents chan GenericMessage, msg []satel.BasicEventElement, chatId int64) { - tgEvents <- GenericMessage{TgChatId{chatId}, msg} + tgEvents <- GenericMessage{msg} } type RealSleeper struct { @@ -144,7 +144,7 @@ func main() { } logger.Print("Created Telegram Bot API client") - tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds)} + tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), chatIds} tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate)) diff --git a/message_contents.go b/message_contents.go index f26fb5a..c8c1e97 100644 --- a/message_contents.go +++ b/message_contents.go @@ -32,12 +32,10 @@ type SatelMsgContent struct { } type GenericMessage struct { - ChatIds ChatId Messages []satel.BasicEventElement } type SatelMessage struct { - ChatIds ChatId Messages []SatelMsgContent } @@ -66,7 +64,6 @@ func (self SatelMsgContent) GetName() string { func (self GenericMessage) Format(template *template.Template, s SatelNameGetter, logger *log.Logger) string { b := strings.Builder{} template.Execute(&b, SatelMessage{ - self.ChatIds, getSatelMessageContent(self.Messages, s, logger), }) return b.String() diff --git a/sender_worker.go b/sender_worker.go index c898e95..477db87 100644 --- a/sender_worker.go +++ b/sender_worker.go @@ -22,18 +22,6 @@ type Sleeper interface { Sleep(ch chan<- interface{}) } -type ChatId interface { - GetTgIds() *[]int64 -} - -type TgChatId struct { - tgChatId int64 -} - -func (self TgChatId) GetTgIds() *[]int64 { - return &[]int64{self.tgChatId} -} - func Consume(events <-chan GenericMessage) { go func() { for range events { diff --git a/templates_test.go b/templates_test.go index 91ab4a4..4a3cd22 100644 --- a/templates_test.go +++ b/templates_test.go @@ -47,7 +47,7 @@ func TestTelegramTemplate(t *testing.T) { tpl, err := template.New("TestTemplate").Parse(TelegramMessageTemplate) assert.NoError(t, err) Consume(SendToTg(testEvents, &mockSender, &wg, log.New(io.Discard, "", log.Ltime), tpl)) - testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest1, tplMessageTest2}} + testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest1, tplMessageTest2}} close(testEvents) wg.Wait()