Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/injected-script/main/sceneChange/twitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function setupWatcherForTwitter() {
const update = () => {
const newFirstSlug = getFirstSlug()
// reset to void wrong value
if (!firstSlug || apply(includes, TWITTER_RESERVED_SLUGS, [firstSlug])) {
if (!newFirstSlug || apply(includes, TWITTER_RESERVED_SLUGS, [newFirstSlug])) {
const event = new no_xray_CustomEvent('scenechange', {
detail: { scene: 'unknown' },
}) as WindowEventMap['scenechange']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ function resolveCurrentVisitingIdentityInner(
) {
const update = async (twitterId: string) => {
const user = await Twitter.getUserByScreenName(twitterId)
if (!user) return
const bio = user.legacy.description

const nickname = user.legacy.name
Expand Down
8 changes: 8 additions & 0 deletions packages/mask/src/utils/hooks/useSceneChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useEffect } from 'react'

export function useSceneChange(handler: (event: WindowEventMap['scenechange']) => void) {
useEffect(() => {
window.addEventListener('scenechange', handler)
return () => window.removeEventListener('scenechange', handler)
}, [handler])
}
37 changes: 26 additions & 11 deletions packages/web3-providers/src/twitter/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { escapeRegExp } from 'lodash-unified'
import urlcat from 'urlcat'
import LRUCache from 'lru-cache'
import type { TwitterBaseAPI } from '../types'

const UPLOAD_AVATAR_URL = 'https://upload.twitter.com/i/media/upload.json'
Expand Down Expand Up @@ -117,6 +118,11 @@ async function getSettings(bearerToken: string, csrfToken: string): Promise<Twit
return response.json()
}

const cache = new LRUCache<string, any>({
max: 20,
ttl: 300_000,
})

export class TwitterAPI implements TwitterBaseAPI.Provider {
async getSettings() {
const { bearerToken, queryToken, csrfToken } = await getTokens()
Expand Down Expand Up @@ -197,7 +203,7 @@ export class TwitterAPI implements TwitterBaseAPI.Provider {
}
}

async getUserByScreenName(screenName: string): Promise<TwitterBaseAPI.User> {
async getUserByScreenName(screenName: string): Promise<TwitterBaseAPI.User | null> {
const { bearerToken, csrfToken, queryId } = await getTokens('UserByScreenName')
const url = urlcat('https://twitter.com/i/api/graphql/:queryId/UserByScreenName', {
queryId,
Expand All @@ -207,17 +213,26 @@ export class TwitterAPI implements TwitterBaseAPI.Provider {
withSuperFollowsUserFields: true,
}),
})
const cacheKey = `${bearerToken}/${csrfToken}/${url}`
const fetchingTask: Promise<Response> =
cache.get(cacheKey) ??
fetch(url, {
headers: {
authorization: `Bearer ${bearerToken}`,
'x-csrf-token': csrfToken,
'content-type': 'application/json',
'x-twitter-auth-type': 'OAuth2Session',
'x-twitter-active-user': 'yes',
referer: `https://twitter.com/${screenName}`,
},
})

const response = await fetch(url, {
headers: {
authorization: `Bearer ${bearerToken}`,
'x-csrf-token': csrfToken,
'content-type': 'application/json',
'x-twitter-auth-type': 'OAuth2Session',
'x-twitter-active-user': 'yes',
referer: `https://twitter.com/${screenName}`,
},
})
cache.set(cacheKey, fetchingTask)
const response = (await fetchingTask).clone()
if (!response.ok) {
cache.delete(cacheKey)
return null
}
const userResponse: TwitterBaseAPI.UserByScreenNameResponse = await response.json()
return userResponse.data.user.result
}
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-providers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ export namespace TwitterBaseAPI {
>
uploadUserAvatar: (screenName: string, image: Blob | File) => Promise<TwitterResult>
updateProfileImage: (screenName: string, media_id_str: string) => Promise<AvatarInfo | undefined>
getUserByScreenName: (screenName: string) => Promise<User>
getUserByScreenName: (screenName: string) => Promise<User | null>
}
}

Expand Down