Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
1import { HEY_ENS_NAMESPACE } from "@hey/data/constants";
2import getAccount from "@hey/helpers/getAccount";
3import getAvatar from "@hey/helpers/getAvatar";
4import type {
5 Maybe,
6 MetadataAttributeFragment,
7 UsernameFragment
8} from "@hey/indexer";
9import {
10 AccountDocument,
11 type AccountFragment,
12 UsernameDocument
13} from "@hey/indexer";
14import apolloClient from "@hey/indexer/apollo/client";
15import { type Hex, zeroAddress } from "viem";
16
17const getAccountAttribute = (
18 key: string,
19 attributes: Maybe<MetadataAttributeFragment[]> = []
20): string => {
21 const attribute = attributes?.find((attr) => attr.key === key);
22 return attribute?.value || "";
23};
24
25interface LensAccount {
26 address: Hex;
27 username: string;
28 texts: {
29 avatar: string;
30 description: string;
31 name?: string;
32 url: string;
33 location?: string;
34 "com.twitter"?: string;
35 };
36}
37
38const defaultAccount: LensAccount = {
39 address: zeroAddress,
40 texts: {
41 avatar: "",
42 "com.twitter": "",
43 description: "",
44 location: "",
45 name: "",
46 url: ""
47 },
48 username: ""
49};
50
51const getLensAccount = async (handle: string): Promise<LensAccount> => {
52 try {
53 const { data: usernameData } = await apolloClient.query<{
54 username: UsernameFragment;
55 }>({
56 fetchPolicy: "no-cache",
57 query: UsernameDocument,
58 variables: {
59 request: {
60 username: { localName: handle, namespace: HEY_ENS_NAMESPACE }
61 }
62 }
63 });
64
65 if (!usernameData.username) {
66 return defaultAccount;
67 }
68
69 const { data } = await apolloClient.query<{
70 account: AccountFragment;
71 }>({
72 fetchPolicy: "no-cache",
73 query: AccountDocument,
74 variables: { request: { address: usernameData.username.ownedBy } }
75 });
76
77 const address = data.account.owner;
78 if (!address) return defaultAccount;
79 return {
80 address: address.toLowerCase() as Hex,
81 texts: {
82 avatar: getAvatar(data.account),
83 "com.twitter": getAccountAttribute(
84 "x",
85 data.account?.metadata?.attributes
86 ),
87 description: data.account.metadata?.bio ?? "",
88 location: getAccountAttribute(
89 "location",
90 data.account?.metadata?.attributes
91 ),
92 name: getAccount(data.account).name,
93 url: `https://hey.xyz${getAccount(data.account).link}`
94 },
95 username: getAccount(data.account).username
96 };
97 } catch {
98 return defaultAccount;
99 }
100};
101
102export default getLensAccount;