-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
94 lines (81 loc) · 2.4 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { inflateSync, deflateSync } from 'node:zlib';
import {
type RenderedProfile,
type IcebreakerProfile,
type Channel,
} from './lib/types.js';
import { CLOUDINARY_AVATAR_URL } from './constants.js';
export function truncateAddress(address: string | undefined) {
return address?.replace(address.slice(6, -4), '...') ?? '';
}
const PROTOCOL_MATCHER = /(^\w+:|^)\/\//;
const TRAILING_SLASH = /\/$/;
const CLOUDINARY_AVATAR_MATCHER =
/^https:\/\/res\.cloudinary\.com\/merkle-manufactory\/image\/fetch\/.*?\//;
export function sanitizeAvatarURL(url?: string) {
if (!url) {
return url;
}
// Cloudinary URLs are broken
if (url.startsWith(CLOUDINARY_AVATAR_URL)) {
return decodeURIComponent(url.replace(CLOUDINARY_AVATAR_MATCHER, ''));
}
return url;
}
export function toRenderedProfile(
profile?: IcebreakerProfile,
): RenderedProfile | undefined {
if (!profile) {
return;
}
return {
avatarUrl: profile.avatarUrl
? profile.avatarUrl.endsWith('.webp')
? '/avatar_black.png'
: (sanitizeAvatarURL(profile.avatarUrl) as string)
: '/avatar_black.png',
displayName: profile.displayName || truncateAddress(profile.walletAddress),
bio: profile.bio,
jobTitle: profile.jobTitle,
location: profile.location,
networkingStatus: profile.networkingStatus,
primarySkill: profile.primarySkill,
credentialsCount: profile.credentials?.length ?? 0,
verifiedChannels:
profile.channels?.flatMap(({ isVerified, type }) =>
isVerified ? type : [],
) ?? [],
verifiedCompanies:
profile.workExperience?.flatMap(({ isVerified, orgWebsite }) =>
isVerified && orgWebsite
? orgWebsite.replace(PROTOCOL_MATCHER, '').replace(TRAILING_SLASH, '')
: [],
) ?? [],
};
}
export function compressProfile(profile?: RenderedProfile) {
if (!profile) {
return;
}
return deflateSync(JSON.stringify(profile)).toString('base64');
}
export function decompressProfile(
compressedProfile?: string,
): RenderedProfile | undefined {
if (!compressedProfile) {
return;
}
try {
return JSON.parse(
inflateSync(Buffer.from(compressedProfile, 'base64')).toString(),
);
} catch {
return;
}
}
export function getFIDFromChannels(channels?: Channel[]) {
return channels
?.find(({ type }) => type === 'farcaster')
?.metadata?.find(({ name }) => name === 'fid')
?.value.toString();
}