From 5a3d3934727afb447fb09c3f583eed90430c30bb Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Thu, 25 Nov 2021 15:39:08 -0800 Subject: [PATCH] Purge on Startup + Fix SetSZA eventbus safety --- peer/cwtch_peer.go | 8 ++++++-- peer/cwtchprofilestorage.go | 33 +++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index e7620bb..2395af0 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -159,8 +159,9 @@ func (cp *cwtchPeer) SetScopedZonedAttribute(scope attr.Scope, zone attr.Zone, k // We always want to publish profile level attributes to the ui // This should be low traffic. - cp.eventBus.Publish(event.NewEvent(event.UpdatedProfileAttribute, map[event.Field]string{event.Key: scopedZonedKey.ToString(), event.Data: value})) - + if cp.eventBus != nil { + cp.eventBus.Publish(event.NewEvent(event.UpdatedProfileAttribute, map[event.Field]string{event.Key: scopedZonedKey.ToString(), event.Data: value})) + } } // SendMessage is a higher level that merges sending messages to contacts and group handles @@ -266,6 +267,9 @@ func FromEncryptedStorage(cps *CwtchProfileStorage) CwtchPeer { cp.state = make(map[string]connections.ConnectionState) // At some point we may want to populate caches here, for now we will assume hitting the // database directly is tolerable + // Clean up anything that wasn't cleaned up on shutdown + // TODO ideally this shouldn't need to be done but the UI sometimes doesn't shut down cleanly + cp.storage.PurgeNonSavedMessages() return cp } diff --git a/peer/cwtchprofilestorage.go b/peer/cwtchprofilestorage.go index b43620c..9548924 100644 --- a/peer/cwtchprofilestorage.go +++ b/peer/cwtchprofilestorage.go @@ -538,7 +538,7 @@ func (cps *CwtchProfileStorage) GetChannelMessageByContentHash(conversation int, rows.Close() // Return the offset **not** the count - return id-1, nil + return id - 1, nil } // GetRowNumberByMessageID looks up the row number of a message by the message ID @@ -693,23 +693,28 @@ func (cps *CwtchProfileStorage) PurgeConversationChannel(conversation int, chann return conversationStmt.Close() } +// PurgeNonSavedMessages deletes all message conversations that are not explicitly set to saved. +func (cps *CwtchProfileStorage) PurgeNonSavedMessages() { + // Purge Messages that are not stored... + ci, err := cps.FetchConversations() + if err == nil { + for _, conversation := range ci { + if !conversation.IsGroup() && !conversation.IsServer() { + if conversation.Attributes[attr.LocalScope.ConstructScopedZonedPath(attr.ProfileZone.ConstructZonedPath(event.SaveHistoryKey)).ToString()] != event.SaveHistoryConfirmed { + log.Infof("purging conversation...") + // TODO: At some point in the future this needs to iterate over channels and make a decision for each on.. + cps.PurgeConversationChannel(conversation.ID, 0) + } + } + } + } +} + // Close closes the underlying database and prepared statements func (cps *CwtchProfileStorage) Close() { if cps.db != nil { - // Purge Messages that are not stored... - ci, err := cps.FetchConversations() - if err == nil { - for _, conversation := range ci { - if !conversation.IsGroup() && !conversation.IsServer() { - if conversation.Attributes[attr.LocalScope.ConstructScopedZonedPath(attr.ProfileZone.ConstructZonedPath(event.SaveHistoryKey)).ToString()] != event.SaveHistoryConfirmed { - log.Infof("purging conversation...") - // TODO: At some point in the future this needs to iterate over channels and make a decision for each on.. - cps.PurgeConversationChannel(conversation.ID, 0) - } - } - } - } + cps.PurgeNonSavedMessages() cps.insertProfileKeyValueStmt.Close() cps.selectProfileKeyValueStmt.Close()