telegram: align chatIDs and chatIDsFile format handling

This commit is contained in:
CrazyMax
2024-12-20 18:25:34 +01:00
parent 8678b1944a
commit a27d3f5019
2 changed files with 32 additions and 56 deletions

View File

@@ -49,10 +49,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
return errors.Wrap(err, "cannot retrieve token secret for Telegram notifier") return errors.Wrap(err, "cannot retrieve token secret for Telegram notifier")
} }
var cids []interface{} cids := c.cfg.ChatIDs
for _, cid := range c.cfg.ChatIDs {
cids = append(cids, cid)
}
cidsRaw, err := utl.GetSecret("", c.cfg.ChatIDsFile) cidsRaw, err := utl.GetSecret("", c.cfg.ChatIDsFile)
if err != nil { if err != nil {
return errors.Wrap(err, "cannot retrieve chat IDs secret for Telegram notifier") return errors.Wrap(err, "cannot retrieve chat IDs secret for Telegram notifier")
@@ -124,41 +121,32 @@ func (c *Client) Send(entry model.NotifEntry) error {
return nil return nil
} }
func parseChatIDs(entries []interface{}) ([]chatID, error) { func parseChatIDs(entries []string) ([]chatID, error) {
var chatIDs []chatID var chatIDs []chatID
for _, entry := range entries { for _, entry := range entries {
switch v := entry.(type) { parts := strings.Split(entry, ":")
case int: if len(parts) < 1 || len(parts) > 2 {
chatIDs = append(chatIDs, chatID{id: int64(v)}) return nil, errors.Errorf("invalid chat ID %q", entry)
case int64:
chatIDs = append(chatIDs, chatID{id: v})
case string:
parts := strings.Split(v, ":")
if len(parts) < 1 || len(parts) > 2 {
return nil, errors.Errorf("invalid chat ID %q", v)
}
id, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return nil, errors.Wrap(err, "invalid chat ID")
}
var topics []int64
if len(parts) == 2 {
topicParts := strings.Split(parts[1], ";")
for _, topicPart := range topicParts {
topic, err := strconv.ParseInt(topicPart, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "invalid topic %q for chat ID %d", topicPart, id)
}
topics = append(topics, topic)
}
}
chatIDs = append(chatIDs, chatID{
id: id,
topics: topics,
})
default:
return nil, errors.Errorf("invalid chat ID %v (type=%T)", entry, entry)
} }
id, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return nil, errors.Wrap(err, "invalid chat ID")
}
var topics []int64
if len(parts) == 2 {
topicParts := strings.Split(parts[1], ";")
for _, topicPart := range topicParts {
topic, err := strconv.ParseInt(topicPart, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "invalid topic %q for chat ID %d", topicPart, id)
}
topics = append(topics, topic)
}
}
chatIDs = append(chatIDs, chatID{
id: id,
topics: topics,
})
} }
return chatIDs, nil return chatIDs, nil
} }

View File

@@ -10,13 +10,13 @@ import (
func TestParseChatIDs(t *testing.T) { func TestParseChatIDs(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
entries []interface{} entries []string
expected []chatID expected []chatID
err error err error
}{ }{
{ {
name: "valid integers", name: "valid chat IDS",
entries: []interface{}{8547439, 1234567}, entries: []string{"8547439", "1234567"},
expected: []chatID{ expected: []chatID{
{id: 8547439}, {id: 8547439},
{id: 1234567}, {id: 1234567},
@@ -25,7 +25,7 @@ func TestParseChatIDs(t *testing.T) {
}, },
{ {
name: "valid strings with topics", name: "valid strings with topics",
entries: []interface{}{"567891234:25", "891256734:25;12"}, entries: []string{"567891234:25", "891256734:25;12"},
expected: []chatID{ expected: []chatID{
{id: 567891234, topics: []int64{25}}, {id: 567891234, topics: []int64{25}},
{id: 891256734, topics: []int64{25, 12}}, {id: 891256734, topics: []int64{25, 12}},
@@ -34,37 +34,25 @@ func TestParseChatIDs(t *testing.T) {
}, },
{ {
name: "invalid format", name: "invalid format",
entries: []interface{}{"invalid_format"}, entries: []string{"invalid_format"},
expected: nil, expected: nil,
err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "invalid_format": invalid syntax`), err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "invalid_format": invalid syntax`),
}, },
{
name: "invalid type",
entries: []interface{}{true},
expected: nil,
err: errors.New("invalid chat ID true (type=bool)"),
},
{ {
name: "empty string", name: "empty string",
entries: []interface{}{""}, entries: []string{""},
expected: nil, expected: nil,
err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "": invalid syntax`), err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "": invalid syntax`),
}, },
{ {
name: "string with invalid topic", name: "string with invalid topic",
entries: []interface{}{"567891234:invalid"}, entries: []string{"567891234:invalid"},
expected: nil, expected: nil,
err: errors.New(`invalid topic "invalid" for chat ID 567891234: strconv.ParseInt: parsing "invalid": invalid syntax`), err: errors.New(`invalid topic "invalid" for chat ID 567891234: strconv.ParseInt: parsing "invalid": invalid syntax`),
}, },
{
name: "mixed valid and invalid entries",
entries: []interface{}{8547439, "567891234:25", "invalid_format", true},
expected: nil,
err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "invalid_format": invalid syntax`),
},
{ {
name: "invalid format with too many parts", name: "invalid format with too many parts",
entries: []interface{}{"567891234:25:extra"}, entries: []string{"567891234:25:extra"},
expected: nil, expected: nil,
err: errors.New(`invalid chat ID "567891234:25:extra"`), err: errors.New(`invalid chat ID "567891234:25:extra"`),
}, },