Skip to content

Node.js based forum software built for the modern web

fix: front-end logic (getters, setters, refresh code to call backend), backend code to query webfinger to get valid intents

Julian Lam8422ec264283
diff --git a/public/src/modules/intents.js b/public/src/modules/intents.js
index 19fae02283..60b22afec8 100644
--- a/public/src/modules/intents.js
+++ b/public/src/modules/intents.js
@@ -1,20 +1,78 @@
'use strict';
import { dialog } from 'bootbox';
+import { get } from 'api';
-export function get() {
- // Checks localStorage for whether an existing handle has been saved, and what intents they support
- // Multiple handles can be saved, so it should return a Map
+const STORAGE_KEY = 'ap:intents:handles';
+
+export function list() {
+ let raw;
+ try {
+ raw = localStorage.getItem(STORAGE_KEY);
+ } catch (e) {
+ // localStorage unavailable (e.g. private browsing)
+ return new Map();
+ }
+
+ if (!raw) {
+ return new Map();
+ }
+
+ let handles;
+ try {
+ handles = JSON.parse(raw);
+ } catch (e) {
+ // Corrupt data — reset
+ return new Map();
+ }
+
+ const map = new Map();
+ if (Array.isArray(handles)) {
+ handles.forEach(entry => {
+ if (entry && entry.handle && Array.isArray(entry.intents)) {
+ map.set(entry.handle, entry.intents);
+ }
+ });
+ }
+ return map;
}
export function save(handle, intents) {
- // Called by refresh, saves a handle into localStorage. Multiple handles can be active at any one time,
- // so possibly call get() and append the handle (or replace as necessary)
+ if (typeof handle !== 'string' || !handle.trim()) {
+ return;
+ }
+ handle = handle.trim();
+ if (!Array.isArray(intents)) {
+ return;
+ }
+
+ const map = list();
+ map.set(handle, intents);
+
+ const entries = Array.from(map.entries()).map(([h, i]) => ({ handle: h, intents: i }));
+ try {
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(entries));
+ } catch (e) {
+ // Storage full or unavailable — silently fail
+ }
}
-export function refresh(handle) {
- // Fires off a network request to backend to check whether the handle passed-in supports Activity Intents
- // should return { intents: Array }, where the array contains something like ['object', 'create'].
+export async function refresh(handle) {
+ if (typeof handle !== 'string' || !handle.trim()) {
+ return null;
+ }
+ handle = handle.trim().replace(/^@/, '');
+
+ try {
+ const result = await get(`/api/v3/intents/query/${handle}`);
+ if (result && Array.isArray(result.intents)) {
+ save(handle, result.intents);
+ return { intents: result.intents };
+ }
+ } catch (e) {
+ // Network or server error — handle may not support intents
+ }
+ return null;
}
export function register() {
diff --git a/src/activitypub/helpers.js b/src/activitypub/helpers.js
index ecc77ef750..78083896e9 100644
--- a/src/activitypub/helpers.js
+++ b/src/activitypub/helpers.js
@@ -146,7 +146,7 @@ Helpers.query = async (id) => {
if (!subject.startsWith('acct:') && !subject.startsWith('did:')) {
subject = `acct:${subject}`;
}
- const payload = { subject, username, hostname, actorUri, publicKey };
+ const payload = { subject, username, hostname, actorUri, publicKey, _raw: body };
const claimedId = new URL(subject).pathname;
webfingerCache.set(claimedId, payload);
if (claimedId !== id) {
diff --git a/src/controllers/write/index.js b/src/controllers/write/index.js
index 52ca4a7777..fbaa7ccfdc 100644
--- a/src/controllers/write/index.js
+++ b/src/controllers/write/index.js
@@ -12,6 +12,7 @@ Write.posts = require('./posts');
Write.chats = require('./chats');
Write.flags = require('./flags');
Write.search = require('./search');
+Write.intents = require('./intents');
Write.admin = require('./admin');
Write.files = require('./files');
Write.utilities = require('./utilities');
diff --git a/src/controllers/write/intents.js b/src/controllers/write/intents.js
index 9a1fdba038..ab3fec5d26 100644
--- a/src/controllers/write/intents.js
+++ b/src/controllers/write/intents.js
@@ -2,9 +2,27 @@
const Intents = module.exports;
+const activitypub = require('../../activitypub');
+
const helpers = require('../helpers');
Intents.query = async (req, res) => {
- const intents = [];
+ let { handle } = req.params;
+ handle = handle.trim().replace(/^@/, '');
+ const webfinger = await activitypub.helpers.query(handle);
+
+ // Parse out intents
+ let intents = [];
+ const prefix = 'https://w3id.org/fep/3b86/';
+ if (!webfinger || !webfinger?._raw?.links) {
+ helpers.formatApiResponse(200, res, { intents });
+ }
+ intents = webfinger._raw.links.reduce((memo, link) => {
+ if (link.rel.startsWith(prefix)) {
+ const intent = link.rel.slice(prefix.length).toLowerCase();
+ memo[intent] = link.template;
+ }
+ return memo;
+ }, {});
helpers.formatApiResponse(200, res, { intents });
};
\ No newline at end of file
diff --git a/src/routes/write/intents.js b/src/routes/write/intents.js
index f833b27f09..27a8b91a8c 100644
--- a/src/routes/write/intents.js
+++ b/src/routes/write/intents.js
@@ -7,7 +7,7 @@ const routeHelpers = require('../helpers');
const { setupApiRoute } = routeHelpers;
module.exports = function () {
- setupApiRoute(router, 'get', '/query', [], controllers.write.intents.query);
+ setupApiRoute(router, 'get', '/query/:handle', [], controllers.write.intents.query);
return router;
};