Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete tombstones and log failing activities #98 #99

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/server/apsystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,73 @@ test('ActivityPubSystem - Handle Delete activity', async t => {
t.falsy(isActivityPresent, 'The activity should be removed from the index/collection')
})

test('ActivityPubSystem - Handle Delete Tombstone 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: {
id: activityId,
type: 'Tombstone'
},
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
16 changes: 12 additions & 4 deletions src/server/apsystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,20 @@ 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')
let object = activity.object
if (typeof object !== 'string') {
if ((object != null) && 'id' in object && typeof object.id === 'string') {
object = object.id
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have a helper to obtain the object id?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think we could stand to have a bunch of helpers, not sure when to fit it in

} else {
this.log.warn({ activity }, 'Error deleting activity, must have activity URL in object field')
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')

await actorStore.inbox.remove(object)
this.log.info({ activityId, object }, 'Deleted activity from inbox')
// Notify CMS that the activity was deleted
await this.hookSystem.dispatchOnApproved(fromActor, activity)
return
Expand Down