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 user = require('./user');
|
||||||
const plugins = require('./plugins');
|
const plugins = require('./plugins');
|
||||||
const privileges = require('./privileges');
|
const privileges = require('./privileges');
|
||||||
|
const activitypub = require('./activitypub');
|
||||||
const utils = require('./utils');
|
const utils = require('./utils');
|
||||||
|
|
||||||
const search = module.exports;
|
const search = module.exports;
|
||||||
@@ -72,10 +73,19 @@ async function searchInContent(data) {
|
|||||||
} else if (data.searchIn === 'bookmarks') {
|
} else if (data.searchIn === 'bookmarks') {
|
||||||
pids = await searchInBookmarks(data, searchCids, searchUids);
|
pids = await searchInBookmarks(data, searchCids, searchUids);
|
||||||
} else {
|
} else {
|
||||||
[pids, tids] = await Promise.all([
|
let result;
|
||||||
doSearch('post', ['posts', 'titlesposts']),
|
if (data.uid && activitypub.helpers.isUri(data.query)) {
|
||||||
doSearch('topic', ['titles', 'titlesposts']),
|
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);
|
const mainPids = await topics.getMainPids(tids);
|
||||||
@@ -118,6 +128,30 @@ async function searchInContent(data) {
|
|||||||
return Object.assign(returnData, metadata);
|
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) {
|
async function searchInBookmarks(data, searchCids, searchUids) {
|
||||||
const { uid, query, matchWords } = data;
|
const { uid, query, matchWords } = data;
|
||||||
const allPids = [];
|
const allPids = [];
|
||||||
|
|||||||
@@ -41,15 +41,26 @@ module.exports = function (User) {
|
|||||||
} else if (searchBy === 'uid') {
|
} else if (searchBy === 'uid') {
|
||||||
uids = [query];
|
uids = [query];
|
||||||
} else {
|
} else {
|
||||||
const searchMethod = data.findUids || findUids;
|
if (!data.findUids && data.uid && activitypub.helpers.isUri(data.query)) {
|
||||||
uids = await searchMethod(query, searchBy, data.hardCap);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const mapping = {
|
if (!uids.length) {
|
||||||
username: 'ap.preferredUsername',
|
const searchMethod = data.findUids || findUids;
|
||||||
fullname: 'ap.name',
|
uids = await searchMethod(query, searchBy, data.hardCap);
|
||||||
};
|
|
||||||
if (meta.config.activitypubEnabled && mapping.hasOwnProperty(searchBy)) {
|
const mapping = {
|
||||||
uids = uids.concat(await searchMethod(query, mapping[searchBy], data.hardCap));
|
username: 'ap.preferredUsername',
|
||||||
|
fullname: 'ap.name',
|
||||||
|
};
|
||||||
|
if (meta.config.activitypubEnabled && mapping.hasOwnProperty(searchBy)) {
|
||||||
|
uids = uids.concat(await searchMethod(query, mapping[searchBy], data.hardCap));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user