From f8e593a1cde3a4aab575be1a131e6f64394e1ae8 Mon Sep 17 00:00:00 2001 From: Akhilesh Thite Date: Mon, 27 May 2024 19:59:13 +0530 Subject: [PATCH 1/5] fix: prevent automatic ingestion of all notes for non-followed actors --- db.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/db.js b/db.js index f09b0c6..4760e8a 100644 --- a/db.js +++ b/db.js @@ -360,7 +360,7 @@ export class ActivityPubDB extends EventTarget { const note = await this.#get(activity.object) if (note.type === TYPE_NOTE) { console.log('Ingesting note:', note) - await this.ingestNote(note) + await this.ingestNote(note, true) } } else if (activity.type === TYPE_DELETE) { // Handle 'Delete' activity type @@ -370,7 +370,13 @@ export class ActivityPubDB extends EventTarget { return true } - async ingestNote (note) { + async ingestNote (note, forceIngest = false) { + const isFollowed = await this.isActorFollowed(note.attributedTo) + if (!isFollowed && !forceIngest) { + console.log('Skipping note ingestion as actor is not followed and forceIngest is false.') + return + } + console.log('Ingesting note', note) // Convert needed fields to date note.published = new Date(note.published) From 0329b41357fb1913f9b0c6fe7118e1da8bd55c9a Mon Sep 17 00:00:00 2001 From: Akhilesh Thite Date: Wed, 5 Jun 2024 19:50:19 +0530 Subject: [PATCH 2/5] perf: optimize note ingestion by checking actor alignment in ingestActivity --- .vscode/settings.json | 3 +++ db.js | 17 +++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6f3a291 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} \ No newline at end of file diff --git a/db.js b/db.js index 4760e8a..1d1113b 100644 --- a/db.js +++ b/db.js @@ -356,11 +356,16 @@ export class ActivityPubDB extends EventTarget { console.log('Ingesting activity:', activity) await this.db.put(ACTIVITIES_STORE, activity) - if (activity.type === TYPE_CREATE || activity.type === TYPE_UPDATE) { + if ((activity.type === TYPE_CREATE || activity.type === TYPE_UPDATE) && activity.actor) { const note = await this.#get(activity.object) if (note.type === TYPE_NOTE) { - console.log('Ingesting note:', note) - await this.ingestNote(note, true) + // Only ingest the note if the note's attributed actor is the same as the activity's actor + if (note.attributedTo === activity.actor) { + console.log('Ingesting note:', note) + await this.ingestNote(note) + } else { + console.log(`Skipping note ingestion for actor mismatch: Note attributed to ${note.attributedTo}, but activity actor is ${activity.actor}`) + } } } else if (activity.type === TYPE_DELETE) { // Handle 'Delete' activity type @@ -370,10 +375,10 @@ export class ActivityPubDB extends EventTarget { return true } - async ingestNote (note, forceIngest = false) { + async ingestNote (note) { const isFollowed = await this.isActorFollowed(note.attributedTo) - if (!isFollowed && !forceIngest) { - console.log('Skipping note ingestion as actor is not followed and forceIngest is false.') + if (!isFollowed) { + console.log('Skipping note ingestion as actor is not followed.') return } From f8af9af08897342aabfaa9a12b4a5ecc51222247 Mon Sep 17 00:00:00 2001 From: Akhilesh Thite Date: Wed, 5 Jun 2024 19:52:49 +0530 Subject: [PATCH 3/5] chore: remove .vscode --- .vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 6f3a291..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "liveServer.settings.port": 5501 -} \ No newline at end of file From fce66f0c2844f7c98af83e4e4864cf9a5c83c8f5 Mon Sep 17 00:00:00 2001 From: Akhilesh Thite Date: Fri, 7 Jun 2024 00:18:39 +0530 Subject: [PATCH 4/5] refactor: ingestNote invocation to optimize performance --- db.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/db.js b/db.js index 1fbeda4..5fef178 100644 --- a/db.js +++ b/db.js @@ -164,12 +164,12 @@ export class ActivityPubDB extends EventTarget { async getNote (url) { try { const note = await this.db.get(NOTES_STORE, url) - if (!note) throw new Error('Not loaded') - return note - } catch { + if (!note) throw new Error('Note not loaded') + return note // Simply return the locally found note. + } catch (error) { + // If the note is not in the local store, fetch it but don't automatically ingest it. const note = await this.#get(url) - await this.ingestNote(note) - return note + return note // Return the fetched note for further processing by the caller. } } From 16e1a036a09ea30715810651043d2b430b6276d9 Mon Sep 17 00:00:00 2001 From: Akhilesh Thite Date: Fri, 7 Jun 2024 01:25:25 +0530 Subject: [PATCH 5/5] remove isFollowed from ingestNote --- db.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/db.js b/db.js index 5fef178..0ab478a 100644 --- a/db.js +++ b/db.js @@ -398,12 +398,6 @@ export class ActivityPubDB extends EventTarget { } async ingestNote (note) { - const isFollowed = await this.isActorFollowed(note.attributedTo) - if (!isFollowed) { - console.log('Skipping note ingestion as actor is not followed.') - return - } - console.log('Ingesting note', note) // Convert needed fields to date note.published = new Date(note.published)