Skip to content

Commit

Permalink
refactor: add a new route to handle Farcaster result
Browse files Browse the repository at this point in the history
  • Loading branch information
nekofar committed Jan 30, 2024
1 parent 9bc1500 commit 121dae1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
Response::redirect(Url::parse("https://lilnouns.wtf")?)
})
.get_async("/:sqid", routes::handle_redirect)
.post_async("/:sqid", routes::handle_fs_result)
.get_async("/:sqid/og.png", routes::handle_og_image)
.post_async("/", routes::handle_creation)
.run(req, env)
Expand Down
87 changes: 87 additions & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,90 @@ pub async fn handle_creation<D>(

Response::error("Bad Request", 400)
}

pub async fn handle_fs_result<D>(req: Request, ctx: RouteContext<D>) -> worker::Result<Response> {
if let Some(sqid) = ctx.param("sqid") {
let sqids = Sqids::default();
let numbers = sqids.decode(&sqid);

let community = match numbers[0] {
1 => Some(LilNouns),
_ => None,
};

let platform = match numbers[1] {
1 => Some(Ethereum),
2 => Some(PropLot),
3 => Some(MetaGov),
_ => None,
};

let (url, title, description, image) = match (community, platform) {

Check warning on line 299 in src/routes.rs

View workflow job for this annotation

GitHub Actions / Build & Test

unused variable: `url`

Check warning on line 299 in src/routes.rs

View workflow job for this annotation

GitHub Actions / Build & Test

unused variable: `title`

Check warning on line 299 in src/routes.rs

View workflow job for this annotation

GitHub Actions / Build & Test

unused variable: `description`

Check warning on line 299 in src/routes.rs

View workflow job for this annotation

GitHub Actions / Build & Test

unused variable: `url`

Check warning on line 299 in src/routes.rs

View workflow job for this annotation

GitHub Actions / Build & Test

unused variable: `title`

Check warning on line 299 in src/routes.rs

View workflow job for this annotation

GitHub Actions / Build & Test

unused variable: `description`
(Some(LilNouns), Some(Ethereum)) => {
let url = format!("{}/{}", "https://lilnouns.wtf/vote", numbers[2]);
let (title, description) = fetch_lil_nouns_data(&ctx.env, numbers[2]).await?;
let image = req
.url()
.unwrap()
.join(format!("{}/og.png", sqid).as_str())
.unwrap()
.to_string();
(url, title, description, image)
}
(Some(LilNouns), Some(PropLot)) => {
let url = format!("{}/{}", "https://lilnouns.proplot.wtf/idea", numbers[2]);
let (title, description) = fetch_prop_lot_data(&ctx.env, numbers[2]).await?;
let image = req
.url()
.unwrap()
.join(format!("{}/og.png", sqid).as_str())
.unwrap()
.to_string();
(url, title, description, image)
}
(Some(LilNouns), Some(MetaGov)) => {
let url = format!("{}/{}", "https://lilnouns.wtf/vote/nounsdao", numbers[2]);
let (title, description) = fetch_meta_gov_data(&ctx.env, numbers[2]).await?;
let image = req
.url()
.unwrap()
.join(format!("{}/og.png", sqid).as_str())
.unwrap()
.to_string();
(url, title, description, image)
}
_ => (String::new(), String::new(), String::new(), String::new()),
};

let html_doc = format!(
r#"
<!DOCTYPE html>
<html lang="en">
<head>
<meta property="fc:frame" content="vNext" />
<meta property="fc:frame:image" content="{}" />
<meta property="fc:frame:button:1" content="{}" />
<meta property="fc:frame:post_url" content="{}" />
</head>
</html>
"#,
image, // Farcaster Image
"Show Result", // Farcaster Button #1
req.url().unwrap().as_str(), // Farcaster Post URL
);

let minified_html = minify(html_doc).expect("Failed to minify HTML");

let response = Response::from_body(ResponseBody::Body(minified_html.as_bytes().to_vec()));

return match response {
Ok(mut res) => {
res.headers_mut().set("Content-Type", "text/html").unwrap();
return Ok(res);
}
Err(e) => Err(e),
};
}

Response::error("Bad Request", 400)
}

0 comments on commit 121dae1

Please sign in to comment.