-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Resolve LID for profile-picture contacts.update #2605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
manishkumar1601
wants to merge
2
commits into
WhiskeySockets:master
Choose a base branch
from
manishkumar1601:fix/picture-contact-update-lid
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−13
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { areJidsSameUser, isLidUser, isPnUser, jidNormalizedUser } from '../WABinary' | ||
|
|
||
| export type ContactPictureIdentityContext = { | ||
| getPNForLID: (lid: string) => Promise<string | null> | ||
| getLIDForPN: (pn: string) => Promise<string | null> | ||
| meId: string | undefined | ||
| meLid: string | undefined | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the best-effort contact identity for a profile-picture notification. | ||
| * `from` must be the already-normalized individual JID (never a group jid). | ||
| * | ||
| * Returns the fields to merge into a `contacts.update` entry. When `from` is a LID we | ||
| * attempt to resolve the PN (and vice-versa) so consumers can correlate the change with a | ||
| * cached contact regardless of which addressing form they store. For non-saved contacts WA | ||
| * omits the canonical identity, so resolution may fail — in that case we still return the | ||
| * raw LID so the event is never empty. | ||
| */ | ||
| export async function resolveContactPictureIdentity( | ||
| from: string, | ||
| ctx: ContactPictureIdentityContext | ||
| ): Promise<{ id: string; lid?: string; phoneNumber?: string }> { | ||
| const result: { id: string; lid?: string; phoneNumber?: string } = { id: from } | ||
|
|
||
| if (isLidUser(from)) { | ||
| result.lid = from | ||
| const resolvedPn = await ctx.getPNForLID(from).catch(() => null) | ||
| const normalizedPn = jidNormalizedUser(resolvedPn || undefined) | ||
| // guard: discard a resolution that points at our own PN unless `from` is our own LID | ||
| const isBogusSelf = | ||
| !!normalizedPn && | ||
| !!ctx.meId && | ||
| areJidsSameUser(normalizedPn, ctx.meId) && | ||
| !(ctx.meLid && areJidsSameUser(from, ctx.meLid)) | ||
| if (normalizedPn && !isBogusSelf) { | ||
| result.id = normalizedPn | ||
| result.phoneNumber = normalizedPn | ||
| } | ||
| } else if (isPnUser(from)) { | ||
| result.phoneNumber = from | ||
| const resolvedLid = await ctx.getLIDForPN(from).catch(() => null) | ||
| if (resolvedLid && isLidUser(resolvedLid)) { | ||
| result.lid = jidNormalizedUser(resolvedLid) | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { jest } from '@jest/globals' | ||
| import { | ||
| type ContactPictureIdentityContext, | ||
| resolveContactPictureIdentity | ||
| } from '../../Utils/contact-picture-identity' | ||
|
|
||
| type ResolverFn = (jid: string) => Promise<string | null> | ||
|
|
||
| describe('resolveContactPictureIdentity', () => { | ||
| let mockGetPNForLID: jest.Mock<ResolverFn> | ||
| let mockGetLIDForPN: jest.Mock<ResolverFn> | ||
|
|
||
| const ME_ID = 'myuser@s.whatsapp.net' | ||
| const ME_LID = 'mylid@lid' | ||
|
|
||
| /** Build a resolver context wired to the per-test mock resolvers and our own identity. */ | ||
| function createContext(): ContactPictureIdentityContext { | ||
| return { | ||
| getPNForLID: mockGetPNForLID, | ||
| getLIDForPN: mockGetLIDForPN, | ||
| meId: ME_ID, | ||
| meLid: ME_LID | ||
| } | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| mockGetPNForLID = jest.fn<ResolverFn>().mockResolvedValue(null) | ||
| mockGetLIDForPN = jest.fn<ResolverFn>().mockResolvedValue(null) | ||
| }) | ||
|
|
||
| it('resolves a LID to a PN and fills id, phoneNumber and lid', async () => { | ||
| mockGetPNForLID.mockResolvedValue('12345:0@s.whatsapp.net') | ||
|
|
||
| const result = await resolveContactPictureIdentity('98765@lid', createContext()) | ||
|
|
||
| expect(result).toEqual({ | ||
| id: '12345@s.whatsapp.net', | ||
| phoneNumber: '12345@s.whatsapp.net', | ||
| lid: '98765@lid' | ||
| }) | ||
| expect(mockGetPNForLID).toHaveBeenCalledWith('98765@lid') | ||
| }) | ||
|
|
||
| it('falls back to the raw LID when it cannot be resolved', async () => { | ||
| mockGetPNForLID.mockResolvedValue(null) | ||
|
|
||
| const result = await resolveContactPictureIdentity('98765@lid', createContext()) | ||
|
|
||
| expect(result).toEqual({ id: '98765@lid', lid: '98765@lid' }) | ||
| expect(result.phoneNumber).toBeUndefined() | ||
| }) | ||
|
|
||
| it('discards a resolution that points at our own PN (bogus self) and keeps the raw LID', async () => { | ||
| // a stranger's LID wrongly resolving to our own number must not be emitted as the contact | ||
| mockGetPNForLID.mockResolvedValue(ME_ID) | ||
|
|
||
| const result = await resolveContactPictureIdentity('98765@lid', createContext()) | ||
|
|
||
| expect(result).toEqual({ id: '98765@lid', lid: '98765@lid' }) | ||
| expect(result.phoneNumber).toBeUndefined() | ||
| }) | ||
|
|
||
| it('keeps the resolved own PN when the source LID is our own LID', async () => { | ||
| mockGetPNForLID.mockResolvedValue(ME_ID) | ||
|
|
||
| const result = await resolveContactPictureIdentity(ME_LID, createContext()) | ||
|
|
||
| expect(result).toEqual({ | ||
| id: ME_ID, | ||
| phoneNumber: ME_ID, | ||
| lid: ME_LID | ||
| }) | ||
| }) | ||
|
|
||
| it('fills lid from getLIDForPN when the input is a PN', async () => { | ||
| mockGetLIDForPN.mockResolvedValue('98765@lid') | ||
|
|
||
| const result = await resolveContactPictureIdentity('12345@s.whatsapp.net', createContext()) | ||
|
|
||
| expect(result).toEqual({ | ||
| id: '12345@s.whatsapp.net', | ||
| phoneNumber: '12345@s.whatsapp.net', | ||
| lid: '98765@lid' | ||
| }) | ||
| expect(mockGetLIDForPN).toHaveBeenCalledWith('12345@s.whatsapp.net') | ||
| }) | ||
|
|
||
| it('only fills phoneNumber when the PN resolves to a non-LID value', async () => { | ||
| mockGetLIDForPN.mockResolvedValue('not-a-lid@s.whatsapp.net') | ||
|
|
||
| const result = await resolveContactPictureIdentity('12345@s.whatsapp.net', createContext()) | ||
|
|
||
| expect(result).toEqual({ id: '12345@s.whatsapp.net', phoneNumber: '12345@s.whatsapp.net' }) | ||
| expect(result.lid).toBeUndefined() | ||
| }) | ||
|
|
||
| it('swallows resolver errors and falls back to the raw LID', async () => { | ||
| mockGetPNForLID.mockRejectedValue(new Error('lookup failed')) | ||
|
|
||
| const result = await resolveContactPictureIdentity('98765@lid', createContext()) | ||
|
|
||
| expect(result).toEqual({ id: '98765@lid', lid: '98765@lid' }) | ||
| }) | ||
|
|
||
| it('does not attempt resolution for hosted LIDs (leaves id untouched)', async () => { | ||
| const result = await resolveContactPictureIdentity('98765@hosted.lid', createContext()) | ||
|
|
||
| expect(result).toEqual({ id: '98765@hosted.lid' }) | ||
| expect(mockGetPNForLID).not.toHaveBeenCalled() | ||
| expect(mockGetLIDForPN).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.