From 4e0fbbc1de0810cfca13f0eb6f4c609bf2ba7315 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Thu, 20 Apr 2023 15:10:37 -0700 Subject: [PATCH] Add UpdatedConversationAttribute Event for the UI --- event/common.go | 7 ++++--- extensions/profile_value.go | 25 ++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/event/common.go b/event/common.go index 39f6627..58c57cd 100644 --- a/event/common.go +++ b/event/common.go @@ -212,9 +212,10 @@ const ( // Profile Attribute Event UpdatedProfileAttribute = Type("UpdatedProfileAttribute") - - StartingStorageMiragtion = Type("StartingStorageMigration") - DoneStorageMigration = Type("DoneStorageMigration") + // Conversation Attribute Update... + UpdatedConversationAttribute = Type("UpdatedConversationAttribute") + StartingStorageMiragtion = Type("StartingStorageMigration") + DoneStorageMigration = Type("DoneStorageMigration") TokenManagerInfo = Type("TokenManagerInfo") TriggerAntispamCheck = Type("TriggerAntispamCheck") diff --git a/extensions/profile_value.go b/extensions/profile_value.go index 3515381..254e288 100644 --- a/extensions/profile_value.go +++ b/extensions/profile_value.go @@ -64,10 +64,29 @@ func (pne ProfileValueExtension) OnContactReceiveValue(profile peer.CwtchPeer, c // Allow public profile parameters to be added as contact specific attributes... scope, zone, _ := szp.GetScopeZonePath() if exists && scope.IsPublic() && zone == attr.ProfileZone { - err := profile.SetConversationAttribute(conversation.ID, szp, value) - if err != nil { - log.Errorf("error setting conversation attribute %v", err) + + // Check the current value of the attribute + currentValue, err := profile.GetConversationAttribute(conversation.ID, szp) + if err == nil && currentValue == value { + // Value exists and the value is the same, short-circuit + return } + + // Save the new Attribute + err = profile.SetConversationAttribute(conversation.ID, szp, value) + if err != nil { + // Something else wen't wrong.. short-circuit + log.Errorf("error setting conversation attribute %v", err) + return + } + + // Finally publish an update for listeners to react to. + scope, zone, zpath := szp.GetScopeZonePath() + profile.PublishEvent(event.NewEvent(event.UpdatedConversationAttribute, map[event.Field]string{ + event.Scope: string(scope), + event.Path: string(zone.ConstructZonedPath(zpath)), + event.Data: value, + })) } }