Skip to content

Commit

Permalink
fix: parse object.id in performUndo() and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magush27 committed Jun 26, 2024
1 parent e66c306 commit 00f74e6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
59 changes: 59 additions & 0 deletions src/server/apsystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,65 @@ test('ActivityPubSystem - List replies', async t => {
t.deepEqual(collection.items, [activity.object])
})

test('ActivityPubSystem - Undo activity', async t => {
const store = newStore()
const hookSystem = new HookSystem(store, mockFetch)
const aps = new ActivityPubSystem('http://localhost', store, mockModCheck, hookSystem, mockServer.log)

const actorMention = '@[email protected]'
const inReplyTo = 'https://example.com/note2'

// Sample data for the tests
const activity: APActivity = {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'Create',
published: new Date().toISOString(),
actor: 'https://example.com/user1',
object: {
type: 'Note',
published: new Date().toISOString(),
content: 'Hello world',
id: 'https://example.com/note1',
inReplyTo,
attributedTo: 'https://example.com/user1'
},
id: 'https://example.com/activity1'
}

const undoActivity: APActivity = {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'Undo',
actor: 'https://example.com/user1',
object: {
id: activity.id,
type: activity.type
},
id: 'https://example.com/undo1'
}

await store.forActor(actorMention).inbox.add(activity)
await aps.approveActivity(actorMention, activity.id as string)

// Log to verify the activity is in the store
const storedActivity = await store.forActor(actorMention).inbox.get(activity.id as string)
console.log('Stored activity:', storedActivity)

t.truthy(storedActivity, 'The activity is stored successfully')

// Add Undo activity
await store.forActor(actorMention).inbox.add(undoActivity)
await aps.approveActivity(actorMention, undoActivity.id as string)

const storedUndoActivity = await store.forActor(actorMention).inbox.get(undoActivity.id as string)
console.log('Stored undo activity:', storedUndoActivity)

t.truthy(storedUndoActivity, 'The undo activity is stored successfully')

// Activity is undone
const collection = await aps.repliesCollection(actorMention, inReplyTo)
t.deepEqual(collection.items, [activity.object])
})

// After all tests, restore all sinon mocks
test.afterEach(() => {
// Restore all sinon mocks
Expand Down
8 changes: 6 additions & 2 deletions src/server/apsystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,13 @@ export default class ActivityPubSystem {
}

async performUndo (fromActor: string, activity: APActivity): Promise<void> {
const { actor, object } = activity
const { actor } = activity
let object = activity.object
if (typeof object !== 'string') {
throw createError(400, 'Undo must point to URL of object')
object = ((object != null) && 'id' in object && typeof object.id === 'string' && object.id) || undefined
if (typeof object !== 'string') {
throw createError(400, 'Undo must point to URL of object')
}
}
if (typeof actor !== 'string') {
throw createError(400, 'Activities must contain an actor string')
Expand Down

0 comments on commit 00f74e6

Please sign in to comment.