diff --git a/src/routes/well-known.js b/src/routes/well-known.js index ac54a1c210..6d7f61c577 100644 --- a/src/routes/well-known.js +++ b/src/routes/well-known.js @@ -1,9 +1,76 @@ 'use strict'; +const nconf = require('nconf'); +const meta = require('../meta'); +const db = require('../database'); + module.exports = function (app, middleware, controllers) { + const url = nconf.get('url'); + const version = nconf.get('version'); + app.use('/.well-known/change-password', (req, res) => { res.redirect('/me/edit/password'); }); app.get('/.well-known/webfinger', controllers['well-known'].webfinger); + + app.get('/.well-known/nodeinfo', (req, res) => { + res.json({ + links: [ + { + rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0', + href: `${url}/nodeinfo/2.0`, + }, + ], + }); + }); + + app.get('/nodeinfo/2.0', async (req, res) => { + const getDaysInMonth = (year, month) => new Date(year, month, 0).getDate(); + + function addMonths(input, months) { + const date = new Date(input); + date.setDate(1); + date.setMonth(date.getMonth() + months); + date.setDate(Math.min(input.getDate(), getDaysInMonth(date.getFullYear(), date.getMonth() + 1))); + return date; + } + + const oneMonthAgo = addMonths(new Date(), -1); + const sixMonthsAgo = addMonths(new Date(), -6); + + const [{ postCount, userCount }, activeMonth, activeHalfyear] = await Promise.all([ + db.getObjectFields('global', ['userCount', 'userCount']), + db.sortedSetCount('users:online', oneMonthAgo.getTime(), '+inf'), + db.sortedSetCount('users:online', sixMonthsAgo.getTime(), '+inf'), + ]); + + res.json({ + version: '2.0', + software: { + name: 'NodeBB', + version: version, + }, + protocols: [ + 'activitypub', + ], + services: { + outbound: [], + inbound: [], + }, + usage: { + users: { + total: userCount, + activeMonth: activeMonth, + activeHalfyear: activeHalfyear, + }, + localPosts: postCount, + }, + openRegistrations: meta.config.registrationType === 'normal', + metadata: { + nodeName: meta.config.title || 'NodeBB', + nodeDescription: meta.config.description || '', + }, + }); + }); };