Skip to content

Commit 534b7c5

Browse files
authored
Merge branch 'tulir:main' into bartender/raw-node-hook
2 parents 419fd39 + 3b5b4fe commit 534b7c5

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

internals.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

notification.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,35 +194,50 @@ func (cli *Client) handleFBDeviceNotification(ctx context.Context, node *waBinar
194194
cli.userDevicesCache[jid] = userDevices
195195
}
196196

197-
func (cli *Client) handleOwnDevicesNotification(ctx context.Context, node *waBinary.Node) {
197+
func (cli *Client) handleOwnDevicesNotification(ctx context.Context, node *waBinary.Node, fromJID types.JID) {
198198
cli.userDevicesCacheLock.Lock()
199199
defer cli.userDevicesCacheLock.Unlock()
200+
ownLID := cli.getOwnLID().ToNonAD()
200201
ownID := cli.getOwnID().ToNonAD()
201202
if ownID.IsEmpty() {
202203
cli.Log.Debugf("Ignoring own device change notification, session was deleted")
203204
return
204205
}
205-
cached, ok := cli.userDevicesCache[ownID]
206-
if !ok {
207-
cli.Log.Debugf("Ignoring own device change notification, device list not cached")
206+
fromJIDPlain := fromJID.ToNonAD()
207+
var altJID types.JID
208+
switch fromJIDPlain {
209+
case ownID:
210+
altJID = ownLID
211+
case ownLID:
212+
altJID = ownID
213+
default:
214+
cli.Log.Warnf("Unexpected own device notification sender %s", fromJID)
208215
return
209216
}
210-
oldHash := participantListHashV2(cached.devices)
217+
var oldHash string
218+
if cached, ok := cli.userDevicesCache[fromJIDPlain]; ok {
219+
oldHash = participantListHashV2(cached.devices)
220+
}
211221
expectedNewHash := node.AttrGetter().String("dhash")
212-
var newDeviceList []types.JID
222+
var newDeviceList, altDeviceList []types.JID
213223
for _, child := range node.GetChildren() {
214224
jid := child.AttrGetter().JID("jid")
215225
if child.Tag == "device" && !jid.IsEmpty() {
216226
newDeviceList = append(newDeviceList, jid)
227+
altDeviceJID := altJID
228+
altDeviceJID.Device = jid.Device
229+
altDeviceList = append(altDeviceList, altDeviceJID)
217230
}
218231
}
219232
newHash := participantListHashV2(newDeviceList)
220233
if newHash != expectedNewHash {
221-
cli.Log.Debugf("Received own device list change notification %s -> %s, but expected hash was %s", oldHash, newHash, expectedNewHash)
234+
cli.Log.Debugf("Received own device list change notification %s -> %s from %s, but expected hash was %s", oldHash, newHash, fromJID, expectedNewHash)
222235
delete(cli.userDevicesCache, ownID)
236+
delete(cli.userDevicesCache, ownLID)
223237
} else {
224-
cli.Log.Debugf("Received own device list change notification %s -> %s", oldHash, newHash)
225-
cli.userDevicesCache[ownID] = deviceCache{devices: newDeviceList, dhash: expectedNewHash}
238+
cli.Log.Debugf("Received own device list change notification %s -> %s from %s", oldHash, newHash, fromJID)
239+
cli.userDevicesCache[fromJIDPlain] = deviceCache{devices: newDeviceList, dhash: expectedNewHash}
240+
cli.userDevicesCache[altJID] = deviceCache{devices: altDeviceList, dhash: participantListHashV2(altDeviceList)}
226241
}
227242
}
228243

@@ -254,7 +269,7 @@ func (cli *Client) handleAccountSyncNotification(ctx context.Context, node *waBi
254269
case "privacy":
255270
cli.handlePrivacySettingsNotification(ctx, &child)
256271
case "devices":
257-
cli.handleOwnDevicesNotification(ctx, &child)
272+
cli.handleOwnDevicesNotification(ctx, &child, node.AttrGetter().JID("from"))
258273
case "picture":
259274
cli.dispatchEvent(&events.Picture{
260275
Timestamp: node.AttrGetter().UnixTime("t"),

0 commit comments

Comments
 (0)