-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
233 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package events | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// Locator stores messages for a given time period. | ||
// It is used to locate the message in the chat by its hash. | ||
// Useful to match messages from admin chat (only text available) to the original message. | ||
// Note: it is not thread-safe, use it from a single goroutine only. | ||
type Locator struct { | ||
ttl time.Duration // how long to keep messages | ||
data map[string]MsgMeta // message hash -> message meta | ||
lastRemoval time.Time // last time cleanup was performed | ||
cleanupDuration time.Duration // how often to perform cleanup | ||
} | ||
|
||
// MsgMeta stores message metadata | ||
type MsgMeta struct { | ||
time time.Time | ||
chatID int64 | ||
userID int64 | ||
msgID int | ||
} | ||
|
||
func (m MsgMeta) String() string { | ||
return fmt.Sprintf("{chatID: %d, userID: %d, msgID: %d, time: %s}", m.chatID, m.userID, m.msgID, m.time.Format(time.RFC3339)) | ||
} | ||
|
||
// NewLocator creates new Locator | ||
func NewLocator(ttl time.Duration) *Locator { | ||
return &Locator{ | ||
ttl: ttl, | ||
data: make(map[string]MsgMeta), | ||
lastRemoval: time.Now(), | ||
cleanupDuration: 5 * time.Minute, | ||
} | ||
} | ||
|
||
// Get returns message MsgMeta for give msg | ||
func (l *Locator) Get(msg string) (MsgMeta, bool) { | ||
hash := l.MsgHash(msg) | ||
res, ok := l.data[hash] | ||
return res, ok | ||
} | ||
|
||
// MsgHash returns sha256 hash of a message | ||
func (l *Locator) MsgHash(msg string) string { | ||
return fmt.Sprintf("%x", sha256.Sum256([]byte(msg))) | ||
} | ||
|
||
// Add adds message to the locator | ||
func (l *Locator) Add(msg string, chatID, userID int64, msgID int) { | ||
l.data[l.MsgHash(msg)] = MsgMeta{ | ||
time: time.Now(), | ||
chatID: chatID, | ||
userID: userID, | ||
msgID: msgID, | ||
} | ||
|
||
if time.Since(l.lastRemoval) < l.cleanupDuration { | ||
return | ||
} | ||
|
||
// remove old messages | ||
for k, v := range l.data { | ||
if time.Since(v.time) > l.ttl { | ||
delete(l.data, k) | ||
} | ||
} | ||
l.lastRemoval = time.Now() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package events | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewLocator(t *testing.T) { | ||
ttl := 10 * time.Minute | ||
locator := NewLocator(ttl) | ||
require.NotNil(t, locator) | ||
|
||
assert.Equal(t, ttl, locator.ttl) | ||
assert.NotZero(t, locator.cleanupDuration) | ||
assert.NotNil(t, locator.data) | ||
assert.WithinDuration(t, time.Now(), locator.lastRemoval, time.Second) | ||
} | ||
|
||
func TestGet(t *testing.T) { | ||
locator := NewLocator(10 * time.Minute) | ||
|
||
// adding a message | ||
msg := "test message" | ||
chatID := int64(123) | ||
userID := int64(456) | ||
msgID := 7890 | ||
locator.Add(msg, chatID, userID, msgID) | ||
|
||
// test retrieval of existing message | ||
info, found := locator.Get("test message") | ||
require.True(t, found) | ||
assert.Equal(t, msgID, info.msgID) | ||
assert.Equal(t, chatID, info.chatID) | ||
assert.Equal(t, userID, info.userID) | ||
|
||
// test retrieval of non-existing message | ||
_, found = locator.Get("no such message") // non-existing msgID | ||
assert.False(t, found) | ||
} | ||
|
||
func TestMsgHash(t *testing.T) { | ||
locator := NewLocator(10 * time.Minute) | ||
|
||
t.Run("hash for empty message", func(t *testing.T) { | ||
hash := locator.MsgHash("") | ||
assert.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hash) | ||
}) | ||
|
||
t.Run("hash for non-empty message", func(t *testing.T) { | ||
hash := locator.MsgHash("test message") | ||
assert.Equal(t, "3f0a377ba0a4a460ecb616f6507ce0d8cfa3e704025d4fda3ed0c5ca05468728", hash) | ||
}) | ||
|
||
t.Run("hash for different non-empty message", func(t *testing.T) { | ||
hash := locator.MsgHash("test message blah") | ||
assert.Equal(t, "21b7035e5ab5664eb7571b1f63d96951d5554a5465302b9cdd2e3de510eda6d8", hash) | ||
}) | ||
} | ||
|
||
func TestAddAndCleanup(t *testing.T) { | ||
ttl := 2 * time.Second | ||
cleanupDuration := 1 * time.Second | ||
locator := NewLocator(ttl) | ||
locator.cleanupDuration = cleanupDuration | ||
|
||
// Adding a message | ||
msg := "test message" | ||
chatID := int64(123) | ||
userID := int64(456) | ||
msgID := 7890 | ||
locator.Add(msg, chatID, userID, msgID) | ||
|
||
hash := locator.MsgHash(msg) | ||
meta, exists := locator.data[hash] | ||
require.True(t, exists) | ||
assert.Equal(t, chatID, meta.chatID) | ||
assert.Equal(t, userID, meta.userID) | ||
assert.Equal(t, msgID, meta.msgID) | ||
|
||
// wait for cleanup duration and add another message to trigger cleanup | ||
time.Sleep(cleanupDuration + time.Second) | ||
locator.Add("another message", 789, 555, 1011) | ||
|
||
_, existsAfterCleanup := locator.data[hash] | ||
assert.False(t, existsAfterCleanup) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters