Skip to content

Commit 75c82d6

Browse files
author
Dmitrii Shcherbakov
committed
Fixed the error of losing the admin list.
1 parent ed90563 commit 75c82d6

File tree

6 files changed

+51
-38
lines changed

6 files changed

+51
-38
lines changed

.mockery.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ filename: "{{.InterfaceName | snakecase}}_mock.go"
77
outpkg: "mocks"
88
packages:
99
geeksonator/internal/observer:
10+
interfaces:
11+
BotProvider:
12+
Cache:
1013
geeksonator/internal/provider/telegram:
14+
interfaces:
15+
BotAPI:

internal/app/geeksonator/app.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
const (
21-
cacheMaxSize = 1
21+
cacheMaxSize = 100
2222
cacheTTL = 24 * time.Hour
2323
)
2424

@@ -66,10 +66,10 @@ func Start() error {
6666

6767
updatesChan := botAPI.GetUpdatesChan(updateConfig)
6868

69-
cache, err := cacher.NewCacher[string, []tgbotapi.ChatMember](
69+
cache, err := cacher.NewCacher[int64, []tgbotapi.ChatMember](
7070
cacheMaxSize,
7171
cacheTTL,
72-
cacher.WithDebug[string, []tgbotapi.ChatMember](logger),
72+
cacher.WithDebug[int64, []tgbotapi.ChatMember](logger),
7373
)
7474
if err != nil {
7575
return fmt.Errorf("cacher.NewCacher: %v", err)

internal/observer/interface.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type BotProvider interface {
1919
// Cache interface for cache.
2020
type Cache interface {
2121
// Get looks up a key's value from the cache.
22-
Get(key string) (value []tgbotapi.ChatMember, ok bool)
22+
Get(key int64) (value []tgbotapi.ChatMember, ok bool)
2323

2424
// Set adds a value to the cache.
25-
Set(key string, value []tgbotapi.ChatMember) error
25+
Set(key int64, value []tgbotapi.ChatMember) error
2626
}

internal/observer/manager.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import (
99
"go.uber.org/zap/zapcore"
1010
)
1111

12-
const (
13-
cacheKey = "admins"
14-
)
15-
1612
// Manager is manager for observer.
1713
type Manager struct {
1814
bot BotProvider
@@ -169,7 +165,7 @@ func (m *Manager) log(msg string, fields ...zapcore.Field) {
169165

170166
// getAdmins returns admins.
171167
func (m *Manager) getAdmins(chatCfg tgbotapi.ChatConfig) ([]tgbotapi.ChatMember, error) {
172-
adminsFromCache, ok := m.cache.Get(cacheKey)
168+
adminsFromCache, ok := m.cache.Get(chatCfg.ChatID)
173169
if ok {
174170
return adminsFromCache, nil
175171
}
@@ -179,7 +175,7 @@ func (m *Manager) getAdmins(chatCfg tgbotapi.ChatConfig) ([]tgbotapi.ChatMember,
179175
return nil, fmt.Errorf("m.bot.GetChatAdministrators: %v", err)
180176
}
181177

182-
if err := m.cache.Set(cacheKey, admins); err != nil {
178+
if err := m.cache.Set(chatCfg.ChatID, admins); err != nil {
183179
m.log("Set admins in cache",
184180
zap.Error(err),
185181
)

internal/observer/manager_test.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestManager_processingUpdate(t *testing.T) {
108108
cache := mocks.NewCacheMock(t)
109109

110110
cache.EXPECT().
111-
Get(cacheKey).
111+
Get(int64(300600)).
112112
Return(
113113
[]tgbotapi.ChatMember{
114114
{
@@ -262,7 +262,7 @@ func TestManager_processingMessage(t *testing.T) {
262262
cache := mocks.NewCacheMock(t)
263263

264264
cache.EXPECT().
265-
Get(cacheKey).
265+
Get(int64(300600)).
266266
Return([]tgbotapi.ChatMember{
267267
{
268268
User: &tgbotapi.User{
@@ -277,7 +277,9 @@ func TestManager_processingMessage(t *testing.T) {
277277
},
278278
args: args{
279279
message: &tgbotapi.Message{
280-
Chat: &tgbotapi.Chat{},
280+
Chat: &tgbotapi.Chat{
281+
ID: 300600,
282+
},
281283
From: &tgbotapi.User{
282284
ID: 100500,
283285
},
@@ -293,7 +295,7 @@ func TestManager_processingMessage(t *testing.T) {
293295
cache := mocks.NewCacheMock(t)
294296

295297
cache.EXPECT().
296-
Get(cacheKey).
298+
Get(int64(300600)).
297299
Return(
298300
[]tgbotapi.ChatMember{
299301
{
@@ -311,7 +313,9 @@ func TestManager_processingMessage(t *testing.T) {
311313
},
312314
args: args{
313315
message: &tgbotapi.Message{
314-
Chat: &tgbotapi.Chat{},
316+
Chat: &tgbotapi.Chat{
317+
ID: 300600,
318+
},
315319
From: &tgbotapi.User{
316320
ID: 100500,
317321
},
@@ -327,7 +331,7 @@ func TestManager_processingMessage(t *testing.T) {
327331
cache := mocks.NewCacheMock(t)
328332

329333
cache.EXPECT().
330-
Get(cacheKey).
334+
Get(int64(300600)).
331335
Return(
332336
[]tgbotapi.ChatMember{
333337
{
@@ -345,7 +349,9 @@ func TestManager_processingMessage(t *testing.T) {
345349
},
346350
args: args{
347351
message: &tgbotapi.Message{
348-
Chat: &tgbotapi.Chat{},
352+
Chat: &tgbotapi.Chat{
353+
ID: 300600,
354+
},
349355
From: &tgbotapi.User{
350356
ID: 100501,
351357
},
@@ -554,7 +560,7 @@ func TestManager_getAdmins(t *testing.T) {
554560
cache := mocks.NewCacheMock(t)
555561

556562
cache.EXPECT().
557-
Get(cacheKey).
563+
Get(int64(300600)).
558564
Return([]tgbotapi.ChatMember{
559565
{
560566
User: &tgbotapi.User{
@@ -568,7 +574,9 @@ func TestManager_getAdmins(t *testing.T) {
568574
}
569575
},
570576
args: args{
571-
chatCfg: tgbotapi.ChatConfig{},
577+
chatCfg: tgbotapi.ChatConfig{
578+
ChatID: 300600,
579+
},
572580
},
573581
wantResult: []tgbotapi.ChatMember{
574582
{
@@ -585,7 +593,9 @@ func TestManager_getAdmins(t *testing.T) {
585593
botProvider := mocks.NewBotProviderMock(t)
586594

587595
botProvider.EXPECT().
588-
GetChatAdministrators(tgbotapi.ChatConfig{}).
596+
GetChatAdministrators(tgbotapi.ChatConfig{
597+
ChatID: 300600,
598+
}).
589599
Return(
590600
[]tgbotapi.ChatMember{
591601
{
@@ -600,11 +610,11 @@ func TestManager_getAdmins(t *testing.T) {
600610
cache := mocks.NewCacheMock(t)
601611

602612
cache.EXPECT().
603-
Get(cacheKey).
613+
Get(int64(300600)).
604614
Return(nil, false)
605615

606616
cache.EXPECT().
607-
Set(cacheKey, []tgbotapi.ChatMember{
617+
Set(int64(300600), []tgbotapi.ChatMember{
608618
{
609619
User: &tgbotapi.User{
610620
ID: 100500,
@@ -619,7 +629,9 @@ func TestManager_getAdmins(t *testing.T) {
619629
}
620630
},
621631
args: args{
622-
chatCfg: tgbotapi.ChatConfig{},
632+
chatCfg: tgbotapi.ChatConfig{
633+
ChatID: 300600,
634+
},
623635
},
624636
wantResult: []tgbotapi.ChatMember{
625637
{

internal/observer/mocks/cache_mock.go

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)