From eb8ff75b63c2ab17e8920925b828ec46167ce430 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 12 Mar 2024 11:33:40 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20only=20dereference=20id=20if=20it=20does?= =?UTF-8?q?n't=20exist=20locally=20=E2=80=94=20fewer=20calls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/activitypub/notes.js | 36 ++++++++++++++++++++++++------------ src/topics/posts.js | 1 - 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/activitypub/notes.js b/src/activitypub/notes.js index e39e64171a..32325e71fb 100644 --- a/src/activitypub/notes.js +++ b/src/activitypub/notes.js @@ -22,21 +22,33 @@ Notes.assert = async (uid, input, options = {}) => { const actors = new Set(); await Promise.all(input.map(async (item) => { - // Dereference only if a url is received - if (activitypub.helpers.isUri(item)) { - item = await activitypub.resolveId(uid, item); - if (!item) { - winston.warn(`[activitypub/notes.assert] Not asserting ${item}`); - return; - } - } - - const id = activitypub.helpers.isUri(item) ? item : item.pid; - const key = `post:${id}`; - const exists = await db.exists(key); + let id = activitypub.helpers.isUri(item) ? item : item.pid; + let key = `post:${id}`; + let exists = await db.exists(key); winston.verbose(`[activitypub/notes.assert] Asserting note id ${id}`); if (id && (!exists || options.update === true)) { + // Dereference only if a url is received + if (activitypub.helpers.isUri(item)) { + winston.verbose(`[activitypub/notes.assert] Dereference check for ${id}`); + const resolvedId = await activitypub.resolveId(uid, item); + if (!resolvedId) { + winston.warn(`[activitypub/notes.assert] Not asserting ${item}`); + return; + } + + if (resolvedId !== id) { + id = resolvedId; + key = `post:${id}`; + exists = await db.exists(key); + winston.verbose(`[activitypub/notes.assert] Re-asserting note id ${id}`); + + if (exists && options.update !== true) { + return; + } + } + } + let postData; winston.verbose(`[activitypub/notes.assert] Not found, retrieving note for persistence...`); if (activitypub.helpers.isUri(item)) { diff --git a/src/topics/posts.js b/src/topics/posts.js index bf967291e1..2ed3d208e7 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -187,7 +187,6 @@ module.exports = function (Topics) { return; } parentPids = _.uniq(parentPids); - await activitypub.notes.assert(uid, parentPids.filter(pid => activitypub.helpers.isUri(pid))); const parentPosts = await posts.getPostsFields(parentPids, ['uid']); const parentUids = _.uniq(parentPosts.map(postObj => postObj && postObj.uid)); const userData = await user.getUsersFields(parentUids, ['username']);