Skip to content

Commit

Permalink
Merge pull request #587 from dappforce/deploy/revalidate
Browse files Browse the repository at this point in the history
Add revalidation api
  • Loading branch information
olehmell authored Mar 12, 2024
2 parents 0961395 + 4cb43d8 commit e9ae083
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/components/auth/LoginModal/LoginModalContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,11 @@ export function EvmLoginStep({
}}
onSuccess={() => {
useMyAccount.getState().finalizeTemporaryAccount()
sendEvent('account_created', { loginBy: 'evm' })
sendEvent(
'account_created',
{ loginBy: 'evm' },
{ ref: getReferralIdInUrl() }
)
closeModal()
useLoginModal.getState().openNextStepModal({ step: 'create-profile' })
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ export default function useOauthLogin({
;(async () => {
const address = await loginAsTemporaryAccount(null)
if (!address || !identity) return
sendEvent('account_created', { loginBy: provider })
sendEvent(
'account_created',
{ loginBy: provider },
{ ref: getReferralIdInUrl() }
)
setReferrerId({ refId: getReferralIdInUrl() })
linkIdentity({
id: session.user?.id,
Expand Down
15 changes: 12 additions & 3 deletions src/pages/api/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export async function getPostsServer(postIds: string[]): Promise<PostData[]> {

const metadataMap: Record<string, LinkMetadata> = {}
const metadataPromises = Array.from(linksToFetch).map(async (link) => {
// link fetching itself has timeout of 20_000, but for data fetching, it faster timeout
// if it needs more than 8s, the post fetching is not delayed, but it will still be fetched and put to redis
// so that in the next fetch, it will have the correct data from the redis
setTimeout(() => {
throw new Error('Link metadata fetching timeout')
}, 8_000)
const metadata = await getLinkMetadata(link)
if (metadata) metadataMap[link] = metadata
})
Expand All @@ -121,16 +127,19 @@ export async function getPostsServer(postIds: string[]): Promise<PostData[]> {
const getMetadataRedisKey = (url: string) => 'metadata:' + url
const METADATA_MAX_AGE = 60 * 60 * 24 * 30 // 1 month
const METADATA_ERROR_MAX_AGE = 60 * 60 * 1 // 1 hour
async function getLinkMetadata(link: string): Promise<LinkMetadata | null> {
export async function getLinkMetadata(
link: string,
revalidate?: boolean
): Promise<LinkMetadata | null> {
const cachedData = await redisCallWrapper((redis) =>
redis?.get(getMetadataRedisKey(link))
)
if (cachedData) {
if (cachedData && !revalidate) {
return JSON.parse(cachedData) as LinkMetadata
}

try {
const metadata = await parser(link, { timeout: 5_000 })
const metadata = await parser(link, { timeout: 20_000 })
const allMetadata = JSON.parse(
JSON.stringify({
...metadata.meta,
Expand Down
29 changes: 29 additions & 0 deletions src/pages/api/revalidation/link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { handlerWrapper } from '@/server/common'
import { z } from 'zod'
import { getLinkMetadata } from '../posts'

const bodySchema = z.object({
link: z.string(),
})
export type RevalidateLinkInput = z.infer<typeof bodySchema>

const handler = handlerWrapper({
inputSchema: bodySchema,
dataGetter: (req) => req.body,
})<{ data: Awaited<ReturnType<typeof getLinkMetadata>> }>({
allowedMethods: ['POST'],
handler: async (data, _, res) => {
try {
const metadata = await getLinkMetadata(data.link, true)
res.json({ success: true, message: 'revalidated', data: metadata })
} catch (err) {
return res.status(500).send({
message: (err as any).message || 'Error revalidating link',
success: false,
data: null,
})
}
},
errorLabel: 'Error revalidating link',
})
export default handler
7 changes: 5 additions & 2 deletions src/stores/my-account.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getReferralIdInUrl } from '@/components/referral/ReferralUrlChanger'
import Toast from '@/components/Toast'
import { ESTIMATED_ENERGY_FOR_ONE_TX } from '@/constants/subsocial'
import { IdentityProvider } from '@/services/datahub/generated-query'
Expand Down Expand Up @@ -113,7 +114,7 @@ const sendLaunchEvent = async (
const sendEvent = useAnalytics.getState().sendEvent

if (!address) {
sendEvent('app_launched')
sendEvent('app_launched', undefined, { ref: getReferralIdInUrl() })
} else {
const [
// linkedTgAccData,
Expand Down Expand Up @@ -141,7 +142,8 @@ const sendLaunchEvent = async (
if (linkedIdentity.status === 'fulfilled')
userProperties.twitterLinked =
linkedIdentity.value?.provider === IdentityProvider.Twitter
if (referrerId.status === 'fulfilled') userProperties.ref = referrerId.value
if (referrerId.status === 'fulfilled')
userProperties.ref = referrerId.value || getReferralIdInUrl()

userProperties.webNotifsEnabled = isWebNotificationsEnabled()

Expand Down Expand Up @@ -206,6 +208,7 @@ const useMyAccountBase = create<State & Actions>()((set, get) => ({
{
cameFrom: parentOrigin,
cohortDate: dayjs().toDate(),
ref: getReferralIdInUrl(),
}
)
} else if (secretKey.startsWith('0x')) {
Expand Down

0 comments on commit e9ae083

Please sign in to comment.