store.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package email
  2. /*
  3. store.go
  4. Persistence. Mail data lives in its own key-value file (system/mail/mail.db)
  5. rather than the shared ao.db, so the generic AGI database helpers that any
  6. script can call never reach another user's account list or contacts.
  7. Records are keyed "<owner>/<id>" so one user's data is always found by
  8. prefix and can never be addressed from another account.
  9. */
  10. import (
  11. "encoding/json"
  12. "errors"
  13. "sort"
  14. "strings"
  15. "sync"
  16. )
  17. const (
  18. tableAccounts = "accounts"
  19. tableSettings = "settings"
  20. tableContacts = "contacts"
  21. tableLabels = "labels"
  22. tableLabelMap = "labelmap"
  23. tableSnooze = "snooze"
  24. tableOutbox = "outbox"
  25. tableConfig = "config"
  26. adminConfigKey = "admin"
  27. )
  28. // KVStore is the slice of mod/database the mail backend needs. Declared as an
  29. // interface so tests can run against a throwaway bolt file.
  30. type KVStore interface {
  31. NewTable(tableName string) error
  32. Write(tableName string, key string, value interface{}) error
  33. Read(tableName string, key string, assignee interface{}) error
  34. KeyExists(tableName string, key string) bool
  35. Delete(tableName string, key string) error
  36. ListTable(tableName string) ([][][]byte, error)
  37. }
  38. // store wraps the KV file with typed helpers.
  39. type store struct {
  40. db KVStore
  41. mutex sync.Mutex //Serialises read-modify-write sequences
  42. }
  43. func newStore(db KVStore) (*store, error) {
  44. if db == nil {
  45. return nil, errors.New("mail store requires a database")
  46. }
  47. for _, table := range []string{tableAccounts, tableSettings, tableContacts, tableLabels,
  48. tableLabelMap, tableSnooze, tableOutbox, tableConfig} {
  49. if err := db.NewTable(table); err != nil {
  50. return nil, err
  51. }
  52. }
  53. return &store{db: db}, nil
  54. }
  55. func ownerKey(owner string, id string) string {
  56. return owner + "/" + id
  57. }
  58. // listOwned decodes every record of a table that belongs to owner.
  59. func listOwned[T any](s *store, table string, owner string) ([]T, error) {
  60. entries, err := s.db.ListTable(table)
  61. if err != nil {
  62. return nil, err
  63. }
  64. prefix := owner + "/"
  65. results := []T{}
  66. for _, entry := range entries {
  67. if len(entry) < 2 || !strings.HasPrefix(string(entry[0]), prefix) {
  68. continue
  69. }
  70. var record T
  71. if err := json.Unmarshal(entry[1], &record); err != nil {
  72. continue
  73. }
  74. results = append(results, record)
  75. }
  76. return results, nil
  77. }
  78. /*
  79. Accounts
  80. */
  81. func (s *store) listAccounts(owner string) ([]*Account, error) {
  82. records, err := listOwned[Account](s, tableAccounts, owner)
  83. if err != nil {
  84. return nil, err
  85. }
  86. accounts := make([]*Account, 0, len(records))
  87. for i := range records {
  88. if records[i].Owner != owner {
  89. continue
  90. }
  91. accounts = append(accounts, &records[i])
  92. }
  93. sort.SliceStable(accounts, func(i, j int) bool {
  94. if accounts[i].Order != accounts[j].Order {
  95. return accounts[i].Order < accounts[j].Order
  96. }
  97. return accounts[i].Created < accounts[j].Created
  98. })
  99. return accounts, nil
  100. }
  101. func (s *store) getAccount(owner string, id string) (*Account, error) {
  102. key := ownerKey(owner, id)
  103. if id == "" || !s.db.KeyExists(tableAccounts, key) {
  104. return nil, ErrAccountNotFound
  105. }
  106. account := Account{}
  107. if err := s.db.Read(tableAccounts, key, &account); err != nil {
  108. return nil, err
  109. }
  110. if account.Owner != owner || account.ID != id {
  111. return nil, ErrAccountNotFound
  112. }
  113. return &account, nil
  114. }
  115. func (s *store) saveAccount(account *Account) error {
  116. if account.Owner == "" || account.ID == "" {
  117. return errors.New("account owner and id are required")
  118. }
  119. return s.db.Write(tableAccounts, ownerKey(account.Owner, account.ID), account)
  120. }
  121. func (s *store) deleteAccount(owner string, id string) error {
  122. key := ownerKey(owner, id)
  123. if !s.db.KeyExists(tableAccounts, key) {
  124. return ErrAccountNotFound
  125. }
  126. return s.db.Delete(tableAccounts, key)
  127. }
  128. /*
  129. Settings
  130. */
  131. func (s *store) getSettings(owner string) UserSettings {
  132. settings := DefaultUserSettings()
  133. if s.db.KeyExists(tableSettings, owner) {
  134. stored := DefaultUserSettings()
  135. if err := s.db.Read(tableSettings, owner, &stored); err == nil {
  136. settings = stored
  137. }
  138. }
  139. settings.normalise()
  140. return settings
  141. }
  142. func (s *store) saveSettings(owner string, settings UserSettings) error {
  143. settings.normalise()
  144. return s.db.Write(tableSettings, owner, settings)
  145. }
  146. /*
  147. Admin configuration
  148. */
  149. func (s *store) getAdminConfig() AdminConfig {
  150. config := defaultAdminConfig()
  151. if s.db.KeyExists(tableConfig, adminConfigKey) {
  152. stored := defaultAdminConfig()
  153. if err := s.db.Read(tableConfig, adminConfigKey, &stored); err == nil {
  154. config = stored
  155. }
  156. }
  157. config.normalise()
  158. return config
  159. }
  160. func (s *store) saveAdminConfig(config AdminConfig) error {
  161. config.normalise()
  162. return s.db.Write(tableConfig, adminConfigKey, config)
  163. }
  164. // deleteOwnedWithPrefix removes every record of owner whose id starts with
  165. // prefix (used when an account is removed).
  166. func (s *store) deleteOwnedWithPrefix(table string, owner string, idPrefix string) {
  167. entries, err := s.db.ListTable(table)
  168. if err != nil {
  169. return
  170. }
  171. prefix := owner + "/" + idPrefix
  172. for _, entry := range entries {
  173. if len(entry) < 1 {
  174. continue
  175. }
  176. key := string(entry[0])
  177. if strings.HasPrefix(key, prefix) {
  178. s.db.Delete(table, key)
  179. }
  180. }
  181. }