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:
Barış Soner Uşaklı
2024-06-12 20:31:36 -04:00
parent a67e1b7caf
commit 3eca978509
2 changed files with 47 additions and 0 deletions

View File

@@ -307,3 +307,49 @@ Helpers.generateTitle = (html) => {
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;
};

View File

@@ -114,6 +114,7 @@ Mocks.post = async (objects) => {
edited = Number.isNaN(edited.valueOf()) ? undefined : edited;
content = sanitize(content, sanitizeConfig);
content = await activitypub.helpers.remoteAnchorToLocalProfile(content);
const payload = {
uid,