-
Notifications
You must be signed in to change notification settings - Fork 2
/
user_address.go
240 lines (191 loc) · 6.24 KB
/
user_address.go
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
233
234
235
236
237
238
239
240
// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package store
import (
"encoding/json"
"fmt"
"github.com/ljanyst/peroxide/pkg/pmapi"
"github.com/pkg/errors"
bolt "go.etcd.io/bbolt"
)
// GetAddress returns the store address by given ID.
func (store *Store) GetAddress(addressID string) (*Address, error) {
store.lock.RLock()
defer store.lock.RUnlock()
storeAddress, ok := store.addresses[addressID]
if !ok {
return nil, fmt.Errorf("addressID %v does not exist", addressID)
}
return storeAddress, nil
}
// RebuildMailboxes truncates all mailbox buckets and recreates them from the metadata bucket again.
func (store *Store) RebuildMailboxes() (err error) {
store.lock.Lock()
defer store.lock.Unlock()
log.WithField("user", store.UserID()).Trace("Truncating mailboxes")
store.addresses = nil
if err = store.truncateMailboxesBucket(); err != nil {
log.WithError(err).Error("Could not truncate mailboxes bucket")
return
}
if err = store.truncateAddressInfoBucket(); err != nil {
log.WithError(err).Error("Could not truncate address info bucket")
return
}
if err = store.init(false); err != nil {
log.WithError(err).Error("Could not init store")
return
}
if err := store.increaseMailboxesVersion(); err != nil {
log.WithError(err).Error("Could not increase structure version")
// Do not return here. The truncation was already done and mode
// was changed in DB so we need to sync so that users start to see
// messages and not block other operations.
}
log.WithField("user", store.UserID()).Trace("Rebuilding mailboxes")
return store.initMailboxesBucket()
}
// createOrDeleteAddressesEvent creates address objects in the store for each necessary address
// and deletes any address objects that shouldn't be there.
// It doesn't do anything to addresses that are rightfully there.
// It should only be called from the event loop.
func (store *Store) createOrDeleteAddressesEvent() (err error) {
store.lock.Lock()
defer store.lock.Unlock()
labels, err := store.initCounts()
if err != nil {
return errors.Wrap(err, "failed to initialise label counts")
}
addrInfo, err := store.GetAddressInfo()
if err != nil {
return errors.Wrap(err, "failed to get addresses and address IDs")
}
// We need at least one address to continue.
if len(addrInfo) < 1 {
return errors.New("no addresses to initialise")
}
// If in combined mode, we only need the user's primary address.
if store.addressMode == combinedMode {
addrInfo = addrInfo[:1]
}
// Go through all addresses that *should* be there.
for _, addr := range addrInfo {
if _, ok := store.addresses[addr.AddressID]; ok {
continue
}
// This address is missing so we create it.
if err = store.addAddress(addr.Address, addr.AddressID, labels); err != nil {
return errors.Wrap(err, "failed to add address to store")
}
}
// Go through all addresses that *should not* be there.
for _, addr := range store.addresses {
belongs := false
for _, a := range addrInfo {
if addr.addressID == a.AddressID {
belongs = true
break
}
}
if belongs {
continue
}
delete(store.addresses, addr.addressID)
}
if err = store.truncateMailboxesBucket(); err != nil {
log.WithError(err).Error("Could not truncate mailboxes bucket")
return
}
return store.initMailboxesBucket()
}
// truncateAddressInfoBucket removes the address info bucket.
func (store *Store) truncateAddressInfoBucket() (err error) {
log.Trace("Truncating address info bucket")
tx := func(tx *bolt.Tx) (err error) {
if err = tx.DeleteBucket(addressInfoBucket); err != nil {
return
}
if _, err = tx.CreateBucketIfNotExists(addressInfoBucket); err != nil {
return
}
return
}
return store.db.Update(tx)
}
// truncateMailboxesBucket removes the mailboxes bucket.
func (store *Store) truncateMailboxesBucket() (err error) {
log.Trace("Truncating mailboxes bucket")
tx := func(tx *bolt.Tx) (err error) {
mbs := tx.Bucket(mailboxesBucket)
return mbs.ForEach(func(addrIDMailbox, _ []byte) (err error) {
addr := mbs.Bucket(addrIDMailbox)
if err = addr.DeleteBucket(imapIDsBucket); err != nil {
return
}
if _, err = addr.CreateBucketIfNotExists(imapIDsBucket); err != nil {
return
}
if err = addr.DeleteBucket(apiIDsBucket); err != nil {
return
}
if _, err = addr.CreateBucketIfNotExists(apiIDsBucket); err != nil {
return
}
return
})
}
return store.db.Update(tx)
}
// initMailboxesBucket recreates the mailboxes bucket from the metadata bucket.
func (store *Store) initMailboxesBucket() error {
return store.db.Update(func(tx *bolt.Tx) error {
i := 0
msgs := []*pmapi.Message{}
err := tx.Bucket(metadataBucket).ForEach(func(k, v []byte) error {
msg := &pmapi.Message{}
if err := json.Unmarshal(v, msg); err != nil {
return err
}
msgs = append(msgs, msg)
// Calling txCreateOrUpdateMessages does some overhead by iterating
// all mailboxes, accessing buckets and so on. It's better to do in
// batches instead of one by one (seconds vs hours for huge accounts).
// Average size of metadata is 1k bytes, sometimes up to 2k bytes.
// 10k messages will take about 20 MB of memory.
i++
if i%10000 == 0 {
store.log.WithField("i", i).Debug("Init mboxes heartbeat")
for _, a := range store.addresses {
if err := a.txCreateOrUpdateMessages(tx, msgs); err != nil {
return err
}
}
msgs = []*pmapi.Message{}
}
return nil
})
if err != nil {
return err
}
for _, a := range store.addresses {
if err := a.txCreateOrUpdateMessages(tx, msgs); err != nil {
return err
}
}
return nil
})
}