small refactoring message cleaning
All checks were successful
continuous-integration/drone/pr Build is passing

This commit is contained in:
Timofey.Kovalev
2021-06-25 15:56:35 +03:00
parent 456982e82c
commit 12fce31ed8
7 changed files with 213 additions and 296 deletions

View File

@ -20,7 +20,7 @@ const (
ctxKeyMessageCleaner
ctxKeyDataBase
ctxKeyPlayersBoard
ctxKeyFragMessages
ctxKeyMessageStorage
)
func createLogger(debug bool) *logrus.Logger {
@ -44,17 +44,16 @@ func createLogger(debug bool) *logrus.Logger {
}
}
func createContext(config *Config, bot *tgbotapi.BotAPI, mc *messageCleaner, ch *commandHandler, db *dbLayer, pb *playersBoard, fms *fragMessages) (context.Context, func()) {
func createContext(config *Config, bot *tgbotapi.BotAPI, ch *commandHandler, db *dbLayer, pb *playersBoard, ms *messageStorage) (context.Context, func()) {
ctx, cancelFunc := context.WithCancel(context.Background())
ctx = context.WithValue(ctx, ctxKeyLogger, createLogger(true))
ctx = context.WithValue(ctx, ctxKeyBotApi, bot)
ctx = context.WithValue(ctx, ctxKeyConfig, config)
ctx = context.WithValue(ctx, ctxKeyMessageCleaner, mc)
ctx = context.WithValue(ctx, ctxKeyCommandHandler, ch)
ctx = context.WithValue(ctx, ctxKeyDataBase, db)
ctx = context.WithValue(ctx, ctxKeyPlayersBoard, pb)
ctx = context.WithValue(ctx, ctxKeyFragMessages, fms)
ctx = context.WithValue(ctx, ctxKeyMessageStorage, ms)
return ctx, cancelFunc
}
@ -86,15 +85,6 @@ func getConfig(ctx context.Context) *Config {
panic("Failed to get config from ctx")
}
func getMessageCleaner(ctx context.Context) *messageCleaner {
v := ctx.Value(ctxKeyMessageCleaner)
if mc, ok := v.(*messageCleaner); ok {
return mc
}
panic("Failed to get message cleaner from ctx")
}
func getCommandHandler(ctx context.Context) *commandHandler {
v := ctx.Value(ctxKeyCommandHandler)
if ch, ok := v.(*commandHandler); ok {
@ -122,11 +112,11 @@ func getDPlayersBoard(ctx context.Context) *playersBoard {
panic("Failed to get players board from ctx")
}
func getFragMessages(ctx context.Context) *fragMessages {
v := ctx.Value(ctxKeyFragMessages)
if fms, ok := v.(*fragMessages); ok {
return fms
func getMessageStorage(ctx context.Context) *messageStorage {
v := ctx.Value(ctxKeyMessageStorage)
if ms, ok := v.(*messageStorage); ok {
return ms
}
panic("Failed to get frag messages from ctx")
panic("Failed to get message storage from ctx")
}