From 4d81529ce2f2f0ac61a6b932f32fa5e75b2f0b26 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 3 Apr 2023 14:33:25 -0700 Subject: [PATCH] Update Profile Extension to remove Duplication --- extensions/profile_value.go | 28 ++++++++++--------- functionality/filesharing/image_previews.go | 11 +++++++- testing/cwtch_peer_server_integration_test.go | 2 +- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/extensions/profile_value.go b/extensions/profile_value.go index 4c9ca5d..2ecd7ed 100644 --- a/extensions/profile_value.go +++ b/extensions/profile_value.go @@ -27,18 +27,24 @@ func (pne ProfileValueExtension) ExperimentsToRegister() []string { return nil } +func (pne ProfileValueExtension) requestProfileInfo(profile peer.CwtchPeer, ci *model.Conversation) { + profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.Name) + profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.ProfileStatus) + profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.ProfileAttribute1) + profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.ProfileAttribute2) + profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.ProfileAttribute3) +} + func (pne ProfileValueExtension) OnEvent(ev event.Event, profile peer.CwtchPeer) { switch ev.EventType { case event.Heartbeat: // once every heartbeat, loop through conversations and, if they are online, request an update to any long info.. - conversations, _ := profile.FetchConversations() - for _, ci := range conversations { - if profile.GetPeerState(ci.Handle) == connections.AUTHENTICATED { - profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.Name) - profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.ProfileStatus) - profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.ProfileAttribute1) - profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.ProfileAttribute2) - profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.ProfileAttribute3) + conversations, err := profile.FetchConversations() + if err == nil { + for _, ci := range conversations { + if profile.GetPeerState(ci.Handle) == connections.AUTHENTICATED { + pne.requestProfileInfo(profile, ci) + } } } case event.PeerStateChange: @@ -47,11 +53,7 @@ func (pne ProfileValueExtension) OnEvent(ev event.Event, profile peer.CwtchPeer) // if we have re-authenticated with thie peer then request their profile image... if connections.ConnectionStateToType()[ev.Data[event.ConnectionState]] == connections.AUTHENTICATED { // Request some profile information... - profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.Name) - profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.ProfileStatus) - profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.ProfileAttribute1) - profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.ProfileAttribute2) - profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.ProfileAttribute3) + pne.requestProfileInfo(profile, ci) } } } diff --git a/functionality/filesharing/image_previews.go b/functionality/filesharing/image_previews.go index 222f200..7fdaa6b 100644 --- a/functionality/filesharing/image_previews.go +++ b/functionality/filesharing/image_previews.go @@ -25,7 +25,7 @@ func (i *ImagePreviewsFunctionality) NotifySettingsUpdate(settings settings.Glob } func (i ImagePreviewsFunctionality) EventsToRegister() []event.Type { - return []event.Type{event.ProtocolEngineCreated, event.NewMessageFromPeer, event.NewMessageFromGroup, event.PeerStateChange} + return []event.Type{event.ProtocolEngineCreated, event.NewMessageFromPeer, event.NewMessageFromGroup, event.PeerStateChange, event.Heartbeat} } func (i ImagePreviewsFunctionality) ExperimentsToRegister() []string { @@ -57,6 +57,15 @@ func (i *ImagePreviewsFunctionality) OnEvent(ev event.Event, profile peer.CwtchP profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.CustomProfileImageKey) } } + case event.Heartbeat: + conversations, err := profile.FetchConversations() + if err == nil { + for _, ci := range conversations { + if profile.GetPeerState(ci.Handle) == connections.AUTHENTICATED { + profile.SendScopedZonedGetValToContact(ci.ID, attr.PublicScope, attr.ProfileZone, constants.CustomProfileImageKey) + } + } + } case event.ProtocolEngineCreated: // Now that the Peer Engine is Activated, Reshare Profile Images key, exists := profile.GetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.CustomProfileImageKey) diff --git a/testing/cwtch_peer_server_integration_test.go b/testing/cwtch_peer_server_integration_test.go index 9415e7e..93d0a56 100644 --- a/testing/cwtch_peer_server_integration_test.go +++ b/testing/cwtch_peer_server_integration_test.go @@ -403,7 +403,7 @@ func TestCwtchPeerIntegration(t *testing.T) { log.Infof("Shutting down ACN...") acn.Close() - time.Sleep(time.Second * 30) // the network status plugin might keep goroutines alive for a minute before killing them + time.Sleep(time.Second * 60) // the network status / heartbeat plugin might keep goroutines alive for a minute before killing them numGoRoutinesPostAppShutdown := runtime.NumGoroutine()