mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
refactor: add method from mentions into core
turn remote url into local profile urls if they are found in remoteUrl:uid
This commit is contained in:
@@ -307,3 +307,49 @@ Helpers.generateTitle = (html) => {
|
|||||||
|
|
||||||
return title;
|
return title;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Helpers.remoteAnchorToLocalProfile = async (content) => {
|
||||||
|
const anchorRegex = /<a.*?href=['"](.+?)['"].*?>(.*?)<\/a>/ig;
|
||||||
|
const anchors = content.matchAll(anchorRegex);
|
||||||
|
const urls = new Set();
|
||||||
|
const matches = [];
|
||||||
|
for (const anchor of anchors) {
|
||||||
|
const [match, url] = anchor;
|
||||||
|
matches.push([match, url]);
|
||||||
|
urls.add(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!urls.size) {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter out urls that don't backreference to a remote id
|
||||||
|
const urlsArray = Array.from(urls);
|
||||||
|
const [backrefs, urlAsIdExists] = await Promise.all([
|
||||||
|
db.getObjectFields('remoteUrl:uid', urlsArray),
|
||||||
|
db.isSortedSetMembers('usersRemote:lastCrawled', urlsArray),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const urlMap = new Map();
|
||||||
|
urlsArray.forEach((url, index) => {
|
||||||
|
if (backrefs[url] || urlAsIdExists[index]) {
|
||||||
|
urlMap.set(url, backrefs[url] || url);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let slugs = await user.getUsersFields(Array.from(urlMap.values()), ['userslug']);
|
||||||
|
slugs = slugs.map(({ userslug }) => userslug);
|
||||||
|
Array.from(urlMap.keys()).forEach((url, idx) => {
|
||||||
|
urlMap.set(url, `/user/${encodeURIComponent(slugs[idx])}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Modify existing anchors to local profile
|
||||||
|
matches.forEach(([match, href]) => {
|
||||||
|
const replacementHref = urlMap.get(href);
|
||||||
|
if (replacementHref) {
|
||||||
|
const replacement = match.replace(href, replacementHref);
|
||||||
|
content = content.split(match).join(replacement);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return content;
|
||||||
|
};
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ Mocks.post = async (objects) => {
|
|||||||
edited = Number.isNaN(edited.valueOf()) ? undefined : edited;
|
edited = Number.isNaN(edited.valueOf()) ? undefined : edited;
|
||||||
|
|
||||||
content = sanitize(content, sanitizeConfig);
|
content = sanitize(content, sanitizeConfig);
|
||||||
|
content = await activitypub.helpers.remoteAnchorToLocalProfile(content);
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
uid,
|
uid,
|
||||||
|
|||||||
Reference in New Issue
Block a user