From 08be6192b96f54355c69f839e38234cd2f9fb8ab Mon Sep 17 00:00:00 2001 From: Braian Mellor Date: Tue, 28 Nov 2023 12:36:14 -0300 Subject: [PATCH] fix: open graph for worlds (#411) --- src/entities/Social/routes.ts | 48 ++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/entities/Social/routes.ts b/src/entities/Social/routes.ts index 16a69575..06d21a0c 100644 --- a/src/entities/Social/routes.ts +++ b/src/entities/Social/routes.ts @@ -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) { @@ -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, + }) +}