Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(integration): keep sibling instances active when "*" is retired

Yudhi Armyndhariscd14fdaafb4c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0a358629c..b0b882497 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- Keep the sessions of a plugin's other enabled instances active when creating, disabling or deleting a session-scoped instance retires an operator-set `*`.
- Omit the name, push name, caption, background colour and font of a received status that has none from `GET /status` instead of returning `null`, as the schema declares.
- Bound the inbound media a PostgreSQL session holds before storing it to `INBOUND_MEDIA_CONCURRENCY` payloads, keeping each chat's arrival order.
- Drop a Baileys edit, revoke or reaction from another chat, or an edit or revoke from the wrong author, aimed at a message still being downloaded, on SQLite too.
diff --git a/src/modules/integration/scope-binding.service.spec.ts b/src/modules/integration/scope-binding.service.spec.ts
index 6b8df2ac4..6ff1bdfa5 100644
--- a/src/modules/integration/scope-binding.service.spec.ts
+++ b/src/modules/integration/scope-binding.service.spec.ts
@@ -105,6 +105,68 @@ describe('ScopeBindingService.onApplicationBootstrap reconciliation', () => {
expect(setPluginSessions).toHaveBeenCalledWith('chatwoot', ['sess-1']);
});
+ // With "*" set by the operator and no wildcard instance, "*" is all that keeps the plugin active for the
+ // sessions of its concrete instances. Retiring it has to leave those sessions bound, as the wildcard
+ // teardown does, or every enabled instance but the one just provisioned stops receiving hooks.
+ describe('retiring "*" while other concrete instances are enabled', () => {
+ const instance = (instanceId: string, sessionScope: string, enabled = true) => ({
+ pluginId: 'chatwoot',
+ instanceId,
+ sessionScope,
+ enabled,
+ });
+
+ it('keeps their sessions bound when another concrete instance is provisioned', async () => {
+ const { loader, audit, sessions, setPluginSessions } = build(true, ['*']);
+ const instances = {
+ list: jest.fn().mockResolvedValue([instance('a', 'sess-1'), instance('b', 'sess-2'), instance('c', 'sess-3')]),
+ } as unknown as PluginInstanceService;
+
+ await new ScopeBindingService(instances, loader, audit, sessions).applyScopeBinding(
+ 'chatwoot',
+ 'sess-3',
+ {},
+ true,
+ );
+
+ expect(sessionsWritten(setPluginSessions)).toEqual(new Set(['sess-1', 'sess-2', 'sess-3']));
+ });
+
+ it('keeps their sessions bound when one concrete instance is disabled', async () => {
+ const { loader, audit, sessions, setPluginSessions } = build(true, ['*']);
+ const instances = {
+ list: jest.fn().mockResolvedValue([instance('a', 'sess-1'), instance('b', 'sess-2', false)]),
+ } as unknown as PluginInstanceService;
+
+ await new ScopeBindingService(instances, loader, audit, sessions).applyScopeBinding(
+ 'chatwoot',
+ 'sess-2',
+ {},
+ false,
+ );
+
+ expect(sessionsWritten(setPluginSessions)).toEqual(new Set(['sess-1']));
+ });
+
+ it('leaves an explicit session list alone: there is no "*" to retire', async () => {
+ // The operator removed sess-2 from the plugin's sessions on purpose; provisioning sess-3 must not
+ // bring it back.
+ const { loader, audit, sessions, setPluginSessions } = build(true, ['sess-1']);
+ const instances = {
+ list: jest.fn().mockResolvedValue([instance('a', 'sess-1'), instance('b', 'sess-2'), instance('c', 'sess-3')]),
+ } as unknown as PluginInstanceService;
+
+ await new ScopeBindingService(instances, loader, audit, sessions).applyScopeBinding(
+ 'chatwoot',
+ 'sess-3',
+ {},
+ true,
+ );
+
+ expect(sessionsWritten(setPluginSessions)).toEqual(new Set(['sess-1', 'sess-3']));
+ });
+ });
+
it('restores an enabled wildcard/null-scope instance as base config + ["*"]', async () => {
const { loader, audit, sessions, updatePluginConfig, setPluginSessions } = build();
const instances = {
diff --git a/src/modules/integration/scope-binding.service.ts b/src/modules/integration/scope-binding.service.ts
index 84ea9b8e7..f81985d56 100644
--- a/src/modules/integration/scope-binding.service.ts
+++ b/src/modules/integration/scope-binding.service.ts
@@ -238,6 +238,12 @@ export class ScopeBindingService implements OnApplicationBootstrap {
// Provisioning retires '*' (this instance narrows the plugin to one session); the additive boot
// path keeps the restored set whole, so an operator's PUT /plugins/:id/sessions survives a restart.
const set = new Set(opts.additive ? current : current.filter(s => s !== '*'));
+ // "*" covered the sessions of the plugin's other concrete instances too, so retiring it narrows the
+ // plugin to those scopes rather than to this one alone (the wildcard teardown above does the same).
+ // An explicit list has no "*" and stays as the operator set it.
+ if (!opts.additive && current.includes('*')) {
+ for (const i of siblings) if (i.enabled && i.sessionScope && i.sessionScope !== '*') set.add(i.sessionScope);
+ }
if (activate) set.add(scope);
else set.delete(scope);
// Preserve '*' while an enabled wildcard/null sibling still binds all sessions ('*' subsumes