-
-
Notifications
You must be signed in to change notification settings - Fork 146
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
Contacts: Saving banner and photos locally #1953
Merged
kaloudis
merged 6 commits into
ZeusLN:master
from
shubhamkmr04:shubham/download-images-before-importing
Jan 20, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
184b093
Downloading banner and photos and saving them locally
shubhamkmr04 c09de86
Contacts: fix loading display
kaloudis 83d2797
Contacts: fix loading animation after import
kaloudis 49383b5
Add ContactUtils and lock version of react-native-fs
shubhamkmr04 cc49f7b
Contacts: Modify delete button state and display it only in case of m…
shubhamkmr04 ba50451
Added test for ContactUtils
shubhamkmr04 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 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 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 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 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,101 @@ | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import ContactUtils from './ContactUtils'; | ||
|
||
// Mocking the dependencies | ||
jest.mock('react-native-fs', () => ({ | ||
DocumentDirectoryPath: '/mocked/document/directory', | ||
downloadFile: jest.fn(() => ({ promise: Promise.resolve() })) | ||
})); | ||
|
||
jest.mock('uuid', () => ({ | ||
v4: jest.fn(() => 'mocked-uuid') | ||
})); | ||
|
||
describe('ContactUtils', () => { | ||
describe('transformContactData', () => { | ||
it('transforms contact data properly', async () => { | ||
// Test Case 1 | ||
const mockContact1 = { | ||
display_name: 'Shubham', | ||
name: 'Shubham', | ||
about: 'Developer', | ||
lud16: 'shubham@zeuspay.com', | ||
nip05: 'nip05Address', | ||
npub: 'npub1a3vuemsaex2rmshv3h5sr8xlxayehzkan3ucw3sxf8l2rtjyuhpshexnw4' | ||
}; | ||
|
||
const transformedContact1 = await ContactUtils.transformContactData( | ||
mockContact1 | ||
); | ||
|
||
// Assertions | ||
expect(uuidv4).toHaveBeenCalled(); | ||
expect(transformedContact1).toEqual({ | ||
photo: '', | ||
name: 'Shubham', | ||
description: 'Developer', | ||
lnAddress: ['shubham@zeuspay.com'], | ||
onchainAddress: [''], | ||
pubkey: [''], | ||
nip05: ['nip05Address'], | ||
nostrNpub: [ | ||
'npub1a3vuemsaex2rmshv3h5sr8xlxayehzkan3ucw3sxf8l2rtjyuhpshexnw4' | ||
], | ||
contactId: 'mocked-uuid', | ||
isFavourite: false, | ||
banner: '', | ||
isSelected: false | ||
}); | ||
|
||
// Test Case 2 | ||
const mockContact2 = { | ||
display_name: 'Evan', | ||
name: 'Evan', | ||
about: 'Maintainer at Zeus', | ||
lud16: 'evan@zeuspay.com', | ||
nip05: 'nip05Address', | ||
npub: 'npub1xnf02f60r9v0e5kty33a404dm79zr7z2eepyrk5gsq3m7pwvsz2sazlpr5' | ||
}; | ||
|
||
const transformedContact2 = await ContactUtils.transformContactData( | ||
mockContact2 | ||
); | ||
|
||
// Assertions | ||
expect(uuidv4).toHaveBeenCalled(); | ||
expect(transformedContact2).toEqual({ | ||
photo: '', | ||
name: 'Evan', | ||
description: 'Maintainer at Zeus', | ||
lnAddress: ['evan@zeuspay.com'], | ||
onchainAddress: [''], | ||
pubkey: [''], | ||
nip05: ['nip05Address'], | ||
nostrNpub: [ | ||
'npub1xnf02f60r9v0e5kty33a404dm79zr7z2eepyrk5gsq3m7pwvsz2sazlpr5' | ||
], | ||
contactId: 'mocked-uuid', | ||
isFavourite: false, | ||
banner: '', | ||
isSelected: false | ||
}); | ||
}); | ||
|
||
it('handles contact transformation ensuring result is not null and is an object.', async () => { | ||
const mockContact = { | ||
display_name: 'Shubham', | ||
about: 'Software Developer', | ||
lud16: 'shubham@zeuspay.com', | ||
nip05: 'nip05Address', | ||
npub: 'npub1a3vuemsaex2rmshv3h5sr8xlxayehzkan3ucw3sxf8l2rtjyuhpshexnw4' | ||
}; | ||
const transformedContact = await ContactUtils.transformContactData( | ||
mockContact | ||
); | ||
|
||
// Expect that the transformation did not result in null but is an object | ||
expect(transformedContact).toBeDefined(); | ||
expect(transformedContact).toBeInstanceOf(Object); | ||
}); | ||
}); | ||
}); |
This file contains 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,83 @@ | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { nip19 } from 'nostr-tools'; | ||
import RNFS from 'react-native-fs'; | ||
|
||
const transformContactData = async (contact: any) => { | ||
try { | ||
const name = contact?.display_name || contact?.name || ''; | ||
const transformedContact = { | ||
photo: '', | ||
name, | ||
description: contact?.about || '', | ||
lnAddress: contact?.lud16 | ||
? [contact?.lud16] | ||
: contact?.lud06 | ||
? [contact?.lud06] | ||
: contact?.lud16 && contact?.lud06 | ||
? [contact?.lud06, contact?.lud16] | ||
: [], | ||
onchainAddress: [''], | ||
pubkey: [''], | ||
nip05: contact?.nip05 ? [contact?.nip05] : [], | ||
nostrNpub: contact?.npub | ||
? [contact?.npub] | ||
: contact?.pubkey | ||
? [nip19.npubEncode(contact.pubkey)] | ||
: [], | ||
contactId: uuidv4(), | ||
isFavourite: false, | ||
banner: '', | ||
isSelected: false | ||
}; | ||
|
||
if (contact?.banner) { | ||
console.log('Downloading banner...'); | ||
const bannerFileName = | ||
'nostrContactBanner_' + transformedContact.name + '.png'; | ||
const bannerFilePath = | ||
RNFS.DocumentDirectoryPath + '/' + bannerFileName; | ||
|
||
try { | ||
// Download the banner and save it locally | ||
await RNFS.downloadFile({ | ||
fromUrl: contact?.banner, | ||
toFile: bannerFilePath | ||
}).promise; | ||
|
||
console.log('Banner download successful!'); | ||
transformedContact.banner = 'file://' + bannerFilePath; | ||
} catch (bannerError) { | ||
console.error('Error downloading banner:', bannerError); | ||
} | ||
} | ||
|
||
console.log('Transformed contact:', transformedContact); | ||
|
||
if (contact?.picture) { | ||
console.log('Downloading image...'); | ||
const fileName = | ||
'nostrContactPhoto_' + transformedContact.contactId + '.png'; | ||
const filePath = RNFS.DocumentDirectoryPath + '/' + fileName; | ||
|
||
try { | ||
// Download the image and save it locally | ||
await RNFS.downloadFile({ | ||
fromUrl: contact?.picture, | ||
toFile: filePath | ||
}).promise; | ||
|
||
console.log('Download successful!'); | ||
transformedContact.photo = 'file://' + filePath; | ||
} catch (photoError) { | ||
console.error('Error downloading photo:', photoError); | ||
} | ||
} | ||
|
||
return transformedContact; | ||
} catch (error) { | ||
console.error('Error transforming contact:', error); | ||
return null; | ||
} | ||
}; | ||
|
||
export default { transformContactData }; |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this required if we're not processing any images?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is not required for now, but unknowingly, we get issues in the test when this mock is removed, so I decided to keep it as it will be helpful in our future implementations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good