Skip to content

Commit

Permalink
publish updates to certain colletions after undo
Browse files Browse the repository at this point in the history
  • Loading branch information
wmurphyrd committed Jan 4, 2021
1 parent c787ff8 commit 39ebf2d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 6 deletions.
16 changes: 14 additions & 2 deletions net/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ module.exports = {
// deleting the activity also removes it from all collections,
// undoing follows, blocks, shares, and likes
toDo.push(apex.store.removeActivity(object, actorId))
// TODO: publish appropriate collection updates (after #8)
resLocal.postWork.push(() => {
// update any collections the activity was removed from
const audience = apex.audienceFromActivity(object).concat(actor.id)
const updates = object._meta?.collection
?.map(colId => apex.publishUndoUpdate(colId, recipient, audience))
return updates && Promise.allSettled(updates)
})
}
break
case 'update':
Expand Down Expand Up @@ -254,7 +260,13 @@ module.exports = {
// deleting the activity also removes it from all collections,
// undoing follows, blocks, shares, and likes
toDo.push(apex.store.removeActivity(object, actor.id))
// TODO: publish appropriate collection updates (after #8)
resLocal.postWork.push(() => {
// update any collections the activity was removed from
const audience = apex.audienceFromActivity(object)
const updates = object._meta?.collection
?.map(colId => apex.publishUndoUpdate(colId, actor, audience))
return updates && Promise.allSettled(updates)
})
}
break
}
Expand Down
18 changes: 18 additions & 0 deletions pub/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
addToOutbox,
buildActivity,
buildTombstone,
publishUndoUpdate,
publishUpdate,
resolveActivity
}
Expand Down Expand Up @@ -136,6 +137,23 @@ async function acceptFollow (actor, targetActivity) {
return { postTask, updated }
}

async function publishUndoUpdate (colId, actor, audience) {
const info = this.utils.iriToCollectionInfo(colId)
let actorId
if (!['followers', 'following', 'liked', 'likes', 'shares'].includes(info?.name)) {
return
}
if (info.activity) {
const activityIRI = this.utils.activityIdToIRI(info.activity)
actorId = (await this.store.getActivity(activityIRI))?.actor[0]
}
if (actor.id === (actorId ?? this.utils.usernameToIRI(info.actor))) {
const colUpdate = await this.getCollection(colId)
return this.publishUpdate(actor, colUpdate, audience)
}
return undefined
}

async function publishUpdate (actor, object, cc) {
const act = await this.buildActivity(
'Update',
Expand Down
59 changes: 58 additions & 1 deletion spec/functional/inbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,64 @@ describe('inbox', function () {
expect(result).toBeFalsy()
done()
})
it('publishes related collection updates')
it('publishes followeres collection updates', async function (done) {
const mockedUser = 'https://mocked.com/user/mocked'
undone.type = 'Follow'
undone.object = [testUser.id]
apex.addMeta(undone, 'collection', testUser.followers[0])
undone.to = [mockedUser]
await apex.store.saveActivity(undone)
nock('https://mocked.com')
.get('/user/mocked')
.reply(200, { id: mockedUser, inbox: 'https://mocked.com/inbox/mocked' })
nock('https://mocked.com')
.post('/inbox/mocked').reply(200)
.on('request', (req, interceptor, body) => {
const sentActivity = JSON.parse(body)
expect(sentActivity.type).toBe('Update')
expect(sentActivity.object.id).toBe(testUser.followers[0])
expect(sentActivity.object.totalItems).toBe(0)
done()
})
request(app)
.post('/inbox/test')
.set('Content-Type', 'application/activity+json')
.send(undo)
.expect(200)
.end(err => { if (err) done(err) })
})
it('publishes activity collection updates', async function (done) {
const mockedUser = 'https://mocked.com/user/mocked'
const likeable = await apex.buildActivity('Create', testUser.id, [], {
object: { type: 'Note', content: 'hello' }
})
undone.actor = [mockedUser]
undone.type = 'Like'
undone.object = [likeable.id]
apex.addMeta(undone, 'collection', likeable.likes[0])
undone.to = [testUser.id]
undo.actor = [mockedUser]
await apex.store.saveActivity(likeable)
await apex.store.saveActivity(undone)
nock('https://mocked.com')
.get('/user/mocked')
.reply(200, { id: mockedUser, inbox: 'https://mocked.com/inbox/mocked' })
nock('https://mocked.com')
.post('/inbox/mocked').reply(200)
.on('request', (req, interceptor, body) => {
const sentActivity = JSON.parse(body)
expect(sentActivity.type).toBe('Update')
expect(sentActivity.object.id).toBe(likeable.likes[0])
expect(sentActivity.object.totalItems).toBe(0)
done()
})
request(app)
.post('/inbox/test')
.set('Content-Type', 'application/activity+json')
.send(undo)
.expect(200)
.end(err => { if (err) done(err) })
})
})
it('fires other activity event', function (done) {
const arriveAct = {
Expand Down
31 changes: 28 additions & 3 deletions spec/functional/outbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ describe('outbox', function () {
.send(undo)
.expect(403, done)
})
it('removes undone activity', async function (done) {
it('removes undone activity', async function () {
await apex.store.saveActivity(undone)
await request(app)
.post('/outbox/test')
Expand All @@ -268,9 +268,34 @@ describe('outbox', function () {
.expect(200)
const result = await apex.store.getActivity(undone.id)
expect(result).toBeFalsy()
done()
})
it('publishes related collection updates')
it('publishes related collection updates', async function (done) {
const mockedUser = 'https://mocked.com/user/mocked'
undone.type = 'Like'
undone.object = [activityNormalized.object[0].id]
apex.addMeta(undone, 'collection', testUser.liked[0])
undone.to = [mockedUser]
await apex.store.saveActivity(activityNormalized)
await apex.store.saveActivity(undone)
nock('https://mocked.com')
.get('/user/mocked')
.reply(200, { id: mockedUser, inbox: 'https://mocked.com/inbox/mocked' })
nock('https://mocked.com')
.post('/inbox/mocked').reply(200)
.on('request', (req, interceptor, body) => {
const sentActivity = JSON.parse(body)
expect(sentActivity.type).toBe('Update')
expect(sentActivity.object.id).toBe(testUser.liked[0])
expect(sentActivity.object.totalItems).toBe(0)
done()
})
request(app)
.post('/outbox/test')
.set('Content-Type', 'application/activity+json')
.send(undo)
.expect(200)
.end(err => { if (err) done(err) })
})
})
describe('update', function () {
let sourceObj
Expand Down

0 comments on commit 39ebf2d

Please sign in to comment.