-
-
Notifications
You must be signed in to change notification settings - Fork 57
feat: update memo when its source message is edited #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nord-winter
wants to merge
4
commits into
usememos:main
Choose a base branch
from
nord-winter:edit-mirror
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b07b41b
store: add message-to-memo mapping
nord-winter 32d3569
handle edited messages by updating their memo
nord-winter eae2efd
fix: scope message-to-memo mapping by chat
nord-winter 555766f
fix: allowlist check and save race in edit path
nord-winter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,153 @@ | ||
| package store | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "log/slog" | ||
| "os" | ||
| "path/filepath" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // messageKey scopes a Telegram message ID to its chat: message IDs are only | ||
| // unique within a chat, not globally. | ||
| type messageKey struct { | ||
| chatID int64 | ||
| messageID int64 | ||
| } | ||
|
|
||
| func (s *Store) GetMemoForMessage(chatID, messageID int64) (string, bool) { | ||
| memoName, ok := s.messageMemoCache.Load(messageKey{chatID: chatID, messageID: messageID}) | ||
| if !ok { | ||
| return "", false | ||
| } | ||
| return memoName.(string), true | ||
| } | ||
|
|
||
| func (s *Store) SetMemoForMessage(chatID, messageID int64, memoName string) { | ||
| s.saveMu.Lock() | ||
| defer s.saveMu.Unlock() | ||
|
|
||
| s.messageMemoCache.Store(messageKey{chatID: chatID, messageID: messageID}, memoName) | ||
| if err := s.saveMessageMemoMapToFile(); err != nil { | ||
| slog.Error("failed to save message memo map to file", "error", err) | ||
| } | ||
| } | ||
|
|
||
| func (s *Store) messageMemoMapDataPath() string { | ||
| return s.Data + ".messages" | ||
| } | ||
|
|
||
| func (s *Store) saveMessageMemoMapToFile() error { | ||
| entries := s.snapshotMessageMemoMap() | ||
| dataPath := s.messageMemoMapDataPath() | ||
| dataDir := filepath.Dir(dataPath) | ||
| tmpFile, err := os.CreateTemp(dataDir, "memogram-messages-*.tmp") | ||
| if err != nil { | ||
| return fmt.Errorf("create temp file: %w", err) | ||
| } | ||
| defer os.Remove(tmpFile.Name()) | ||
|
|
||
| writer := bufio.NewWriter(tmpFile) | ||
| for _, entry := range entries { | ||
| if _, err := fmt.Fprintf(writer, "%d:%d:%s\n", entry.key.chatID, entry.key.messageID, entry.memoName); err != nil { | ||
| tmpFile.Close() | ||
| return fmt.Errorf("write data file: %w", err) | ||
| } | ||
| } | ||
| if err := writer.Flush(); err != nil { | ||
| tmpFile.Close() | ||
| return fmt.Errorf("flush data file: %w", err) | ||
| } | ||
| if err := tmpFile.Sync(); err != nil { | ||
| tmpFile.Close() | ||
| return fmt.Errorf("sync data file: %w", err) | ||
| } | ||
| if err := tmpFile.Close(); err != nil { | ||
| return fmt.Errorf("close data file: %w", err) | ||
| } | ||
|
|
||
| if err := os.Rename(tmpFile.Name(), dataPath); err != nil { | ||
| return fmt.Errorf("replace data file: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *Store) loadMessageMemoMapFromFile() error { | ||
| dataPath := s.messageMemoMapDataPath() | ||
| if _, err := os.Stat(dataPath); os.IsNotExist(err) { | ||
| file, err := os.Create(dataPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer file.Close() | ||
| } | ||
|
|
||
| file, err := os.Open(dataPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer file.Close() | ||
|
|
||
| scanner := bufio.NewScanner(file) | ||
| for scanner.Scan() { | ||
| line := strings.TrimSpace(scanner.Text()) | ||
| if line == "" || strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
| key, memoName := parseMessageMemoLine(line) | ||
| if key.messageID == 0 || memoName == "" { | ||
| continue | ||
| } | ||
| s.messageMemoCache.Store(key, memoName) | ||
| } | ||
| return scanner.Err() | ||
| } | ||
|
|
||
| func parseMessageMemoLine(line string) (messageKey, string) { | ||
| parts := strings.SplitN(line, ":", 3) | ||
| if len(parts) != 3 { | ||
| return messageKey{}, "" | ||
| } | ||
| chatID, err := strconv.ParseInt(parts[0], 10, 64) | ||
| if err != nil { | ||
| return messageKey{}, "" | ||
| } | ||
| messageID, err := strconv.ParseInt(parts[1], 10, 64) | ||
| if err != nil { | ||
| return messageKey{}, "" | ||
| } | ||
| return messageKey{chatID: chatID, messageID: messageID}, parts[2] | ||
| } | ||
|
|
||
| type messageMemoEntry struct { | ||
| key messageKey | ||
| memoName string | ||
| } | ||
|
|
||
| func (s *Store) snapshotMessageMemoMap() []messageMemoEntry { | ||
| entries := make([]messageMemoEntry, 0) | ||
| s.messageMemoCache.Range(func(key, value interface{}) bool { | ||
| messageKey, ok := key.(messageKey) | ||
| if !ok { | ||
| return true | ||
| } | ||
| memoName, ok := value.(string) | ||
| if !ok { | ||
| return true | ||
| } | ||
| entries = append(entries, messageMemoEntry{key: messageKey, memoName: memoName}) | ||
| return true | ||
| }) | ||
|
|
||
| sort.Slice(entries, func(i, j int) bool { | ||
| if entries[i].key.chatID != entries[j].key.chatID { | ||
| return entries[i].key.chatID < entries[j].key.chatID | ||
| } | ||
| return entries[i].key.messageID < entries[j].key.messageID | ||
| }) | ||
|
|
||
| return entries | ||
| } | ||
This file contains hidden or 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,77 @@ | ||
| package store | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestSaveAndLoadMessageMemoMap(t *testing.T) { | ||
| dataPath := filepath.Join(t.TempDir(), "data.txt") | ||
|
|
||
| store := NewStore(dataPath) | ||
| if err := store.Init(); err != nil { | ||
| t.Fatalf("init store: %v", err) | ||
| } | ||
|
|
||
| store.SetMemoForMessage(1, 101, "memos/abc123") | ||
| store.SetMemoForMessage(1, 202, "memos/def456") | ||
|
|
||
| reloaded := NewStore(dataPath) | ||
| if err := reloaded.Init(); err != nil { | ||
| t.Fatalf("init reloaded store: %v", err) | ||
| } | ||
|
|
||
| memoName, ok := reloaded.GetMemoForMessage(1, 101) | ||
| if !ok || memoName != "memos/abc123" { | ||
| t.Fatalf("expected memos/abc123 for message 101, got %q", memoName) | ||
| } | ||
|
|
||
| memoName, ok = reloaded.GetMemoForMessage(1, 202) | ||
| if !ok || memoName != "memos/def456" { | ||
| t.Fatalf("expected memos/def456 for message 202, got %q", memoName) | ||
| } | ||
| } | ||
|
|
||
| func TestGetMemoForMessageMissing(t *testing.T) { | ||
| dataPath := filepath.Join(t.TempDir(), "data.txt") | ||
|
|
||
| store := NewStore(dataPath) | ||
| if err := store.Init(); err != nil { | ||
| t.Fatalf("init store: %v", err) | ||
| } | ||
|
|
||
| if _, ok := store.GetMemoForMessage(1, 999); ok { | ||
| t.Fatalf("expected no mapping for message 999") | ||
| } | ||
| } | ||
|
|
||
| func TestMessageMemoMapScopedByChat(t *testing.T) { | ||
| dataPath := filepath.Join(t.TempDir(), "data.txt") | ||
|
|
||
| store := NewStore(dataPath) | ||
| if err := store.Init(); err != nil { | ||
| t.Fatalf("init store: %v", err) | ||
| } | ||
|
|
||
| // Same message ID in two different chats must not collide: Telegram | ||
| // message IDs are only unique within a chat. | ||
| store.SetMemoForMessage(1, 5, "memos/chat-one") | ||
| store.SetMemoForMessage(2, 5, "memos/chat-two") | ||
|
|
||
| // Reload from disk to also cover the chatID:messageID:memoName persisted format, | ||
| // not just the in-memory cache. | ||
| reloaded := NewStore(dataPath) | ||
| if err := reloaded.Init(); err != nil { | ||
| t.Fatalf("init reloaded store: %v", err) | ||
| } | ||
|
|
||
| memoName, ok := reloaded.GetMemoForMessage(1, 5) | ||
| if !ok || memoName != "memos/chat-one" { | ||
| t.Fatalf("expected memos/chat-one for chat 1 message 5, got %q", memoName) | ||
| } | ||
|
|
||
| memoName, ok = reloaded.GetMemoForMessage(2, 5) | ||
| if !ok || memoName != "memos/chat-two" { | ||
| t.Fatalf("expected memos/chat-two for chat 2 message 5, got %q", memoName) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.