Skip to content

Commit

Permalink
Merge pull request #90 from hyphacoop/auto-accept-remote-deletes
Browse files Browse the repository at this point in the history
Auto-accept remote deletes
  • Loading branch information
RangerMauve authored Oct 30, 2024
2 parents c45769d + 53543e0 commit 5530ce7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
69 changes: 66 additions & 3 deletions src/server/apsystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { MockFetch } from './fixtures/mockFetch.js'
import { generateKeypair } from 'http-signed-fetch'

// Helper function to create a new Store instance
function newStore(): Store {
function newStore (): Store {
return new Store(new MemoryLevel({ valueEncoding: 'json' }))
}

Expand All @@ -34,7 +34,7 @@ const mockFetch: FetchLike = async (input: RequestInfo | URL, init?: RequestInit
}
const mockHooks = new HookSystem(mockStore, mockFetch)

function noop(): void {
function noop (): void {
}
const mockLog = {
info: noop,
Expand Down Expand Up @@ -393,7 +393,6 @@ test('ActivityPubSystem - Undo activity', async t => {
await t.throwsAsync(async () => {
return await store.forActor(actorMention).inbox.get(activity.id as string)
})

})

test('ActivityPubSystem - Interacted store', async t => {
Expand Down Expand Up @@ -561,6 +560,70 @@ test('ActivityPubSystem - Backfill Inbox', async t => {
// expect requests for the outbox and notes, expect request in inbox
})

test('ActivityPubSystem - Handle Delete activity', async t => {
const store = newStore()
const mockFetch = new MockFetch()
const hookSystem = new HookSystem(store, mockFetch.fetch as FetchLike)
const moderation = new ModerationChecker(store)
const aps = new ActivityPubSystem(
'http://localhost',
store,
moderation,
hookSystem,
mockLog,
mockFetch.fetch as FetchLike
)

const actorMention = '@[email protected]'
const activityId = 'https://example.com/activity1'

const actorUrl = mockFetch.mockActor(actorMention)

await store.forActor(actorMention).setInfo({
keypair: { ...generateKeypair() },
actorUrl,
publicKeyId: 'testAccount#main-key'
})

const activity: APActivity = {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'Like',
published: new Date().toISOString(),
actor: actorUrl,
object: 'https://example.com/note1',
id: activityId
}
await store.forActor(actorMention).inbox.add(activity)

const deleteActivity: APActivity = {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'Delete',
published: new Date().toISOString(),
actor: actorUrl,
object: activityId,
id: 'https://example.com/activity2'
}

mockFetch.addAPObject(deleteActivity)

await aps.ingestActivity(actorMention, deleteActivity)

try {
await store.forActor(actorMention).inbox.get(activityId)
t.fail('The activity should be deleted from the inbox')
} catch (error) {
if (error instanceof Error) {
t.true(error.message.includes('Activity not found'), 'The activity should be deleted from the inbox')
} else {
t.fail('Unexpected error type')
}
}

const storedActivities = await store.forActor(actorMention).inbox.list({ object: activityId })
const isActivityPresent = storedActivities.some((a) => a.id === activityId)
t.falsy(isActivityPresent, 'The activity should be removed from the index/collection')
})

// After all tests, restore all sinon mocks
test.afterEach(() => {
// Restore all sinon mocks
Expand Down
9 changes: 9 additions & 0 deletions src/server/apsystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,15 @@ export default class ActivityPubSystem {
)
return
}
// Remove activity from inbox and any other index/collection
if (typeof activity.object !== 'string') {
throw createError(400, 'Error deleting activity, must have activity URL in object field')
}
await actorStore.inbox.remove(activity.object)
this.log.info({ activityId }, 'Deleted activity from inbox')
// Notify CMS that the activity was deleted
await this.hookSystem.dispatchOnApproved(fromActor, activity)
return
}
await actorStore.inbox.add(activity)

Expand Down

0 comments on commit 5530ce7

Please sign in to comment.