rubberguppe/routes/user.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict'
const express = require('express')
const router = express.Router()
2019-09-21 21:20:14 +00:00
const pub = require('../pub')
const net = require('../net')
2018-09-15 07:01:19 +00:00
router.get('/:name', net.validators.jsonld, 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.')
2018-09-15 07:01:19 +00:00
}
2019-09-28 15:23:24 +00:00
console.log('User json ', name)
pub.actor.getOrCreateActor(name)
.then(group => {
return res.json(pub.utils.toJSONLD(group))
})
.catch(err => {
2019-10-01 01:55:20 +00:00
console.log(err.message)
res.status(500).send(`Error creating group ${name}`)
})
})
// HTML version
router.get('/:name', function (req, res) {
const name = req.params.name
if (!name) {
return res.status(400).send('Bad request.')
}
2019-09-28 15:23:24 +00:00
console.log('User html ', name)
pub.actor.getOrCreateActor(name)
.then(group => {
const groupAcct = `${group.preferredUsername}@${req.app.get('domain')}`
return res.render('group.html', { group, groupAcct })
})
.catch(err => {
2019-10-01 01:55:20 +00:00
console.log(err.message)
res.status(500).send(`Error creating group ${name}`)
})
2019-09-22 05:20:37 +00:00
})
2018-09-15 07:01:19 +00:00
router.get('/:name/followers', net.validators.jsonld, function (req, res) {
2019-09-22 05:20:37 +00:00
const name = req.params.name
if (!name) {
2019-09-22 05:20:37 +00:00
return res.status(400).send('Bad request.')
}
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-22 05:20:37 +00:00
.project({ _id: 0, actor: 1 })
.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))
})
.catch(err => {
2019-10-01 01:55:20 +00:00
console.log(err.message)
return res.status(500).send()
})
2019-09-22 05:20:37 +00:00
})
2019-09-22 05:20:37 +00:00
module.exports = router