From b4b337583970df7cb4e6745549d420d4d3a00904 Mon Sep 17 00:00:00 2001 From: Wukong Sun <158803171+swkatmask@users.noreply.github.com> Date: Sun, 28 Apr 2024 13:36:48 +0800 Subject: [PATCH] fix: mf-6200 nft avatar decoration (#11598) --- .github/workflows/compile.yml | 5 ++- .../injection/NFT/useSaveAvatarInFacebook.ts | 2 +- .../injection/NFT/NFTAvatarSettingDialog.tsx | 2 +- .../twitter.com/collecting/identity.ts | 2 +- .../injection/NFT/NFTAvatarInTwitter.tsx | 21 ++++++------ .../injection/NFT/useSaveAvatarInTwitter.tsx | 2 +- .../src/Application/UploadAvatarDialog.tsx | 5 ++- packages/plugins/Avatar/src/constants.ts | 4 +-- packages/plugins/Avatar/src/hooks/useSave.ts | 21 ++++++------ .../Avatar/src/hooks/useSaveAddress.ts | 33 ++++++++++--------- .../plugins/Avatar/src/hooks/useSaveAvatar.ts | 10 +++--- .../plugins/Avatar/src/hooks/useSaveKV.ts | 15 +++------ .../Avatar/src/hooks/useSaveStringStorage.ts | 17 ++++------ .../Avatar/src/hooks/useSaveToNextID.ts | 8 ++--- .../src/AvatarStore/helpers/getAvatar.ts | 2 +- .../helpers/getAvatarFromNextID.ts | 4 +-- .../AvatarStore/helpers/getPersonaAvatar.ts | 2 +- .../{getAvatarFromStorage.ts => storage.ts} | 9 +++++ .../web3-providers/src/AvatarStore/index.ts | 3 +- .../src/Storage/apis/Storage.ts | 2 +- .../src/Twitter/apis/getUserByScreenName.ts | 12 +++---- .../Twitter/apis/getUserByScreenNameShow.ts | 12 +++---- .../Twitter/apis/getUserViaTwitterIdentity.ts | 11 ++----- packages/web3-providers/src/entry.ts | 2 +- 24 files changed, 97 insertions(+), 109 deletions(-) rename packages/web3-providers/src/AvatarStore/helpers/{getAvatarFromStorage.ts => storage.ts} (63%) diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml index 7d0672f42fa1..deaa7a3d3458 100644 --- a/.github/workflows/compile.yml +++ b/.github/workflows/compile.yml @@ -57,7 +57,10 @@ jobs: # See https://github.com/getsentry/action-release/issues/187 - name: Change git configuration shell: bash - run: git config --unset-all extensions.worktreeconfig + run: | + if [ -n "$(git config --get extensions.worktreeconfig)" ]; then + git config --unset-all extensions.worktreeconfig + fi - name: Create Sentry release uses: getsentry/action-release@v1 if: github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/released' diff --git a/packages/mask/content-script/site-adaptors/facebook.com/injection/NFT/useSaveAvatarInFacebook.ts b/packages/mask/content-script/site-adaptors/facebook.com/injection/NFT/useSaveAvatarInFacebook.ts index 2d311b1f3658..bbfbf4e76b8c 100644 --- a/packages/mask/content-script/site-adaptors/facebook.com/injection/NFT/useSaveAvatarInFacebook.ts +++ b/packages/mask/content-script/site-adaptors/facebook.com/injection/NFT/useSaveAvatarInFacebook.ts @@ -12,7 +12,7 @@ export function useSaveAvatarInFacebook(identity: IdentityResolved) { const { account } = useChainContext() const [NFTEvent, setNFTEvent] = useState(null) - const [, saveNFTAvatar] = useSaveStringStorage(NetworkPluginID.PLUGIN_EVM) + const saveNFTAvatar = useSaveStringStorage(NetworkPluginID.PLUGIN_EVM) const onSave = useCallback(async () => { if (!account || !identity.identifier) return diff --git a/packages/mask/content-script/site-adaptors/instagram.com/injection/NFT/NFTAvatarSettingDialog.tsx b/packages/mask/content-script/site-adaptors/instagram.com/injection/NFT/NFTAvatarSettingDialog.tsx index 16a985975aed..557aaf589308 100644 --- a/packages/mask/content-script/site-adaptors/instagram.com/injection/NFT/NFTAvatarSettingDialog.tsx +++ b/packages/mask/content-script/site-adaptors/instagram.com/injection/NFT/NFTAvatarSettingDialog.tsx @@ -29,7 +29,7 @@ export function NFTAvatarSettingDialog() { const { classes } = useStyles() const { account } = useChainContext() const identity = useCurrentVisitingIdentity() - const [, saveNFTAvatar] = useSaveStringStorage(NetworkPluginID.PLUGIN_EVM) + const saveNFTAvatar = useSaveStringStorage(NetworkPluginID.PLUGIN_EVM) const onChange = useCallback( async (info: SelectTokenInfo) => { diff --git a/packages/mask/content-script/site-adaptors/twitter.com/collecting/identity.ts b/packages/mask/content-script/site-adaptors/twitter.com/collecting/identity.ts index 7cf67b8cc4c3..a85db7092677 100644 --- a/packages/mask/content-script/site-adaptors/twitter.com/collecting/identity.ts +++ b/packages/mask/content-script/site-adaptors/twitter.com/collecting/identity.ts @@ -133,7 +133,7 @@ function resolveCurrentVisitingIdentityInner( const handle = user.screenName const ownerHandle = ownerRef.value.identifier?.userId - const isOwner = !!(ownerHandle && handle.toLowerCase() === ownerHandle.toLowerCase()) + const isOwner = !!ownerHandle && handle.toLowerCase() === ownerHandle.toLowerCase() const avatar = user.avatarURL const bio = user.bio const homepage = user.homepage diff --git a/packages/mask/content-script/site-adaptors/twitter.com/injection/NFT/NFTAvatarInTwitter.tsx b/packages/mask/content-script/site-adaptors/twitter.com/injection/NFT/NFTAvatarInTwitter.tsx index 74cc03d9135e..1748b56c72dd 100644 --- a/packages/mask/content-script/site-adaptors/twitter.com/injection/NFT/NFTAvatarInTwitter.tsx +++ b/packages/mask/content-script/site-adaptors/twitter.com/injection/NFT/NFTAvatarInTwitter.tsx @@ -6,7 +6,6 @@ import { useLocation, useWindowSize } from 'react-use' import { AvatarStore, Twitter } from '@masknet/web3-providers' import { useInjectedCSS } from './useInjectedCSS.js' import { useUpdatedAvatar } from './useUpdatedAvatar.js' -import { useSaveAvatarInTwitter } from './useSaveAvatarInTwitter.js' import { searchAvatarMetaSelector, searchAvatarSelector, searchTwitterAvatarSelector } from '../../utils/selector.js' import { useCurrentVisitingIdentity } from '../../../../components/DataSource/useActivatedUI.js' @@ -89,21 +88,21 @@ export function NFTAvatarInTwitter() { function useNFTCircleAvatar(size: number) { const identity = useCurrentVisitingIdentity() - const savedAvatar = useSaveAvatarInTwitter(identity) + const userId = identity.identifier?.userId || '' + const identityAvatarId = Twitter.getAvatarId(identity.avatar) const store = useSyncExternalStore(AvatarStore.subscribe, AvatarStore.getSnapshot) - const avatar = - savedAvatar ?? store.retrieveAvatar(identity.identifier?.userId, Twitter.getAvatarId(identity.avatar)) - const token = store.retrieveToken(identity.identifier?.userId, Twitter.getAvatarId(identity.avatar)) + const avatar = store.retrieveAvatar(userId, identityAvatarId) + const token = store.retrieveToken(userId, identityAvatarId) - const showAvatar = useMemo(() => { - const avatarUrl = searchAvatarSelector().evaluate()?.getAttribute('src') - if (!avatarUrl || !avatar?.avatarId) return false - return Twitter.getAvatarId(avatarUrl ?? '') === avatar.avatarId - }, [avatar?.avatarId, identity.avatar]) + useEffect(() => { + AvatarStore.dispatch(userId, identityAvatarId) + }, [userId, identityAvatarId]) + + const showAvatar = avatar?.avatarId ? identityAvatarId === avatar.avatarId : false return { - showAvatar: Boolean(size && avatar && showAvatar && token), + showAvatar: Boolean(size && showAvatar && token), avatar, token, } diff --git a/packages/mask/content-script/site-adaptors/twitter.com/injection/NFT/useSaveAvatarInTwitter.tsx b/packages/mask/content-script/site-adaptors/twitter.com/injection/NFT/useSaveAvatarInTwitter.tsx index b588c9290807..d217ec3f39fb 100644 --- a/packages/mask/content-script/site-adaptors/twitter.com/injection/NFT/useSaveAvatarInTwitter.tsx +++ b/packages/mask/content-script/site-adaptors/twitter.com/injection/NFT/useSaveAvatarInTwitter.tsx @@ -10,7 +10,7 @@ import { useAsync } from 'react-use' export function useSaveAvatarInTwitter(identity: IdentityResolved) { const { account } = useChainContext() - const [, saveNFTAvatar] = useSaveStringStorage(NetworkPluginID.PLUGIN_EVM) + const saveNFTAvatar = useSaveStringStorage(NetworkPluginID.PLUGIN_EVM) const onSave = useCallback(async () => { if (!account || !identity.identifier) return diff --git a/packages/plugins/Avatar/src/Application/UploadAvatarDialog.tsx b/packages/plugins/Avatar/src/Application/UploadAvatarDialog.tsx index 619ff3ac1a69..975cc3219d9a 100644 --- a/packages/plugins/Avatar/src/Application/UploadAvatarDialog.tsx +++ b/packages/plugins/Avatar/src/Application/UploadAvatarDialog.tsx @@ -47,8 +47,7 @@ async function uploadAvatar(blob: Blob, userId: string): Promise { diff --git a/packages/plugins/Avatar/src/constants.ts b/packages/plugins/Avatar/src/constants.ts index 41cae16ab34b..084120c0bb5c 100644 --- a/packages/plugins/Avatar/src/constants.ts +++ b/packages/plugins/Avatar/src/constants.ts @@ -1,4 +1,4 @@ -import { PluginID, NetworkPluginID, EnhanceableSite, getSiteType } from '@masknet/shared-base' +import { NetworkPluginID, PluginID } from '@masknet/shared-base' import { ChainId } from '@masknet/web3-shared-evm' export const NFT_AVATAR_DB_NAME = 'com.maskbook.user' @@ -11,5 +11,3 @@ export const PLUGIN_DESCRIPTION = 'NFT Avatar' export const SUPPORTED_CHAIN_IDS: ChainId[] = [ChainId.Mainnet, ChainId.Matic, ChainId.BSC] export const supportPluginIds = [NetworkPluginID.PLUGIN_EVM, NetworkPluginID.PLUGIN_FLOW, NetworkPluginID.PLUGIN_SOLANA] - -export const StorageKey = `${PLUGIN_NAME}-${(getSiteType() || EnhanceableSite.Twitter).replace('.com', '')}` diff --git a/packages/plugins/Avatar/src/hooks/useSave.ts b/packages/plugins/Avatar/src/hooks/useSave.ts index 44ebf28c5f6d..ff06107c4579 100644 --- a/packages/plugins/Avatar/src/hooks/useSave.ts +++ b/packages/plugins/Avatar/src/hooks/useSave.ts @@ -1,22 +1,22 @@ -import { useAsyncFn } from 'react-use' -import { NetworkPluginID, type BindingProof, type ECKeyIdentifier } from '@masknet/shared-base' +import { type ECKeyIdentifier, NetworkPluginID, type BindingProof } from '@masknet/shared-base' import type { AvatarNextID, TwitterBaseAPI } from '@masknet/web3-providers/types' import { ChainId, SchemaType } from '@masknet/web3-shared-evm' import type { AllChainsNonFungibleToken } from '../types.js' import { useSaveKV } from './useSaveKV.js' import { useSaveToNextID } from './useSaveToNextID.js' import { useSaveStringStorage } from './useSaveStringStorage.js' +import { useCallback } from 'react' export type AvatarInfo = TwitterBaseAPI.AvatarInfo & { avatarId: string } export function useSave(pluginID: NetworkPluginID) { - const [, saveToNextID] = useSaveToNextID() - const [, saveToStringStorage] = useSaveStringStorage(pluginID) - const [, saveToKV] = useSaveKV(pluginID) + const saveToNextID = useSaveToNextID() + const saveToStringStorage = useSaveStringStorage(pluginID) + const saveToKV = useSaveKV(pluginID) - return useAsyncFn( + return useCallback( async ( account: string, isBindAccount: boolean, @@ -32,11 +32,11 @@ export function useSave(pluginID: NetworkPluginID) { userId: data.userId, imageUrl: data.imageUrl, avatarId: data.avatarId, - address: token.contract.address ?? '', + address: token.contract.address, ownerAddress: account, tokenId: token.tokenId || token.id, - chainId: (token.contract.chainId ?? ChainId.Mainnet) as ChainId, - schema: (token.contract.schema ?? SchemaType.ERC721) as SchemaType, + chainId: (token.contract.chainId || ChainId.Mainnet) as ChainId, + schema: (token.contract.schema || SchemaType.ERC721) as SchemaType, } try { @@ -46,8 +46,7 @@ export function useSave(pluginID: NetworkPluginID) { return await saveToStringStorage(data.userId, account, info) } default: - if (!proof) return - return await saveToKV(info, account, persona, proof) + return await saveToKV(info, account) } } catch { return diff --git a/packages/plugins/Avatar/src/hooks/useSaveAddress.ts b/packages/plugins/Avatar/src/hooks/useSaveAddress.ts index 24e033ae7fd4..2743ccd3ceca 100644 --- a/packages/plugins/Avatar/src/hooks/useSaveAddress.ts +++ b/packages/plugins/Avatar/src/hooks/useSaveAddress.ts @@ -1,24 +1,27 @@ -import { useAsyncFn } from 'react-use' import { Web3Storage } from '@masknet/web3-providers' import type { AddressStorageV2 } from '@masknet/web3-providers/types' import { type NetworkPluginID, EnhanceableSite } from '@masknet/shared-base' import { NFT_AVATAR_DB_NAME } from '../constants.js' +import { useCallback } from 'react' export function useSaveAddress() { - return useAsyncFn(async (userId: string, pluginID: NetworkPluginID, account: string, network?: EnhanceableSite) => { - const addressStorage = Web3Storage.createKVStorage( - `${NFT_AVATAR_DB_NAME}_${network ?? EnhanceableSite.Twitter}`, - ) - if (!addressStorage) return + return useCallback( + async (userId: string, pluginID: NetworkPluginID, account: string, network?: EnhanceableSite) => { + const addressStorage = Web3Storage.createKVStorage( + `${NFT_AVATAR_DB_NAME}_${network || EnhanceableSite.Twitter}`, + ) + if (!addressStorage) return - const prevData = (await addressStorage.get(userId).catch(() => ({}))) as - | AddressStorageV2 - | undefined + const prevData = (await addressStorage.get(userId).catch(() => ({}))) as + | AddressStorageV2 + | undefined - await addressStorage.set(userId, { - ...prevData, - [pluginID]: account, - [userId]: { address: account, networkPluginID: pluginID }, - } as AddressStorageV2) - }, []) + await addressStorage.set(userId, { + ...prevData, + [pluginID]: account, + [userId]: { address: account, networkPluginID: pluginID }, + } as AddressStorageV2) + }, + [], + ) } diff --git a/packages/plugins/Avatar/src/hooks/useSaveAvatar.ts b/packages/plugins/Avatar/src/hooks/useSaveAvatar.ts index babe64401591..fb14a6735571 100644 --- a/packages/plugins/Avatar/src/hooks/useSaveAvatar.ts +++ b/packages/plugins/Avatar/src/hooks/useSaveAvatar.ts @@ -1,14 +1,14 @@ -import { useAsyncFn } from 'react-use' +import { NetworkPluginID, type EnhanceableSite } from '@masknet/shared-base' import { Web3Storage } from '@masknet/web3-providers' import { type AvatarNextID } from '@masknet/web3-providers/types' -import { NetworkPluginID, type EnhanceableSite } from '@masknet/shared-base' -import { useSaveAddress } from './useSaveAddress.js' +import { useCallback } from 'react' import { NFT_AVATAR_METADATA_STORAGE } from '../constants.js' +import { useSaveAddress } from './useSaveAddress.js' export function useSaveAvatar(pluginID?: NetworkPluginID) { - const [, saveAddress] = useSaveAddress() + const saveAddress = useSaveAddress() - return useAsyncFn( + return useCallback( async (siteType: EnhanceableSite, account: string, avatar: AvatarNextID, sign: string) => { if (avatar.userId === '$unknown') return await saveAddress(avatar.userId, avatar.pluginId ?? NetworkPluginID.PLUGIN_EVM, account, siteType) diff --git a/packages/plugins/Avatar/src/hooks/useSaveKV.ts b/packages/plugins/Avatar/src/hooks/useSaveKV.ts index 4da9c0862d26..fcefea026be4 100644 --- a/packages/plugins/Avatar/src/hooks/useSaveKV.ts +++ b/packages/plugins/Avatar/src/hooks/useSaveKV.ts @@ -1,20 +1,15 @@ -import { useAsyncFn } from 'react-use' +import { getEnhanceableSiteType, type NetworkPluginID } from '@masknet/shared-base' import { useWeb3Connection } from '@masknet/web3-hooks-base' -import { - type BindingProof, - type ECKeyIdentifier, - type NetworkPluginID, - getEnhanceableSiteType, -} from '@masknet/shared-base' import type { AvatarNextID } from '@masknet/web3-providers/types' import { useSaveAvatar } from './useSaveAvatar.js' +import { useCallback } from 'react' export function useSaveKV(pluginID: NetworkPluginID) { const Web3 = useWeb3Connection(pluginID) - const [, saveAvatar] = useSaveAvatar(pluginID) + const saveAvatar = useSaveAvatar(pluginID) - return useAsyncFn( - async (info: AvatarNextID, account: string, persona: ECKeyIdentifier, proof: BindingProof) => { + return useCallback( + async (info: AvatarNextID, account: string) => { const siteType = getEnhanceableSiteType() if (!siteType) return diff --git a/packages/plugins/Avatar/src/hooks/useSaveStringStorage.ts b/packages/plugins/Avatar/src/hooks/useSaveStringStorage.ts index 551f345273b2..f341d8e0e4d2 100644 --- a/packages/plugins/Avatar/src/hooks/useSaveStringStorage.ts +++ b/packages/plugins/Avatar/src/hooks/useSaveStringStorage.ts @@ -1,20 +1,15 @@ -import { useAsyncFn } from 'react-use' -import { Web3Storage } from '@masknet/web3-providers' +import { getEnhanceableSiteType, type NetworkPluginID } from '@masknet/shared-base' +import { setAvatarToStorage } from '@masknet/web3-providers' import type { AvatarNextID } from '@masknet/web3-providers/types' -import { type NetworkPluginID, getEnhanceableSiteType, EnhanceableSite } from '@masknet/shared-base' import { useSaveAddress } from './useSaveAddress.js' -import { PLUGIN_NAME } from '../constants.js' +import { useCallback } from 'react' export function useSaveStringStorage(pluginID: NetworkPluginID) { - const [, saveAddress] = useSaveAddress() + const saveAddress = useSaveAddress() - return useAsyncFn( + return useCallback( async (userId: string, address: string, avatar: AvatarNextID) => { - const stringStorage = Web3Storage.createFireflyStorage( - `${PLUGIN_NAME}-${(getEnhanceableSiteType() || EnhanceableSite.Twitter).replace('.com', '')}`, - address, - ) - await stringStorage.set?.(userId, avatar) + await setAvatarToStorage(userId, address, avatar) await saveAddress(avatar.userId, pluginID, address, getEnhanceableSiteType()) return avatar diff --git a/packages/plugins/Avatar/src/hooks/useSaveToNextID.ts b/packages/plugins/Avatar/src/hooks/useSaveToNextID.ts index 387f4521fd1b..75076d7029ea 100644 --- a/packages/plugins/Avatar/src/hooks/useSaveToNextID.ts +++ b/packages/plugins/Avatar/src/hooks/useSaveToNextID.ts @@ -1,14 +1,14 @@ -import { useAsyncFn } from 'react-use' import { type BindingProof, type ECKeyIdentifier, NetworkPluginID, getEnhanceableSiteType } from '@masknet/shared-base' import { Web3Storage } from '@masknet/web3-providers' import type { AvatarNextID } from '@masknet/web3-providers/types' import { signWithPersona } from '@masknet/plugin-infra/dom/context' import { useSaveAddress } from './useSaveAddress.js' import { PLUGIN_ID } from '../constants.js' +import { useCallback } from 'react' export function useSaveToNextID() { - const [, saveAddress] = useSaveAddress() - return useAsyncFn( + const saveAddress = useSaveAddress() + return useCallback( async ( info: AvatarNextID, account: string, @@ -23,7 +23,7 @@ export function useSaveToNextID() { const storage = Web3Storage.createNextIDStorage(proof.identity, proof.platform, persona, signWithPersona) await storage.set(PLUGIN_ID, info) - await saveAddress(info.userId, info.pluginId ?? NetworkPluginID.PLUGIN_EVM, account, siteType) + await saveAddress(info.userId, info.pluginId || NetworkPluginID.PLUGIN_EVM, account, siteType) return info }, diff --git a/packages/web3-providers/src/AvatarStore/helpers/getAvatar.ts b/packages/web3-providers/src/AvatarStore/helpers/getAvatar.ts index aaf6e8081446..b08a5cb9a67d 100644 --- a/packages/web3-providers/src/AvatarStore/helpers/getAvatar.ts +++ b/packages/web3-providers/src/AvatarStore/helpers/getAvatar.ts @@ -2,7 +2,7 @@ import { EnhanceableSite, NetworkPluginID, createLookupTableResolver } from '@ma import { getAddress } from './getAddress.js' import { Web3Storage } from '../../Storage/apis/Storage.js' import { RSS3_KEY_SITE, type AvatarNextID } from '../types.js' -import { getAvatarFromStorage } from './getAvatarFromStorage.js' +import { getAvatarFromStorage } from './storage.js' import { getAvatarFromRSS3 } from './getAvatarFromRSS3.js' const NFT_AVATAR_METADATA_STORAGE = 'com.maskbook.avatar.metadata.storage' diff --git a/packages/web3-providers/src/AvatarStore/helpers/getAvatarFromNextID.ts b/packages/web3-providers/src/AvatarStore/helpers/getAvatarFromNextID.ts index 0a61df3b89cb..f43f56a91977 100644 --- a/packages/web3-providers/src/AvatarStore/helpers/getAvatarFromNextID.ts +++ b/packages/web3-providers/src/AvatarStore/helpers/getAvatarFromNextID.ts @@ -41,7 +41,7 @@ export async function getAvatarFromNextID( siteType: EnhanceableSite, userId: string, avatarId: string, - persona: string, + persona?: string, ): Promise | undefined> { const platform = siteType === EnhanceableSite.Twitter ? NextIDPlatform.Twitter : undefined if (!platform) return @@ -49,7 +49,7 @@ export async function getAvatarFromNextID( const bindings = await NextIDProof.queryAllExistedBindingsByPlatform(platform, userId) if (persona) { - const binding = bindings.filter((x) => x.persona.toLowerCase() === persona.toLowerCase())?.[0] + const binding = bindings.find((x) => x.persona.toLowerCase() === persona.toLowerCase()) if (binding) return getAvatarFromNextIDStorage(platform, userId, avatarId, binding.persona) } for (const binding of bindings.sort((a, b) => sortPersonaBindings(a, b, userId))) { diff --git a/packages/web3-providers/src/AvatarStore/helpers/getPersonaAvatar.ts b/packages/web3-providers/src/AvatarStore/helpers/getPersonaAvatar.ts index 4ddfce843043..1564e3740f3d 100644 --- a/packages/web3-providers/src/AvatarStore/helpers/getPersonaAvatar.ts +++ b/packages/web3-providers/src/AvatarStore/helpers/getPersonaAvatar.ts @@ -9,7 +9,7 @@ export async function getPersonaAvatar( siteType: EnhanceableSite, userId: string, avatarId: string, - persona: string, + persona?: string, ): Promise | null> { // only twitter is supported if (siteType !== EnhanceableSite.Twitter) return null diff --git a/packages/web3-providers/src/AvatarStore/helpers/getAvatarFromStorage.ts b/packages/web3-providers/src/AvatarStore/helpers/storage.ts similarity index 63% rename from packages/web3-providers/src/AvatarStore/helpers/getAvatarFromStorage.ts rename to packages/web3-providers/src/AvatarStore/helpers/storage.ts index 55d2569669f5..6c6c9f70dbd6 100644 --- a/packages/web3-providers/src/AvatarStore/helpers/getAvatarFromStorage.ts +++ b/packages/web3-providers/src/AvatarStore/helpers/storage.ts @@ -8,3 +8,12 @@ export function getAvatarFromStorage(userId: string, address: string) { const stringStorage = Web3Storage.createFireflyStorage(KEY, address) return stringStorage.get?.>(userId) } + +export function setAvatarToStorage( + userId: string, + address: string, + avatar: AvatarNextID, +) { + const stringStorage = Web3Storage.createFireflyStorage(KEY, address) + return stringStorage.set?.>(userId, avatar) +} diff --git a/packages/web3-providers/src/AvatarStore/index.ts b/packages/web3-providers/src/AvatarStore/index.ts index 69f40096e2ea..5b3c5d587bb3 100644 --- a/packages/web3-providers/src/AvatarStore/index.ts +++ b/packages/web3-providers/src/AvatarStore/index.ts @@ -47,7 +47,7 @@ class AvatarStoreProvider extends StoreProvider implements StoreAPI.Provi if (!address) return null const avatar = - siteType === EnhanceableSite.Twitter && avatarId && publicKey ? + siteType === EnhanceableSite.Twitter && avatarId ? await getPersonaAvatar(siteType, userId, avatarId, publicKey) : await getAvatar(siteType, userId) if (!avatar) return null @@ -75,3 +75,4 @@ class AvatarStoreProvider extends StoreProvider implements StoreAPI.Provi } export const AvatarStore = new AvatarStoreProvider() +export { getAvatarFromStorage, setAvatarToStorage } from './helpers/storage.js' diff --git a/packages/web3-providers/src/Storage/apis/Storage.ts b/packages/web3-providers/src/Storage/apis/Storage.ts index 842682dabc01..65d0cdf54f20 100644 --- a/packages/web3-providers/src/Storage/apis/Storage.ts +++ b/packages/web3-providers/src/Storage/apis/Storage.ts @@ -11,7 +11,7 @@ export class Web3Storage { } static createFireflyStorage(namespace: string, address: string) { - return new FireflyStorage.FireflyStorage(namespace, address || '') + return new FireflyStorage.FireflyStorage(namespace, address) } static createRSS3Storage(namespace: string) { diff --git a/packages/web3-providers/src/Twitter/apis/getUserByScreenName.ts b/packages/web3-providers/src/Twitter/apis/getUserByScreenName.ts index 6cc7d26f469c..e09db52333e2 100644 --- a/packages/web3-providers/src/Twitter/apis/getUserByScreenName.ts +++ b/packages/web3-providers/src/Twitter/apis/getUserByScreenName.ts @@ -1,9 +1,8 @@ import urlcat from 'urlcat' -import { getHeaders } from './getTokens.js' -import { fetchGlobal } from '../../helpers/fetchGlobal.js' -import { Expiration } from '../../helpers/fetchSquashed.js' -import { Duration, staleCached } from '../../helpers/fetchCached.js' import type { TwitterBaseAPI } from '../../entry-types.js' +import { staleCached } from '../../helpers/fetchCached.js' +import { fetchGlobal } from '../../helpers/fetchGlobal.js' +import { getHeaders } from './getTokens.js' const features = { responsive_web_twitter_blue_verified_badge_is_enabled: true, @@ -59,10 +58,7 @@ export async function getUserByScreenName(screenName: string): Promise { const request = await createRequest(screenName) - const response = await fetchCachedJSON(request, undefined, { - cacheDuration: Duration.ONE_DAY, - squashExpiration: Expiration.ONE_SECOND, - }) + const response = await fetchJSON(request, undefined) return createUser(response) } diff --git a/packages/web3-providers/src/Twitter/apis/getUserViaTwitterIdentity.ts b/packages/web3-providers/src/Twitter/apis/getUserViaTwitterIdentity.ts index 3bd70007706b..27599724193e 100644 --- a/packages/web3-providers/src/Twitter/apis/getUserViaTwitterIdentity.ts +++ b/packages/web3-providers/src/Twitter/apis/getUserViaTwitterIdentity.ts @@ -1,8 +1,7 @@ import urlcat from 'urlcat' -import { fetchCachedJSON } from '../../helpers/fetchJSON.js' -import { Duration, staleCached } from '../../helpers/fetchCached.js' -import { Expiration } from '../../helpers/fetchSquashed.js' import type { TwitterBaseAPI } from '../../entry-types.js' +import { staleCached } from '../../helpers/fetchCached.js' +import { fetchJSON } from '../../helpers/fetchJSON.js' const TWITTER_IDENTITY_URL = 'https://mr8asf7i4h.execute-api.us-east-1.amazonaws.com/prod/twitter-identity' @@ -20,15 +19,11 @@ function createUser(response: TwitterBaseAPI.IdentifyResponse): TwitterBaseAPI.U } } export async function getUserViaTwitterIdentity(screenName: string): Promise { - const identity = await fetchCachedJSON( + const identity = await fetchJSON( urlcat(TWITTER_IDENTITY_URL, { screenName, }), undefined, - { - cacheDuration: Duration.ONE_DAY, - squashExpiration: Expiration.ONE_SECOND, - }, ) return createUser(identity) } diff --git a/packages/web3-providers/src/entry.ts b/packages/web3-providers/src/entry.ts index f2763df6067d..7d32de69813d 100644 --- a/packages/web3-providers/src/entry.ts +++ b/packages/web3-providers/src/entry.ts @@ -116,4 +116,4 @@ export { Calendar } from './Calendar/index.js' export { NFTSpam } from './NFTSpam/index.js' // Stores -export { AvatarStore } from './AvatarStore/index.js' +export { AvatarStore, getAvatarFromStorage, setAvatarToStorage } from './AvatarStore/index.js'