Skip to content

feat: POST /user/contacts — save a contact to the device addressbook - #129

Open
FlavioPulli wants to merge 1 commit into
evolution-foundation:mainfrom
FlavioPulli:feat/save-contact-endpoint
Open

feat: POST /user/contacts — save a contact to the device addressbook#129
FlavioPulli wants to merge 1 commit into
evolution-foundation:mainfrom
FlavioPulli:feat/save-contact-endpoint

Conversation

@FlavioPulli

@FlavioPulli FlavioPulli commented Jul 24, 2026

Copy link
Copy Markdown

WhatsApp syncs the contact list across devices via app state (the same channel as mute/pin/archive), and whatsmeow already applies incoming contact mutations back into Store.Contacts. The API exposes only the read side (GET /user/contacts); this adds the write side.

POST /user/contacts {phone, fullName, firstName?} builds a contact-index mutation on the critical_unblock_low patch carrying a ContactAction (FullName/FirstName + SaveOnPrimaryAddressbook: true, which asks the primary phone to also store the contact in the system addressbook) and sends it through the official Client.SendAppState primitive. After the resync SendAppState triggers, the saved contact shows up in GET /user/contacts like any contact saved on the phone itself.

Verified end to end on a paired Android device (contact appears in the phone's WhatsApp list and system addressbook); in our production since 2026-07-22.

Summary by Sourcery

Add write-side support for user contacts by wiring a new SaveContact flow from HTTP handler through the user service to WhatsApp app state mutations so contacts saved via the API appear in the synced contact list and device address book.

New Features:

  • Add POST /user/contacts endpoint to save or update a contact in the WhatsApp contact list and device address book.

Enhancements:

  • Introduce UserService.SaveContact and corresponding handler to encapsulate contact creation logic and app state mutation handling.

…sbook

WhatsApp syncs the contact list across devices via app state (the same
channel as mute/pin/archive), and whatsmeow already applies incoming
"contact" mutations back into Store.Contacts. The API exposes only the
read side (GET /user/contacts); this adds the write side: a
"contact"-index mutation on the critical_unblock_low patch carrying a
ContactAction (FullName/FirstName + SaveOnPrimaryAddressbook, which asks
the primary phone to also store the contact in the system addressbook),
sent through the official Client.SendAppState primitive.

After the resync SendAppState triggers, the saved contact shows up in
GET /user/contacts like any contact saved on the phone itself.

Battle-tested in production since 2026-07-22 (verified end to end on a
paired Android device: contact appears in the phone's WhatsApp contact
list and system addressbook).
@sourcery-ai

sourcery-ai Bot commented Jul 24, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds a POST /user/contacts endpoint and corresponding handler/service logic that builds and sends a WhatsApp app state contact mutation via whatsmeow to save a contact to the device and primary address book.

Sequence diagram for POST /user/contacts saving a contact via app state

sequenceDiagram
  actor ApiClient
  participant GinRouter
  participant UserHandler
  participant UserService
  participant WhatsmeowClient

  ApiClient->>GinRouter: POST /user/contacts
  GinRouter->>UserHandler: SaveContact(ctx)
  UserHandler->>UserHandler: ctx.MustGet(instance)
  UserHandler->>UserHandler: ctx.ShouldBindBodyWithJSON(SaveContactStruct)
  UserHandler->>UserHandler: [validate phone and fullName]
  UserHandler->>UserService: SaveContact(data, instance)
  UserService->>UserService: ensureClientConnected(instance.Id)
  UserService->>WhatsmeowClient: SendAppState(patch)
  WhatsmeowClient-->>UserService: result
  UserService->>UserService: LogInfo(Contact saved)
  UserService-->>UserHandler: nil / error
  UserHandler-->>ApiClient: 200 {"message":"success"} / 500 {"error":...}
Loading

File-Level Changes

Change Details Files
Introduce service-layer operation to construct and send WhatsApp app-state contact mutations to save contacts.
  • Define SaveContactStruct request model with phone and name fields mapped to JSON.
  • Implement userService.SaveContact to validate inputs, derive firstName when missing, build contact-index mutation on the critical_unblock_low patch using waSyncAction.ContactAction, and send via Client.SendAppState.
  • Use a fixed contactMutationVersion constant and log both success and error cases using the instance-scoped logger.
pkg/user/service/user_service.go
pkg/user/service/save_contact.go
Expose the save-contact capability via a new HTTP handler and route.
  • Extend the UserService and UserHandler interfaces with SaveContact methods.
  • Add POST /user/contacts route wired to userHandler.SaveContact.
  • Implement userHandler.SaveContact to fetch the instance from context, bind and validate JSON body, call the service SaveContact method, and return appropriate HTTP status codes and messages.
pkg/routes/routes.go
pkg/user/handler/user_handler.go
pkg/user/handler/save_contact.go

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:

  • The handler duplicates validation that already exists in SaveContact (checking Number and FullName non-empty); consider consolidating this validation in one place to avoid drift and inconsistent error messages.
  • In SaveContact handler, ctx.ShouldBindBodyWithJSON is not a standard gin method; replacing it with ctx.ShouldBindJSON(&data) (and using a non-pointer SaveContactStruct for binding) will be more idiomatic and avoid potential runtime issues.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The handler duplicates validation that already exists in `SaveContact` (checking `Number` and `FullName` non-empty); consider consolidating this validation in one place to avoid drift and inconsistent error messages.
- In `SaveContact` handler, `ctx.ShouldBindBodyWithJSON` is not a standard gin method; replacing it with `ctx.ShouldBindJSON(&data)` (and using a non-pointer `SaveContactStruct` for binding) will be more idiomatic and avoid potential runtime issues.

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.

1 participant