| 1 |
package backend |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/mail" |
| 6 |
"sync" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/ProtonMail/gluon/rfc822" |
| 10 |
"github.com/ProtonMail/go-srp" |
| 11 |
"github.com/ProtonMail/gopenpgp/v2/crypto" |
| 12 |
"github.com/bradenaw/juniper/xslices" |
| 13 |
"github.com/google/uuid" |
| 14 |
"github.com/henrybear327/go-proton-api" |
| 15 |
"golang.org/x/exp/maps" |
| 16 |
"golang.org/x/exp/slices" |
| 17 |
) |
| 18 |
|
| 19 |
type Backend struct { |
| 20 |
domain string |
| 21 |
|
| 22 |
accounts map[string]*account |
| 23 |
accLock sync.RWMutex |
| 24 |
|
| 25 |
attachments map[string]*attachment |
| 26 |
attLock sync.Mutex |
| 27 |
|
| 28 |
attData map[string][]byte |
| 29 |
attDataLock sync.Mutex |
| 30 |
|
| 31 |
messages map[string]*message |
| 32 |
msgLock sync.Mutex |
| 33 |
|
| 34 |
labels map[string]*label |
| 35 |
lblLock sync.Mutex |
| 36 |
|
| 37 |
updates map[ID]update |
| 38 |
updatesLock sync.RWMutex |
| 39 |
maxUpdatesPerEvent int |
| 40 |
|
| 41 |
srp map[string]*srp.Server |
| 42 |
srpLock sync.Mutex |
| 43 |
|
| 44 |
authLife time.Duration |
| 45 |
enableDedup bool |
| 46 |
} |
| 47 |
|
| 48 |
func New(authLife time.Duration, domain string, enableDedup bool) *Backend { |
| 49 |
return &Backend{ |
| 50 |
domain: domain, |
| 51 |
accounts: make(map[string]*account), |
| 52 |
attachments: make(map[string]*attachment), |
| 53 |
attData: make(map[string][]byte), |
| 54 |
messages: make(map[string]*message), |
| 55 |
labels: make(map[string]*label), |
| 56 |
updates: make(map[ID]update), |
| 57 |
maxUpdatesPerEvent: 0, |
| 58 |
srp: make(map[string]*srp.Server), |
| 59 |
authLife: authLife, |
| 60 |
enableDedup: enableDedup, |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func (b *Backend) SetAuthLife(authLife time.Duration) { |
| 65 |
b.authLife = authLife |
| 66 |
} |
| 67 |
|
| 68 |
func (b *Backend) SetMaxUpdatesPerEvent(max int) { |
| 69 |
b.maxUpdatesPerEvent = max |
| 70 |
} |
| 71 |
|
| 72 |
func (b *Backend) CreateUser(username string, password []byte) (string, error) { |
| 73 |
b.accLock.Lock() |
| 74 |
defer b.accLock.Unlock() |
| 75 |
|
| 76 |
salt, err := crypto.RandomToken(16) |
| 77 |
if err != nil { |
| 78 |
return "", err |
| 79 |
} |
| 80 |
|
| 81 |
passphrase, err := hashPassword(password, salt) |
| 82 |
if err != nil { |
| 83 |
return "", err |
| 84 |
} |
| 85 |
|
| 86 |
srpAuth, err := srp.NewAuthForVerifier(password, modulus, salt) |
| 87 |
if err != nil { |
| 88 |
return "", err |
| 89 |
} |
| 90 |
|
| 91 |
verifier, err := srpAuth.GenerateVerifier(2048) |
| 92 |
if err != nil { |
| 93 |
return "", err |
| 94 |
} |
| 95 |
|
| 96 |
armKey, err := GenerateKey(username, username, passphrase, "rsa", 2048) |
| 97 |
if err != nil { |
| 98 |
return "", err |
| 99 |
} |
| 100 |
|
| 101 |
userID := uuid.NewString() |
| 102 |
|
| 103 |
b.accounts[userID] = newAccount(userID, username, armKey, salt, verifier) |
| 104 |
|
| 105 |
return userID, nil |
| 106 |
} |
| 107 |
|
| 108 |
func (b *Backend) RemoveUser(userID string) error { |
| 109 |
b.accLock.Lock() |
| 110 |
defer b.accLock.Unlock() |
| 111 |
|
| 112 |
user, ok := b.accounts[userID] |
| 113 |
if !ok { |
| 114 |
return fmt.Errorf("user %s does not exist", userID) |
| 115 |
} |
| 116 |
|
| 117 |
for _, labelID := range user.labelIDs { |
| 118 |
delete(b.labels, labelID) |
| 119 |
} |
| 120 |
|
| 121 |
for _, messageID := range user.messageIDs { |
| 122 |
for _, attID := range b.messages[messageID].attIDs { |
| 123 |
if xslices.CountFunc(maps.Values(b.attachments), func(att *attachment) bool { |
| 124 |
return att.attDataID == b.attachments[attID].attDataID |
| 125 |
}) == 1 { |
| 126 |
delete(b.attData, b.attachments[attID].attDataID) |
| 127 |
} |
| 128 |
|
| 129 |
delete(b.attachments, attID) |
| 130 |
} |
| 131 |
|
| 132 |
delete(b.messages, messageID) |
| 133 |
} |
| 134 |
|
| 135 |
delete(b.accounts, userID) |
| 136 |
|
| 137 |
return nil |
| 138 |
} |
| 139 |
|
| 140 |
func (b *Backend) RefreshUser(userID string, refresh proton.RefreshFlag) error { |
| 141 |
return b.withAcc(userID, func(acc *account) error { |
| 142 |
updateID, err := b.newUpdate(&userRefreshed{refresh: refresh}) |
| 143 |
if err != nil { |
| 144 |
return err |
| 145 |
} |
| 146 |
|
| 147 |
if refresh == proton.RefreshAll { |
| 148 |
acc.updateIDs = []ID{updateID} |
| 149 |
} else { |
| 150 |
acc.updateIDs = append(acc.updateIDs, updateID) |
| 151 |
} |
| 152 |
|
| 153 |
return nil |
| 154 |
}) |
| 155 |
} |
| 156 |
|
| 157 |
func (b *Backend) CreateUserKey(userID string, password []byte) error { |
| 158 |
b.accLock.Lock() |
| 159 |
defer b.accLock.Unlock() |
| 160 |
|
| 161 |
user, ok := b.accounts[userID] |
| 162 |
if !ok { |
| 163 |
return fmt.Errorf("user %s does not exist", userID) |
| 164 |
} |
| 165 |
|
| 166 |
salt, err := crypto.RandomToken(16) |
| 167 |
if err != nil { |
| 168 |
return err |
| 169 |
} |
| 170 |
|
| 171 |
passphrase, err := hashPassword(password, salt) |
| 172 |
if err != nil { |
| 173 |
return err |
| 174 |
} |
| 175 |
|
| 176 |
armKey, err := GenerateKey(user.username, user.username, passphrase, "rsa", 2048) |
| 177 |
if err != nil { |
| 178 |
return err |
| 179 |
} |
| 180 |
|
| 181 |
user.keys = append(user.keys, key{keyID: uuid.NewString(), key: armKey}) |
| 182 |
|
| 183 |
return nil |
| 184 |
} |
| 185 |
|
| 186 |
func (b *Backend) RemoveUserKey(userID, keyID string) error { |
| 187 |
b.accLock.Lock() |
| 188 |
defer b.accLock.Unlock() |
| 189 |
|
| 190 |
user, ok := b.accounts[userID] |
| 191 |
if !ok { |
| 192 |
return fmt.Errorf("user %s does not exist", userID) |
| 193 |
} |
| 194 |
|
| 195 |
idx := xslices.IndexFunc(user.keys, func(key key) bool { |
| 196 |
return key.keyID == keyID |
| 197 |
}) |
| 198 |
|
| 199 |
if idx == -1 { |
| 200 |
return fmt.Errorf("key %s does not exist", keyID) |
| 201 |
} |
| 202 |
|
| 203 |
user.keys = append(user.keys[:idx], user.keys[idx+1:]...) |
| 204 |
|
| 205 |
return nil |
| 206 |
} |
| 207 |
|
| 208 |
func (b *Backend) CreateAddress(userID, email string, password []byte, withKey bool, status proton.AddressStatus, addrType proton.AddressType) (string, error) { |
| 209 |
return b.createAddress(userID, email, password, withKey, status, addrType, false) |
| 210 |
} |
| 211 |
|
| 212 |
func (b *Backend) CreateAddressAsUpdate(userID, email string, password []byte, withKey bool, status proton.AddressStatus, addrType proton.AddressType) (string, error) { |
| 213 |
return b.createAddress(userID, email, password, withKey, status, addrType, true) |
| 214 |
} |
| 215 |
|
| 216 |
func (b *Backend) createAddress(userID, email string, password []byte, withKey bool, status proton.AddressStatus, addrType proton.AddressType, issueUpdateInsteadOfCreate bool) (string, error) { |
| 217 |
return withAcc(b, userID, func(acc *account) (string, error) { |
| 218 |
var keys []key |
| 219 |
|
| 220 |
if withKey { |
| 221 |
token, err := crypto.RandomToken(32) |
| 222 |
if err != nil { |
| 223 |
return "", err |
| 224 |
} |
| 225 |
|
| 226 |
armKey, err := GenerateKey(acc.username, email, token, "rsa", 2048) |
| 227 |
if err != nil { |
| 228 |
return "", err |
| 229 |
} |
| 230 |
|
| 231 |
passphrase, err := hashPassword([]byte(password), acc.salt) |
| 232 |
if err != nil { |
| 233 |
return "", err |
| 234 |
} |
| 235 |
|
| 236 |
userKR, err := acc.keys[0].unlock(passphrase) |
| 237 |
if err != nil { |
| 238 |
return "", err |
| 239 |
} |
| 240 |
|
| 241 |
encToken, sigToken, err := encryptWithSignature(userKR, token) |
| 242 |
if err != nil { |
| 243 |
return "", err |
| 244 |
} |
| 245 |
|
| 246 |
keys = append(keys, key{ |
| 247 |
keyID: uuid.NewString(), |
| 248 |
key: armKey, |
| 249 |
tok: encToken, |
| 250 |
sig: sigToken, |
| 251 |
}) |
| 252 |
} |
| 253 |
|
| 254 |
addressID := uuid.NewString() |
| 255 |
|
| 256 |
acc.addresses[addressID] = &address{ |
| 257 |
addrID: addressID, |
| 258 |
email: email, |
| 259 |
order: len(acc.addresses) + 1, |
| 260 |
status: status, |
| 261 |
addrType: addrType, |
| 262 |
keys: keys, |
| 263 |
} |
| 264 |
|
| 265 |
var update update |
| 266 |
if issueUpdateInsteadOfCreate { |
| 267 |
update = &addressUpdated{addressID: addressID} |
| 268 |
} else { |
| 269 |
update = &addressCreated{addressID: addressID} |
| 270 |
} |
| 271 |
|
| 272 |
updateID, err := b.newUpdate(update) |
| 273 |
if err != nil { |
| 274 |
return "", err |
| 275 |
} |
| 276 |
|
| 277 |
acc.updateIDs = append(acc.updateIDs, updateID) |
| 278 |
|
| 279 |
return addressID, nil |
| 280 |
}) |
| 281 |
} |
| 282 |
|
| 283 |
func (b *Backend) ChangeAddressType(userID, addrId string, addrType proton.AddressType) error { |
| 284 |
return b.withAcc(userID, func(acc *account) error { |
| 285 |
for _, addr := range acc.addresses { |
| 286 |
if addr.addrID == addrId { |
| 287 |
addr.addrType = addrType |
| 288 |
return nil |
| 289 |
} |
| 290 |
} |
| 291 |
return fmt.Errorf("no addrID matching %s for user %s", addrId, userID) |
| 292 |
}) |
| 293 |
} |
| 294 |
|
| 295 |
func (b *Backend) CreateAddressKey(userID, addrID string, password []byte) error { |
| 296 |
return b.withAcc(userID, func(acc *account) error { |
| 297 |
token, err := crypto.RandomToken(32) |
| 298 |
if err != nil { |
| 299 |
return err |
| 300 |
} |
| 301 |
|
| 302 |
armKey, err := GenerateKey(acc.username, acc.addresses[addrID].email, token, "rsa", 2048) |
| 303 |
if err != nil { |
| 304 |
return err |
| 305 |
} |
| 306 |
|
| 307 |
passphrase, err := hashPassword([]byte(password), acc.salt) |
| 308 |
if err != nil { |
| 309 |
return err |
| 310 |
} |
| 311 |
|
| 312 |
userKR, err := acc.keys[0].unlock(passphrase) |
| 313 |
if err != nil { |
| 314 |
return err |
| 315 |
} |
| 316 |
|
| 317 |
encToken, sigToken, err := encryptWithSignature(userKR, token) |
| 318 |
if err != nil { |
| 319 |
return err |
| 320 |
} |
| 321 |
|
| 322 |
acc.addresses[addrID].keys = append(acc.addresses[addrID].keys, key{ |
| 323 |
keyID: uuid.NewString(), |
| 324 |
key: armKey, |
| 325 |
tok: encToken, |
| 326 |
sig: sigToken, |
| 327 |
}) |
| 328 |
|
| 329 |
updateID, err := b.newUpdate(&addressUpdated{addressID: addrID}) |
| 330 |
if err != nil { |
| 331 |
return err |
| 332 |
} |
| 333 |
|
| 334 |
acc.updateIDs = append(acc.updateIDs, updateID) |
| 335 |
|
| 336 |
return nil |
| 337 |
}) |
| 338 |
} |
| 339 |
|
| 340 |
func (b *Backend) RemoveAddress(userID, addrID string) error { |
| 341 |
return b.withAcc(userID, func(acc *account) error { |
| 342 |
if _, ok := acc.addresses[addrID]; !ok { |
| 343 |
return fmt.Errorf("address %s not found", addrID) |
| 344 |
} |
| 345 |
|
| 346 |
delete(acc.addresses, addrID) |
| 347 |
|
| 348 |
updateID, err := b.newUpdate(&addressDeleted{addressID: addrID}) |
| 349 |
if err != nil { |
| 350 |
return err |
| 351 |
} |
| 352 |
|
| 353 |
acc.updateIDs = append(acc.updateIDs, updateID) |
| 354 |
|
| 355 |
return nil |
| 356 |
}) |
| 357 |
} |
| 358 |
|
| 359 |
func (b *Backend) RemoveAddressKey(userID, addrID, keyID string) error { |
| 360 |
return b.withAcc(userID, func(acc *account) error { |
| 361 |
idx := xslices.IndexFunc(acc.addresses[addrID].keys, func(key key) bool { |
| 362 |
return key.keyID == keyID |
| 363 |
}) |
| 364 |
|
| 365 |
if idx < 0 { |
| 366 |
return fmt.Errorf("key %s not found", keyID) |
| 367 |
} |
| 368 |
|
| 369 |
acc.addresses[addrID].keys = append(acc.addresses[addrID].keys[:idx], acc.addresses[addrID].keys[idx+1:]...) |
| 370 |
|
| 371 |
updateID, err := b.newUpdate(&addressUpdated{addressID: addrID}) |
| 372 |
if err != nil { |
| 373 |
return err |
| 374 |
} |
| 375 |
|
| 376 |
acc.updateIDs = append(acc.updateIDs, updateID) |
| 377 |
|
| 378 |
return nil |
| 379 |
}) |
| 380 |
} |
| 381 |
|
| 382 |
// TODO: Implement this when we support subscriptions in the test server. |
| 383 |
func (b *Backend) CreateSubscription(userID, planID string) error { |
| 384 |
return nil |
| 385 |
} |
| 386 |
|
| 387 |
func (b *Backend) CreateMessage( |
| 388 |
userID, addrID string, |
| 389 |
subject string, |
| 390 |
sender *mail.Address, |
| 391 |
toList, ccList, bccList, replytos []*mail.Address, |
| 392 |
armBody string, |
| 393 |
mimeType rfc822.MIMEType, |
| 394 |
flags proton.MessageFlag, |
| 395 |
date time.Time, |
| 396 |
unread, starred bool, |
| 397 |
) (string, error) { |
| 398 |
return withAcc(b, userID, func(acc *account) (string, error) { |
| 399 |
return withMessages(b, func(messages map[string]*message) (string, error) { |
| 400 |
msg := newMessage(addrID, subject, sender, toList, ccList, bccList, replytos, armBody, mimeType, "", date) |
| 401 |
|
| 402 |
msg.flags |= flags |
| 403 |
msg.unread = unread |
| 404 |
msg.starred = starred |
| 405 |
|
| 406 |
addrListEqual := func(l1 []*mail.Address, l2 []*mail.Address) bool { |
| 407 |
s1 := xslices.Map(l1, func(addr *mail.Address) string { |
| 408 |
return addr.Address |
| 409 |
}) |
| 410 |
s2 := xslices.Map(l2, func(addr *mail.Address) string { |
| 411 |
return addr.Address |
| 412 |
}) |
| 413 |
|
| 414 |
return slices.Equal(s1, s2) |
| 415 |
} |
| 416 |
|
| 417 |
var foundDuplicate bool |
| 418 |
|
| 419 |
if b.enableDedup { |
| 420 |
for _, m := range messages { |
| 421 |
if m.addrID != msg.addrID { |
| 422 |
continue |
| 423 |
} |
| 424 |
|
| 425 |
toEqual := addrListEqual(m.toList, msg.toList) |
| 426 |
bccEqual := addrListEqual(m.bccList, msg.bccList) |
| 427 |
ccEqual := addrListEqual(m.ccList, msg.ccList) |
| 428 |
|
| 429 |
if m.sender.Address == msg.sender.Address && |
| 430 |
toEqual && |
| 431 |
bccEqual && |
| 432 |
ccEqual && |
| 433 |
m.subject == msg.subject { |
| 434 |
msg.messageID = m.messageID |
| 435 |
foundDuplicate = true |
| 436 |
break |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
if !foundDuplicate { |
| 442 |
messages[msg.messageID] = msg |
| 443 |
|
| 444 |
updateID, err := b.newUpdate(&messageCreated{messageID: msg.messageID}) |
| 445 |
if err != nil { |
| 446 |
return "", err |
| 447 |
} |
| 448 |
|
| 449 |
acc.messageIDs = append(acc.messageIDs, msg.messageID) |
| 450 |
acc.updateIDs = append(acc.updateIDs, updateID) |
| 451 |
} |
| 452 |
|
| 453 |
return msg.messageID, nil |
| 454 |
}) |
| 455 |
}) |
| 456 |
} |
| 457 |
|
| 458 |
func (b *Backend) Encrypt(userID, addrID, decBody string) (string, error) { |
| 459 |
return withAcc(b, userID, func(acc *account) (string, error) { |
| 460 |
pubKey, err := acc.addresses[addrID].keys[0].getPubKey() |
| 461 |
if err != nil { |
| 462 |
return "", err |
| 463 |
} |
| 464 |
|
| 465 |
kr, err := crypto.NewKeyRing(pubKey) |
| 466 |
if err != nil { |
| 467 |
return "", err |
| 468 |
} |
| 469 |
|
| 470 |
enc, err := kr.Encrypt(crypto.NewPlainMessageFromString(decBody), nil) |
| 471 |
if err != nil { |
| 472 |
return "", err |
| 473 |
} |
| 474 |
|
| 475 |
return enc.GetArmored() |
| 476 |
}) |
| 477 |
} |
| 478 |
|
| 479 |
func (b *Backend) withAcc(userID string, fn func(acc *account) error) error { |
| 480 |
b.accLock.RLock() |
| 481 |
defer b.accLock.RUnlock() |
| 482 |
|
| 483 |
acc, ok := b.accounts[userID] |
| 484 |
if !ok { |
| 485 |
return fmt.Errorf("account %s not found", userID) |
| 486 |
} |
| 487 |
|
| 488 |
return fn(acc) |
| 489 |
} |
| 490 |
|
| 491 |
func (b *Backend) withAccEmail(email string, fn func(acc *account) error) error { |
| 492 |
b.accLock.RLock() |
| 493 |
defer b.accLock.RUnlock() |
| 494 |
|
| 495 |
for _, acc := range b.accounts { |
| 496 |
for _, addr := range acc.addresses { |
| 497 |
if addr.email == email { |
| 498 |
return fn(acc) |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
return fmt.Errorf("account %s not found", email) |
| 504 |
} |
| 505 |
|
| 506 |
func withAcc[T any](b *Backend, userID string, fn func(acc *account) (T, error)) (T, error) { |
| 507 |
b.accLock.RLock() |
| 508 |
defer b.accLock.RUnlock() |
| 509 |
|
| 510 |
for _, acc := range b.accounts { |
| 511 |
if acc.userID == userID { |
| 512 |
return fn(acc) |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
return *new(T), fmt.Errorf("account not found") |
| 517 |
} |
| 518 |
|
| 519 |
func withAccName[T any](b *Backend, username string, fn func(acc *account) (T, error)) (T, error) { |
| 520 |
b.accLock.RLock() |
| 521 |
defer b.accLock.RUnlock() |
| 522 |
|
| 523 |
for _, acc := range b.accounts { |
| 524 |
if acc.username == username { |
| 525 |
return fn(acc) |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
return *new(T), fmt.Errorf("account not found") |
| 530 |
} |
| 531 |
|
| 532 |
func withAccEmail[T any](b *Backend, email string, fn func(acc *account) (T, error)) (T, error) { |
| 533 |
b.accLock.RLock() |
| 534 |
defer b.accLock.RUnlock() |
| 535 |
|
| 536 |
for _, acc := range b.accounts { |
| 537 |
if _, ok := acc.getAddr(email); ok { |
| 538 |
return fn(acc) |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
return *new(T), fmt.Errorf("account not found") |
| 543 |
} |
| 544 |
|
| 545 |
func withAccAuth[T any](b *Backend, authUID, authAcc string, fn func(acc *account) (T, error)) (T, error) { |
| 546 |
b.accLock.Lock() |
| 547 |
defer b.accLock.Unlock() |
| 548 |
|
| 549 |
for _, acc := range b.accounts { |
| 550 |
acc.authLock.Lock() |
| 551 |
defer acc.authLock.Unlock() |
| 552 |
|
| 553 |
val, ok := acc.auth[authUID] |
| 554 |
if !ok { |
| 555 |
continue |
| 556 |
} |
| 557 |
|
| 558 |
if time.Since(val.creation) > b.authLife { |
| 559 |
acc.auth[authUID] = auth{ref: val.ref, creation: val.creation} |
| 560 |
} else if val.acc == authAcc { |
| 561 |
return fn(acc) |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
return *new(T), fmt.Errorf("account not found") |
| 566 |
} |
| 567 |
|
| 568 |
func (b *Backend) withMessages(fn func(map[string]*message) error) error { |
| 569 |
b.msgLock.Lock() |
| 570 |
defer b.msgLock.Unlock() |
| 571 |
|
| 572 |
return fn(b.messages) |
| 573 |
} |
| 574 |
|
| 575 |
func withMessages[T any](b *Backend, fn func(map[string]*message) (T, error)) (T, error) { |
| 576 |
b.msgLock.Lock() |
| 577 |
defer b.msgLock.Unlock() |
| 578 |
|
| 579 |
return fn(b.messages) |
| 580 |
} |
| 581 |
|
| 582 |
func withAtts[T any](b *Backend, fn func(map[string]*attachment) (T, error)) (T, error) { |
| 583 |
b.attLock.Lock() |
| 584 |
defer b.attLock.Unlock() |
| 585 |
|
| 586 |
return fn(b.attachments) |
| 587 |
} |
| 588 |
|
| 589 |
func (b *Backend) withLabels(fn func(map[string]*label) error) error { |
| 590 |
b.lblLock.Lock() |
| 591 |
defer b.lblLock.Unlock() |
| 592 |
|
| 593 |
return fn(b.labels) |
| 594 |
} |
| 595 |
|
| 596 |
func withLabels[T any](b *Backend, fn func(map[string]*label) (T, error)) (T, error) { |
| 597 |
b.lblLock.Lock() |
| 598 |
defer b.lblLock.Unlock() |
| 599 |
|
| 600 |
return fn(b.labels) |
| 601 |
} |
| 602 |
|
| 603 |
func (b *Backend) newUpdate(event update) (ID, error) { |
| 604 |
return withUpdates(b, func(updates map[ID]update) (ID, error) { |
| 605 |
updateID := ID(len(updates)) |
| 606 |
|
| 607 |
updates[updateID] = event |
| 608 |
|
| 609 |
return updateID, nil |
| 610 |
}) |
| 611 |
} |
| 612 |
|
| 613 |
func withUpdates[T any](b *Backend, fn func(map[ID]update) (T, error)) (T, error) { |
| 614 |
b.updatesLock.Lock() |
| 615 |
defer b.updatesLock.Unlock() |
| 616 |
|
| 617 |
return fn(b.updates) |
| 618 |
} |