Skip to content

Node.js based forum software built for the modern web

feat: support the ability to search for posts and users by url

Julian Lam4cbb1f2a42fe
diff --git a/src/search.js b/src/search.js
index df249ec1f6..a61f9286ea 100644
--- a/src/search.js
+++ b/src/search.js
@@ -10,6 +10,7 @@ const categories = require('./categories');
const user = require('./user');
const plugins = require('./plugins');
const privileges = require('./privileges');
+const activitypub = require('./activitypub');
const utils = require('./utils');
const search = module.exports;
@@ -72,10 +73,19 @@ async function searchInContent(data) {
} else if (data.searchIn === 'bookmarks') {
pids = await searchInBookmarks(data, searchCids, searchUids);
} else {
- [pids, tids] = await Promise.all([
- doSearch('post', ['posts', 'titlesposts']),
- doSearch('topic', ['titles', 'titlesposts']),
- ]);
+ let result;
+ if (data.uid && activitypub.helpers.isUri(data.query)) {
+ result = await fetchRemoteObject(data.uid, data.query);
+ }
+
+ if (result) {
+ [pids, tids] = result;
+ } else {
+ [pids, tids] = await Promise.all([
+ doSearch('post', ['posts', 'titlesposts']),
+ doSearch('topic', ['titles', 'titlesposts']),
+ ]);
+ }
}
const mainPids = await topics.getMainPids(tids);
@@ -118,6 +128,30 @@ async function searchInContent(data) {
return Object.assign(returnData, metadata);
}
+async function fetchRemoteObject(uid, uri) {
+ try {
+ let id = uri;
+ let exists = await posts.exists(id);
+ let tid = exists ? await posts.getPostField(id, 'tid') : undefined;
+ if (!exists) {
+ let type;
+ ({ id, type } = await activitypub.get('uid', 0, id));
+ if (activitypub._constants.acceptedPostTypes.includes(type)) {
+ exists = await posts.exists(id);
+ if (!exists) {
+ ({ tid } = await activitypub.notes.assert(uid, id));
+ } else {
+ tid = await posts.getPostField(id, 'tid');
+ }
+ }
+ }
+
+ return tid ? [[id], []] : null;
+ } catch (e) {
+ return null;
+ }
+}
+
async function searchInBookmarks(data, searchCids, searchUids) {
const { uid, query, matchWords } = data;
const allPids = [];
diff --git a/src/user/search.js b/src/user/search.js
index a42bc61387..e613513e92 100644
--- a/src/user/search.js
+++ b/src/user/search.js
@@ -41,15 +41,26 @@ module.exports = function (User) {
} else if (searchBy === 'uid') {
uids = [query];
} else {
- const searchMethod = data.findUids || findUids;
- uids = await searchMethod(query, searchBy, data.hardCap);
-
- const mapping = {
- username: 'ap.preferredUsername',
- fullname: 'ap.name',
- };
- if (meta.config.activitypubEnabled && mapping.hasOwnProperty(searchBy)) {
- uids = uids.concat(await searchMethod(query, mapping[searchBy], data.hardCap));
+ if (!data.findUids && data.uid && activitypub.helpers.isUri(data.query)) {
+ const assertion = await activitypub.actors.assert([data.query]);
+ if (assertion === true) {
+ uids = [query];
+ } else if (Array.isArray(assertion) && assertion.length) {
+ uids = assertion.map(u => u.id);
+ }
+ }
+
+ if (!uids.length) {
+ const searchMethod = data.findUids || findUids;
+ uids = await searchMethod(query, searchBy, data.hardCap);
+
+ const mapping = {
+ username: 'ap.preferredUsername',
+ fullname: 'ap.name',
+ };
+ if (meta.config.activitypubEnabled && mapping.hasOwnProperty(searchBy)) {
+ uids = uids.concat(await searchMethod(query, mapping[searchBy], data.hardCap));
+ }
}
}