From bf193e6542cd44eb34714366f4c437cbbabea274 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Wed, 7 Jul 2021 08:23:00 -0700 Subject: [PATCH] Extract getTimeline into PeerHandler --- lib.go | 6 +----- utils/manager.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib.go b/lib.go index 421bdb9..744db47 100644 --- a/lib.go +++ b/lib.go @@ -566,11 +566,7 @@ func GetMessagesByContentHash(profileOnion, handle string, contentHash string) s profile := application.GetPeer(profileOnion) ph := utils.NewPeerHelper(profile) var err error - if ph.IsGroup(handle) { - indexedMessages,err = profile.GetGroup(handle).Timeline.GetMessagesByHash(contentHash) - } else { - indexedMessages,err = profile.GetContact(handle).Timeline.GetMessagesByHash(contentHash) - } + indexedMessages, err = ph.GetTimeline(handle).GetMessagesByHash(contentHash) if err != nil { indexedMessages = []model.LocallyIndexedMessage{} } diff --git a/utils/manager.go b/utils/manager.go index 8e13ef1..c704596 100644 --- a/utils/manager.go +++ b/utils/manager.go @@ -36,6 +36,20 @@ func (p *PeerHelper) IsServer(id string) bool { return ok } +// GetTimeline returns a pointer to the timeline associated with the conversation handle +func (p *PeerHelper) GetTimeline(handle string) *model.Timeline { + if p.IsServer(handle) { + // This should *never* happen + log.Errorf("server accessed as contact when getting timeline...") + return &model.Timeline{} + } + // We return a pointer to the timeline to avoid copying, accessing Timeline is thread-safe + if p.IsGroup(handle) { + return &p.peer.GetGroup(handle).Timeline + } + return &p.peer.GetContact(handle).Timeline +} + /* func getOrDefault(id, key string, defaultVal string) string { var val string