2019-09-21 21:53:55 +00:00
|
|
|
'use strict'
|
|
|
|
const express = require('express')
|
|
|
|
const router = express.Router()
|
2019-09-21 21:20:14 +00:00
|
|
|
const pub = require('../pub')
|
2018-09-15 07:01:19 +00:00
|
|
|
|
2019-09-13 01:03:04 +00:00
|
|
|
router.get('/:name', async function (req, res) {
|
2019-09-22 05:20:37 +00:00
|
|
|
const name = req.params.name
|
2018-09-15 07:01:19 +00:00
|
|
|
if (!name) {
|
2019-09-22 05:20:37 +00:00
|
|
|
return res.status(400).send('Bad request.')
|
|
|
|
} else {
|
|
|
|
const db = req.app.get('db')
|
2019-09-22 05:05:30 +00:00
|
|
|
const user = await pub.actor.getOrCreateActor(name, db)
|
2019-09-13 01:03:04 +00:00
|
|
|
if (user) {
|
2019-09-21 21:20:14 +00:00
|
|
|
return res.json(pub.utils.toJSONLD(user))
|
2019-03-26 05:17:24 +00:00
|
|
|
}
|
2019-09-13 01:03:04 +00:00
|
|
|
return res.status(404).send('Person not found')
|
2018-09-15 07:01:19 +00:00
|
|
|
}
|
2019-09-22 05:20:37 +00:00
|
|
|
})
|
2018-09-15 07:01:19 +00:00
|
|
|
|
2019-09-17 01:58:32 +00:00
|
|
|
router.get('/:name/followers', function (req, res) {
|
2019-09-22 05:20:37 +00:00
|
|
|
const name = req.params.name
|
2019-09-17 01:58:32 +00:00
|
|
|
if (!name) {
|
2019-09-22 05:20:37 +00:00
|
|
|
return res.status(400).send('Bad request.')
|
2019-09-17 01:58:32 +00:00
|
|
|
}
|
|
|
|
const db = req.app.get('db')
|
|
|
|
db.collection('streams')
|
|
|
|
.find({
|
|
|
|
type: 'Follow',
|
2019-09-22 05:20:37 +00:00
|
|
|
'_meta._target': pub.utils.usernameToIRI(name)
|
2019-09-17 01:58:32 +00:00
|
|
|
})
|
2019-09-22 05:20:37 +00:00
|
|
|
.project({ _id: 0, actor: 1 })
|
2019-09-17 01:58:32 +00:00
|
|
|
.toArray()
|
|
|
|
.then(follows => {
|
2019-09-21 21:20:14 +00:00
|
|
|
const followers = follows.map(pub.utils.actorFromActivity)
|
|
|
|
return res.json(pub.utils.arrayToCollection(followers))
|
2019-09-17 01:58:32 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err)
|
|
|
|
return res.status(500).send()
|
|
|
|
})
|
2019-09-22 05:20:37 +00:00
|
|
|
})
|
2019-03-26 04:26:17 +00:00
|
|
|
|
2019-09-22 05:20:37 +00:00
|
|
|
module.exports = router
|