Skip to content

Commit

Permalink
feat: enable to pass an entity as profile (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
leanmendoza authored Sep 30, 2024
1 parent b8730e6 commit 72a3701
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/lib/api/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ class PeerApi {
return null
}
}

async fetchProfileEntity(content: string, peerUrl: string) {
try {
const entity = await json<Entity>(`${peerUrl}/content/contents/${content}`)
if (entity.type !== 'profile') {
throw new Error('The content is not a profile')
}

return entity.metadata as Profile
} catch (error) {
console.error('There was an error loading the profile', error)
return null
}
}
}

export const peerApi = new PeerApi()
36 changes: 31 additions & 5 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,34 @@ function isValidAddress(address: string): boolean {
return /^0x[a-fA-F0-9]{40}$/g.test(address)
}

function sanitizeProfile(profile: string | null | undefined) {
function isValidCIDv1(entity: string): boolean {
return /^baf[a-z2-7]{56}$/.test(entity)
}

function sanitizeProfile(profile: string | null | undefined): {
type: 'address' | 'entity',
value: string
} | null {
if (!profile) {
return null
}

if (profile === DEFAULT_PROFILE) {
return profile
return { type: 'address', value: profile }
}

// Accept numbered default profiles. Eg: default1, default2, etc
if (profile.startsWith(DEFAULT_PROFILE)) {
return isNaN(Number(profile.replace(DEFAULT_PROFILE, ''))) ? null : profile
return isNaN(Number(profile.replace(DEFAULT_PROFILE, ''))) ? null : { type: 'address', value: profile }
}

if (isValidAddress(profile)) {
return profile
return { type: 'address', value: profile }
}

// for now it only supports CIDv1
if (isValidCIDv1(profile)) {
return { type: 'entity', value: profile }
}

return null
Expand Down Expand Up @@ -173,6 +185,16 @@ async function fetchProfile(profile: string, peerUrl: string) {
})
}

async function fetchProfileEntity(profile: string, peerUrl: string) {
return profileMemo.memo(profile, async () => {
const resp = await peerApi
.fetchProfileEntity(profile, peerUrl)
.then((profile) => (profile && profile.avatars.length > 0 ? profile.avatars[0] : null))
.catch((error: Error) => console.log(`Failed to load profile="${profile}"`, error))
return resp || null
})
}

async function fetchItemFromContract(options: {
contractAddress: string
itemId?: string | null
Expand Down Expand Up @@ -267,7 +289,11 @@ export async function createConfig(options: PreviewOptions = {}): Promise<Previe

// load profile
const sanitizedProfile = sanitizeProfile(options.profile)
const profilePromise = sanitizedProfile ? fetchProfile(sanitizedProfile, peerUrl) : Promise.resolve(null)
const profilePromise = sanitizedProfile ?
sanitizedProfile.type === 'address' ?
fetchProfile(sanitizedProfile.value, peerUrl) :
fetchProfileEntity(sanitizedProfile.value, peerUrl)
: Promise.resolve(null)

// await promises
const [item, profile] = await Promise.all([itemPromise, profilePromise] as const)
Expand Down

0 comments on commit 72a3701

Please sign in to comment.