fix tag matching of filter and make hasTags op case-sensitive (#5386)

Co-authored-by: youngcw <calebyoung94@gmail.com>
This commit is contained in:
POGMAN
2025-07-26 18:03:48 +02:00
committed by GitHub
parent 45530638fe
commit c44b32805f
3 changed files with 27 additions and 19 deletions

View File

@@ -329,6 +329,10 @@ const CONDITION_TYPES = {
);
}
if (op === 'hasTags') {
return value;
}
return value.toLowerCase();
},
},

View File

@@ -390,30 +390,35 @@ export function conditionsToAQL(
}
}
const apply = (field, op, value) => {
const apply = (field, aqlOp, value) => {
if (type === 'number') {
if (options) {
if (options.outflow) {
return {
$and: [
{ amount: { $lt: 0 } },
{ [field]: { $transform: '$neg', [op]: value } },
{ [field]: { $transform: '$neg', [aqlOp]: value } },
],
};
} else if (options.inflow) {
return {
$and: [{ amount: { $gt: 0 } }, { [field]: { [op]: value } }],
$and: [{ amount: { $gt: 0 } }, { [field]: { [aqlOp]: value } }],
};
}
}
return { amount: { [op]: value } };
return { amount: { [aqlOp]: value } };
} else if (type === 'string') {
return { [field]: { $transform: '$lower', [op]: value } };
return {
[field]: {
$transform: op !== 'hasTags' ? '$lower' : undefined,
[aqlOp]: value,
},
};
} else if (type === 'date') {
return { [field]: { [op]: value.date } };
return { [field]: { [aqlOp]: value.date } };
}
return { [field]: { [op]: value } };
return { [field]: { [aqlOp]: value } };
};
switch (op) {
@@ -530,24 +535,17 @@ export function conditionsToAQL(
return { $or: values.map(v => apply(field, '$eq', v)) };
case 'hasTags':
const words = value.split(/\s+/);
const tagValues = [];
words.forEach(word => {
const startsWithHash = word.startsWith('#');
const containsMultipleHash = word.slice(1).includes('#');
const correctlyFormatted = word.match(/#[\w\d\p{Emoji}-]+/gu);
const validHashtag =
startsWithHash && !containsMultipleHash && correctlyFormatted;
if (validHashtag) {
tagValues.push(word);
for (const [_, tag] of value.matchAll(/(?<!#)(#[^#\s]+)/g)) {
if (!tagValues.find(t => t.tag === tag)) {
tagValues.push(tag);
}
});
}
return {
$and: tagValues.map(v => {
const regex = new RegExp(
`(^|\\s)${v.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(\\s|$)`,
`(?<!#)${v.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}([\\s#]|$)`,
);
return apply(field, '$regexp', regex.source);
}),

View File

@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [pogman-code]
---
Consolidate `has tags` filter