feat: POST /user/contacts — save a contact to the device addressbook - #129
Open
FlavioPulli wants to merge 1 commit into
Open
feat: POST /user/contacts — save a contact to the device addressbook#129FlavioPulli wants to merge 1 commit into
FlavioPulli wants to merge 1 commit into
Conversation
…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).
Reviewer's GuideAdds 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 statesequenceDiagram
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":...}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The handler duplicates validation that already exists in
SaveContact(checkingNumberandFullNamenon-empty); consider consolidating this validation in one place to avoid drift and inconsistent error messages. - In
SaveContacthandler,ctx.ShouldBindBodyWithJSONis not a standard gin method; replacing it withctx.ShouldBindJSON(&data)(and using a non-pointerSaveContactStructfor 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.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
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.
WhatsApp syncs the contact list across devices via app state (the same channel as mute/pin/archive), and whatsmeow already applies incoming
contactmutations back intoStore.Contacts. The API exposes only the read side (GET /user/contacts); this adds the write side.POST /user/contacts{phone, fullName, firstName?}builds acontact-index mutation on thecritical_unblock_lowpatch carrying aContactAction(FullName/FirstName+SaveOnPrimaryAddressbook: true, which asks the primary phone to also store the contact in the system addressbook) and sends it through the officialClient.SendAppStateprimitive. After the resync SendAppState triggers, the saved contact shows up inGET /user/contactslike 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:
Enhancements: