rubberguppe/routes/user.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

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