Dismiss function
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
)
|
||||
|
||||
type DismissedStore struct {
|
||||
IDs map[string]bool `json:"ids"`
|
||||
path string
|
||||
}
|
||||
|
||||
func LoadDismissed(path string) (*DismissedStore, error) {
|
||||
s := &DismissedStore{
|
||||
IDs: make(map[string]bool),
|
||||
path: path,
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
return s, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := json.Unmarshal(data, s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.path = path
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *DismissedStore) Save() error {
|
||||
data, err := json.MarshalIndent(s, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(s.path, data, 0644)
|
||||
}
|
||||
|
||||
func (s *DismissedStore) Dismiss(id string) {
|
||||
s.IDs[id] = true
|
||||
}
|
||||
|
||||
func (s *DismissedStore) IsDismissed(id string) bool {
|
||||
return s.IDs[id]
|
||||
}
|
||||
Reference in New Issue
Block a user