Skip to content

Commit

Permalink
fix: open graph for worlds (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
braianj authored Nov 28, 2023
1 parent 2c81874 commit 08be619
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/entities/Social/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import copies from "../../intl/en.json"
import toCanonicalPosition from "../../utils/position/toCanonicalPosition"
import PlaceModel from "../Place/model"
import { AggregatePlaceAttributes, PlaceListOrderBy } from "../Place/types"
import { placeUrl, siteUrl } from "../Place/utils"
import { placeUrl, siteUrl, worldUrl } from "../Place/utils"

export default routes((router) => {
router.get("/place/", handleRaw(injectPlaceMetadata, "html"))
router.get("/world/", handleRaw(injectWorldMetadata, "html"))
})

async function readFile(req: Request) {
Expand Down Expand Up @@ -76,3 +77,48 @@ export async function injectPlaceMetadata(req: Request, res: Response) {
url,
})
}

export async function injectWorldMetadata(req: Request, res: Response) {
const worldName = String(req.query.name || "")
const id = String(req.query.id || "")
const page = await readFile(req)

let place: AggregatePlaceAttributes | null = null
if (id && isUUID(id)) {
place = await PlaceModel.findByIdWithAggregates(id, {
user: undefined,
})
} else if (worldName) {
place = (
await PlaceModel.findWorld({
names: [worldName],
offset: 0,
limit: 1,
only_favorites: false,
order_by: PlaceListOrderBy.LIKE_SCORE_BEST,
order: "asc",
search: "",
})
)[0]
}

if (place) {
const url = worldUrl(place)
res.set("link", `<${url.toString()}>; rel=canonical`)

return replaceHelmetMetadata(page.toString(), {
...(copies.social.place as any),
title: escape(place.title || "") + " | Decentraland Place",
description: escape((place.description || "").trim()),
image: place.image || "",
url: placeUrl(place),
"twitter:card": "summary_large_image",
})
}

const url = siteUrl().toString() + req.originalUrl.slice(1)
return replaceHelmetMetadata(page.toString(), {
...(copies.social.place as any),
url,
})
}

0 comments on commit 08be619

Please sign in to comment.