Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): clean revoked messages and lid mappings on import

Yudhi Armyndharis2ae8b0a962af
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c4187db71..faa958ac5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -109,6 +109,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A built-in PostgreSQL, Redis or MinIO container is created from the image already on the host instead of pulling it every time, so it still starts when the registry is unreachable.
- `POST /api/infra/import-data` retires the plugin bindings of instances the restored backup drops or disables and re-applies the restored ones, so a dropped session-scoped instance no longer keeps receiving message hooks with its old endpoint and credentials.
- `POST /api/infra/import-data` re-keys `chat_states` rows from a backup taken before 0.23.5, so their mute, archive and pin state is read again.
+- `POST /api/infra/import-data` applies the 0.24.0 cleanup to the rows it restores: revoked messages lose their media, quote and reactions, and lid mappings stored with `status` or a broadcast-list id as the phone are dropped.
- Concurrent group creates and participant adds can no longer together exceed the send-pacing cold-reachout daily allowance.
- `GET /api/sessions/:sessionId/contacts/profile-pictures` answers `409` when the engine is not ready, instead of `200` with every picture null.
- Listing messages (`GET /api/sessions/:sessionId/messages` and the `MessageList` MCP tool) reads a page in size-bounded chunks, so a page of large inline media no longer holds every payload in memory at once.
diff --git a/src/database/migrations/1786900000000-ScrubRevokedMessageContent.ts b/src/database/migrations/1786900000000-ScrubRevokedMessageContent.ts
index 668914ff6..1693b79d3 100644
--- a/src/database/migrations/1786900000000-ScrubRevokedMessageContent.ts
+++ b/src/database/migrations/1786900000000-ScrubRevokedMessageContent.ts
@@ -9,18 +9,23 @@ import { MigrationInterface, QueryRunner } from 'typeorm';
* before that in line. The archived files those pointers referenced become unreferenced, and the
* chat-media orphan sweep reaps them after its grace window.
*
- * Idempotent, and `down` is a no-op: the cleared content is not recoverable.
+ * Idempotent, and `down` is a no-op: the cleared content is not recoverable. `scrub` is shared with
+ * import-data, which restores rows from archives taken before this migration ran.
*/
export class ScrubRevokedMessageContent1786900000000 implements MigrationInterface {
name = 'ScrubRevokedMessageContent1786900000000';
- public async up(queryRunner: QueryRunner): Promise<void> {
+ static async scrub(queryRunner: QueryRunner): Promise<void> {
if (!(await queryRunner.hasTable('messages'))) return;
await queryRunner.query(
`UPDATE "messages" SET "body" = '', "metadata" = NULL, "mediaPath" = NULL, "mediaMimetype" = NULL WHERE "type" = 'revoked'`,
);
}
+ public async up(queryRunner: QueryRunner): Promise<void> {
+ await ScrubRevokedMessageContent1786900000000.scrub(queryRunner);
+ }
+
public async down(): Promise<void> {
// Nothing to restore.
}
diff --git a/src/database/migrations/1786950000000-ScrubNonPhoneLidMappings.ts b/src/database/migrations/1786950000000-ScrubNonPhoneLidMappings.ts
index f0c3f802a..dbfac5372 100644
--- a/src/database/migrations/1786950000000-ScrubNonPhoneLidMappings.ts
+++ b/src/database/migrations/1786950000000-ScrubNonPhoneLidMappings.ts
@@ -11,12 +11,17 @@ import { MigrationInterface, QueryRunner } from 'typeorm';
* under. A deleted row is only a cache miss, so the next lookup asks WhatsApp again. A null phone is a
* cached negative result and stays.
*
- * Idempotent, and `down` is a no-op: the removed values were wrong.
+ * Idempotent, and `down` is a no-op: the removed values were wrong. `scrub` is shared with
+ * import-data, which restores rows from archives taken before this migration ran.
*/
export class ScrubNonPhoneLidMappings1786950000000 implements MigrationInterface {
name = 'ScrubNonPhoneLidMappings1786950000000';
public async up(queryRunner: QueryRunner): Promise<void> {
+ await ScrubNonPhoneLidMappings1786950000000.scrub(queryRunner);
+ }
+
+ static async scrub(queryRunner: QueryRunner): Promise<void> {
if (!(await queryRunner.hasTable('lid_mappings'))) return;
const notDigits =
queryRunner.dataSource.options.type === 'postgres' ? `"phone" !~ '^[0-9]+$'` : `"phone" GLOB '*[^0-9]*'`;
diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.ts
index 4ec269dcb..3640af006 100644
--- a/src/modules/infra/infra-data.controller.spec.ts
+++ b/src/modules/infra/infra-data.controller.spec.ts
@@ -2145,6 +2145,57 @@ describe('InfraDataController.importData status_updates + runtime reconciliation
expect(await storedChatStates()).toEqual([{ sessionId: 'session-a', chatId: '[email protected]' }]);
});
+ // The revoked-content and lid-mapping scrubs have already run on the target too, so rows restored
+ // from an archive taken before them are cleaned by the import itself.
+ it('clears restored revoked messages and drops restored lid mappings whose phone is not a number', async () => {
+ await seedSession('s1');
+ const dump = await build().exportData();
+ const at = dump.tables.sessions[0].updatedAt;
+ const message = (id: string, chatId: string, type: string) => ({
+ id,
+ sessionId: 's1',
+ waMessageId: `W-${id}`,
+ chatId,
+ chatName: null,
+ author: null,
+ from: chatId,
+ body: 'hello',
+ type,
+ direction: MessageDirection.INCOMING,
+ timestamp: 1,
+ metadata: { quote: { body: 'quoted' }, reactions: [{ emoji: 'x' }] },
+ status: MessageStatus.DELIVERED,
+ createdAt: at,
+ mediaPath: `media/s1/${id}.jpg`,
+ mediaMimetype: 'image/jpeg',
+ });
+ const lid = (id: string, phone: string) => ({ lid: id, phone, sessionId: 's1', updatedAt: at });
+
+ const res = await build().importData({
+ tables: {
+ ...dump.tables,
+ messages: [message('revoked', '[email protected]', 'revoked'), message('kept', '120363@broadcast', 'image')],
+ lidMappings: [lid('1', 'status'), lid('2', '120363'), lid('3', '628123')],
+ },
+ });
+
+ expect(res.imported).toBe(true);
+ expect(await ds.query('SELECT id, body, metadata, "mediaPath", "mediaMimetype" FROM messages ORDER BY id')).toEqual(
+ [
+ {
+ id: 'kept',
+ body: 'hello',
+ metadata: JSON.stringify({ quote: { body: 'quoted' }, reactions: [{ emoji: 'x' }] }),
+ mediaPath: 'media/s1/kept.jpg',
+ mediaMimetype: 'image/jpeg',
+ },
+ { id: 'revoked', body: '', metadata: null, mediaPath: null, mediaMimetype: null },
+ ],
+ );
+ expect(await ds.query('SELECT lid, phone FROM lid_mappings')).toEqual([{ lid: '3', phone: '628123' }]);
+ });
+
it('exports and restores automation_rules, which the session wipe would otherwise cascade away', async () => {
await seedSession('s1');
const ruleRepo = ds.getRepository(AutomationRule);
diff --git a/src/modules/infra/infra-data.service.ts b/src/modules/infra/infra-data.service.ts
index ffeefdccf..5700d18dd 100644
--- a/src/modules/infra/infra-data.service.ts
+++ b/src/modules/infra/infra-data.service.ts
@@ -13,6 +13,8 @@ import { ChatStateStoreService } from '../../engine/adapters/baileys-chat-state-
import { SessionOwnershipService } from '../session/session-ownership.service';
import { ScopeBindingService } from '../integration/scope-binding.service';
import { ReKeyChatStatesBySessionId1786500000000 } from '../../database/migrations/1786500000000-ReKeyChatStatesBySessionId';
+import { ScrubRevokedMessageContent1786900000000 } from '../../database/migrations/1786900000000-ScrubRevokedMessageContent';
+import { ScrubNonPhoneLidMappings1786950000000 } from '../../database/migrations/1786950000000-ScrubNonPhoneLidMappings';
import { Session as SessionEntity, SessionStatus } from '../session/entities/session.entity';
import { In } from 'typeorm';
import { DateUtils } from 'typeorm/util/DateUtils';
@@ -843,6 +845,17 @@ export class InfraDataService {
}
}
+ // The same holds for the 0.24.0 scrubs: an archive taken before them restores revoked messages
+ // with their media, quote and reactions, and lid mappings with a broadcast id as the phone.
+ if (warnings.length === 0) {
+ try {
+ await ScrubRevokedMessageContent1786900000000.scrub(queryRunner);
+ await ScrubNonPhoneLidMappings1786950000000.scrub(queryRunner);
+ } catch (error) {
+ warnings.push(`Failed to clean restored rows: ${error instanceof Error ? error.message : String(error)}`);
+ }
+ }
+
// "Replace all data" must be all-or-nothing: the import already DELETEd every row, so if any
// INSERT failed we must roll back (restoring the pre-import data) rather than commit a
// half-wiped DB and report success. A partial restore reported as imported:true was how