Repository navigation
newsletter: preserve observations and decode milestone positions #54
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
purpshell
merged 1 commit into
codex/upstream-sync-20261027
from
codex/newsletter-observations-20261027
Nov 12, 2026
Merged
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
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,56 @@ | ||
| package whatsmeow | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| waBinary "github.com/polymorfa/hypermeow/binary" | ||
| "github.com/polymorfa/hypermeow/proto/waE2E" | ||
| "github.com/polymorfa/hypermeow/types" | ||
| "github.com/polymorfa/hypermeow/types/events" | ||
| ) | ||
|
|
||
| func TestNewsletterLiveUpdatesPreserveWrappedAndLegacyMessages(t *testing.T) { | ||
| for _, wrapped := range []bool{false, true} { | ||
| name := "legacy" | ||
| if wrapped { | ||
| name = "wrapped" | ||
| } | ||
| t.Run(name, func(t *testing.T) { | ||
| cli, _, _ := newTestRemote(t) | ||
| var got *events.NewsletterLiveUpdate | ||
| cli.AddEventHandler(func(evt any) { | ||
| got, _ = evt.(*events.NewsletterLiveUpdate) | ||
| }) | ||
| payload, err := proto.Marshal(&waE2E.Message{Conversation: proto.String("synthetic channel update")}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| children := []waBinary.Node{{ | ||
| Tag: "message", Attrs: waBinary.Attrs{"server_id": "7", "id": "synthetic-message", "type": "text", "t": "1700000000"}, | ||
| Content: []waBinary.Node{ | ||
| {Tag: "plaintext", Content: payload}, | ||
| {Tag: "views_count", Attrs: waBinary.Attrs{"count": "11"}}, | ||
| {Tag: "forwards_count", Attrs: waBinary.Attrs{"count": "3"}}, | ||
| }, | ||
| }} | ||
| if wrapped { | ||
| children = []waBinary.Node{{Tag: "messages", Content: children}} | ||
| } | ||
| jid := types.NewJID("120363000000001", types.NewsletterServer) | ||
| cli.handleNewsletterNotification(context.Background(), &waBinary.Node{ | ||
| Tag: "notification", Attrs: waBinary.Attrs{"from": jid, "t": "1700000000"}, | ||
| Content: []waBinary.Node{{Tag: "live_updates", Content: children}}, | ||
| }) | ||
| if got == nil || got.JID != jid || len(got.Messages) != 1 { | ||
| t.Fatalf("live update lost its message: %+v", got) | ||
| } | ||
| message := got.Messages[0] | ||
| if message.MessageServerID != 7 || message.ViewsCount != 11 || message.ForwardsCount != 3 || message.Message.GetConversation() != "synthetic channel update" { | ||
| t.Fatalf("live update lost content or counts: %+v", message) | ||
| } | ||
| }) | ||
| } | ||
| } |
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,84 @@ | ||
| package whatsmeow | ||
|
|
||
| import ( | ||
| waBinary "github.com/polymorfa/hypermeow/binary" | ||
| "github.com/polymorfa/hypermeow/types" | ||
| ) | ||
|
|
||
| func newsletterCounter(attrs *waBinary.AttrUtility) (int, bool) { | ||
| value, ok := attrs.GetInt64("count", true) | ||
| return int(value), ok && attrs.OK() && value >= 0 && int64(int(value)) == value | ||
| } | ||
|
|
||
| // Preserve wire observations separately from the legacy scalar count fields. | ||
| // Invalid or ambiguous counters are omitted, never turned into reported zeros. | ||
| func parseNewsletterObservations(msg *types.NewsletterMessage, children []waBinary.Node) bool { | ||
| var badViews, badForwards, badReactions, sawForwards, sawReactions bool | ||
| seenViews := make(map[string]struct{}, 2) | ||
| for _, child := range children { | ||
| switch child.Tag { | ||
| case "views_count": | ||
| attrs := child.AttrGetter() | ||
| definition := attrs.OptionalString("type") | ||
| count, valid := newsletterCounter(attrs) | ||
| _, duplicate := seenViews[definition] | ||
| if !valid || len(msg.ViewCounts) == 2 || duplicate { | ||
| badViews = true | ||
| } | ||
| if !badViews { | ||
| msg.ViewsCount = count | ||
| seenViews[definition] = struct{}{} | ||
| msg.ViewCounts = append(msg.ViewCounts, types.NewsletterViewCount{Type: definition, Count: count}) | ||
| msg.ViewsCountPresent = true | ||
| } | ||
| case "forwards_count": | ||
| count, valid := newsletterCounter(child.AttrGetter()) | ||
| badForwards = badForwards || !valid || sawForwards | ||
| sawForwards = true | ||
| msg.ForwardsCountPresent = !badForwards | ||
| if !badForwards { | ||
| msg.ForwardsCount = count | ||
| } | ||
| case "reactions": | ||
| badReactions = badReactions || sawReactions | ||
| sawReactions = true | ||
| counts := make(map[string]int) | ||
| entries := 0 | ||
| for _, reaction := range child.GetChildren() { | ||
| if reaction.Tag != "reaction" { | ||
| continue | ||
| } | ||
| entries++ | ||
| if entries > 1000 { | ||
| badReactions = true | ||
| break | ||
| } | ||
| attrs := reaction.AttrGetter() | ||
| code := attrs.String("code") | ||
| count, valid := newsletterCounter(attrs) | ||
| if !valid || code == "" { | ||
| badReactions = true | ||
| continue | ||
| } | ||
| if _, duplicate := counts[code]; duplicate { | ||
| badReactions = true | ||
| continue | ||
| } | ||
| counts[code] = count | ||
| } | ||
| msg.ReactionCounts = counts | ||
| } | ||
| } | ||
| if badViews { | ||
| msg.ViewCounts = nil | ||
| msg.ViewsCountPresent = false | ||
| msg.ViewsCount = 0 | ||
| } | ||
| if badForwards { | ||
| msg.ForwardsCount = 0 | ||
| } | ||
| if badReactions { | ||
| msg.ReactionCounts = nil | ||
| } | ||
| return !badViews && !badForwards && !badReactions | ||
| } | ||
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,138 @@ | ||
| package whatsmeow | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "slices" | ||
| "testing" | ||
|
|
||
| waBinary "github.com/polymorfa/hypermeow/binary" | ||
| "github.com/polymorfa/hypermeow/types" | ||
| "github.com/polymorfa/hypermeow/types/events" | ||
| ) | ||
|
|
||
| func TestNewsletterObservationsPreserveReportedZeroAndMissingCounts(t *testing.T) { | ||
| for _, reported := range []bool{false, true} { | ||
| cli, _, _ := newTestRemote(t) | ||
| var observed *events.NewsletterLiveUpdate | ||
| cli.AddEventHandler(func(evt any) { observed, _ = evt.(*events.NewsletterLiveUpdate) }) | ||
| var counters []waBinary.Node | ||
| if reported { | ||
| counters = []waBinary.Node{ | ||
| {Tag: "views_count", Attrs: waBinary.Attrs{"count": "0", "type": "views"}}, | ||
| {Tag: "forwards_count", Attrs: waBinary.Attrs{"count": "0"}}, | ||
| {Tag: "reactions"}, | ||
| } | ||
| } | ||
| cli.handleNewsletterNotification(context.Background(), &waBinary.Node{ | ||
| Tag: "notification", Attrs: waBinary.Attrs{"from": types.NewJID("123", types.NewsletterServer)}, | ||
| Content: []waBinary.Node{{Tag: "live_updates", Content: []waBinary.Node{{Tag: "messages", Content: []waBinary.Node{ | ||
| {Tag: "message", Attrs: waBinary.Attrs{"server_id": "123"}, Content: counters}, | ||
| }}}}}, | ||
| }) | ||
| if observed == nil || len(observed.Messages) != 1 || observed.Messages[0].MessageID != "" { | ||
| t.Fatal("position-only observation was discarded or invented an original ID") | ||
| } | ||
| message := observed.Messages[0] | ||
| if message.ViewsCountPresent != reported || message.ForwardsCountPresent != reported || (message.ReactionCounts != nil) != reported { | ||
| t.Fatalf("reported=%v lost count presence: %+v", reported, message) | ||
| } | ||
| if reported && !slices.Equal(message.ViewCounts, []types.NewsletterViewCount{{Type: "views", Count: 0}}) { | ||
| t.Fatalf("reported zero lost its view definition: %+v", message.ViewCounts) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestNewsletterObservationsRetainBothViewDefinitionsInEitherOrder(t *testing.T) { | ||
| for _, reverse := range []bool{false, true} { | ||
| cli, _, _ := newTestRemote(t) | ||
| definitions := []waBinary.Node{ | ||
| {Tag: "views_count", Attrs: waBinary.Attrs{"count": "7"}}, | ||
| {Tag: "views_count", Attrs: waBinary.Attrs{"count": "11", "type": "views"}}, | ||
| } | ||
| expected := []types.NewsletterViewCount{{Count: 7}, {Type: "views", Count: 11}} | ||
| if reverse { | ||
| slices.Reverse(definitions) | ||
| slices.Reverse(expected) | ||
| } | ||
| messages := cli.parseNewsletterMessages(&waBinary.Node{Content: []waBinary.Node{{Tag: "message", Attrs: waBinary.Attrs{"server_id": "123"}, Content: definitions}}}) | ||
| if len(messages) != 1 || !messages[0].ViewsCountPresent || !slices.Equal(messages[0].ViewCounts, expected) { | ||
| t.Fatalf("view definitions were collapsed or reordered: %+v", messages) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestNewsletterObservationsDoNotFabricateMalformedCounts(t *testing.T) { | ||
| for _, counters := range [][]waBinary.Node{ | ||
| {{Tag: "views_count", Attrs: waBinary.Attrs{"count": "-1"}}, {Tag: "forwards_count", Attrs: waBinary.Attrs{"count": "invalid"}}}, | ||
| {{Tag: "views_count", Attrs: waBinary.Attrs{"count": "1"}}, {Tag: "views_count", Attrs: waBinary.Attrs{"count": "2"}}, {Tag: "views_count", Attrs: waBinary.Attrs{"count": "3"}}}, | ||
| {{Tag: "forwards_count", Attrs: waBinary.Attrs{"count": "1"}}, {Tag: "forwards_count", Attrs: waBinary.Attrs{"count": "2"}}}, | ||
| {{Tag: "views_count", Attrs: waBinary.Attrs{"count": "9223372036854775808"}}}, | ||
| {{Tag: "reactions", Content: []waBinary.Node{{Tag: "reaction", Attrs: waBinary.Attrs{"code": "x", "count": "bad"}}}}}, | ||
| {{Tag: "reactions", Content: []waBinary.Node{{Tag: "reaction", Attrs: waBinary.Attrs{"code": "x", "count": "1"}}, {Tag: "reaction", Attrs: waBinary.Attrs{"code": "x", "count": "2"}}}}}, | ||
| } { | ||
| cli, _, _ := newTestRemote(t) | ||
| messages := cli.parseNewsletterMessages(&waBinary.Node{Content: []waBinary.Node{{Tag: "message", Attrs: waBinary.Attrs{"server_id": "123"}, Content: counters}}}) | ||
| if len(messages) != 1 || messages[0].ViewsCountPresent || messages[0].ForwardsCountPresent || messages[0].ViewCounts != nil || messages[0].ReactionCounts != nil || messages[0].ViewsCount != 0 || messages[0].ForwardsCount != 0 { | ||
| t.Fatalf("malformed count became an observation: %+v", messages) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestNewsletterReactionLimitDoesNotSilentlyTruncate(t *testing.T) { | ||
| cli, _, _ := newTestRemote(t) | ||
| for _, count := range []int{1000, 1001} { | ||
| reactions := make([]waBinary.Node, count) | ||
| for i := range reactions { | ||
| reactions[i] = waBinary.Node{Tag: "reaction", Attrs: waBinary.Attrs{"code": fmt.Sprint(i), "count": "1"}} | ||
| } | ||
| messages := cli.parseNewsletterMessages(&waBinary.Node{Content: []waBinary.Node{{Tag: "message", Attrs: waBinary.Attrs{"server_id": "123"}, Content: []waBinary.Node{{Tag: "reactions", Content: reactions}}}}}) | ||
| if count == 1000 && len(messages[0].ReactionCounts) != 1000 || count == 1001 && messages[0].ReactionCounts != nil { | ||
| t.Fatal("reaction bound dropped a valid list or exposed a truncated list") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestNewsletterMilestoneNativeMexProducer(t *testing.T) { | ||
| for _, position := range []string{`"123"`, `123`} { | ||
| cli, _, _ := newTestRemote(t) | ||
| var got *events.NewsletterMilestone | ||
| cli.AddEventHandler(func(evt any) { got, _ = evt.(*events.NewsletterMilestone) }) | ||
| cli.handleMexNotification(context.Background(), &waBinary.Node{ | ||
| Tag: "notification", Attrs: waBinary.Attrs{"t": "1700000000"}, Content: []waBinary.Node{{ | ||
| Tag: "update", Attrs: waBinary.Attrs{"op_name": "NotificationNewsletterMilestone"}, | ||
| Content: []byte(`{"data":{"xwa2_notify_newsletter_milestone":{"newsletter_id":"123@newsletter","message_server_id":` + position + `,"type":"MESSAGE_FORWARDS","value":0}}}`), | ||
| }}, | ||
| }) | ||
| if got == nil || got.MessageServerID != 123 || got.Mex.OpName != "NotificationNewsletterMilestone" || got.Mex.Timestamp.Unix() != 1700000000 { | ||
| t.Fatalf("native milestone delivery lost identity or source context: %+v", got) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestNewsletterObservationsRejectDuplicateViewDefinitions(t *testing.T) { | ||
| for _, definition := range []string{"", "views"} { | ||
| for _, secondCount := range []string{"0", "7"} { | ||
| t.Run(fmt.Sprintf("type=%q/second=%s", definition, secondCount), func(t *testing.T) { | ||
| cli, _, _ := newTestRemote(t) | ||
| counters := []waBinary.Node{ | ||
| {Tag: "views_count", Attrs: waBinary.Attrs{"count": "0", "type": definition}}, | ||
| {Tag: "views_count", Attrs: waBinary.Attrs{"count": secondCount, "type": definition}}, | ||
| {Tag: "forwards_count", Attrs: waBinary.Attrs{"count": "0"}}, | ||
| {Tag: "reactions"}, | ||
| } | ||
| messages := cli.parseNewsletterMessages(&waBinary.Node{Content: []waBinary.Node{{Tag: "message", Attrs: waBinary.Attrs{"server_id": "123"}, Content: counters}}}) | ||
| if len(messages) != 1 { | ||
| t.Fatalf("ambiguous views discarded the message: %+v", messages) | ||
| } | ||
| message := messages[0] | ||
| if message.ViewsCountPresent || message.ViewCounts != nil { | ||
| t.Fatalf("duplicate view definition became a valid observation: %+v", message) | ||
| } | ||
| if !message.ForwardsCountPresent || message.ForwardsCount != 0 || message.ReactionCounts == nil || len(message.ReactionCounts) != 0 { | ||
| t.Fatalf("ambiguous views changed independent reported observations: %+v", message) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| } |
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
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.