-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
329 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { getMessage } from "@/lib/messages"; | ||
import { getUserAddresses } from "@/lib/farcaster"; | ||
import { meetsRequirement } from "@/lib/unlock"; | ||
import { getImage } from "@/lib/utils"; | ||
|
||
interface Button { | ||
label: string; | ||
action: string; | ||
} | ||
|
||
export const renderMessageForFid = async ( | ||
origin: string, | ||
messageId: string, | ||
fid: string | null | ||
) => { | ||
const message = await getMessage(messageId); | ||
if (!message) { | ||
return new Response("Message not found", { status: 404 }); | ||
} | ||
|
||
if (!fid) { | ||
return render(origin, message, "pending", `${origin}/api/${message.id}/`, [ | ||
{ | ||
label: "Reveal 🔓", | ||
action: "post", | ||
}, | ||
]); | ||
} | ||
|
||
const addresses = await getUserAddresses(fid); | ||
if (addresses.length === 0) { | ||
return render(origin, message, "no-wallet"); | ||
} | ||
|
||
const isMember = ( | ||
await Promise.all( | ||
addresses.map((userAddress: string) => { | ||
return meetsRequirement( | ||
userAddress as `0x${string}`, | ||
message.frame.gate | ||
); | ||
}) | ||
) | ||
).some((balance) => !!balance); | ||
|
||
if (isMember) { | ||
// No action (yet!) | ||
return render(origin, message, "clear"); | ||
} else if (message.frame.checkoutUrl) { | ||
// Show the checkout button | ||
return render( | ||
origin, | ||
message, | ||
"hidden", | ||
`${origin}/api/${message.id}/checkout`, | ||
[ | ||
{ | ||
label: "Get the tokens!", | ||
action: "post_redirect", | ||
}, | ||
] | ||
); | ||
} else { | ||
// No checkout button! | ||
return render(origin, message, "hidden"); | ||
} | ||
}; | ||
|
||
export const render = async ( | ||
base: string, | ||
message: { id: string; author: string; frame: any }, | ||
status: "pending" | "hidden" | "clear" | "no-wallet", | ||
postUrl?: string, | ||
buttons?: Button[] | ||
) => { | ||
const image = getImage(base, message, status); | ||
return new Response( | ||
`<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta name="description" content="${message.frame.description}"> | ||
<meta property="og:description" content="${message.frame.description}"> | ||
<meta property="og:url" content="${base}/c/${message.id}"> | ||
<meta property="og:image" content="${base}/api/og/${message.id}"> | ||
<meta property="og:type" content="website"> | ||
<meta name="twitter:card" content="summary_large_image"> | ||
<meta name="twitter:description" content="${message.frame.description}"> | ||
<meta name="twitter:image" content="${base}/api/og/${message.id}"> | ||
<meta property="fc:frame" content="vNext" /> | ||
<meta property="fc:frame:image" content="${image}" /> | ||
${ | ||
postUrl | ||
? `<meta property="fc:frame:post_url" content="${postUrl}" />` | ||
: `` | ||
} | ||
${(buttons || []) | ||
.map((button, i) => { | ||
return `<meta property="fc:frame:button:${i + 1}" content="${ | ||
button.label | ||
}" /> | ||
<meta property="fc:frame:button:${i + 1}:action" content="${ | ||
button.action | ||
}" />`; | ||
}) | ||
.join("\n")} | ||
</head> | ||
<body> | ||
<img src="${image}" /> | ||
<p> | ||
${(buttons || []) | ||
.map((button) => { | ||
return `<button>${button.label}</button>`; | ||
}) | ||
.join("<br />")} | ||
</p> | ||
</body> | ||
</html>`, | ||
{ | ||
headers: { | ||
"Content-Type": "text/html", | ||
}, | ||
status: 200, | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,34 @@ | ||
import { getMessage } from "@/lib/messages"; | ||
import { getUserAddresses, validateMessage } from "@/lib/farcaster"; | ||
import { balanceOf } from "@/lib/unlock"; | ||
import { getImage } from "@/lib/utils"; | ||
import { AppConfig } from "@/app/AppConfig"; | ||
import { validateMessage } from "@/lib/farcaster"; | ||
import { renderMessageForFid } from "./render"; | ||
|
||
export async function POST( | ||
request: Request, | ||
{ params }: { params: { message: string } } | ||
) { | ||
const message = await getMessage(params.message); | ||
if (!message) { | ||
return new Response("Message not found", { status: 404 }); | ||
} | ||
const u = new URL(request.url); | ||
const body = await request.json(); | ||
const { trustedData } = body; | ||
|
||
if (!trustedData) { | ||
return new Response("Missing trustedData", { status: 441 }); | ||
} | ||
const fcMessage = await validateMessage(trustedData.messageBytes); | ||
if (!fcMessage.valid) { | ||
if (!fcMessage.valid || !fcMessage.message.data.fid) { | ||
return new Response("Invalid message", { status: 442 }); | ||
} | ||
|
||
const addresses = await getUserAddresses(fcMessage.message.data.fid); | ||
if (addresses.length === 0) { | ||
return new Response( | ||
`<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta property="fc:frame" content="vNext" /> | ||
<meta property="fc:frame:image" content="${new URL( | ||
`${AppConfig.siteUrl}/api/og/no-wallet` | ||
).toString()}" /> | ||
</head> | ||
</html>` | ||
); | ||
} | ||
|
||
const balances = await Promise.all( | ||
addresses.map((userAddress: string) => { | ||
return balanceOf( | ||
userAddress as `0x${string}`, | ||
message.frame.gate.contract as `0x${string}`, | ||
message.frame.gate.network | ||
); | ||
}) | ||
return renderMessageForFid( | ||
u.origin, | ||
params.message, | ||
fcMessage.message.data.fid | ||
); | ||
} | ||
|
||
const isMember = balances.some((balance) => balance > 0); | ||
|
||
if (isMember) { | ||
return new Response( | ||
`<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta property="fc:frame" content="vNext" /> | ||
<meta property="fc:frame:image" content="${getImage( | ||
message, | ||
"clear" | ||
)}" /> | ||
</head> | ||
</html>` | ||
); | ||
} else if (message.frame.checkoutUrl) { | ||
return new Response( | ||
`<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta property="fc:frame" content="vNext" /> | ||
<meta property="fc:frame:image" content="${getImage( | ||
message, | ||
"hidden" | ||
)}" /> | ||
<meta property="fc:frame:button:1" content="Get Membership NFT!" /> | ||
<meta property="fc:frame:button:1:action" content="post_redirect" /> | ||
<meta property="fc:frame:post_url" content="${ | ||
AppConfig.siteUrl | ||
}/api/${message.id}/checkout" /> | ||
</head> | ||
</html>`, | ||
{ | ||
status: 200, | ||
} | ||
); | ||
} else { | ||
return new Response( | ||
`<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta property="fc:frame" content="vNext" /> | ||
<meta property="fc:frame:image" content="${getImage( | ||
message, | ||
"hidden" | ||
)}" /> | ||
</head> | ||
</html>`, | ||
{ | ||
status: 200, | ||
} | ||
); | ||
} | ||
// REMOVE ME FOR PROD! | ||
export async function GET( | ||
request: Request, | ||
{ params }: { params: { message: string } } | ||
) { | ||
const u = new URL(request.url); | ||
const fid = u.searchParams.get("fid"); | ||
return renderMessageForFid(u.origin, params.message, fid); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { renderMessageForFid } from "@/app/api/[message]/render"; | ||
|
||
export async function GET( | ||
request: Request, | ||
{ params }: { params: { message: string } } | ||
) { | ||
const u = new URL(request.url); | ||
const fid = u.searchParams.get("fid"); | ||
return renderMessageForFid(u.origin, params.message, fid); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.