Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ All notable HyperMeow changes are documented here. HyperMeow uses commit pseudo-
### Fixed

- Redact shared-device and root secrets from app-state mutations in trace logs.
- Decode newsletter milestone positions from either quoted or numeric integers without losing precision.
- Preserve reported newsletter counter presence, explicit zeros and distinct view definitions. Omit malformed or ambiguous observations, including duplicate view definitions, instead of reporting fabricated zeros or truncated reaction lists.
- Keep connection handler completion pending until an active handler exits, and process retired stream errors before automatic reconnect decisions. Manual disconnect retains its five-second wait bound.
- Keep the newest pairing secret through bursts of rotation notifications and refresh the active QR code.
- Report the destination in `SendFBMessage` responses and decrypt full bot edits using their target message ID.
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ go get github.com/polymorfa/hypermeow@main
go mod tidy
```

## Newsletter observations

`NewsletterMessage.ViewCounts` preserves the reported view definitions, including
an untyped legacy count and a separate `views` count. It does not select an
account's view-definition gate. `ViewsCountPresent` and `ForwardsCountPresent`
distinguish a reported zero from an absent or malformed counter. The existing
scalar count fields remain available.

A nil `ReactionCounts` means no valid reaction observation; an allocated empty
map preserves an explicitly empty list. These observations do not reproduce the
client's local database reconciliation. Original message IDs and timestamps may
be absent in a live observation; its server position is a separate reference.

`NewsletterMilestone` accepts quoted and numeric decimal integer positions
without converting through floating point. An absent, `null` or zero position
means the milestone has no message (for example a follower-count milestone) and
decodes as position 0. It rejects negative, empty, fractional and overflowing
positions. Its JSON output keeps quoted positions.

## Core compatibility

HyperMeow retains upstream's core support for:
Expand Down
56 changes: 56 additions & 0 deletions newsletter_live_update_test.go
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)
}
})
}
}
84 changes: 84 additions & 0 deletions newsletter_observation.go
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
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
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
}
138 changes: 138 additions & 0 deletions newsletter_observation_test.go
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)
}
})
}
}
}
16 changes: 5 additions & 11 deletions notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,11 @@ func (cli *Client) parseNewsletterMessages(node *waBinary.Node) []*types.Newslet
ViewsCount: 0,
ReactionCounts: nil,
}
for _, subchild := range child.GetChildren() {
children := child.GetChildren()
if !parseNewsletterObservations(&msg, children) {
cli.Log.Warnf("Ignoring malformed newsletter observation counters")
}
for _, subchild := range children {
switch subchild.Tag {
case "plaintext":
byteContent, ok := subchild.Content.([]byte)
Expand All @@ -420,16 +424,6 @@ func (cli *Client) parseNewsletterMessages(node *waBinary.Node) []*types.Newslet
msg.Message = nil
}
}
case "views_count":
msg.ViewsCount = subchild.AttrGetter().Int("count")
case "forwards_count":
msg.ForwardsCount = subchild.AttrGetter().Int("count")
case "reactions":
msg.ReactionCounts = make(map[string]int)
for _, reaction := range subchild.GetChildren() {
rag := reaction.AttrGetter()
msg.ReactionCounts[rag.String("code")] = rag.Int("count")
}
}
}
output = append(output, &msg)
Expand Down
Loading
Loading