Skip to content

Commit

Permalink
fix cli tests and fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf committed Jun 27, 2024
1 parent ba0f0c0 commit a156c80
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
5 changes: 2 additions & 3 deletions cmd/cli/telegram_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ func downgradeTelegramUsersRecords(logger moira.Logger, database moira.Database)

var newValue string

chat := telegram.Chat{}
err = json.Unmarshal([]byte(oldValue), &chat)
if err != nil {
chat := &telegram.Chat{}
if err = json.Unmarshal([]byte(oldValue), chat); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/telegram_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func createNewTelegramUserRecords(database moira.Database) {
ctx := d.Context()

client.Set(ctx, "moira-telegram-users:some telegram group", `{"type":"group","chatId":-1001494975744}`, -1)
client.Set(ctx, "moira-telegram-users:@durov", `{"type":"private","chatId":1`, -1)
client.Set(ctx, "moira-telegram-users:@durov", `{"type":"private","chatId":1}`, -1)
client.Set(ctx, "moira-telegram-users:moira-bot-host:123", "D4VdnzZDTS/xXF87THARWw==", -1)
}
}
12 changes: 6 additions & 6 deletions senders/telegram/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ func (sender *Sender) getChatFromDb(contactValue string) (*Chat, error) {
return nil, fmt.Errorf("failed to get username chat: %w", err)
}

chat := Chat{}
if err := json.Unmarshal([]byte(chatRaw), &chat); err != nil {
chat := &Chat{}
if err := json.Unmarshal([]byte(chatRaw), chat); err != nil {
// For Moira < 2.12.0 compatibility
// Before 2.12.0 `moira-telegram-users:user` only stored telegram channel IDs
// After 2.12.0 `moira-telegram-users:user` stores Chat structure
if errors.As(err, &unmarshalTypeError) {
chatID, err := strconv.ParseInt(chatRaw, 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse chatRaw: %s as int64: %w", chatRaw, err)
chatID, parseErr := strconv.ParseInt(chatRaw, 10, 64)
if parseErr != nil {
return nil, fmt.Errorf("failed to parse chatRaw: %s as int64: %w", chatRaw, parseErr)
}

return &Chat{
Expand All @@ -174,7 +174,7 @@ func (sender *Sender) getChatFromDb(contactValue string) (*Chat, error) {
return nil, fmt.Errorf("failed to unmarshal chat data %s: %w", chatRaw, err)
}

return &chat, nil
return chat, nil
}

func (sender *Sender) getChatFromTelegram(username string) (*Chat, error) {
Expand Down

0 comments on commit a156c80

Please sign in to comment.