1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
// Package store contains the local, non-secret Aegis cache.
package store
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"sync"
"time"
"aegis/internal/filter"
)
const (
stateVersion = 1
maxArticleBytes = 16 << 20
maxDraftBytes = 2 << 20
)
type Article struct {
Key string `json:"key"`
Group string `json:"group"`
Number int64 `json:"number"`
Subject string `json:"subject,omitempty"`
From string `json:"from,omitempty"`
MessageID string `json:"message_id,omitempty"`
Raw string `json:"raw,omitempty"`
Read bool `json:"read"`
Bookmarked bool `json:"bookmarked"`
Tags []string `json:"tags,omitempty"`
FetchedAt time.Time `json:"fetched_at"`
}
type Draft struct {
ID string `json:"id"`
Groups string `json:"groups,omitempty"`
From string `json:"from,omitempty"`
Subject string `json:"subject,omitempty"`
Body string `json:"body,omitempty"`
References string `json:"references,omitempty"`
FollowupTo string `json:"followup_to,omitempty"`
UpdatedAt time.Time `json:"updated_at"`
}
type State struct {
Version int `json:"version"`
Articles map[string]Article `json:"articles"`
Drafts map[string]Draft `json:"drafts"`
Filters []filter.Rule `json:"filters,omitempty"`
}
type Store struct {
mu sync.RWMutex
path string
state State
}
func DefaultPath() (string, error) {
dir, err := os.UserConfigDir()
if err != nil {
return "", fmt.Errorf("locate user configuration directory: %w", err)
}
return filepath.Join(dir, "aegis", "state.json"), nil
}
func Open(path string) (*Store, error) {
if path == "" {
return nil, errors.New("state path is required")
}
s := &Store{path: path, state: emptyState()}
data, err := os.ReadFile(path)
if errors.Is(err, os.ErrNotExist) {
return s, nil
}
if err != nil {
return nil, fmt.Errorf("read local state: %w", err)
}
if len(data) > 32<<20 {
return nil, errors.New("local state is too large")
}
if err := json.Unmarshal(data, &s.state); err != nil {
return nil, fmt.Errorf("decode local state: %w", err)
}
if s.state.Version == 0 {
s.state.Version = stateVersion
}
if s.state.Version != stateVersion {
return nil, fmt.Errorf("unsupported local state version %d", s.state.Version)
}
if s.state.Articles == nil {
s.state.Articles = make(map[string]Article)
}
if s.state.Drafts == nil {
s.state.Drafts = make(map[string]Draft)
}
return s, nil
}
func emptyState() State {
return State{Version: stateVersion, Articles: make(map[string]Article), Drafts: make(map[string]Draft)}
}
func (s *Store) Save() error {
s.mu.RLock()
data, err := json.MarshalIndent(s.state, "", " ")
s.mu.RUnlock()
if err != nil {
return fmt.Errorf("encode local state: %w", err)
}
dir := filepath.Dir(s.path)
if err := os.MkdirAll(dir, 0o700); err != nil {
return fmt.Errorf("create local state directory: %w", err)
}
if err := os.Chmod(dir, 0o700); err != nil {
return fmt.Errorf("protect local state directory: %w", err)
}
tmp, err := os.CreateTemp(dir, ".state-*.tmp")
if err != nil {
return fmt.Errorf("create local state temporary file: %w", err)
}
tmpName := tmp.Name()
defer os.Remove(tmpName)
if err := tmp.Chmod(0o600); err != nil {
_ = tmp.Close()
return err
}
if _, err := tmp.Write(append(data, '\n')); err != nil {
_ = tmp.Close()
return fmt.Errorf("write local state: %w", err)
}
if err := tmp.Sync(); err != nil {
_ = tmp.Close()
return fmt.Errorf("sync local state: %w", err)
}
if err := tmp.Close(); err != nil {
return err
}
if err := os.Rename(tmpName, s.path); err != nil {
return fmt.Errorf("commit local state: %w", err)
}
return nil
}
func (s *Store) Snapshot() State {
s.mu.RLock()
defer s.mu.RUnlock()
result := emptyState()
result.Filters = append(result.Filters, s.state.Filters...)
for key, article := range s.state.Articles {
article.Tags = append([]string(nil), article.Tags...)
result.Articles[key] = article
}
for key, draft := range s.state.Drafts {
result.Drafts[key] = draft
}
return result
}
func (s *Store) UpsertArticle(article Article) error {
if article.Key == "" {
return errors.New("article key is required")
}
if len(article.Raw) > maxArticleBytes {
return errors.New("article exceeds local cache limit")
}
s.mu.Lock()
if old, ok := s.state.Articles[article.Key]; ok {
article.Read = old.Read
article.Bookmarked = old.Bookmarked
if len(article.Tags) == 0 {
article.Tags = append([]string(nil), old.Tags...)
}
}
if article.FetchedAt.IsZero() {
article.FetchedAt = time.Now().UTC()
}
s.state.Articles[article.Key] = article
s.mu.Unlock()
return nil
}
func (s *Store) SetRead(key string, read bool) error {
s.mu.Lock()
defer s.mu.Unlock()
article, ok := s.state.Articles[key]
if !ok {
return errors.New("article not found")
}
article.Read = read
s.state.Articles[key] = article
return nil
}
func (s *Store) SetBookmarked(key string, bookmarked bool) error {
s.mu.Lock()
defer s.mu.Unlock()
article, ok := s.state.Articles[key]
if !ok {
return errors.New("article not found")
}
article.Bookmarked = bookmarked
s.state.Articles[key] = article
return nil
}
func (s *Store) SaveDraft(draft Draft) error {
if draft.ID == "" {
return errors.New("draft ID is required")
}
if len(draft.Body) > maxDraftBytes {
return errors.New("draft exceeds local size limit")
}
draft.UpdatedAt = time.Now().UTC()
s.mu.Lock()
s.state.Drafts[draft.ID] = draft
s.mu.Unlock()
return nil
}
func (s *Store) DeleteDraft(id string) {
s.mu.Lock()
delete(s.state.Drafts, id)
s.mu.Unlock()
}
func (s *Store) SetFilters(rules []filter.Rule) {
s.mu.Lock()
s.state.Filters = append([]filter.Rule(nil), rules...)
s.mu.Unlock()
}
|