Skip to content

fix(send): handle document-with-caption and @lid JIDs in mentionAll - #137

Open
cesar-carlos wants to merge 1 commit into
evolution-foundation:mainfrom
cesar-carlos:fix/mention-all-media
Open

fix(send): handle document-with-caption and @lid JIDs in mentionAll#137
cesar-carlos wants to merge 1 commit into
evolution-foundation:mainfrom
cesar-carlos:fix/mention-all-media

Conversation

@cesar-carlos

@cesar-carlos cesar-carlos commented Jul 28, 2026

Copy link
Copy Markdown

Summary

  • Fixes panic / lost mentions when /send/media sends a document with caption together with mentionAll / mentionedJid (DocumentMessage is moved under DocumentWithCaptionMessage but ContextInfo was still written on the nil pointer).
  • Prefer participant.PhoneNumber over @lid JID when building mentionAll lists (WhatsApp mentions need @s.whatsapp.net).
  • Extract setMessageMentionedJIDs / participantMentionJID helpers and add unit tests.

Closes #114

Test plan

  • go test ./pkg/sendMessage/service/ -run 'TestParticipantMentionJID|TestSetMessageMentionedJIDs'
  • go vet ./pkg/sendMessage/service/
  • Manual: POST /send/media to a group with type=document, caption, mentionAll=true returns 200 and notifies participants
  • Manual: same with type=image + caption + mentionAll=true

Made with Cursor

Summary by Sourcery

Fix mention handling for group messages, especially documents with captions, and centralize mention JID selection and assignment logic.

Bug Fixes:

  • Prevent panics and lost mentions when sending document messages with captions using mentionAll or mentionedJid.
  • Ensure group mentionAll uses phone-number JIDs instead of @lid JIDs so WhatsApp mentions resolve correctly.

Enhancements:

  • Extract shared helpers for computing participant mention JIDs and setting MentionedJID on different message types to reduce duplication and handle document-with-caption messages consistently.

Tests:

  • Add unit tests covering participant mention JID selection, MentionedJID assignment across message types, and the document-with-caption panic regression case.

Document+caption moves DocumentMessage under DocumentWithCaptionMessage
but mentionAll still wrote ContextInfo on the nil pointer. Prefer
PhoneNumber over LID when building mention JIDs.

Closes evolution-foundation#114

Co-authored-by: Cursor <cursoragent@cursor.com>
@sourcery-ai

sourcery-ai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Reviewer's Guide

Refactors how MentionedJID is set on outgoing messages, fixes crashes when sending document-with-caption messages with mentions, and ensures mention-all uses phone-number JIDs instead of @lid JIDs, with new helpers and unit tests to cover the behavior.

Sequence diagram for mentionAll handling with document-with-caption messages

sequenceDiagram
    actor Client
    participant SendService
    participant GroupInfo
    participant MentionHelpers
    participant WAMessage

    Client->>SendService: SendMessage(instance, msg)
    SendService->>GroupInfo: groupInfo.Participants
    loop participants
        SendService->>MentionHelpers: participantMentionJID(participant)
        MentionHelpers-->>SendService: mentionJID
    end
    SendService->>MentionHelpers: setMessageMentionedJIDs(msg, messageType, mentionedJIDs)
    alt [messageType is DocumentMessage]
        alt [message has DocumentMessage]
            MentionHelpers->>WAMessage: msg.DocumentMessage.ContextInfo.MentionedJID = mentionedJIDs
        else [message has DocumentWithCaptionMessage]
            MentionHelpers->>WAMessage: msg.DocumentWithCaptionMessage.Message.DocumentMessage.ContextInfo.MentionedJID = mentionedJIDs
        end
    else [other message types]
        MentionHelpers->>WAMessage: <type>.ContextInfo.MentionedJID = mentionedJIDs
    end
Loading

File-Level Changes

Change Details Files
Refactor mention-all and explicit mention handling to use a shared helper for setting MentionedJID across message types, including document-with-caption messages to avoid panics.
  • Replace duplicated switch logic in SendMessage for mentionAll and MentionedJID with calls to a new setMessageMentionedJIDs helper
  • In setMessageMentionedJIDs, guard each message type against nil pointers before writing ContextInfo
  • Handle DocumentMessage specially so that when documents have captions, MentionedJID is written into the nested DocumentWithCaptionMessage.Message.DocumentMessage instead of the top-level DocumentMessage
pkg/sendMessage/service/send_service.go
pkg/sendMessage/service/mention.go
Ensure mention-all uses phone-number JIDs where available instead of @lid JIDs so mentions resolve to @s.whatsapp.net.
  • Introduce participantMentionJID helper to prefer participant.PhoneNumber over participant.JID when building mention lists
  • Update mentionAll loop to use participantMentionJID when appending group participant IDs
pkg/sendMessage/service/send_service.go
pkg/sendMessage/service/mention.go
Add unit tests to lock in mention JID selection and document-with-caption behavior and prevent regressions.
  • Add tests for participantMentionJID to validate preference of phone-number JID and fallback behavior
  • Add tests for setMessageMentionedJIDs across several message types including document-with-caption to verify MentionedJID is set correctly
  • Add a regression test to ensure document-with-caption messages do not panic and do not re-populate the deprecated top-level DocumentMessage
pkg/sendMessage/service/send_service_mention_test.go

Assessment against linked issues

Issue Objective Addressed Explanation
#114 Allow sending media (image/video/document) with caption text and mentionAll/mentionedJid in a single /send/media request without failure or needing to split into separate messages.
#114 Ensure mentionAll uses correct participant JIDs (non-@lid, @s.whatsapp.net-compatible) so that mentions are delivered reliably in group messages.
#114 Correctly apply MentionedJID/ContextInfo for all supported message types, including document-with-caption, to avoid panics or lost mentions.

Possibly linked issues

  • #(not specified): PR fixes ContextInfo/MentionedJID for media-with-caption + mentionAll, matching the issue’s failing combined media flow

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • setMessageMentionedJIDs relies on the messageType string; consider refactoring callers to pass a more structured enum/constant or derive the type from the populated message field to avoid bugs from typos or mismatches between the string and the actual message content.
  • In setMessageMentionedJIDs, it might be useful to handle or log unexpected/unsupported messageType values (e.g., via a default case) so that future message types fail visibly instead of silently doing nothing.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- `setMessageMentionedJIDs` relies on the `messageType` string; consider refactoring callers to pass a more structured enum/constant or derive the type from the populated message field to avoid bugs from typos or mismatches between the string and the actual message content.
- In `setMessageMentionedJIDs`, it might be useful to handle or log unexpected/unsupported `messageType` values (e.g., via a default case) so that future message types fail visibly instead of silently doing nothing.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mentionAll + media: Sending media with mentionAll causes failure / requires split workaround

1 participant