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')
|
2019-09-28 03:17:54 +00:00
|
|
|
const net = require('../net')
|
2018-09-15 07:01:19 +00:00
|
|
|
|
2019-11-18 01:31:58 +00:00
|
|
|
// list active groups
|
|
|
|
router.get('/', net.validators.jsonld, function (req, res) {
|
|
|
|
const db = req.app.get('db')
|
|
|
|
db.collection('streams')
|
|
|
|
.aggregate([
|
2021-01-21 04:19:03 +00:00
|
|
|
{ $sort: { _id: -1 } }, // start from most recent
|
2019-11-18 01:31:58 +00:00
|
|
|
{ $limit: 10000 }, // don't traverse the entire history
|
|
|
|
{ $match: { type: 'Announce' } },
|
|
|
|
{ $group: { _id: '$actor', postCount: { $sum: 1 } } },
|
|
|
|
{ $lookup: { from: 'objects', localField: '_id', foreignField: 'id', as: 'actor' } },
|
|
|
|
// merge joined actor up
|
|
|
|
{ $replaceRoot: { newRoot: { $mergeObjects: [{ $arrayElemAt: ['$actor', 0] }, '$$ROOT'] } } },
|
2019-11-18 01:46:41 +00:00
|
|
|
{ $project: { _id: 0, _meta: 0, actor: 0 } }
|
2019-11-18 01:31:58 +00:00
|
|
|
])
|
|
|
|
.sort({ postCount: -1 })
|
2021-01-21 04:19:03 +00:00
|
|
|
.limit(Number.parseInt(req.query.n) || 50)
|
2019-11-18 01:31:58 +00:00
|
|
|
.toArray()
|
|
|
|
.then(groups => { console.log(JSON.stringify(groups)); return groups })
|
|
|
|
.then(groups => res.json(groups))
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err.message)
|
|
|
|
return res.status(500).send()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-09-28 03:17:54 +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)
|
2019-09-28 03:17:54 +00:00
|
|
|
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)
|
2019-09-28 03:17:54 +00:00
|
|
|
res.status(500).send(`Error creating group ${name}`)
|
|
|
|
})
|
|
|
|
})
|
2018-09-15 07:01:19 +00:00
|
|
|
|
2019-09-28 03:17:54 +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
|
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 => {
|
2019-10-01 01:55:20 +00:00
|
|
|
console.log(err.message)
|
2019-09-17 01:58:32 +00:00
|
|
|
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
|