Skip to content

Commit

Permalink
fix: reduce twitter image requests (#10867)
Browse files Browse the repository at this point in the history
* fix: reduce twitter image requests

* fix: storage type

* fix: cspell
  • Loading branch information
guanbinrui committed Sep 26, 2023
1 parent 17f75b8 commit a07c140
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
6 changes: 2 additions & 4 deletions packages/mask/src/site-adaptors/twitter.com/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ export const hostLeadingUrlAutoTwitter = (isMobile: boolean) =>
// https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/entities-object#photo_format
export const canonifyImgUrl = (url: string) => {
const parsed = new URL(url)
if (parsed.hostname !== 'pbs.twimg.com') {
return url
}
if (parsed.hostname !== 'pbs.twimg.com') return url
const { searchParams } = parsed
searchParams.set('name', 'orig')
// we can't understand original image format when given url labeled as webp
Expand All @@ -24,7 +22,7 @@ export const canonifyImgUrl = (url: string) => {
const pngURL = parsed.href
searchParams.set('format', 'jpg')
const jpgURL = parsed.href
return [pngURL, jpgURL]
return [jpgURL, pngURL]
}
return parsed.href
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mask/src/utils/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function canAccessAsContent(url: string) {
const target = new URL(url, location.href)
if (
location.origin.endsWith('twitter.com') &&
['https://abs.twimg.com', 'https://upload.twitter.com', 'https://api.twitter.com'].includes(target.origin)
(target.origin.endsWith('twitter.com') || target.origin.endsWith('twimg.com'))
)
return true

Expand Down
12 changes: 5 additions & 7 deletions packages/shared/src/hooks/usePersonaContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useAsync } from 'react-use'
import compareDesc from 'date-fns/compareDesc'
import isBefore from 'date-fns/isBefore'
import { unionWith, uniqBy } from 'lodash-es'
import { createContainer } from 'unstated-next'
import {
Expand All @@ -16,9 +19,6 @@ import {
} from '@masknet/shared-base'
import { useValueRef } from '@masknet/shared-base-ui'
import { usePersonaProofs } from './usePersonaProofs.js'
import compareDesc from 'date-fns/compareDesc'
import isBefore from 'date-fns/isBefore'
import { useAsync } from 'react-use'
import { Web3Storage } from '@masknet/web3-providers'
import { PERSONA_AVATAR_DB_NAMESPACE } from '../constants.js'
import type { PersonaAvatarData } from '../types.js'
Expand Down Expand Up @@ -66,11 +66,9 @@ function usePersonaContext(initialState?: {
const lastUpdateTime = await initialState.queryPersonaAvatarLastUpdateTime(currentPersona.identifier)
const storage = Web3Storage.createKVStorage(PERSONA_AVATAR_DB_NAMESPACE)
try {
const remote: PersonaAvatarData = await storage.get<PersonaAvatarData>(
currentPersona.identifier.rawPublicKey,
)
const remote = await storage.get<PersonaAvatarData>(currentPersona.identifier.rawPublicKey)

if (lastUpdateTime && isBefore(lastUpdateTime, remote.updateAt)) {
if (remote && lastUpdateTime && isBefore(lastUpdateTime, remote.updateAt)) {
return remote.imageUrl
}
return currentPersona.avatar
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-providers/src/Storage/storages/KV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export class KVStorage implements StorageAPI.Storage {
async get<T>(key: string) {
const cacheKey = `${this.namespace}_${key}`
const cache = this.cache?.get(cacheKey)

return cache ?? this.getKV<T>().get(key)
const storage = cache ?? this.getKV<T>().get(key)
return storage as T | undefined
}

async set<T>(key: string, value: T) {
Expand Down

0 comments on commit a07c140

Please sign in to comment.