From 7ad59f26948cead67371cf2e58c157da2d80cbc1 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 21 Aug 2024 13:20:05 +0300 Subject: [PATCH] userinfo: implement `use_outdated_profiles` config option Fixes #538 --- pkg/connector/chatinfo.go | 12 ++++++++++++ pkg/connector/dbmeta.go | 4 +++- pkg/signalid/dbmeta.go | 8 ++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/connector/chatinfo.go b/pkg/connector/chatinfo.go index 69af7352..7bd392f3 100644 --- a/pkg/connector/chatinfo.go +++ b/pkg/connector/chatinfo.go @@ -49,6 +49,10 @@ func (s *SignalClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost) ( if err != nil { return nil, err } + meta := ghost.Metadata.(*signalid.GhostMetadata) + if !s.Main.Config.UseOutdatedProfiles && meta.ProfileFetchedAt.After(contact.Profile.FetchedAt) { + return nil, nil + } return s.contactToUserInfo(contact), nil } @@ -74,6 +78,14 @@ func (s *SignalClient) contactToUserInfo(contact *types.Recipient) *bridgev2.Use ui := &bridgev2.UserInfo{ IsBot: &isBot, Identifiers: []string{}, + ExtraUpdates: func(ctx context.Context, ghost *bridgev2.Ghost) (changed bool) { + meta := ghost.Metadata.(*signalid.GhostMetadata) + if meta.ProfileFetchedAt.Before(contact.Profile.FetchedAt) { + changed = meta.ProfileFetchedAt.IsZero() && !contact.Profile.FetchedAt.IsZero() + meta.ProfileFetchedAt.Time = contact.Profile.FetchedAt + } + return false + }, } if contact.E164 != "" { ui.Identifiers = append(ui.Identifiers, "tel:"+contact.E164) diff --git a/pkg/connector/dbmeta.go b/pkg/connector/dbmeta.go index 14f59f2d..4ee3b083 100644 --- a/pkg/connector/dbmeta.go +++ b/pkg/connector/dbmeta.go @@ -27,7 +27,9 @@ func (s *SignalConnector) GetDBMetaTypes() database.MetaTypes { Portal: func() any { return &signalid.PortalMetadata{} }, - Ghost: nil, + Ghost: func() any { + return &signalid.GhostMetadata{} + }, Message: func() any { return &signalid.MessageMetadata{} }, diff --git a/pkg/signalid/dbmeta.go b/pkg/signalid/dbmeta.go index 2b5a164f..5a96b07d 100644 --- a/pkg/signalid/dbmeta.go +++ b/pkg/signalid/dbmeta.go @@ -16,6 +16,10 @@ package signalid +import ( + "go.mau.fi/util/jsontime" +) + type PortalMetadata struct { Revision uint32 `json:"revision,omitempty"` } @@ -23,3 +27,7 @@ type PortalMetadata struct { type MessageMetadata struct { ContainsAttachments bool `json:"contains_attachments,omitempty"` } + +type GhostMetadata struct { + ProfileFetchedAt jsontime.UnixMilli `json:"profile_fetched_at"` +}