Skip to content

Commit

Permalink
support wildcard address
Browse files Browse the repository at this point in the history
  • Loading branch information
mfornos committed Nov 8, 2023
1 parent 1cbb7bd commit e3b11c8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
14 changes: 10 additions & 4 deletions src/services/monitoring/ops/criteria.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Criteria } from '@sodazone/ocelloids';

export function sendersCriteria(senders: string[]) : Criteria {
return {
'extrinsic.signer.id': { $in: senders }
};
export function sendersCriteria(senders: string[] | '*') : Criteria {
if (Array.isArray(senders)) {
return {
'extrinsic.signer.id': { $in: senders }
};
} else {
return {
'extrinsic.signer': { $exists: true }
};
}
}

export function messageCriteria(recipients: number[]) : Criteria {
Expand Down
4 changes: 2 additions & 2 deletions src/services/monitoring/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@ export const $QuerySubscription = z.object({
}).min(0),
senders: z.array(z.string()).min(
1, 'at least 1 sender address is required'
),
).or(z.literal('*')),
destinations: z.array(z.number({
required_error: 'destination id is required'
}).min(0)),
notify: z.union([
notify: z.discriminatedUnion('type', [
$WebhookNotification,
$LogNotification
])
Expand Down
38 changes: 30 additions & 8 deletions src/services/persistence/subs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export class SubsStore {
) {
const delKeys: string[] = [];

// delete updated destinations
source.destinations.filter(
d => modified.destinations.indexOf(d) < 0
).forEach(d => {
Expand All @@ -159,15 +160,26 @@ export class SubsStore {
);
}
});
source.senders.filter(
s => modified.senders.indexOf(s) < 0
).forEach(s => {
for (const d of source.destinations) {
delKeys.push(
this.#uniquePathKey(source.origin, d, s)
);

// delete updated senders
const { senders } = source;

if (Array.isArray(senders)) { // existing []
const newSenders = modified.senders;
if (Array.isArray(newSenders)) { // new []
senders.filter(
s => newSenders.indexOf(s) < 0
).forEach(s => {
delKeys.push(...this.#expandUniqueDestKeys(source, s));
});
} else { // new '*'
senders.forEach(s => {
delKeys.push(...this.#expandUniqueDestKeys(source, s));
});
}
});
} else { // existing '*'
delKeys.push(...this.#expandUniqueDestKeys(source, '*'));
}

if (delKeys.length > 0) {
const batch = this.#uniques.batch();
Expand All @@ -185,6 +197,16 @@ export class SubsStore {
}
}

#expandUniqueDestKeys(source: QuerySubscription, sender: string) {
const keys = [];
for (const d of source.destinations) {
keys.push(
this.#uniquePathKey(source.origin, d, sender)
);
}
return keys;
}

#uniquePathKey(networkId: number, destination: number, sender: string) {
return `${networkId}:${destination}:${sender}`;
}
Expand Down

0 comments on commit e3b11c8

Please sign in to comment.