Repository navigation
Add sticker pack media and sticker sync operations #59
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
51b676e
Add sticker pack media, bounded archives and sticker sync operations
purpshell 38a4337
Upload stickers on their MMS path with image keys
purpshell 8c42013
Upload pack thumbnails under the parent master key
purpshell 114743d
Preserve sticker result handler failures through decryption
purpshell d583a2d
style(proto): match CI goimports formatting
purpshell 5c0eb0d
fix(stickerpack): bound ZIP directory admission and serialized output
purpshell abe7bb5
fix(stickerpack): guard extreme reads and both ZIP64 interpretations
purpshell 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,33 @@ | ||
| package appstate | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "fmt" | ||
|
|
||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| "github.com/polymorfa/hypermeow/proto/waSyncAction" | ||
| ) | ||
|
|
||
| // BuildFavoriteSticker builds a regular_low v7 favorite/unfavorite mutation. | ||
| // The hash is the plaintext SHA-256. Favoriting requires downloadable media details. | ||
| // Evidence: WAWebStickersFavoriteSyncAction, whatsapp-1053201649, lines 20-25, 81-99. | ||
| func BuildFavoriteSticker(hash []byte, action *waSyncAction.StickerAction) (PatchInfo, error) { | ||
| if len(hash) != 32 || action == nil || action.IsFavorite == nil { | ||
| return PatchInfo{}, fmt.Errorf("sticker hash and favorite state are required") | ||
| } | ||
| if action.GetIsFavorite() && (len(action.MediaKey) != 32 || len(action.FileEncSHA256) != 32 || action.GetDirectPath() == "" || action.GetMimetype() == "") { | ||
| return PatchInfo{}, fmt.Errorf("favorite sticker requires media details") | ||
| } | ||
| return PatchInfo{Type: WAPatchRegularLow, Mutations: []MutationInfo{{Index: []string{IndexFavoriteSticker, base64.StdEncoding.EncodeToString(hash)}, Version: 7, Value: &waSyncAction.SyncActionValue{StickerAction: proto.Clone(action).(*waSyncAction.StickerAction)}}}}, nil | ||
| } | ||
|
|
||
| // BuildRemoveRecentSticker removes a recent sticker up to lastSentTimestampMS. | ||
| // The timestamp guard preserves a sticker sent again after the removal was made. | ||
| // Evidence: WAWebStickersRemoveRecentSyncAction, whatsapp-1053201649, lines 16-21, 35-38, 46-55. | ||
| func BuildRemoveRecentSticker(hash []byte, lastSentTimestampMS int64) (PatchInfo, error) { | ||
| if len(hash) != 32 || lastSentTimestampMS < 0 { | ||
| return PatchInfo{}, fmt.Errorf("valid sticker hash and last sent timestamp are required") | ||
| } | ||
| return PatchInfo{Type: WAPatchRegularLow, Mutations: []MutationInfo{{Index: []string{IndexRemoveRecentSticker, base64.StdEncoding.EncodeToString(hash)}, Version: 7, Value: &waSyncAction.SyncActionValue{RemoveRecentStickerAction: &waSyncAction.RemoveRecentStickerAction{LastStickerSentTS: proto.Int64(lastSentTimestampMS)}}}}}, 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package appstate | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| "github.com/polymorfa/hypermeow/proto/waSyncAction" | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| func TestStickerPatches(t *testing.T) { | ||
| hash := bytes.Repeat([]byte{1}, 32) | ||
| p, err := BuildFavoriteSticker(hash, &waSyncAction.StickerAction{IsFavorite: proto.Bool(false)}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if p.Type != WAPatchRegularLow || p.Mutations[0].Version != 7 || p.Mutations[0].Index[0] != IndexFavoriteSticker { | ||
| t.Fatal("wrong favorite patch") | ||
| } | ||
| if _, err = BuildFavoriteSticker(hash, &waSyncAction.StickerAction{IsFavorite: proto.Bool(true)}); err == nil { | ||
| t.Fatal("favorite without media accepted") | ||
| } | ||
| p, err = BuildRemoveRecentSticker(hash, 123) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if p.Mutations[0].Value.GetRemoveRecentStickerAction().GetLastStickerSentTS() != 123 || p.Mutations[0].Version != 7 { | ||
| t.Fatal("missing timestamp") | ||
| } | ||
| if _, err = BuildRemoveRecentSticker(nil, 123); err == nil { | ||
| t.Fatal("missing hash accepted") | ||
| } | ||
| } | ||
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
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
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,38 @@ | ||
| package whatsmeow | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "fmt" | ||
|
|
||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| "github.com/polymorfa/hypermeow/proto/waE2E" | ||
| ) | ||
|
|
||
| // BuildStickerReuploadRequest requests fresh media details from the primary device. | ||
| // Send it with SendPeerMessage. Acceptance of the request does not mean the phone | ||
| // found or uploaded the stickers; results arrive in events.StickerReupload. | ||
| func (cli *Client) BuildStickerReuploadRequest(hashes [][]byte) (*waE2E.Message, error) { | ||
| if len(hashes) == 0 { | ||
| return nil, fmt.Errorf("at least one sticker hash is required") | ||
| } | ||
| req := &waE2E.PeerDataOperationRequestMessage{PeerDataOperationRequestType: waE2E.PeerDataOperationRequestType_UPLOAD_STICKER.Enum()} | ||
| seen := make(map[string]bool) | ||
| for _, hash := range hashes { | ||
| if len(hash) != 32 { | ||
| return nil, fmt.Errorf("sticker hashes must contain 32 bytes") | ||
| } | ||
| key := base64.StdEncoding.EncodeToString(hash) | ||
| if !seen[key] { | ||
| req.RequestStickerReupload = append(req.RequestStickerReupload, &waE2E.PeerDataOperationRequestMessage_RequestStickerReupload{FileSHA256: proto.String(key)}) | ||
| seen[key] = true | ||
| } | ||
| } | ||
| return &waE2E.Message{ProtocolMessage: &waE2E.ProtocolMessage{Type: waE2E.ProtocolMessage_PEER_DATA_OPERATION_REQUEST_MESSAGE.Enum(), PeerDataOperationRequestMessage: req}}, nil | ||
| } | ||
|
|
||
| // BuildRecentStickerBootstrapRequest requests recents in a later history sync. | ||
| // Send with SendPeerMessage. The primary device must be available to respond. | ||
| func (cli *Client) BuildRecentStickerBootstrapRequest() *waE2E.Message { | ||
| return &waE2E.Message{ProtocolMessage: &waE2E.ProtocolMessage{Type: waE2E.ProtocolMessage_PEER_DATA_OPERATION_REQUEST_MESSAGE.Enum(), PeerDataOperationRequestMessage: &waE2E.PeerDataOperationRequestMessage{PeerDataOperationRequestType: waE2E.PeerDataOperationRequestType_SEND_RECENT_STICKER_BOOTSTRAP.Enum()}}} | ||
| } |
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,48 @@ | ||
| package whatsmeow | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| "github.com/polymorfa/hypermeow/proto/waE2E" | ||
| "github.com/polymorfa/hypermeow/store" | ||
| "github.com/polymorfa/hypermeow/types" | ||
| "github.com/polymorfa/hypermeow/types/events" | ||
| waLog "github.com/polymorfa/hypermeow/util/log" | ||
| ) | ||
|
|
||
| func TestStickerResultPreservesHandlerFailureAndSuppressesMessage(t *testing.T) { | ||
| for _, wrapped := range []bool{false, true} { | ||
| for _, success := range []bool{false, true} { | ||
| cli := NewClient(&store.Device{}, waLog.Noop) | ||
| results, messages := 0, 0 | ||
| cli.AddEventHandlerWithSuccessStatus(func(event any) bool { | ||
| switch e := event.(type) { | ||
| case *events.StickerReupload: | ||
| results++ | ||
| if e.RequestID != "request" { | ||
| t.Errorf("request identity lost: %s", e.RequestID) | ||
| } | ||
| return success | ||
| case *events.Message: | ||
| messages++ | ||
| } | ||
| return true | ||
| }) | ||
| msg := &waE2E.Message{ProtocolMessage: &waE2E.ProtocolMessage{PeerDataOperationRequestResponseMessage: &waE2E.PeerDataOperationRequestResponseMessage{PeerDataOperationRequestType: waE2E.PeerDataOperationRequestType_UPLOAD_STICKER.Enum(), StanzaID: proto.String("request")}}} | ||
| if wrapped { | ||
| msg = &waE2E.Message{DeviceSentMessage: &waE2E.DeviceSentMessage{Message: msg}} | ||
| } | ||
| info := &types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: true, Sender: types.JID{User: "fixture", Server: types.DefaultUserServer}}} | ||
| failed := cli.handleDecryptedMessage(context.Background(), info, msg, 0) | ||
| if failed == success { | ||
| t.Errorf("wrapped=%t success=%t: handler failure=%t", wrapped, success, failed) | ||
| } | ||
| if results != 1 || messages != 0 { | ||
| t.Errorf("wrapped=%t success=%t: results=%d ordinary messages=%d", wrapped, success, results, messages) | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.