Skip to content

Commit

Permalink
chore: cleanup code (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekomeowww authored May 24, 2023
1 parent 5078822 commit 6d03b71
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 26 deletions.
3 changes: 1 addition & 2 deletions internal/bots/telegram/handlers/recap/recap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/nekomeowww/insights-bot/internal/models/tgchats"
"github.com/nekomeowww/insights-bot/pkg/bots/tgbot"
"github.com/nekomeowww/insights-bot/pkg/types/telegram"
"github.com/samber/lo"
"go.uber.org/fx"
)
Expand Down Expand Up @@ -99,7 +98,7 @@ func (h CommandHandler) CommandHelp() string {
}

func (h *CommandHandler) Handle(c *tgbot.Context) (tgbot.Response, error) {
enabled, err := h.tgchats.HasChatHistoriesRecapEnabled(c.Update.Message.Chat.ID, telegram.ChatType(c.Update.Message.Chat.Type), c.Update.Message.Chat.Title)
enabled, err := h.tgchats.HasChatHistoriesRecapEnabled(c.Update.Message.Chat.ID, c.Update.Message.Chat.Title)
if err != nil {
return nil, tgbot.NewExceptionError(err).WithMessage("生成失败,请稍后再试。").WithReply(c.Update.Message)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/bots/telegram/handlers/recap/toggle_recap.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (h *EnableRecapCommandHandler) Handle(c *tgbot.Context) (tgbot.Response, er
return nil, tgbot.NewExceptionError(err).WithMessage("聊天记录回顾功能开启失败,请稍后再试!").WithReply(c.Update.Message)
}

err = h.tgchats.QueueOneSendChatHistoriesRecapTaskForChatID(c.Update.Message.Chat.ID, telegram.ChatType(c.Update.Message.Chat.Type), c.Update.Message.Chat.Title)
err = h.tgchats.QueueOneSendChatHistoriesRecapTaskForChatID(c.Update.Message.Chat.ID)
if err != nil {
return nil, tgbot.NewExceptionError(err).WithMessage("聊天记录回顾功能开启失败,请稍后再试!").WithReply(c.Update.Message)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/bots/telegram/middlewares/record_messsages.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func RecordMessage(chatHistories *chathistories.Model, tgchats *tgchats.Model) f
return
}

enabled, err := tgchats.HasChatHistoriesRecapEnabled(c.Update.Message.Chat.ID, chatType, c.Update.Message.Chat.Title)
enabled, err := tgchats.HasChatHistoriesRecapEnabled(c.Update.Message.Chat.ID, c.Update.Message.Chat.Title)
if err != nil {
c.Logger.Error(err)
return
Expand Down
22 changes: 10 additions & 12 deletions internal/models/tgchats/feature_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"github.com/nekomeowww/insights-bot/pkg/types/timecapsules"
)

func (m *Model) findOneFeatureFlag(chatID int64, chatType telegram.ChatType, chatTitle string) (*ent.TelegramChatFeatureFlags, error) {
func (m *Model) findOneFeatureFlag(chatID int64, chatTitle string) (*ent.TelegramChatFeatureFlags, error) {
featureFlags, err := m.ent.TelegramChatFeatureFlags.
Query().
Where(
telegramchatfeatureflags.ChatID(chatID),
telegramchatfeatureflags.ChatType(string(chatType)),
telegramchatfeatureflags.ChatTypeIn(string(telegram.ChatTypeGroup), string(telegram.ChatTypeSuperGroup)),
).
First(context.Background())
if err != nil {
Expand All @@ -28,7 +28,7 @@ func (m *Model) findOneFeatureFlag(chatID int64, chatType telegram.ChatType, cha
return nil, err
}

if featureFlags.ChatTitle == "" {
if featureFlags.ChatTitle == "" && chatTitle != "" {
_, err = m.ent.TelegramChatFeatureFlags.
UpdateOne(featureFlags).
SetChatTitle(chatTitle).
Expand All @@ -46,7 +46,7 @@ func (m *Model) EnableChatHistoriesRecap(chatID int64, chatType telegram.ChatTyp
return nil
}

featureFlags, err := m.findOneFeatureFlag(chatID, chatType, chatTitle)
featureFlags, err := m.findOneFeatureFlag(chatID, chatTitle)
if err != nil {
return err
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func (m *Model) DisableChatHistoriesRecap(chatID int64, chatType telegram.ChatTy
return nil
}

featureFlags, err := m.findOneFeatureFlag(chatID, chatType, chatTitle)
featureFlags, err := m.findOneFeatureFlag(chatID, chatTitle)
if err != nil {
return err
}
Expand Down Expand Up @@ -120,8 +120,8 @@ func (m *Model) DisableChatHistoriesRecap(chatID int64, chatType telegram.ChatTy
return nil
}

func (m *Model) HasChatHistoriesRecapEnabled(chatID int64, chatType telegram.ChatType, chatTitle string) (bool, error) {
featureFlags, err := m.findOneFeatureFlag(chatID, chatType, chatTitle)
func (m *Model) HasChatHistoriesRecapEnabled(chatID int64, chatTitle string) (bool, error) {
featureFlags, err := m.findOneFeatureFlag(chatID, chatTitle)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -155,15 +155,15 @@ func (m *Model) QueueSendChatHistoriesRecapTask() {
}

for _, chat := range chats {
err = m.QueueOneSendChatHistoriesRecapTaskForChatID(chat.ChatID, telegram.ChatType(chat.ChatType), chat.ChatTitle)
err = m.QueueOneSendChatHistoriesRecapTaskForChatID(chat.ChatID)
if err != nil {
m.logger.Errorf("failed to queue send chat histories recap task: %v", err)
continue
}
}
}

func (m *Model) QueueOneSendChatHistoriesRecapTaskForChatID(chatID int64, chatType telegram.ChatType, chatTitle string) error {
func (m *Model) QueueOneSendChatHistoriesRecapTaskForChatID(chatID int64) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

Expand Down Expand Up @@ -194,9 +194,7 @@ func (m *Model) QueueOneSendChatHistoriesRecapTaskForChatID(chatID int64, chatTy
m.logger.Infof("scheduled one send chat histories recap task for %d at %s", chatID, schedule)

return m.digger.BuryUtil(ctx, timecapsules.AutoRecapCapsule{
ChatID: chatID,
ChatType: chatType,
ChatTitle: chatTitle,
ChatID: chatID,
}, schedule.UnixMilli())
}

Expand Down
4 changes: 2 additions & 2 deletions internal/models/tgchats/feature_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestChatHistoriesRecap(t *testing.T) {

assert.True(featureFlag.FeatureChatHistoriesRecap)

enabled, err := model.HasChatHistoriesRecapEnabled(chatID, telegram.ChatTypeGroup, "")
enabled, err := model.HasChatHistoriesRecapEnabled(chatID, "")
require.NoError(err)
assert.True(enabled)
})
Expand All @@ -56,7 +56,7 @@ func TestChatHistoriesRecap(t *testing.T) {
require.NotNil(featureFlag)
assert.False(featureFlag.FeatureChatHistoriesRecap)

enabled, err := model.HasChatHistoriesRecapEnabled(chatID, telegram.ChatTypeGroup, "")
enabled, err := model.HasChatHistoriesRecapEnabled(chatID, "")
require.NoError(err)
assert.False(enabled)
})
Expand Down
6 changes: 3 additions & 3 deletions internal/services/autorecap/autorecap.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (m *AutoRecapService) sendChatHistoriesRecap(

_, err := lo.Attempt(10, func(index int) error {
var err error
enabled, err = m.tgchats.HasChatHistoriesRecapEnabled(capsule.Payload.ChatID, capsule.Payload.ChatType, capsule.Payload.ChatTitle)
enabled, err = m.tgchats.HasChatHistoriesRecapEnabled(capsule.Payload.ChatID, "")
if err != nil {
m.logger.Errorf("failed to check chat histories recap enabled: %v", err)
return err
Expand All @@ -85,7 +85,7 @@ func (m *AutoRecapService) sendChatHistoriesRecap(
})
if err != nil {
// requeue if failed
err = m.tgchats.QueueOneSendChatHistoriesRecapTaskForChatID(capsule.Payload.ChatID, capsule.Payload.ChatType, capsule.Payload.ChatTitle)
err = m.tgchats.QueueOneSendChatHistoriesRecapTaskForChatID(capsule.Payload.ChatID)
if err != nil {
m.logger.Errorf("failed to queue one send chat histories recap task for chat %d: %v", capsule.Payload.ChatID, err)
}
Expand All @@ -99,7 +99,7 @@ func (m *AutoRecapService) sendChatHistoriesRecap(
}

// always requeue
err = m.tgchats.QueueOneSendChatHistoriesRecapTaskForChatID(capsule.Payload.ChatID, capsule.Payload.ChatType, capsule.Payload.ChatTitle)
err = m.tgchats.QueueOneSendChatHistoriesRecapTaskForChatID(capsule.Payload.ChatID)
if err != nil {
m.logger.Errorf("failed to queue one send chat histories recap task for chat %d: %v", capsule.Payload.ChatID, err)
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/types/timecapsules/auto_recap.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package timecapsules

import "github.com/nekomeowww/insights-bot/pkg/types/telegram"

type AutoRecapCapsule struct {
ChatID int64 `json:"chat_id"`
ChatType telegram.ChatType `json:"chat_type"`
ChatTitle string `json:"chat_title"`
ChatID int64 `json:"chat_id"`
}

0 comments on commit 6d03b71

Please sign in to comment.