From f1db842fc0796cf819666b78727012e6b65148f0 Mon Sep 17 00:00:00 2001
From: Jeffrey Phillips Freeman <the@jeffreyfreeman.me>
Date: Mon, 9 Oct 2023 20:15:05 -0400
Subject: [PATCH] Converted all api calls to use variables

---
 README.md                   |  3 +++
 api/activitypub/actor.ts    | 24 ++++++++++++------------
 api/activitypub/inbox.ts    |  5 ++---
 api/activitypub/sendNote.ts |  2 +-
 api/card.ts                 |  9 +++++----
 api/mentions.js             |  4 ++--
 api/nodeinfo/2.1.ts         |  4 ++--
 api/well-known/nodeinfo.ts  |  2 +-
 api/well-known/webfinger.ts | 15 +++++++++++----
 9 files changed, 39 insertions(+), 29 deletions(-)

diff --git a/README.md b/README.md
index 1ee3239ed..8a4de89c8 100644
--- a/README.md
+++ b/README.md
@@ -40,6 +40,8 @@ This project (excluding post content itself) is released under the Apache Licens
 ** NEXT_PUBLIC_FIREBASE_PROJECT_ID
 ** ACTIVITYPUB_URL
 ** ACTIVITYPUB_USER
+** ACTIVITYPUB_URL_ALIAS
+** ACTIVITYPUB_USER_ALIAS
 ** ACTIVITYPUB_NAME
 ** ACTIVITYPUB_SUMMARY
 * Modify /layouts/partials/top_list_* to represent the section titles you want to use.
@@ -56,3 +58,4 @@ This project (excluding post content itself) is released under the Apache Licens
 * Go through the /api and manually change references (this step will either be explained later or not needed soon, just a reminder placeholder)
 
 The above steps might be reduced by using template processing on otherwise unprocessed files (normally not part of hugo). See the following for more info https://vercel.com/docs/functions/serverless-functions/runtimes/node-js
+NOTE: Once we move the project over to a dedicated git, change the node info under api and track the version
diff --git a/api/activitypub/actor.ts b/api/activitypub/actor.ts
index c45f959ec..5270bfd7b 100644
--- a/api/activitypub/actor.ts
+++ b/api/activitypub/actor.ts
@@ -6,7 +6,7 @@ export default function (req: VercelRequest, res: VercelResponse) {
   if ("accept" in headers) {
     const accept = headers["accept"];
     if (accept != null && accept.split(",").indexOf("text/html") > -1) {
-      return res.redirect(302, "https://flear.org/").end();
+      return res.redirect(302,  `${process.env.ACTIVITYPUB_URL}`).end();
     }
   }
 
@@ -15,24 +15,24 @@ export default function (req: VercelRequest, res: VercelResponse) {
   res.json({
     "@context": ["https://www.w3.org/ns/activitystreams", { "@language": "en- GB" }],
     "type": "Person",
-    "id": "https://flear.org/flear",
-    "outbox": "https://flear.org/outbox",
-    "following": "https://flear.org/following",
-    "followers": "https://flear.org/followers",
-    "inbox": "https://flear.org/inbox",
-    "preferredUsername": "flear",
-    "name": "FLEAR (Free & Libre Engineers for Amateur Radio)",
-    "summary": "FLEAR is non-profit promoting open-source and open-standards in Amateur Radio",
+    "id": `${process.env.ACTIVITYPUB_URL}${process.env.ACTIVITYPUB_USER.toLowerCase()}`,
+    "outbox": `${process.env.ACTIVITYPUB_URL}outbox`,
+    "following": `${process.env.ACTIVITYPUB_URL}following`,
+    "followers": `${process.env.ACTIVITYPUB_URL}followers`,
+    "inbox": `${process.env.ACTIVITYPUB_URL}inbox`,
+    "preferredUsername": `${process.env.ACTIVITYPUB_USER.toLowerCase()}`,
+    "name": `${process.env.ACTIVITYPUB_NAME}`,
+    "summary": `${process.env.ACTIVITYPUB_SUMMARY}`,
     "icon": {
       "type": "Image",
       "mediaType": "image/png",
-      "url": "https://flear.org/images/logo.png"
+      "url": `${process.env.ACTIVITYPUB_URL}images/logo.png`
     },
     "publicKey": {
       "@context": "https://w3id.org/security/v1",
       "@type": "Key",
-      "id": "https://flear.org/flear#main-key",
-      "owner": "https://flear.org/flear",
+      "id": `${process.env.ACTIVITYPUB_URL}${process.env.ACTIVITYPUB_USER.toLowerCase()}#main-key`,
+      "owner": `${process.env.ACTIVITYPUB_URL}${process.env.ACTIVITYPUB_USER.toLowerCase()}`,
       "publicKeyPem": process.env.ACTIVITYPUB_PUBLIC_KEY
     }
   });
diff --git a/api/activitypub/inbox.ts b/api/activitypub/inbox.ts
index b73e59431..8465e77b5 100644
--- a/api/activitypub/inbox.ts
+++ b/api/activitypub/inbox.ts
@@ -223,13 +223,12 @@ async function saveFollow(message: AP.Follow, actorInformation: AP.Actor) {
   await followDocRef.set(message);
 
   const guid = uuid();
-  const domain = 'flear.org';
 
   const acceptRequest: AP.Accept = <AP.Accept>{
     "@context": "https://www.w3.org/ns/activitystreams",
-    'id': `https://${domain}/${guid}`,
+    'id': `${process.env.ACTIVITYPUB_URL}${guid}`,
     'type': 'Accept',
-    'actor': "https://flear.org/flear",
+    'actor': `${process.env.ACTIVITYPUB_URL}${process.env.ACTIVITYPUB_USER.toLowerCase()}`,
     'object': message.id
   };
 
diff --git a/api/activitypub/sendNote.ts b/api/activitypub/sendNote.ts
index 66a5423be..03c0091ef 100644
--- a/api/activitypub/sendNote.ts
+++ b/api/activitypub/sendNote.ts
@@ -60,7 +60,7 @@ export default async function (req: VercelRequest, res: VercelResponse) {
   }
 
   // Get my outbox because it contains all my notes.
-  const outboxResponse = await fetch('https://flear.org/outbox');
+  const outboxResponse = await fetch(`${process.env.ACTIVITYPUB_URL}outbox`);
   const outbox = <OrderedCollection>(await outboxResponse.json());
 
   const followersCollection = db.collection('followers');
diff --git a/api/card.ts b/api/card.ts
index 13dcdf0e8..cbd081d64 100644
--- a/api/card.ts
+++ b/api/card.ts
@@ -8,13 +8,14 @@ export default function (req: Request) {
 
   const url = new URL(req.url);
   const title = url.searchParams.get("title");
-  const imgUrl = url.searchParams.get("imgUrl") || "https://flear.org/images/logo.png";
+  const imgUrl = url.searchParams.get("imgUrl") || `${process.env.ACTIVITYPUB_URL}images/logo.png`;
   const width = url.searchParams.get("width") || "800"
   const height = url.searchParams.get("height") || "418";
+  const apDomain = (new URL(`${process.env.ACTIVITYPUB_URL}`)).hostname;
 
-  return new ImageResponse({ "type": "div", "props": { "style": { "display": "flex", "height": "100%", "width": "100%", "alignItems": "center", "justifyContent": "center", "letterSpacing": "-.02em", "fontWeight": 700, "background": "white" }, "children": [{ "type": "div", "props": { "style": { "left": 42, "top": 42, "position": "absolute", "display": "flex", "alignItems": "center" }, "children": [{ "type": "span", "props": { "style": { "width": 24, "height": 24, "background": "black" } } }, { "type": "span", "props": { "style": { "marginLeft": 8, "fontSize": 20 }, "children": "flear.org" } }] } }, { "type": "div", "props": { "style": { "display": "flex", "flexWrap": "wrap", "justifyContent": "center", "padding": "20px 50px", "margin": "0 42px", "fontSize": 40, "width": "auto", "maxWidth": 550, "textAlign": "center", "backgroundColor": "black", "color": "white", "lineHeight": 1.4 }, "children": title } }] } }, 
-  { 
-    width, 
+  return new ImageResponse({ "type": "div", "props": { "style": { "display": "flex", "height": "100%", "width": "100%", "alignItems": "center", "justifyContent": "center", "letterSpacing": "-.02em", "fontWeight": 700, "background": "white" }, "children": [{ "type": "div", "props": { "style": { "left": 42, "top": 42, "position": "absolute", "display": "flex", "alignItems": "center" }, "children": [{ "type": "span", "props": { "style": { "width": 24, "height": 24, "background": "black" } } }, { "type": "span", "props": { "style": { "marginLeft": 8, "fontSize": 20 }, "children": `apDomain`} }] } }, { "type": "div", "props": { "style": { "display": "flex", "flexWrap": "wrap", "justifyContent": "center", "padding": "20px 50px", "margin": "0 42px", "fontSize": 40, "width": "auto", "maxWidth": 550, "textAlign": "center", "backgroundColor": "black", "color": "white", "lineHeight": 1.4 }, "children": title } }] } }, 
+  {
+    width,
     height
   }); // 800px by 418px
 }
diff --git a/api/mentions.js b/api/mentions.js
index f873225f4..1518480e0 100644
--- a/api/mentions.js
+++ b/api/mentions.js
@@ -44,7 +44,7 @@ img.profile.photo {
 </style>
 </head>
 <body>
-<div class="comments webmentions">            
+<div class="comments webmentions">
   <h4>Likes and bookmarks</h4>
   ${data.filter(item => item['wm-property'] === 'like-of' || item['wm-property'] === 'bookmark-of').map(item => html`<a href="${sanitize(item.author.url)}" target="_blank"><img src="${sanitize(item.author.photo)}" alt="${sanitize(item.author.name)}" class="profile photo" loading="lazy"></a>`)}
   <h4>Reposts</h4>
@@ -82,7 +82,7 @@ class FromWhatWGReadableStream extends Readable {
 }
 
 module.exports = async (req, res) => {
-  const { url = 'https://flear.org/', count = 200 } = req.query;
+  const { url = `${process.env.ACTIVITYPUB_URL}following`, count = 200 } = req.query;
   const referer = req.headers.referer;
   const cacheAge = 12 * 60 * 60;
 
diff --git a/api/nodeinfo/2.1.ts b/api/nodeinfo/2.1.ts
index b8e6cdc6f..d1e14bb99 100644
--- a/api/nodeinfo/2.1.ts
+++ b/api/nodeinfo/2.1.ts
@@ -9,7 +9,7 @@ export default function (req: VercelRequest, res: VercelResponse) {
       "name": "flear.org",
       "repository": "https://git.qoto.org/flear/flear-site",
       "homepage": "https://flear.org/",
-        "version": "0.0.1"
+      "version": "0.0.1"
     },
     "protocols": [
       "activitypub"
@@ -27,7 +27,7 @@ export default function (req: VercelRequest, res: VercelResponse) {
       }
     },
     "metadata": {
-      "nodeName": "flear.org"
+      "nodeName": `${process.env.ACTIVITYPUB_NAME}`
     }
   });
 }
diff --git a/api/well-known/nodeinfo.ts b/api/well-known/nodeinfo.ts
index 282b0cb2a..0aa9ab234 100644
--- a/api/well-known/nodeinfo.ts
+++ b/api/well-known/nodeinfo.ts
@@ -8,7 +8,7 @@ export default function (req: VercelRequest, res: VercelResponse) {
     "links": [
       {
         "rel": "http://nodeinfo.diaspora.software/ns/schema/2.1",
-        "href": "https://flear.org/nodeinfo/2.1"
+        "href": `${process.env.ACTIVITYPUB_URL}nodeinfo/2.1`
       }
     ]
   });
diff --git a/api/well-known/webfinger.ts b/api/well-known/webfinger.ts
index a5a75011e..2c2ae2091 100644
--- a/api/well-known/webfinger.ts
+++ b/api/well-known/webfinger.ts
@@ -4,16 +4,23 @@ export default function (req: VercelRequest, res: VercelResponse) {
   const { resource } = req.query;
   res.statusCode = 200;
   res.setHeader("Content-Type", `application/jrd+json`);
-  res.end(`{  
-    "subject": "acct:flear@flear.org",
+  const apDomain = (new URL(`${process.env.ACTIVITYPUB_URL}`)).hostname;
+  let apAlias;
+  if( process.env.ACTIVITYPUB_URL_ALIAS && process.env.ACTIVITYPUB_USER_ALIAS ) {
+    apAlias = `${process.env.ACTIVITYPUB_URL_ALIAS}${process.env.ACTIVITYPUB_USER_ALIAS.toLowerCase()}`;
+  } else {
+    apAlias = ""
+  }
+  res.end(`{
+    "subject": `acct:${process.env.ACTIVITYPUB_USER.toLowerCase()}@${apDomain}`,
     "aliases": [
-      "https://qoto.org/@flear"
+      `${apAlias}`
     ],
     "links": [
       {
         "rel": "self",
         "type": "application/activity+json",
-        "href": "https://flear.org/flear"
+        "href": `${process.env.ACTIVITYPUB_URL}${process.env.ACTIVITYPUB_USER.toLowerCase()}`
       }
     ]
   }`);
-- 
GitLab