mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: support the ability to search for posts and users by url
This commit is contained in:
@@ -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;
|
||||
@@ -71,12 +72,21 @@ async function searchInContent(data) {
|
||||
pids = await topics.search(tid, cleanedTerm);
|
||||
} else if (data.searchIn === 'bookmarks') {
|
||||
pids = await searchInBookmarks(data, searchCids, searchUids);
|
||||
} else {
|
||||
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 = [];
|
||||
|
||||
@@ -41,6 +41,16 @@ module.exports = function (User) {
|
||||
} else if (searchBy === 'uid') {
|
||||
uids = [query];
|
||||
} else {
|
||||
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);
|
||||
|
||||
@@ -52,6 +62,7 @@ module.exports = function (User) {
|
||||
uids = uids.concat(await searchMethod(query, mapping[searchBy], data.hardCap));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uids = await filterAndSortUids(uids, data);
|
||||
if (data.hardCap > 0) {
|
||||
|
||||
Reference in New Issue
Block a user