Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Download profile avatar with correct profile key
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFerr committed Feb 20, 2024
1 parent 1f1f653 commit a546016
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
37 changes: 15 additions & 22 deletions pkg/signalmeow/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ func (cli *Client) StoreContactDetailsAsContact(ctx context.Context, contactDeta
return existingContact, nil
}

func (cli *Client) fetchContactThenTryAndUpdateWithProfile(ctx context.Context, profileUUID uuid.UUID) (*types.Contact, error) {
func (cli *Client) fetchContactThenTryAndUpdateWithProfile(ctx context.Context, profileUUID uuid.UUID) (existingContact *types.Contact, otherSourceUUID uuid.UUID, err error) {
log := zerolog.Ctx(ctx).With().
Str("action", "fetch contact then try and update with profile").
Stringer("profile_uuid", profileUUID).
Logger()
contactChanged := false

existingContact, err := cli.Store.ContactStore.LoadContact(ctx, profileUUID)
existingContact, err = cli.Store.ContactStore.LoadContact(ctx, profileUUID)
if err != nil {
log.Err(err).Msg("error loading contact")
return nil, err
return
}
if existingContact == nil {
log.Debug().Msg("creating new contact")
Expand All @@ -116,10 +116,9 @@ func (cli *Client) fetchContactThenTryAndUpdateWithProfile(ctx context.Context,
} else {
log.Debug().Msg("updating existing contact")
}
profile, lastFetched, err := cli.RetrieveProfileByID(ctx, profileUUID)
if err != nil {
log.Err(err).Msg("error retrieving profile")
//return nil, nil, err
profile, lastFetched, fetchErr := cli.RetrieveProfileByID(ctx, profileUUID)
if fetchErr != nil {
log.Err(fetchErr).Msg("error retrieving profile")
// Don't return here, we still want to return what we have
} else if profile != nil {
if existingContact.Profile.Name != profile.Name {
Expand All @@ -146,26 +145,20 @@ func (cli *Client) fetchContactThenTryAndUpdateWithProfile(ctx context.Context,

if contactChanged {
existingContact.ProfileFetchTs = lastFetched.UnixMilli()
err := cli.Store.ContactStore.StoreContact(ctx, *existingContact)
err = cli.Store.ContactStore.StoreContact(ctx, *existingContact)
if err != nil {
log.Err(err).Msg("error storing contact")
return nil, err
return
}
}

if err != nil {
var otherContact *types.Contact
otherContact, err = cli.Store.ContactStore.LoadContactWithLatestOtherProfile(ctx, existingContact)
if err != nil {
log.Err(err).Msg("error retrieving contact with a newer profile from other users")
} else if otherContact != nil {
existingContact.Profile.Name = otherContact.Profile.Name
existingContact.Profile.About = otherContact.Profile.About
existingContact.Profile.AboutEmoji = otherContact.Profile.AboutEmoji
existingContact.Profile.AvatarPath = otherContact.Profile.AvatarPath
if fetchErr != nil {
otherSourceUUID, fetchErr = cli.Store.ContactStore.UpdateContactWithLatestProfile(ctx, existingContact)
if fetchErr != nil {
log.Err(fetchErr).Msg("error retrieving latest profile for contact from other users")
}
}
return existingContact, nil
return
}

func (cli *Client) UpdateContactE164(ctx context.Context, uuid uuid.UUID, e164 string) error {
Expand Down Expand Up @@ -195,7 +188,7 @@ func (cli *Client) UpdateContactE164(ctx context.Context, uuid uuid.UUID, e164 s
return cli.Store.ContactStore.StoreContact(ctx, *existingContact)
}

func (cli *Client) ContactByID(ctx context.Context, uuid uuid.UUID) (*types.Contact, error) {
func (cli *Client) ContactByID(ctx context.Context, uuid uuid.UUID) (contact *types.Contact, otherSourceUUID uuid.UUID, err error) {
return cli.fetchContactThenTryAndUpdateWithProfile(ctx, uuid)
}

Expand All @@ -208,7 +201,7 @@ func (cli *Client) ContactByE164(ctx context.Context, e164 string) (*types.Conta
if contact == nil {
return nil, nil
}
contact, err = cli.fetchContactThenTryAndUpdateWithProfile(ctx, contact.UUID)
contact, _, err = cli.fetchContactThenTryAndUpdateWithProfile(ctx, contact.UUID)
return contact, err
}

Expand Down
46 changes: 36 additions & 10 deletions pkg/signalmeow/store/contact_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
type ContactStore interface {
LoadContact(ctx context.Context, theirUUID uuid.UUID) (*types.Contact, error)
LoadContactByE164(ctx context.Context, e164 string) (*types.Contact, error)
LoadContactWithLatestOtherProfile(ctx context.Context, contact *types.Contact) (*types.Contact, error)
UpdateContactWithLatestProfile(ctx context.Context, contact *types.Contact) (sourceUUID uuid.UUID, err error)
StoreContact(ctx context.Context, contact types.Contact) error
AllContacts(ctx context.Context) ([]*types.Contact, error)
UpdatePhone(ctx context.Context, theirUUID uuid.UUID, newE164 string) error
Expand All @@ -58,13 +58,7 @@ const (
getAllContactsOfUserQuery = getAllContactsQuery + `WHERE our_aci_uuid = $1`
getContactByUUIDQuery = getAllContactsQuery + `WHERE our_aci_uuid = $1 AND aci_uuid = $2`
getContactByPhoneQuery = getAllContactsQuery + `WHERE our_aci_uuid = $1 AND e164_number = $2`

getContactWithLatestOtherProfileQuery = getAllContactsQuery + `
WHERE our_aci_uuid <> $1 AND aci_uuid = $2 AND LENGTH(COALESCE(profile_key, '')) > 0
ORDER BY profile_fetch_ts DESC LIMIT 1
`

upsertContactQuery = `
upsertContactQuery = `
INSERT INTO signalmeow_contacts (
our_aci_uuid,
aci_uuid,
Expand Down Expand Up @@ -149,8 +143,40 @@ func (s *SQLStore) LoadContactByE164(ctx context.Context, e164 string) (*types.C
return scanContact(s.db.QueryRow(ctx, getContactByPhoneQuery, s.ACI, e164))
}

func (s *SQLStore) LoadContactWithLatestOtherProfile(ctx context.Context, contact *types.Contact) (*types.Contact, error) {
return scanContact(s.db.QueryRow(ctx, getContactWithLatestOtherProfileQuery, s.ACI, contact.UUID))
func (s *SQLStore) UpdateContactWithLatestProfile(ctx context.Context, contact *types.Contact) (sourceUUID uuid.UUID, err error) {
var profileKey []byte
err = s.db.QueryRow(
ctx,
`SELECT
profile_key,
profile_name,
profile_about,
profile_about_emoji,
profile_avatar_path,
our_aci_uuid
FROM signalmeow_contacts
WHERE
our_aci_uuid <> $1 AND
aci_uuid = $2 AND
LENGTH(COALESCE(profile_key, '')) > 0
ORDER BY profile_fetch_ts DESC LIMIT 1`,
s.ACI,
contact.UUID,
).Scan(
&profileKey,
&contact.Profile.Name,
&contact.Profile.About,
&contact.Profile.AboutEmoji,
&contact.Profile.AvatarPath,
&sourceUUID,
)
if errors.Is(err, sql.ErrNoRows) {
err = nil
} else if err == nil {
profileKeyConverted := libsignalgo.ProfileKey(profileKey)
contact.Profile.Key = &profileKeyConverted
}
return
}

func (s *SQLStore) AllContacts(ctx context.Context) ([]*types.Contact, error) {
Expand Down
5 changes: 4 additions & 1 deletion puppet.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,17 @@ func (puppet *Puppet) UpdateInfo(ctx context.Context, source *User) {
ctx = log.WithContext(ctx)
var err error
log.Debug().Msg("Fetching contact info to update puppet")
info, err := source.Client.ContactByID(ctx, puppet.SignalID)
info, sourceUUID, err := source.Client.ContactByID(ctx, puppet.SignalID)
if err != nil {
log.Err(err).Msg("Failed to fetch contact info")
return
}

log.Trace().Msg("Updating puppet info")

if sourceUUID != uuid.Nil {
source = puppet.bridge.GetUserBySignalID(sourceUUID)
}
update := false
if info.E164 != "" && puppet.Number != info.E164 {
puppet.Number = info.E164
Expand Down

0 comments on commit a546016

Please sign in to comment.