fix(channels): return channel messages instead of an always-empty list (#625)#629
Merged
Merged
Conversation
…625) whatsapp-web.js 1.34.x has no client.getChannelById; the adapter called it, so getChannelMessages/getChannelById always threw and the error was swallowed (into [] / null). GET /channels worked only because it uses the real getChannels(). - Resolve the channel from getChannels() (whose items are Channel instances that carry fetchMessages()) and read its messages; stop swallowing errors. - An unknown/unsubscribed channel now returns 404 (ChannelNotFoundError, a NotFoundException subclass) instead of a silent empty 200 — consistent with getChannelById/findOne. - Drop the phantom getChannelById from the BusinessClient type.
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.
Summary
Fixes #625. On the whatsapp-web.js engine,
GET /api/sessions/:sessionId/channels/:channelId/messagesalways returned an empty array (200,content-length: 2) even for channels with messages.Root cause
The adapter resolved the channel with
client.getChannelById(channelId), but whatsapp-web.js 1.34.x has nogetChannelByIdmethod on itsClient. A hand-writtenBusinessClienttype falsely declared it, so the compiler was satisfied while the runtime call threwTypeError: ...getChannelById is not a function. Atry/catchthen swallowed that into[].GET /channels(list) was unaffected because it uses the realgetChannels().The same phantom call sat in
getChannelById(theGET /channels/:channelIddetail endpoint), which therefore always resolved tonull.Changes
getChannelMessagesnow resolves the channel fromgetChannels()— whose items are realChannelinstances that carryfetchMessages()— and reads its messages. The error is no longer swallowed.404via a newChannelNotFoundError(aNotFoundExceptionsubclass, mirroring the existingMessageNotFoundError) instead of a silent empty200. This maps to404through NestJS's built-in handler with the message intact and is consistent withGET /channels/:channelId, which already returns404for the same condition.getChannelById(detail endpoint) now resolves from the subscribed-channel list, so it returns the channel (ornull→404at the service) instead of alwaysnull.getChannelByIdfrom theBusinessClienttype.Notes
whatsapp-web.js reads channel messages from the session's local message store (
Channel.fetchMessageswalks the in-memory store +loadEarlierMsgs). This change makes the endpoint actually return whatever the engine has synced; for a freshly-subscribed channel whose history has not yet hydrated locally, the store can still be sparse. That is a separate engine-level limitation, independent of the always-empty bug fixed here.The Baileys engine is unaffected: its channel methods intentionally reject with
EngineNotSupportedError(channels are a whatsapp-web.js-only feature), so they fail explicitly rather than returning a misleading empty list.Verification
getChannels()(not the phantom method),404on unknown channel, empty channel →[], andgetChannelById→nullfor an unknown id.