Skip to content

Node.js based forum software built for the modern web

Unify intents controller — single endpoint for all intents

Julian Lam6e1adf7384d1
diff --git a/public/src/client/intents.js b/public/src/client/intents.js
new file mode 100644
index 0000000000..7c9f9d46fa
--- /dev/null
+++ b/public/src/client/intents.js
@@ -0,0 +1,25 @@
+'use strict';
+
+export function init() {
+ switch(ajaxify.data.intent) {
+ case 'create': {
+ if (ajaxify.data?.cid) {
+ ajaxify.go(`category/${ajaxify.data.cid}`);
+ app.newTopic(ajaxify.data.cid);
+ } else if (ajaxify.data?.tid) {
+ if (ajaxify.data?.toPid) {
+ ajaxify.go(`/post/${ajaxify.data.toPid}`);
+ } else {
+ ajaxify.go(`topic/${ajaxify.data.tid}`);
+ }
+ app.newReply({ tid: ajaxify.data.tid, toPid: ajaxify.data.toPid });
+ }
+ break;
+ }
+
+ case 'like': {
+ ajaxify.go(`/post/${ajaxify.data.pid}`);
+ // intents.confirm
+ }
+ }
+}
diff --git a/public/src/client/intents/create.js b/public/src/client/intents/create.js
deleted file mode 100644
index 49d7fa4779..0000000000
--- a/public/src/client/intents/create.js
+++ /dev/null
@@ -1,15 +0,0 @@
-'use strict';
-
-export function init() {
- if (ajaxify.data?.cid) {
- ajaxify.go(`category/${ajaxify.data.cid}`);
- app.newTopic(ajaxify.data.cid);
- } else if (ajaxify.data?.tid) {
- if (ajaxify.data?.toPid) {
- ajaxify.go(`/post/${ajaxify.data.toPid}`);
- } else {
- ajaxify.go(`topic/${ajaxify.data.tid}`);
- }
- app.newReply({ tid: ajaxify.data.tid, toPid: ajaxify.data.toPid });
- }
-}
diff --git a/public/src/modules/intents.js b/public/src/modules/intents.js
index 56d7cdf5ba..2de6fd1ae7 100644
--- a/public/src/modules/intents.js
+++ b/public/src/modules/intents.js
@@ -216,6 +216,34 @@ export function trigger(intent, parameters) {
const intents = map.get(handle);
let url = intents && intents[requiredIntent];
+ // For local intents (like, dislike, follow) — route through /intents/{intent}
+ if (['like', 'dislike', 'follow'].includes(requiredIntent)) {
+ const params = new URLSearchParams();
+ if (parameters) {
+ // Extract pid/uid from object parameter if present
+ const obj = parameters.object || parameters.pid || parameters.uid;
+ if (obj) {
+ if (requiredIntent === 'like' || requiredIntent === 'dislike') {
+ // Extract pid from URL if it's a full URL
+ if (typeof obj === 'string' && obj.includes('/post/')) {
+ params.set('pid', obj.split('/post/')[1]);
+ } else {
+ params.set('pid', obj);
+ }
+ } else {
+ // Extract uid from URL if it's a full URL
+ if (typeof obj === 'string' && obj.includes('/user/')) {
+ params.set('uid', obj.split('/user/')[1]);
+ } else {
+ params.set('uid', obj);
+ }
+ }
+ }
+ }
+ ajaxify.go(`/intents/${requiredIntent}${params.toString() ? `?${params.toString()}` : ''}`);
+ return;
+ }
+
// Replace template placeholders with URL-encoded parameter values
if (url && parameters && typeof parameters === 'object') {
Object.keys(parameters).forEach((prop) => {
diff --git a/src/controllers/intents.js b/src/controllers/intents.js
index 8da6c42ccf..08a02289b7 100644
--- a/src/controllers/intents.js
+++ b/src/controllers/intents.js
@@ -1,11 +1,17 @@
'use strict';
+const nconf = require('nconf');
const posts = require('../posts');
const categories = require('../categories');
+const user = require('../user');
const helpers = require('./helpers');
const activitypub = require('../activitypub');
const utils = require('../utils');
+const config = {
+ url: nconf.get('url'),
+};
+
const Intents = module.exports;
Intents.create = async (req, res, next) => {
@@ -14,59 +20,110 @@ Intents.create = async (req, res, next) => {
helpers.redirect(res, '/login');
}
- const payload = {};
+ const intent = req.params?.intent || 'create';
+ const payload = { intent };
+
+ switch (intent) {
+ case 'create': {
+ if (req.query?.toPid) {
+ let { toPid } = req.query;
+
+ if (!utils.isNumber(toPid)) {
+ const resolved = await activitypub.helpers.resolveLocalId(toPid);
+ const { type, id: localId } = resolved || {};
+ if (type === 'post') {
+ toPid = localId || toPid;
+ }
+ }
- if (req.query?.toPid) {
- let { toPid } = req.query;
+ if (utils.isNumber(toPid)) {
+ // Local post ID — verify it exists
+ const exists = await posts.exists(toPid);
+ if (exists) {
+ const tid = await posts.getPostField(toPid, 'tid');
+ if (tid) {
+ payload.toPid = parseInt(toPid, 10);
+ payload.tid = tid;
+ }
+ }
+ } else {
+ // Not a valid local post — assert as ActivityPub note
+ const result = await activitypub.notes.assert(0, toPid);
+ if (result && result.tid) {
+ payload.toPid = toPid;
+ payload.tid = result.tid;
+ }
+ }
+ } else if (req.query?.cid) {
+ let { cid } = req.query;
- if (!utils.isNumber(toPid)) {
- const resolved = await activitypub.helpers.resolveLocalId(toPid);
- const { type, id: localId } = resolved || {};
- if (type === 'post') {
- toPid = localId || toPid;
+ if (!utils.isNumber(cid)) {
+ const resolved = await activitypub.helpers.resolveLocalId(cid);
+ const { type, id: localId } = resolved || {};
+ if (type === 'category') {
+ cid = localId || cid;
+ }
+ }
+
+ // Verify category exists
+ const exists = await categories.exists(cid);
+ if (!exists) {
+ return next();
+ }
+ payload.cid = cid;
}
+
+ if (!['toPid', 'tid', 'cid'].some(prop => payload.hasOwnProperty(prop))) {
+ return next();
+ }
+ break;
}
- if (utils.isNumber(toPid)) {
- // Local post ID — verify it exists
- const exists = await posts.exists(toPid);
- if (exists) {
- const tid = await posts.getPostField(toPid, 'tid');
- if (tid) {
- payload.toPid = utils.isNumber(toPid) ? parseInt(toPid, 10) : toPid;
- payload.tid = tid;
+ case 'like':
+ case 'dislike': {
+ let { pid } = req.query;
+
+ if (!utils.isNumber(pid)) {
+ const resolved = await activitypub.helpers.resolveLocalId(pid);
+ const { type, id: localId } = resolved || {};
+ if (type === 'post') {
+ pid = localId || pid;
}
}
- } else {
- // Not a valid local post — assert as ActivityPub note
- const result = await activitypub.notes.assert(0, toPid);
- if (result && result.tid) {
- payload.toPid = toPid;
- payload.tid = result.tid;
+
+ const exists = await posts.exists(pid);
+ if (exists) {
+ payload.pid = pid;
+ } else {
+ // todo: assert the post and then set payload.pid
}
+ break;
}
- } else if (req.query?.cid) {
- let { cid } = req.query;
-
- if (!utils.isNumber(cid)) {
- const resolved = await activitypub.helpers.resolveLocalId(cid);
- const { type, id: localId } = resolved || {};
- if (type === 'category') {
- cid = localId || cid;
+
+ case 'follow': {
+ let { uid } = req.query;
+
+ if (!utils.isNumber(uid)) {
+ const resolved = await activitypub.helpers.resolveLocalId(uid);
+ const { type, id: localId } = resolved || {};
+ if (type === 'user') {
+ uid = localId || uid;
+ }
}
+
+ const exists = await user.exists(uid);
+ if (exists) {
+ payload.uid = uid;
+ } else {
+ // todo: activitypub.actors.assert then set payload.uid
+ }
+ break;
}
- // Verify category exists
- const exists = await categories.exists(cid);
- if (!exists) {
+ default:
return next();
- }
- payload.cid = cid;
}
- if (!['toPid', 'tid', 'cid'].some(prop => payload.hasOwnProperty(prop))) {
- return next();
- }
+ res.render('intents', payload);
- res.render('intents/create', payload);
};
\ No newline at end of file
diff --git a/src/routes/index.js b/src/routes/index.js
index 547b0bcaf2..663cfaa8a2 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -38,7 +38,7 @@ _mounts.main = (app, middleware, controllers) => {
setupPageRoute(app, '/search', [], controllers.search.search);
setupPageRoute(app, '/reset/:code?', [middleware.delayLoading], controllers.reset);
setupPageRoute(app, '/tos', [], controllers.termsOfUse);
- setupPageRoute(app, '/intents/create', controllers.intents.create);
+ setupPageRoute(app, '/intents/:intent', controllers.intents.create);
setupPageRoute(app, '/email/unsubscribe/:token', [], controllers.accounts.settings.unsubscribe);
app.post('/email/unsubscribe/:token', controllers.accounts.settings.unsubscribePost);
diff --git a/src/views/intents/create.tpl b/src/views/intents.tpl
similarity index 100%
rename from src/views/intents/create.tpl
rename to src/views/intents.tpl