-
Notifications
You must be signed in to change notification settings - Fork 320
feat: POST /user/contacts — save a contact to the device addressbook #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FlavioPulli
wants to merge
1
commit into
evolution-foundation:develop
Choose a base branch
from
FlavioPulli:feat/save-contact-endpoint-develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package user_handler | ||
|
|
||
| // HTTP handler for saving a contact to the device addressbook. | ||
| // See the rationale in pkg/user/service/save_contact.go. | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| instance_model "github.com/EvolutionAPI/evolution-go/pkg/instance/model" | ||
| user_service "github.com/EvolutionAPI/evolution-go/pkg/user/service" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // Save a contact to the device addressbook | ||
| // @Summary Save a contact | ||
| // @Description Save/update a contact in the WhatsApp contact list (app state), asking the | ||
| // @Description primary device to also store it in the system addressbook. | ||
| // @Tags User | ||
| // @Accept json | ||
| // @Produce json | ||
| // @Param message body user_service.SaveContactStruct true "Contact data" | ||
| // @Success 200 {object} gin.H "success" | ||
| // @Failure 400 {object} gin.H "Error on validation" | ||
| // @Failure 500 {object} gin.H "Internal server error" | ||
| // @Router /user/contacts [post] | ||
| func (u *userHandler) SaveContact(ctx *gin.Context) { | ||
| getInstance := ctx.MustGet("instance") | ||
|
|
||
| instance, ok := getInstance.(*instance_model.Instance) | ||
| if !ok { | ||
| ctx.JSON(http.StatusInternalServerError, gin.H{"error": "instance not found"}) | ||
| return | ||
| } | ||
|
|
||
| var data *user_service.SaveContactStruct | ||
| err := ctx.ShouldBindBodyWithJSON(&data) | ||
| if err != nil { | ||
| ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
| return | ||
| } | ||
|
|
||
| if len(data.Number) < 1 || len(data.FullName) < 1 { | ||
| ctx.JSON(http.StatusBadRequest, gin.H{"error": "phone and fullName are required"}) | ||
| return | ||
| } | ||
|
|
||
| if err := u.userService.SaveContact(data, instance); err != nil { | ||
| ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| return | ||
| } | ||
|
|
||
| ctx.JSON(http.StatusOK, gin.H{"message": "success"}) | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package user_service | ||
|
|
||
| // Save a contact to the device addressbook. | ||
| // | ||
| // WhatsApp syncs the contact list across devices via APP STATE (the same | ||
| // channel as mute/pin/archive): a "contact"-index mutation on the | ||
| // critical_unblock_low patch — which whatsmeow itself applies back into | ||
| // Store.Contacts on receipt (appstate.go, IndexContact → PutContactName). | ||
| // The API currently exposes only the READ side (GET /user/contacts); this | ||
| // adds the WRITE side: build the ContactAction (FullName/FirstName + | ||
| // SaveOnPrimaryAddressbook, which asks the primary phone to also store it in | ||
| // the SYSTEM addressbook) and send it with the official primitive | ||
| // Client.SendAppState. After the resync SendAppState triggers, the contact | ||
| // shows up in GET /user/contacts. | ||
| // | ||
| // Own file + minimal call-site lines (interface/handler/route) to keep the | ||
| // change easy to review and rebase. | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
|
|
||
| instance_model "github.com/EvolutionAPI/evolution-go/pkg/instance/model" | ||
| "go.mau.fi/whatsmeow/appstate" | ||
| waSyncAction "go.mau.fi/whatsmeow/proto/waSyncAction" | ||
| "go.mau.fi/whatsmeow/types" | ||
| "google.golang.org/protobuf/proto" | ||
| ) | ||
|
|
||
| type SaveContactStruct struct { | ||
| Number string `json:"phone"` | ||
| FullName string `json:"fullName"` | ||
| FirstName string `json:"firstName"` | ||
| } | ||
|
|
||
| // contactMutationVersion is the static version of the "contact" index | ||
| // mutation (each index has its own — mute=2, pin=5, …; reference | ||
| // implementations use 2 for contact). A wrong value fails CLEANLY in | ||
| // SendAppState (the server rejects the patch), with no side effects. | ||
| const contactMutationVersion = 2 | ||
|
|
||
| func (u *userService) SaveContact(data *SaveContactStruct, instance *instance_model.Instance) error { | ||
| client, err := u.ensureClientConnected(instance.Id) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| number := strings.TrimSpace(data.Number) | ||
| fullName := strings.TrimSpace(data.FullName) | ||
| if number == "" || fullName == "" { | ||
| return errors.New("phone and fullName are required") | ||
| } | ||
| firstName := strings.TrimSpace(data.FirstName) | ||
| if firstName == "" { | ||
| firstName = strings.Fields(fullName)[0] | ||
| } | ||
| jid := types.NewJID(number, types.DefaultUserServer) | ||
|
|
||
| patch := appstate.PatchInfo{ | ||
| Type: appstate.WAPatchCriticalUnblockLow, // the patch that carries the contact list | ||
| Mutations: []appstate.MutationInfo{{ | ||
| Index: []string{appstate.IndexContact, jid.String()}, | ||
| Version: contactMutationVersion, | ||
| Value: &waSyncAction.SyncActionValue{ | ||
| ContactAction: &waSyncAction.ContactAction{ | ||
| FullName: proto.String(fullName), | ||
| FirstName: proto.String(firstName), | ||
| SaveOnPrimaryAddressbook: proto.Bool(true), | ||
| }, | ||
| }, | ||
| }}, | ||
| } | ||
| if err := client.SendAppState(context.Background(), patch); err != nil { | ||
| u.loggerWrapper.GetLogger(instance.Id).LogError("[%s] SaveContact %s: %v", instance.Id, jid.String(), err) | ||
| return err | ||
| } | ||
| u.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Contact saved to addressbook: %s (%s)", instance.Id, fullName, jid.String()) | ||
| return nil | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Binding into a pointer-to-pointer is likely incorrect and may prevent proper JSON deserialization.
datais declared as*user_service.SaveContactStruct, so&datais**user_service.SaveContactStruct, which Gin’s binders don’t expect and may break deserialization. Use a concrete value instead: