feat(label): add reliable full label sync endpoint - #2660
Open
HulianBuligon wants to merge 6 commits into
Open
Conversation
Delegates to service.syncLabels() via waMonitor, following the same pattern as fetchLabels and handleLabel.
New endpoint that forces WhatsApp to re-download labels via Baileys resyncAppState(['regular'], true) before returning updated labels from DB. Uses same dataValidate pattern as findLabels.
Uses Baileys client.resyncAppState(['regular'], true) to force an incremental re-download of WhatsApp app state (labels). Waits 3s for LABELS_EDIT/LABELS_ASSOCIATION event handlers to process, then returns updated labels from DB via fetchLabels(). Key: isLatest=true means incremental (safe, no disconnect). isLatest=false would download full snapshot and cause disconnection.
Throws BadRequestException as syncLabels is only available for Baileys (WhatsApp Web) connections.
Throws BadRequestException as syncLabels is only available for Baileys (WhatsApp Web) connections.
Contributor
Reviewer's GuideImplements a new full label sync endpoint for Baileys WhatsApp channels that forces a complete app-state resync, snapshots label associations, and transactionally refreshes Chat.labels projections, while wiring the endpoint through controller/router layers, adding unsupported stubs for other channels, and covering behavior with targeted tests. Sequence diagram for the new full label sync endpointsequenceDiagram
actor Client
participant LabelRouter
participant LabelController
participant BaileysService as BaileysStartupService
participant Prisma
Client->>LabelRouter: GET syncLabels
LabelRouter->>LabelController: syncLabels(instance)
LabelController->>BaileysService: syncLabels()
BaileysService->>BaileysService: authState.state.keys.set(app-state-sync-version)
BaileysService->>BaileysService: client.resyncAppState(regular)
BaileysService->>BaileysService: eventProcessingQueue (await)
loop During resync
BaileysService->>BaileysService: collectLabelAssociationSnapshot(data)
end
alt [collector.observedLabelState]
BaileysService->>Prisma: replaceChatLabelsFromSnapshot(instanceId, labelsByChatId)
Prisma-->>BaileysService: transaction committed
else [no label state observed]
BaileysService->>BaileysService: logger.warn(keep existing projection)
end
BaileysService->>BaileysService: setTimeout(3000)
BaileysService->>Prisma: fetchLabels()
Prisma-->>BaileysService: LabelDto[]
BaileysService-->>LabelController: LabelDto[]
LabelController-->>LabelRouter: LabelDto[]
LabelRouter-->>Client: 200 OK (LabelDto[])
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
HulianBuligon
marked this pull request as ready for review
July 27, 2026 17:42
Contributor
There was a problem hiding this comment.
Sorry @HulianBuligon, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📋 Description
This is an updated successor to #2421, rebased onto the current
developbranch. It preserves the five original commits and authorship from @josuecocoman, then adds an isolated correctness fix in06349eba.The original endpoint requested an incremental
regularapp-state sync. That refreshes label metadata, but it does not provide a complete projection from which staleChat.labelsassociations can be removed.This version:
regularapp-state sync version before resyncing, forcing a complete snapshot;label_jidassociations while the snapshot is processed;Chat.labelsprojection through Prisma;The endpoint and unsupported-channel stubs from #2421 remain unchanged.
🔗 Related Issue
Maintainers can merge this PR directly or use the isolated fix commit to update #2421.
🧪 Type of Change
🧪 Testing
The regression tests cover both full replacement of stale labels and the fail-closed path where no label state is observed.
✅ Checklist
📝 Additional Notes
The snapshot replacement uses Prisma operations supported by the repository's PostgreSQL and MySQL providers. No schema or migration change is required.
Summary by Sourcery
Add a full WhatsApp Baileys label resync endpoint that rebuilds chat label projections from a forced app-state snapshot while keeping other channels unchanged.
New Features:
Bug Fixes:
Enhancements:
Tests: