Skip to content

Commit

Permalink
fix: switch from distinct to aggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Nov 11, 2024
1 parent a9d86ef commit 446af27
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
1 change: 0 additions & 1 deletion app/controllers/api/v1/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ async function lookup(ctx) {
//
if (domain.is_global) {
// TODO: implement caching here
// TODO: maxTimeMS: 30000
const arr = await Users.aggregate([
{
$match: {
Expand Down
44 changes: 34 additions & 10 deletions app/models/aliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,12 +677,24 @@ Aliases.pre('save', async function (next) {
if (domain.is_global && !alias.is_new_user && user) {
// user must be on a paid plan to use a global domain
if (user.plan === 'free' && !alias.is_update) {
const domainIds = await conn.models.Domains.distinct('_id', {
is_global: true
});
const arr = await conn.models.Domains.aggregate([
{
$match: {
is_global: true
}
},
{
$group: {
_id: '$_id'
}
}
])
.allowDiskUse(true)
.exec();

const aliasCount = await alias.constructor.countDocuments({
user: user._id,
domain: { $in: domainIds }
domain: { $in: arr.map((v) => v._id) }
});
throw Boom.paymentRequired(
i18n.translateError(
Expand Down Expand Up @@ -763,17 +775,29 @@ Aliases.pre('save', async function (next) {

async function updateDomainCatchallRegexBooleans(alias) {
try {
const names = await alias.constructor.distinct('name', {
domain: alias.domain
});
const arr = await alias.constructor
.aggregate([
{
$match: {
domain: alias.domain
}
},
{
$group: {
_id: '$name'
}
}
])
.allowDiskUse(true)
.exec();

let hasCatchall = false;
let hasRegex = false;

for (const name of names) {
for (const v of arr) {
if (hasCatchall && hasRegex) break;
if (name === '*') hasCatchall = true;
else if (name.startsWith('/')) hasRegex = true;
if (v._id === '*') hasCatchall = true;
else if (v._id.startsWith('/')) hasRegex = true;
}

await conn.models.Domains.findByIdAndUpdate(alias.domain, {
Expand Down
15 changes: 9 additions & 6 deletions app/models/domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -1154,17 +1154,20 @@ Domains.pre('save', async function (next) {
return next();
}

const names = await conn.models.Aliases.distinct('name', {
domain: this._id
});
const arr = await conn.models.Aliases.aggregate([
{ $match: { domain: this._id } },
{ $group: { _id: '$name' } }
])
.allowDiskUse(true)
.exec();

let hasCatchall = false;
let hasRegex = false;

for (const name of names) {
for (const v of arr) {
if (hasCatchall && hasRegex) break;
if (name === '*') hasCatchall = true;
else if (name.startsWith('/')) hasRegex = true;
if (v._id === '*') hasCatchall = true;
else if (v._id.startsWith('/')) hasRegex = true;
}

this.has_catchall = hasCatchall;
Expand Down

0 comments on commit 446af27

Please sign in to comment.