Only emit key changes when poked over federation

This commit is contained in:
Neil Alexander 2022-02-22 12:05:04 +00:00
parent bbe7d37928
commit 4c051e0fb5
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
2 changed files with 26 additions and 4 deletions

View file

@ -241,14 +241,33 @@ func (u *DeviceListUpdater) update(ctx context.Context, event gomatrixserverlib.
StreamID: event.StreamID,
},
}
// DeviceKeysJSON will side-effect modify this, so we create it
// separately to above os that DeviceKeys isn't pointer'd to the
// same place in both "keys" and "existingKeys"
deviceKeysCopy := *keys[0].DeviceKeys
existingKeys := []api.DeviceMessage{
{
Type: keys[0].Type,
DeviceKeys: &deviceKeysCopy,
StreamID: keys[0].StreamID,
},
}
// fetch what keys we had already and only emit changes
if err = u.db.DeviceKeysJSON(ctx, existingKeys); err != nil {
// non-fatal, log and continue
util.GetLogger(ctx).WithError(err).WithField("user_id", event.UserID).Errorf(
"failed to query device keys json for calculating diffs",
)
}
err = u.db.StoreRemoteDeviceKeys(ctx, keys, nil)
if err != nil {
return false, fmt.Errorf("failed to store remote device keys for %s (%s): %w", event.UserID, event.DeviceID, err)
}
// ALWAYS emit key changes when we've been poked over federation even if there's no change
// just in case this poke is important for something.
err = u.producer.ProduceKeyChanges(keys)
if err != nil {
if err = emitDeviceKeyChanges(u.producer, existingKeys, keys); err != nil {
return false, fmt.Errorf("failed to produce device key changes for %s (%s): %w", event.UserID, event.DeviceID, err)
}
return false, nil

View file

@ -711,6 +711,9 @@ func (a *KeyInternalAPI) uploadOneTimeKeys(ctx context.Context, req *api.Perform
}
func emitDeviceKeyChanges(producer KeyChangeProducer, existing, new []api.DeviceMessage) error {
logrus.Warnf("XXX: Existing: %+v", existing)
logrus.Warnf("XXX: New: %+v", new)
// find keys in new that are not in existing
var keysAdded []api.DeviceMessage
for _, newKey := range new {