Skip to content

Commit 4adfce0

Browse files
authored
fix(domaincache): ZMS-247 implement domain cache to store all used domains of users (#845)
* implement domain cache to store all used domains of users * add missing await before promise * ignore duplicate key error, fix bug (use correct addressData object) * fix comment * settings-handler domain cache improve name Domain cache will be used primarily to filter inbound emails as sort of a bloom filter
1 parent 765c46c commit 4adfce0

4 files changed

Lines changed: 42 additions & 0 deletions

File tree

indexes.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,15 @@ indexes:
745745
enumerable: 1
746746
key: 1
747747

748+
# Indexes for the domaincache collection
749+
- collection: domaincache
750+
type: users
751+
index:
752+
name: by_domain
753+
unique: true
754+
key:
755+
domain: 1
756+
748757
deleteindexes:
749758
- collection: settings
750759
index: key_enumerable

lib/api/addresses.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,11 @@ module.exports = (db, server, userHandler, settingsHandler) => {
532532
// insert alias address to email address registry
533533
try {
534534
r = await db.users.collection('addresses').insertOne(addressData);
535+
try {
536+
await db.database.collection('domaincache').insertOne({ domain: addressData.address.split('@')[1] });
537+
} catch {
538+
// ignore
539+
}
535540
} catch (err) {
536541
res.status(500);
537542
return res.json({
@@ -1724,6 +1729,12 @@ module.exports = (db, server, userHandler, settingsHandler) => {
17241729

17251730
try {
17261731
r = await db.users.collection('addresses').insertOne(addressData);
1732+
1733+
try {
1734+
await db.database.collection('domaincache').insertOne({ domain: addressData.address.split('@')[1] });
1735+
} catch {
1736+
// ignore
1737+
}
17271738
} catch (err) {
17281739
res.status(500);
17291740
return res.json({

lib/settings-handler.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ const SETTING_KEYS = [
147147
type: 'number',
148148
constKey: 'BULK_BATCH_SIZE',
149149
schema: Joi.number()
150+
},
151+
152+
{
153+
key: 'const:domaincache:enabled',
154+
name: 'Domain cache enabled status',
155+
description: 'A true/false boolean value indicating whether domains are stored in a separate cache. Only these domains are allowed for inbound emails',
156+
type: 'boolean',
157+
confValue: false,
158+
schema: Joi.boolean()
150159
}
151160
];
152161

lib/user-handler.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,12 @@ class UserHandler {
16091609
try {
16101610
// insert alias address to email address registry
16111611
await this.users.collection('addresses').insertOne(addressData, { writeConcern: 'majority' });
1612+
1613+
try {
1614+
await this.users.collection('domaincache').insertOne({ domain: addressData.address.split('@')[1] });
1615+
} catch {
1616+
// ignore
1617+
}
16121618
} catch (err) {
16131619
try {
16141620
// try to rollback
@@ -3676,6 +3682,13 @@ class UserHandler {
36763682
if (!r || !r.insertedId) {
36773683
throw new Error('Failed to insert');
36783684
}
3685+
3686+
try {
3687+
await this.users.collection('domaincache').insertOne({ domain: address.address.split('@')[1] });
3688+
} catch {
3689+
// ignore
3690+
}
3691+
36793692
recoveredAddresses.push(address);
36803693
log.info('Restore', 'ADDRRESTORE user=%s address=%s email=%s', user, address._id, address.address);
36813694
} catch (err) {

0 commit comments

Comments
 (0)