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

client-to-server Unfollow #36

Merged
merged 3 commits into from Mar 7, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -296,6 +296,12 @@ Override this by setting `response.locals.apex.authorizedUserId` to an actor IRI
if the `authorizedUserId` is the item's owner.
Override this by setting `response.locals.apex.authorized` to `true` (allow) or `false` (deny)

* Client-to-server unfollow: to remove an actor from followers, an "Undo" must be sent with
the object being the prior outgoing "Follow" and likewise to remove from following a "Reject"
must be sent with object being the prior incoming "Follow." As these past activity ids are
not readily available to clients, the client may send the undo or reject with an Actor id as
the object, and the server will find the related Follow and substitute it before publishing.

### Federation notes

* **http signaures**
Expand Down
21 changes: 20 additions & 1 deletion net/validators.js
Expand Up @@ -297,8 +297,27 @@ function outboxActivityObject (req, res, next) {
object = activity.object[0]
object.id = apex.utils.objectIdToIRI()
}
Promise.resolve(object).then(obj => {
Promise.resolve(object).then(async obj => {
resLocal.object = obj
// for unfollow, clients dont have easy access to old follow activitiy ids,
// so they can send an actor id and server will find related follow
if (!obj && type === 'undo') {
const actorId = apex.objectIdFromActivity(activity)
const follow = await apex.store
.findActivityByCollectionAndObjectId(resLocal.target.following[0], actorId, true)
if (follow) {
activity.object = [follow]
resLocal.object = follow
}
} else if (!obj && type === 'reject') {
const actorId = apex.objectIdFromActivity(activity)
const follow = await apex.store
.findActivityByCollectionAndActorId(resLocal.target.followers[0], actorId, true)
if (follow) {
activity.object = [follow]
resLocal.object = follow
}
}
next()
}).catch(err => {
apex.logger.warn('Error resolving outbox activity object', err.message)
Expand Down
9 changes: 6 additions & 3 deletions pub/activity.js
Expand Up @@ -229,7 +229,10 @@ async function resolveActivity (id, includeMeta) {
// resolve remote activity object
activity = await this.requestObject(id)
}
// cache
await this.store.saveActivity(activity)
return activity
// avoid saving non-acticity objects to streams collection
// if they are encountered during activity validation
if (this.validateActivity(activity)) {
await this.store.saveActivity(activity)
return activity
}
}
74 changes: 74 additions & 0 deletions spec/functional/outbox.spec.js
Expand Up @@ -344,6 +344,49 @@ describe('outbox', function () {
.expect(201)
.end(err => { if (err) done(err) })
})
it('unfollows if object is actor', async function (done) {
const mockedUser = 'https://mocked.com/user/mocked'
undone.type = 'Follow'
undone.object = [mockedUser]
apex.addMeta(undone, 'collection', testUser.following[0])
undone.to = [mockedUser]
await apex.store.saveActivity(undone)
expect((await apex.getFollowing(testUser, Infinity, true)).orderedItems)
.toEqual([mockedUser])
undo = {
type: 'Undo',
actor: testUser.id,
object: mockedUser,
to: mockedUser
}
let sentActivity
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) => {
sentActivity = JSON.parse(body)
})
// ignore update
nock('https://mocked.com')
.post('/inbox/mocked').reply(200)

app.once('apex-outbox', async () => {
// user id is replaced with related follow activity
expect(sentActivity.object.id).toBe(undone.id)
// follows updated
expect((await apex.getFollowing(testUser, Infinity, true)).orderedItems)
.toEqual([])
done()
})
request(app)
.post('/authorized/outbox/test')
.set('Content-Type', 'application/activity+json')
.send(undo)
.expect(201)
.end(err => { if (err) done(err) })
})
})
describe('update', function () {
let sourceObj
Expand Down Expand Up @@ -606,6 +649,37 @@ describe('outbox', function () {
.expect(201)
.end(err => { if (err) done(err) })
})
it('rejects prior follow if object is actor', async function (done) {
const mockedUser = 'https://mocked.com/user/mocked'
const mockedUserObj = { id: mockedUser, type: 'Actor', inbox: ['https://mocked.com/inbox/mocked'] }
let sentActivity
nock('https://mocked.com')
.get('/user/mocked').reply(200, mockedUserObj)
nock('https://mocked.com')
.post('/inbox/mocked').reply(200)
.on('request', (req, interceptor, body) => {
sentActivity = JSON.parse(body)
})
await apex.store.saveObject(mockedUserObj)
follow.actor = [mockedUser]
await apex.store.saveActivity(follow)
reject.object = mockedUser
reject.to = mockedUser
app.once('apex-outbox', async () => {
expect(sentActivity.object.id).toBe(follow.id)
expect((await apex.getFollowers(testUser, Infinity, true)).orderedItems)
.toEqual([])
done()
})
expect((await apex.getFollowers(testUser, Infinity, true)).orderedItems)
.toEqual([mockedUserObj])
request(app)
.post('/authorized/outbox/test')
.set('Content-Type', 'application/activity+json')
.send(reject)
.expect(201)
.end(err => { if (err) done(err) })
})
})
describe('delete', function () {
let toDelete
Expand Down
18 changes: 18 additions & 0 deletions store/index.js
Expand Up @@ -134,6 +134,24 @@ class ApexStore extends IApexStore {
.then(act => unescape(act))
}

findActivityByCollectionAndObjectId (collection, objectId, includeMeta) {
return this.db.collection('streams')
.find({ '_meta.collection': collection, object: objectId })
.limit(1)
.project(includeMeta ? this.metaProj : this.projection)
.next()
.then(act => unescape(act))
}

findActivityByCollectionAndActorId (collection, actorId, includeMeta) {
return this.db.collection('streams')
.find({ '_meta.collection': collection, actor: actorId })
.limit(1)
.project(includeMeta ? this.metaProj : this.projection)
.next()
.then(act => unescape(act))
}

getContext (documentUrl) {
return this.db.collection('contexts')
.findOne({ documentUrl }, { projection: { _id: 0 } })
Expand Down
10 changes: 9 additions & 1 deletion store/interface.js
Expand Up @@ -15,7 +15,15 @@ module.exports = class IApexStore {
throw new Error('Not implemented')
}

getActivity (id) {
getActivity (id, includeMeta) {
throw new Error('Not implemented')
}

findActivityByCollectionAndObjectId (collection, objectId, includeMeta) {
throw new Error('Not implemented')
}

findActivityByCollectionAndActorId (collection, actorId, includeMeta) {
throw new Error('Not implemented')
}

Expand Down