From 75c82d637da45d3fc8fff2733eca4040c97ab4d6 Mon Sep 17 00:00:00 2001 From: Dmitrii Shcherbakov Date: Wed, 6 Aug 2025 11:56:45 +0500 Subject: [PATCH] Fixed the error of losing the admin list. --- .mockery.yaml | 5 ++++ internal/app/geeksonator/app.go | 6 ++--- internal/observer/interface.go | 4 +-- internal/observer/manager.go | 8 ++---- internal/observer/manager_test.go | 38 ++++++++++++++++++--------- internal/observer/mocks/cache_mock.go | 28 ++++++++++---------- 6 files changed, 51 insertions(+), 38 deletions(-) diff --git a/.mockery.yaml b/.mockery.yaml index 5a542ea..d35e381 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -7,4 +7,9 @@ filename: "{{.InterfaceName | snakecase}}_mock.go" outpkg: "mocks" packages: geeksonator/internal/observer: + interfaces: + BotProvider: + Cache: geeksonator/internal/provider/telegram: + interfaces: + BotAPI: diff --git a/internal/app/geeksonator/app.go b/internal/app/geeksonator/app.go index 6b48520..8bd92a4 100644 --- a/internal/app/geeksonator/app.go +++ b/internal/app/geeksonator/app.go @@ -18,7 +18,7 @@ import ( ) const ( - cacheMaxSize = 1 + cacheMaxSize = 100 cacheTTL = 24 * time.Hour ) @@ -66,10 +66,10 @@ func Start() error { updatesChan := botAPI.GetUpdatesChan(updateConfig) - cache, err := cacher.NewCacher[string, []tgbotapi.ChatMember]( + cache, err := cacher.NewCacher[int64, []tgbotapi.ChatMember]( cacheMaxSize, cacheTTL, - cacher.WithDebug[string, []tgbotapi.ChatMember](logger), + cacher.WithDebug[int64, []tgbotapi.ChatMember](logger), ) if err != nil { return fmt.Errorf("cacher.NewCacher: %v", err) diff --git a/internal/observer/interface.go b/internal/observer/interface.go index 817b0b1..775fc97 100644 --- a/internal/observer/interface.go +++ b/internal/observer/interface.go @@ -19,8 +19,8 @@ type BotProvider interface { // Cache interface for cache. type Cache interface { // Get looks up a key's value from the cache. - Get(key string) (value []tgbotapi.ChatMember, ok bool) + Get(key int64) (value []tgbotapi.ChatMember, ok bool) // Set adds a value to the cache. - Set(key string, value []tgbotapi.ChatMember) error + Set(key int64, value []tgbotapi.ChatMember) error } diff --git a/internal/observer/manager.go b/internal/observer/manager.go index 081f56b..32241ab 100644 --- a/internal/observer/manager.go +++ b/internal/observer/manager.go @@ -9,10 +9,6 @@ import ( "go.uber.org/zap/zapcore" ) -const ( - cacheKey = "admins" -) - // Manager is manager for observer. type Manager struct { bot BotProvider @@ -169,7 +165,7 @@ func (m *Manager) log(msg string, fields ...zapcore.Field) { // getAdmins returns admins. func (m *Manager) getAdmins(chatCfg tgbotapi.ChatConfig) ([]tgbotapi.ChatMember, error) { - adminsFromCache, ok := m.cache.Get(cacheKey) + adminsFromCache, ok := m.cache.Get(chatCfg.ChatID) if ok { return adminsFromCache, nil } @@ -179,7 +175,7 @@ func (m *Manager) getAdmins(chatCfg tgbotapi.ChatConfig) ([]tgbotapi.ChatMember, return nil, fmt.Errorf("m.bot.GetChatAdministrators: %v", err) } - if err := m.cache.Set(cacheKey, admins); err != nil { + if err := m.cache.Set(chatCfg.ChatID, admins); err != nil { m.log("Set admins in cache", zap.Error(err), ) diff --git a/internal/observer/manager_test.go b/internal/observer/manager_test.go index 93caa62..55114ce 100644 --- a/internal/observer/manager_test.go +++ b/internal/observer/manager_test.go @@ -108,7 +108,7 @@ func TestManager_processingUpdate(t *testing.T) { cache := mocks.NewCacheMock(t) cache.EXPECT(). - Get(cacheKey). + Get(int64(300600)). Return( []tgbotapi.ChatMember{ { @@ -262,7 +262,7 @@ func TestManager_processingMessage(t *testing.T) { cache := mocks.NewCacheMock(t) cache.EXPECT(). - Get(cacheKey). + Get(int64(300600)). Return([]tgbotapi.ChatMember{ { User: &tgbotapi.User{ @@ -277,7 +277,9 @@ func TestManager_processingMessage(t *testing.T) { }, args: args{ message: &tgbotapi.Message{ - Chat: &tgbotapi.Chat{}, + Chat: &tgbotapi.Chat{ + ID: 300600, + }, From: &tgbotapi.User{ ID: 100500, }, @@ -293,7 +295,7 @@ func TestManager_processingMessage(t *testing.T) { cache := mocks.NewCacheMock(t) cache.EXPECT(). - Get(cacheKey). + Get(int64(300600)). Return( []tgbotapi.ChatMember{ { @@ -311,7 +313,9 @@ func TestManager_processingMessage(t *testing.T) { }, args: args{ message: &tgbotapi.Message{ - Chat: &tgbotapi.Chat{}, + Chat: &tgbotapi.Chat{ + ID: 300600, + }, From: &tgbotapi.User{ ID: 100500, }, @@ -327,7 +331,7 @@ func TestManager_processingMessage(t *testing.T) { cache := mocks.NewCacheMock(t) cache.EXPECT(). - Get(cacheKey). + Get(int64(300600)). Return( []tgbotapi.ChatMember{ { @@ -345,7 +349,9 @@ func TestManager_processingMessage(t *testing.T) { }, args: args{ message: &tgbotapi.Message{ - Chat: &tgbotapi.Chat{}, + Chat: &tgbotapi.Chat{ + ID: 300600, + }, From: &tgbotapi.User{ ID: 100501, }, @@ -554,7 +560,7 @@ func TestManager_getAdmins(t *testing.T) { cache := mocks.NewCacheMock(t) cache.EXPECT(). - Get(cacheKey). + Get(int64(300600)). Return([]tgbotapi.ChatMember{ { User: &tgbotapi.User{ @@ -568,7 +574,9 @@ func TestManager_getAdmins(t *testing.T) { } }, args: args{ - chatCfg: tgbotapi.ChatConfig{}, + chatCfg: tgbotapi.ChatConfig{ + ChatID: 300600, + }, }, wantResult: []tgbotapi.ChatMember{ { @@ -585,7 +593,9 @@ func TestManager_getAdmins(t *testing.T) { botProvider := mocks.NewBotProviderMock(t) botProvider.EXPECT(). - GetChatAdministrators(tgbotapi.ChatConfig{}). + GetChatAdministrators(tgbotapi.ChatConfig{ + ChatID: 300600, + }). Return( []tgbotapi.ChatMember{ { @@ -600,11 +610,11 @@ func TestManager_getAdmins(t *testing.T) { cache := mocks.NewCacheMock(t) cache.EXPECT(). - Get(cacheKey). + Get(int64(300600)). Return(nil, false) cache.EXPECT(). - Set(cacheKey, []tgbotapi.ChatMember{ + Set(int64(300600), []tgbotapi.ChatMember{ { User: &tgbotapi.User{ ID: 100500, @@ -619,7 +629,9 @@ func TestManager_getAdmins(t *testing.T) { } }, args: args{ - chatCfg: tgbotapi.ChatConfig{}, + chatCfg: tgbotapi.ChatConfig{ + ChatID: 300600, + }, }, wantResult: []tgbotapi.ChatMember{ { diff --git a/internal/observer/mocks/cache_mock.go b/internal/observer/mocks/cache_mock.go index 1fc8a37..2e7ee38 100644 --- a/internal/observer/mocks/cache_mock.go +++ b/internal/observer/mocks/cache_mock.go @@ -22,15 +22,15 @@ func (_m *CacheMock) EXPECT() *CacheMock_Expecter { } // Get provides a mock function with given fields: key -func (_m *CacheMock) Get(key string) ([]tgbotapi.ChatMember, bool) { +func (_m *CacheMock) Get(key int64) ([]tgbotapi.ChatMember, bool) { ret := _m.Called(key) var r0 []tgbotapi.ChatMember var r1 bool - if rf, ok := ret.Get(0).(func(string) ([]tgbotapi.ChatMember, bool)); ok { + if rf, ok := ret.Get(0).(func(int64) ([]tgbotapi.ChatMember, bool)); ok { return rf(key) } - if rf, ok := ret.Get(0).(func(string) []tgbotapi.ChatMember); ok { + if rf, ok := ret.Get(0).(func(int64) []tgbotapi.ChatMember); ok { r0 = rf(key) } else { if ret.Get(0) != nil { @@ -38,7 +38,7 @@ func (_m *CacheMock) Get(key string) ([]tgbotapi.ChatMember, bool) { } } - if rf, ok := ret.Get(1).(func(string) bool); ok { + if rf, ok := ret.Get(1).(func(int64) bool); ok { r1 = rf(key) } else { r1 = ret.Get(1).(bool) @@ -53,14 +53,14 @@ type CacheMock_Get_Call struct { } // Get is a helper method to define mock.On call -// - key string +// - key int64 func (_e *CacheMock_Expecter) Get(key interface{}) *CacheMock_Get_Call { return &CacheMock_Get_Call{Call: _e.mock.On("Get", key)} } -func (_c *CacheMock_Get_Call) Run(run func(key string)) *CacheMock_Get_Call { +func (_c *CacheMock_Get_Call) Run(run func(key int64)) *CacheMock_Get_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string)) + run(args[0].(int64)) }) return _c } @@ -70,17 +70,17 @@ func (_c *CacheMock_Get_Call) Return(value []tgbotapi.ChatMember, ok bool) *Cach return _c } -func (_c *CacheMock_Get_Call) RunAndReturn(run func(string) ([]tgbotapi.ChatMember, bool)) *CacheMock_Get_Call { +func (_c *CacheMock_Get_Call) RunAndReturn(run func(int64) ([]tgbotapi.ChatMember, bool)) *CacheMock_Get_Call { _c.Call.Return(run) return _c } // Set provides a mock function with given fields: key, value -func (_m *CacheMock) Set(key string, value []tgbotapi.ChatMember) error { +func (_m *CacheMock) Set(key int64, value []tgbotapi.ChatMember) error { ret := _m.Called(key, value) var r0 error - if rf, ok := ret.Get(0).(func(string, []tgbotapi.ChatMember) error); ok { + if rf, ok := ret.Get(0).(func(int64, []tgbotapi.ChatMember) error); ok { r0 = rf(key, value) } else { r0 = ret.Error(0) @@ -95,15 +95,15 @@ type CacheMock_Set_Call struct { } // Set is a helper method to define mock.On call -// - key string +// - key int64 // - value []tgbotapi.ChatMember func (_e *CacheMock_Expecter) Set(key interface{}, value interface{}) *CacheMock_Set_Call { return &CacheMock_Set_Call{Call: _e.mock.On("Set", key, value)} } -func (_c *CacheMock_Set_Call) Run(run func(key string, value []tgbotapi.ChatMember)) *CacheMock_Set_Call { +func (_c *CacheMock_Set_Call) Run(run func(key int64, value []tgbotapi.ChatMember)) *CacheMock_Set_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string), args[1].([]tgbotapi.ChatMember)) + run(args[0].(int64), args[1].([]tgbotapi.ChatMember)) }) return _c } @@ -113,7 +113,7 @@ func (_c *CacheMock_Set_Call) Return(_a0 error) *CacheMock_Set_Call { return _c } -func (_c *CacheMock_Set_Call) RunAndReturn(run func(string, []tgbotapi.ChatMember) error) *CacheMock_Set_Call { +func (_c *CacheMock_Set_Call) RunAndReturn(run func(int64, []tgbotapi.ChatMember) error) *CacheMock_Set_Call { _c.Call.Return(run) return _c }