Skip to content

Commit 0fd50bb

Browse files
aaazaleazapu
authored andcommitted
phonenumbers: delete phone number from cache when user deletes it (keybase#18684)
1 parent 84634db commit 0fd50bb

9 files changed

Lines changed: 326 additions & 17 deletions

File tree

‎go/chat/utils/utils_test.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,11 @@ func (c *contactStoreMock) RetrieveContacts(libkb.MetaContext) ([]keybase1.Proce
457457
return c.contacts, nil
458458
}
459459

460+
func (c *contactStoreMock) UnresolveContactsWithComponent(mctx libkb.MetaContext,
461+
phoneNumber *keybase1.PhoneNumber, email *keybase1.EmailAddress) {
462+
panic("unexpected call to UnresolveContactsWithComponent in mock")
463+
}
464+
460465
func TestAttachContactNames(t *testing.T) {
461466
tc := externalstest.SetupTest(t, "chat-utils", 0)
462467
defer tc.Cleanup()

‎go/contacts/cache.go‎

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ func (c *lookupResultCache) cleanup(mctx libkb.MetaContext) {
122122
}
123123
}
124124

125-
func (c *CachedContactsProvider) getCache(mctx libkb.MetaContext) lookupResultCache {
125+
func (s *ContactCacheStore) getCache(mctx libkb.MetaContext) (obj lookupResultCache, created bool) {
126126
var conCache lookupResultCache
127127
var createCache bool
128-
cacheKey := c.Store.dbKey(mctx.CurrentUID())
129-
found, err := c.Store.encryptedDB.Get(mctx.Ctx(), cacheKey, &conCache)
128+
cacheKey := s.dbKey(mctx.CurrentUID())
129+
found, err := s.encryptedDB.Get(mctx.Ctx(), cacheKey, &conCache)
130130
if err != nil {
131131
mctx.Warning("Unable to pull contact lookup cache: %s", err)
132132
createCache = true
@@ -143,12 +143,17 @@ func (c *CachedContactsProvider) getCache(mctx libkb.MetaContext) lookupResultCa
143143
if createCache {
144144
conCache = newLookupResultCache()
145145
}
146-
return conCache
146+
return conCache, createCache
147147
}
148148

149-
func (c *CachedContactsProvider) CleanCache(mctx libkb.MetaContext) error {
150-
cacheKey := c.Store.dbKey(mctx.CurrentUID())
151-
return c.Store.encryptedDB.Delete(mctx.Ctx(), cacheKey)
149+
func (s *ContactCacheStore) putCache(mctx libkb.MetaContext, cacheObj lookupResultCache) error {
150+
cacheKey := s.dbKey(mctx.CurrentUID())
151+
return s.encryptedDB.Put(mctx.Ctx(), cacheKey, cacheObj)
152+
}
153+
154+
func (s *ContactCacheStore) ClearCache(mctx libkb.MetaContext) error {
155+
cacheKey := s.dbKey(mctx.CurrentUID())
156+
return s.encryptedDB.Delete(mctx.Ctx(), cacheKey)
152157
}
153158

154159
func (c *CachedContactsProvider) LookupAll(mctx libkb.MetaContext, emails []keybase1.EmailAddress,
@@ -171,7 +176,7 @@ func (c *CachedContactsProvider) LookupAll(mctx libkb.MetaContext, emails []keyb
171176
c.lock.Lock()
172177
defer c.lock.Unlock()
173178

174-
conCache := c.getCache(mctx)
179+
conCache, _ := c.Store.getCache(mctx)
175180

176181
var remainingEmails []keybase1.EmailAddress
177182
var remainingNumbers []keybase1.RawPhoneNumber
@@ -234,9 +239,7 @@ func (c *CachedContactsProvider) LookupAll(mctx libkb.MetaContext, emails []keyb
234239
}
235240

236241
conCache.cleanup(mctx)
237-
238-
cacheKey := c.Store.dbKey(mctx.CurrentUID())
239-
cerr := c.Store.encryptedDB.Put(mctx.Ctx(), cacheKey, conCache)
242+
cerr := c.Store.putCache(mctx, conCache)
240243
if cerr != nil {
241244
mctx.Warning("Unable to update cache: %s", cerr)
242245
}
@@ -252,3 +255,28 @@ func (c *CachedContactsProvider) FindUsernames(mctx libkb.MetaContext, uids []ke
252255
func (c *CachedContactsProvider) FindFollowing(mctx libkb.MetaContext, uids []keybase1.UID) (map[keybase1.UID]bool, error) {
253256
return c.Provider.FindFollowing(mctx, uids)
254257
}
258+
259+
// RemoveContactsCachePhoneEntry removes cached lookup for phone number.
260+
func (s *ContactCacheStore) RemoveContactsCacheEntries(mctx libkb.MetaContext,
261+
phone *keybase1.PhoneNumber, email *keybase1.EmailAddress) {
262+
// TODO: Use a phoneNumber | email variant instead of two pointers.
263+
cacheObj, created := s.getCache(mctx)
264+
if created {
265+
// There was no cache.
266+
return
267+
}
268+
if phone != nil {
269+
// TODO: this type conversion shouldn't have to be here,
270+
// since this cache should take `PhoneNumber`s.
271+
delete(cacheObj.Lookups, MakePhoneLookupKey(keybase1.RawPhoneNumber(*phone)))
272+
mctx.Debug("ContactCacheStore: Removing phone number %q from lookup cache", *phone)
273+
}
274+
if email != nil {
275+
delete(cacheObj.Lookups, MakeEmailLookupKey(*email))
276+
mctx.Debug("ContactCacheStore: Removing email %q from lookup cache", *email)
277+
}
278+
err := s.putCache(mctx, cacheObj)
279+
if err != nil {
280+
mctx.Warning("ContactCacheStore: Unable to update cache: %s", err)
281+
}
282+
}

‎go/contacts/cache_test.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ func TestLookupCacheExpiration(t *testing.T) {
238238

239239
// Old entries from previous lookups should have been cleared, only
240240
// last lookup should be cached.
241-
cacheObj := cacheProvider.getCache(mctx)
241+
cacheObj, created := cacheProvider.Store.getCache(mctx)
242+
require.False(t, created)
242243
require.Len(t, cacheObj.Lookups, 1)
243244
_, ok := cacheObj.Lookups[MakePhoneLookupKey("+48111222333")]
244245
require.True(t, ok)

‎go/contacts/phonebook.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,40 @@ func (s *SavedContactsStore) RetrieveContacts(mctx libkb.MetaContext) (ret []key
9292
}
9393
return cache.Contacts, nil
9494
}
95+
96+
func (s *SavedContactsStore) UnresolveContactsWithComponent(mctx libkb.MetaContext,
97+
phoneNumber *keybase1.PhoneNumber, email *keybase1.EmailAddress) {
98+
// TODO: Use a phoneNumber | email variant instead of two pointers.
99+
contactList, err := s.RetrieveContacts(mctx)
100+
if err != nil {
101+
mctx.Warning("Failed to get cached contact list: %x", err)
102+
return
103+
}
104+
for i, con := range contactList {
105+
var unresolve bool
106+
switch {
107+
case phoneNumber != nil && con.Component.PhoneNumber != nil:
108+
unresolve = *con.Component.PhoneNumber == keybase1.RawPhoneNumber(*phoneNumber)
109+
case email != nil && con.Component.Email != nil:
110+
unresolve = *con.Component.Email == *email
111+
}
112+
113+
if unresolve {
114+
// Unresolve contact.
115+
con.Resolved = false
116+
con.Username = ""
117+
con.Uid = ""
118+
con.Following = false
119+
con.FullName = ""
120+
// TODO: DisplayName/DisplayLabel logic infects yet another file /
121+
// module. But it will sort itself out once we get rid of both.
122+
con.DisplayName = con.ContactName
123+
con.DisplayLabel = con.Component.FormatDisplayLabel(false /* addLabel */)
124+
contactList[i] = con
125+
}
126+
}
127+
err = s.SaveProcessedContacts(mctx, contactList)
128+
if err != nil {
129+
mctx.Warning("Failed to put cached contact list: %x", err)
130+
}
131+
}

‎go/emails/user.go‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package emails
55

66
import (
7+
"github.com/keybase/client/go/contacts"
78
"github.com/keybase/client/go/libkb"
89
"github.com/keybase/client/go/protocol/keybase1"
910
)
@@ -23,6 +24,14 @@ func AddEmail(mctx libkb.MetaContext, email keybase1.EmailAddress, visibility ke
2324
return err
2425
}
2526

27+
func clearEmailsFromContactCache(mctx libkb.MetaContext, email keybase1.EmailAddress) {
28+
cache := contacts.NewContactCacheStore(mctx.G())
29+
cache.RemoveContactsCacheEntries(mctx, nil /* phoneNumber */, &email)
30+
if sync := mctx.G().SyncedContactList; sync != nil {
31+
sync.UnresolveContactsWithComponent(mctx, nil /* phoneNumber */, &email)
32+
}
33+
}
34+
2635
func DeleteEmail(mctx libkb.MetaContext, email keybase1.EmailAddress) error {
2736
payload := make(libkb.JSONPayload)
2837
payload["email"] = email
@@ -34,7 +43,14 @@ func DeleteEmail(mctx libkb.MetaContext, email keybase1.EmailAddress) error {
3443
}
3544

3645
_, err := mctx.G().API.PostJSON(mctx, arg)
37-
return err
46+
if err != nil {
47+
return err
48+
}
49+
50+
// Now remove this email from contact lookup cache and from synced
51+
// contacts.
52+
clearEmailsFromContactCache(mctx, email)
53+
return nil
3854
}
3955

4056
func SetPrimaryEmail(mctx libkb.MetaContext, email keybase1.EmailAddress) error {
@@ -77,7 +93,13 @@ func SetVisibilityEmail(mctx libkb.MetaContext, email keybase1.EmailAddress, vis
7793
}
7894

7995
_, err := mctx.G().API.PostJSON(mctx, arg)
80-
return err
96+
if err != nil {
97+
return err
98+
}
99+
if visibility == keybase1.IdentityVisibility_PRIVATE {
100+
clearEmailsFromContactCache(mctx, email)
101+
}
102+
return nil
81103
}
82104

83105
func SetVisibilityAllEmail(mctx libkb.MetaContext, visibility keybase1.IdentityVisibility) error {

‎go/libkb/interfaces.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,4 +1095,5 @@ type StandaloneChatConnector interface {
10951095
type SyncedContactListProvider interface {
10961096
SaveProcessedContacts(MetaContext, []keybase1.ProcessedContact) error
10971097
RetrieveContacts(MetaContext) ([]keybase1.ProcessedContact, error)
1098+
UnresolveContactsWithComponent(MetaContext, *keybase1.PhoneNumber, *keybase1.EmailAddress)
10981099
}

‎go/phonenumbers/user.go‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package phonenumbers
55

66
import (
7+
"github.com/keybase/client/go/contacts"
78
"github.com/keybase/client/go/libkb"
89
"github.com/keybase/client/go/protocol/keybase1"
910
)
@@ -61,6 +62,16 @@ func GetPhoneNumbers(mctx libkb.MetaContext) ([]keybase1.UserPhoneNumber, error)
6162
return resp.PhoneNumbers, nil
6263
}
6364

65+
func clearPhoneNumbersFromContactCache(mctx libkb.MetaContext, phoneNumber keybase1.PhoneNumber) {
66+
// Now remove this number from contact lookup cache and from synced
67+
// contacts.
68+
cache := contacts.NewContactCacheStore(mctx.G())
69+
cache.RemoveContactsCacheEntries(mctx, &phoneNumber, nil /* email */)
70+
if sync := mctx.G().SyncedContactList; sync != nil {
71+
sync.UnresolveContactsWithComponent(mctx, &phoneNumber, nil /* email */)
72+
}
73+
}
74+
6475
func DeletePhoneNumber(mctx libkb.MetaContext, phoneNumber keybase1.PhoneNumber) error {
6576
payload := make(libkb.JSONPayload)
6677
payload["phone_number"] = phoneNumber
@@ -72,7 +83,11 @@ func DeletePhoneNumber(mctx libkb.MetaContext, phoneNumber keybase1.PhoneNumber)
7283
}
7384

7485
_, err := mctx.G().API.Delete(mctx, arg)
75-
return err
86+
if err != nil {
87+
return err
88+
}
89+
clearPhoneNumbersFromContactCache(mctx, phoneNumber)
90+
return nil
7691
}
7792

7893
func SetVisibilityPhoneNumber(mctx libkb.MetaContext, phoneNumber keybase1.PhoneNumber, visibility keybase1.IdentityVisibility) error {
@@ -87,7 +102,13 @@ func SetVisibilityPhoneNumber(mctx libkb.MetaContext, phoneNumber keybase1.Phone
87102
}
88103

89104
_, err := mctx.G().API.PostJSON(mctx, arg)
90-
return err
105+
if err != nil {
106+
return nil
107+
}
108+
if visibility == keybase1.IdentityVisibility_PRIVATE {
109+
clearPhoneNumbersFromContactCache(mctx, phoneNumber)
110+
}
111+
return nil
91112
}
92113

93114
func SetVisibilityAllPhoneNumber(mctx libkb.MetaContext, visibility keybase1.IdentityVisibility) error {

‎go/service/contacts_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func setupContactSyncTest(t *testing.T) (tc libkb.TestContext, test contactSyncT
5454
}
5555

5656
func (c *contactSyncTest) clearCache(mctx libkb.MetaContext) error {
57-
return c.contactsCache.CleanCache(mctx)
57+
return c.contactsCache.Store.ClearCache(mctx)
5858
}
5959

6060
func TestContactSyncAndSearch(t *testing.T) {

0 commit comments

Comments
 (0)