Skip to content

Commit

Permalink
Add opengraph
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyLeite committed Jul 11, 2024
1 parent fb29d3b commit df98d57
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
32 changes: 27 additions & 5 deletions netlify/functions/steamjoin.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Context } from "@netlify/functions";
import xml2js from "xml2js";

export default async (req: Request, context: Context) => {
const { user_id } = context.params;
Expand All @@ -8,24 +9,45 @@ export default async (req: Request, context: Context) => {

const steam_player = steam_json.response.players[0];
if (!steam_player) {
let response = new Response("Player not found", { status: 404 });
const response = new Response("Player not found", { status: 404 });
return response;
}

const game_id = steam_player.gameid;
if (!game_id) {
let response = new Response("Player is not currently in a game", { status: 400 });
const response = new Response("Player is not currently in a game", { status: 400 });
return response;
}

const lobby_id = steam_player.lobbysteamid;
if (!lobby_id) {
let response = new Response("Player is not currently in a lobby", { status: 400 });
const response = new Response("Player is not currently in a lobby", { status: 400 });
return response;
}

let response = new Response("Moved", { status: 301 });
response.headers.set("Location", `steam://joinlobby/${game_id}/${lobby_id}/${user_id}`);
const location = `steam://joinlobby/${game_id}/${lobby_id}/${user_id}`;

const profile_response = await fetch(`${steam_player.profileurl}?xml=true`);
const profile_text = await profile_response.text();
const parser = new xml2js.Parser();
const doc = await parser.parseStringPromise(profile_text);

//console.log(profile_text);
console.log(doc.profile);

const htmlContent = `
<html prefix="og: https://ogp.me/ns#">
<head>
<meta property="og:title" content="${doc.profile.steamID}" />
<meta property="og:type" content="website" />
<meta property="og:url" content="${location}" />
<meta property="og:image" content="${doc.profile.inGameInfo[0].gameIcon[0]}" />
<meta property="og:description" content="I'm in a lobby for \`${doc.profile.inGameInfo[0].gameName[0]}\`! Join me by clicking the link" />
</head>
</html>`;

const response = new Response(htmlContent, { status: 301 });
response.headers.set("Location", location);

return response;
}
Expand Down
28 changes: 27 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"@netlify/functions": "^2.8.1",
"netlify-cli": "^17.33.0"
"netlify-cli": "^17.33.0",
"xml2js": "^0.6.2"
}
}

0 comments on commit df98d57

Please sign in to comment.