Skip to content

Commit

Permalink
invitations now without re-entering firstname, lastname and email if …
Browse files Browse the repository at this point in the history
…already in browser
  • Loading branch information
silkroadnomad committed Apr 1, 2024
1 parent 1c31ea2 commit 1dd0707
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 18 deletions.
10 changes: 6 additions & 4 deletions src/lib/components/ContactForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,20 @@
<Row>
<Column>
{#if $selectedAddr._id}
{#if $selectedAddr.owner === $orbitdb.identity.id}
{#if $selectedAddr.owner === $orbitdb?.identity?.id}
<Button data-cy="updateContact" size="sm" on:click={() => updateContact()}>Update</Button>
<Button data-cy="newContact" size="sm" on:click={() => $selectedAddr = {}}>New</Button>
{/if}
<Button data-cy="deleteContact" size="sm" on:click={() => deleteContact()}>Delete</Button>

{:else}
<Button data-cy="addContact" size="sm" on:click={async () => {
await addContact(isOnBoarding)

if(isOnBoarding) {
//await requestAddress(messageObj.sender,true) //TODO
window.location.hash="/"
await updateContact(true)
window.location.hash="/"
}else{
addContact()
}
}}>{!isOnBoarding?"Add":"Add & View Contact Data "}</Button>
{/if}
Expand Down
36 changes: 31 additions & 5 deletions src/lib/components/OnBoarding.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
selectedAddr,
orbitdb,
libp2p,
// dbMyAddressBook,
dbMyAddressBook,
myAddressBook,
connectedPeers
} from "../../stores.js";
import { requestAddress } from "../../lib/network/p2p-operations.js"
import ContactForm from "./ContactForm.svelte"
import { sha256 } from '../../utils/utils.js';
// import { page } from '$app/stores'
Expand All @@ -19,8 +21,11 @@
// const aliceMultiAddress = $page.url.searchParams.get('onBoardingToken') ((JSON.parse(decodeURI($query.split("&")[1]).split("=")[1])
// const aliceMultiAddress = JSON.parse(decodeURI($query.split("&")[1]).split("=")[1])
const aliceMultiAddress = undefined
let scannedContact
let scannedContact //the contact which was scanned
let ourContact //if we have an address book available
let requested
let dummyWritten
let aliceConnected
$:{ //connect to a multiAddress if one was given!
Expand All @@ -36,15 +41,36 @@
$:{
if(!requested &&
$connectedPeers>1 && //if connected
$did!==undefined &&
$orbitdb!==undefined ){
$connectedPeers >1 && //if connected
$did !== undefined &&
$orbitdb !== undefined ){
requestAddress($did,false,onBoardingToken)
requested = true
}
}
$:scannedContact = $myAddressBook?.filter((it) => { return it.owner === $did })
$:{ //we need a dummy - if we have already real data don't ask them from the invited person!
if(!dummyWritten &&
$libp2p &&
$connectedPeers>0 &&
$dbMyAddressBook !==undefined && // !Array.isArray($dbMyAddressBook) && //TODO why is this an array at some point?
$orbitdb !== undefined ){
ourContact = $myAddressBook?.filter((it) => { return it.owner === $orbitdb?.identity?.id })
if(ourContact.length>0) window.location.hash="/" //no need to ask for email, firstname anyore - go straight to contact list
else { //create a dummy contact for us so we can write it into the requesters db as long the user didn't write the contact data
$selectedAddr.owner = $orbitdb?.identity?.id
$selectedAddr.lastName = $orbitdb?.identity?.id
$selectedAddr.own = true
$selectedAddr.sharedAddress = $dbMyAddressBook?.address
sha256(JSON.stringify($selectedAddr)).then((sha256Hash)=>{
$selectedAddr._id = sha256Hash
$dbMyAddressBook.put($selectedAddr).then((orbitHash)=>console.log("dummy written with orbitHash",orbitHash))
}) //hash is staying until end of life - we have only one address
}
dummyWritten=true
}
}
$:{
if($selectedAddr.email && ! $selectedAddr.firstName && ! $selectedAddr.lastName){
Expand Down
6 changes: 5 additions & 1 deletion src/lib/network/p2p-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,19 +454,23 @@ function startInvitationCheckWorker() {
*/
export async function writeMyAddressIntoRequesterDB(requesterDB) {
try {

//1. Take out our address (the one where is the owner like our identity)
const writeFirstOfOurAddresses = _myAddressBook.filter((it) => {
return it.owner === _orbitdb?.identity?.id
})[0]
delete writeFirstOfOurAddresses.own;

//delete the dummy which alice added for us!
//find our dummy which Alice added for it (the one with our id again)
const all = await requesterDB.all()
const foundDummy = all.filter((it) => {
return it.value.owner === _orbitdb?.identity?.id
})

//now overwrite the dummy with new data (but take our id)
for (const foundDummyKey in foundDummy) {
// await requesterDB.del(foundDummy[foundDummyKey].key)|
delete writeFirstOfOurAddresses.id //delete an id if it should contain one
writeFirstOfOurAddresses._id = foundDummy[foundDummyKey].value._id
const hash = await requesterDB.put(writeFirstOfOurAddresses);
notify(`wrote my address into requesters db with hash ${hash}`);
Expand Down
19 changes: 12 additions & 7 deletions src/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,32 @@ export async function loadContact(id) {
*/
export async function addContact(isOnBoarding) {

_selectedAddr.owner = _orbitdb?.identity?.id
_selectedAddr.sharedAddress = _dbMyAddressBook?.address
_selectedAddr._id = await sha256(JSON.stringify(_selectedAddr)) //TODO this hash is staying so far until the end of life
const hash = await _dbMyAddressBook.put(_selectedAddr)
if(!isOnBoarding){
_selectedAddr.owner = _orbitdb?.identity?.id
_selectedAddr.sharedAddress = _dbMyAddressBook?.address
_selectedAddr._id = await sha256(JSON.stringify(_selectedAddr))
const hash = await _dbMyAddressBook.put(_selectedAddr)
notify(`Contact added successfully to ipfs/orbitdb! ${hash}`);
selectedAddr.set({})
selectedTab.set(0)
}
else{
else{ //here we should have already _id, owner etc fields from the dummy before (otherwise we get double entries in Alice contact list)
console.log("going to root now")
const hash = await _dbMyAddressBook.put(_selectedAddr)
notify(`Contact added successfully to ipfs/orbitdb! ${hash}`);
window.location.pathname = "/"
selectedAddr.set({})
selectedTab.set(0)
}
notify(`Contact added successfully to ipfs/orbitdb! ${hash}`);

}

/**
* 1. Save the current edited address into the Svelte storage (local only)
* 2. Inform the subscribers about the address update
* @returns {Promise<void>}
*/
export async function updateContact() {
export async function updateContact(isOnBoarding) {
const newAddrBook = _myAddressBook.filter( el => el._id !== _selectedAddr._id )
_selectedAddr.owner = _orbitdb?.identity?.id
await _dbMyAddressBook.put(_selectedAddr)
Expand All @@ -70,6 +73,8 @@ export async function updateContact() {
}
selectedAddr.set({})
selectedTab.set(0)
if(isOnBoarding) window.location.pathname = "/"

}

export async function deleteContact() {
Expand Down
3 changes: 2 additions & 1 deletion src/stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export const connectedPeers = writable(0)



export const myAddressBook = writable([])
/** this is the orbitdb which holds the contact data */
export const myAddressBook = writable()
/** an orbitdb which will replace the myAddressBook store based on localstorage //TODO */
export const dbMyAddressBook = writable([])

Expand Down

0 comments on commit 1dd0707

Please sign in to comment.