fix: #12821, improvements to search bar and search logic

- Entering a webfinger handle in search bar will override default
  `searchIn` to `users`
- When an activitypub uri is entered, it is fetched and its `type`
  is checked. If `type` is `user`, a user search is conducted
  instead. If `type` is not post-like or user-like, a regular
  full-text search is performed (prior, it just threw an
  exception!)
This commit is contained in:
Julian Lam
2024-09-30 13:01:29 -04:00
parent 43e3f4128b
commit 0b83e5b889
2 changed files with 21 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ define('search', [
const quickSearchContainer = searchFields.find('#quick-search-container');
const toggleVisibility = searchFields.hasClass('hidden');
const webfingerRegex = /^(@|acct:)?[\w-]+@.+$/; // should match src/activitypub/helpers.js
if (toggleVisibility) {
searchInput.off('blur').on('blur', function dismissSearch() {
@@ -70,6 +71,12 @@ define('search', [
const data = Search.getSearchPreferences();
data.term = input.val();
data.in = searchOptions.in;
// Override search target if webfinger handle entered
if (webfingerRegex.test(data.term)) {
data.in = 'users';
}
hooks.fire('action:search.submit', {
searchOptions: data,
searchElements: searchElements,

View File

@@ -92,7 +92,14 @@ async function searchInContent(data) {
if (local.type === 'post') {
result = [[local.id], []];
} else {
result = await fetchRemoteObject(data.uid, data.query);
try {
result = await fetchRemoteObject(data);
if (result.hasOwnProperty('users')) {
return result;
}
} catch (e) {
// ...
}
}
}
@@ -146,7 +153,9 @@ async function searchInContent(data) {
return Object.assign(returnData, metadata);
}
async function fetchRemoteObject(uid, uri) {
async function fetchRemoteObject(data) {
const { uid, query: uri } = data;
try {
let id = uri;
let exists = await posts.exists(id);
@@ -161,6 +170,9 @@ async function fetchRemoteObject(uid, uri) {
} else {
tid = await posts.getPostField(id, 'tid');
}
} else if (activitypub._constants.acceptableActorTypes.has(type)) {
data.searchIn = 'users';
return await user.search(data);
}
}