generated from well-known-components/template-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Get profiles from catalyst and map to friends
- Loading branch information
1 parent
c1b68ac
commit 77fbe7e
Showing
10 changed files
with
259 additions
and
32 deletions.
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
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,17 @@ | ||
import { User } from '@dcl/protocol/out-js/decentraland/social_service/v2/social_service_v2.gen' | ||
import { Entity } from '@dcl/schemas' | ||
import { getProfileAvatar, getProfilePictureUrl } from './profiles' | ||
import { normalizeAddress } from '../utils/address' | ||
|
||
export function parseProfilesToFriends(profiles: Entity[], contentServerUrl: string): User[] { | ||
return profiles.map((profile) => { | ||
const { userId, name, hasClaimedName } = getProfileAvatar(profile) | ||
|
||
return { | ||
address: normalizeAddress(userId), | ||
name, | ||
hasClaimedName, | ||
profilePictureUrl: getProfilePictureUrl(contentServerUrl, profile) | ||
} | ||
}) | ||
} |
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,29 @@ | ||
import { Entity } from '@dcl/schemas' | ||
|
||
type Avatar = { | ||
userId: string | ||
name: string | ||
hasClaimedName: boolean | ||
snapshots: { | ||
face256: string | ||
} | ||
} | ||
|
||
export function getProfileAvatar(profile: Entity): Avatar { | ||
const [avatar] = profile.metadata.avatars | ||
|
||
if (!avatar) throw new Error('Missing profile avatar') | ||
|
||
return avatar | ||
} | ||
|
||
export function getProfilePictureUrl(baseUrl: string, profile: Entity): string { | ||
if (!baseUrl) throw new Error('Missing baseUrl for profile picture') | ||
|
||
const avatar = getProfileAvatar(profile) | ||
const hash = avatar?.snapshots.face256 | ||
|
||
if (!hash) throw new Error('Missing snapshot hash for profile picture') | ||
|
||
return `${baseUrl}/contents/${hash}` | ||
} |
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,43 @@ | ||
import { Entity, EntityType } from '@dcl/schemas' | ||
|
||
export const mockProfile: Entity = { | ||
version: '1', | ||
id: 'profile-id', | ||
type: EntityType.PROFILE, | ||
metadata: { | ||
avatars: [ | ||
{ | ||
userId: '0x123', | ||
name: 'TestUser', | ||
hasClaimedName: true, | ||
snapshots: { | ||
face256: 'bafybeiasdfqwer' | ||
} | ||
} | ||
] | ||
}, | ||
pointers: ['0x123'], | ||
timestamp: new Date().getTime(), | ||
content: [ | ||
{ | ||
file: 'face256', | ||
hash: 'bafybeiasdfqwer' | ||
} | ||
] | ||
} | ||
|
||
export const createMockProfile = (address: string): Entity => ({ | ||
...mockProfile, | ||
pointers: [address], | ||
metadata: { | ||
...mockProfile.metadata, | ||
avatars: [ | ||
{ | ||
...mockProfile.metadata.avatars[0], | ||
userId: address, | ||
name: `Profile name ${address}`, | ||
hasClaimedName: true | ||
} | ||
] | ||
} | ||
}) |
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,42 @@ | ||
import { Entity } from '@dcl/schemas' | ||
import { parseProfilesToFriends } from '../../../src/logic/friends' | ||
import { normalizeAddress } from '../../../src/utils/address' | ||
import { mockProfile } from '../../mocks/profile' | ||
|
||
describe('parseProfilesToFriends', () => { | ||
it('should convert profile entities to friend users', () => { | ||
const contentServerUrl = 'https://peer.decentraland.org' | ||
const anotherProfile = { | ||
...mockProfile, | ||
metadata: { | ||
...mockProfile.metadata, | ||
avatars: [ | ||
{ | ||
...mockProfile.metadata.avatars[0], | ||
userId: '0x123aBcDE', | ||
name: 'TestUser2', | ||
hasClaimedName: false | ||
} | ||
] | ||
} | ||
} | ||
const profiles = [mockProfile, anotherProfile] | ||
|
||
const result = parseProfilesToFriends(profiles, contentServerUrl) | ||
|
||
expect(result).toEqual([ | ||
{ | ||
address: '0x123', | ||
name: 'TestUser', | ||
hasClaimedName: true, | ||
profilePictureUrl: 'https://peer.decentraland.org/contents/bafybeiasdfqwer' | ||
}, | ||
{ | ||
address: '0x123abcde', | ||
name: 'TestUser2', | ||
hasClaimedName: false, | ||
profilePictureUrl: 'https://peer.decentraland.org/contents/bafybeiasdfqwer' | ||
} | ||
]) | ||
}) | ||
}) |
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,74 @@ | ||
import { Entity } from '@dcl/schemas' | ||
import { getProfileAvatar, getProfilePictureUrl } from '../../../src/logic/profiles' | ||
import { mockProfile } from '../../mocks/profile' | ||
|
||
describe('getProfileAvatar', () => { | ||
it('should extract avatar information from profile entity', () => { | ||
const avatar = getProfileAvatar(mockProfile) | ||
|
||
expect(avatar).toEqual({ | ||
userId: '0x123', | ||
name: 'TestUser', | ||
hasClaimedName: true, | ||
snapshots: { | ||
face256: 'bafybeiasdfqwer' | ||
} | ||
}) | ||
}) | ||
|
||
it('should handle profile without avatars gracefully', () => { | ||
const emptyProfile: Entity = { | ||
...mockProfile, | ||
metadata: { | ||
avatars: [] | ||
} | ||
} | ||
|
||
expect(() => getProfileAvatar(emptyProfile)).toThrow('Missing profile avatar') | ||
}) | ||
}) | ||
|
||
describe('getProfilePictureUrl', () => { | ||
const baseUrl = 'https://peer.dcl.local/content' | ||
|
||
it('should construct correct profile picture URL', () => { | ||
const url = getProfilePictureUrl(baseUrl, mockProfile) | ||
|
||
expect(url).toBe('https://peer.dcl.local/content/contents/bafybeiasdfqwer') | ||
}) | ||
|
||
it('should handle missing avatar data gracefully', () => { | ||
const emptyProfile: Entity = { | ||
...mockProfile, | ||
metadata: { | ||
avatars: [] | ||
} | ||
} | ||
|
||
expect(() => getProfilePictureUrl(baseUrl, emptyProfile)).toThrow('Missing profile avatar') | ||
}) | ||
|
||
it('should handle missing snapshots data', () => { | ||
const profileWithoutSnapshot: Entity = { | ||
...mockProfile, | ||
metadata: { | ||
avatars: [ | ||
{ | ||
userId: '0x123', | ||
name: 'TestUser', | ||
hasClaimedName: true, | ||
snapshots: {} | ||
} | ||
] | ||
} | ||
} | ||
|
||
expect(() => getProfilePictureUrl(baseUrl, profileWithoutSnapshot)).toThrow( | ||
'Missing snapshot hash for profile picture' | ||
) | ||
}) | ||
|
||
it('should throw on empty baseUrl', () => { | ||
expect(() => getProfilePictureUrl('', mockProfile)).toThrow('Missing baseUrl') | ||
}) | ||
}) |