2020-02-01 08:58:04 +00:00
const semver = require ( "semver" )
2020-01-28 06:23:53 +00:00
const child _process = require ( "child_process" )
2020-02-01 08:58:04 +00:00
const md = require ( "mixin-deep" )
const constants = require ( "../../lib/constants" )
2020-01-30 12:51:59 +00:00
const { history } = require ( "../../lib/collectors" )
2020-02-02 15:09:40 +00:00
const switcher = require ( "../../lib/utils/torswitcher" )
2020-01-30 12:51:59 +00:00
const { redirect } = require ( "pinski/plugins" )
2020-01-12 12:50:21 +00:00
function reply ( statusCode , content ) {
return {
statusCode : statusCode ,
contentType : "application/json" ,
content : JSON . stringify ( content )
}
}
2020-02-01 08:58:04 +00:00
// Load current commit hash
2020-01-28 06:23:53 +00:00
let commit = ""
{
const p = child _process . spawn ( "git" , [ "rev-parse" , "--short" , "HEAD" ] )
p . on ( "error" , console . error )
p . stdout . on ( "data" , data => {
const string = data . toString ( )
commit = "-" + string . match ( /[0-9a-f]+/ ) [ 0 ]
} )
}
2020-02-01 08:58:04 +00:00
// Set up inner versioning
2020-02-02 15:09:40 +00:00
const displayVersions = [ "1.0" , "1.1" , "1.2" , "1.3" ]
2020-02-01 08:58:04 +00:00
const versions = new Map ( displayVersions . map ( v => [ v , semver . coerce ( v ) ] ) )
const features = [
"PAGE_PROFILE" ,
"PAGE_POST" ,
"API_STATS" ,
2020-02-01 09:12:42 +00:00
"API_STATS_SEMVER" ,
2020-02-01 08:58:04 +00:00
"PAGE_HOME" ,
"API_INSTANCES" ,
"BLOCK_DETECT_USER_HTML"
]
2020-02-05 10:14:52 +00:00
if ( constants . has _privacy _policy ) features . push ( "PRIVACY_POLICY" )
2020-04-12 15:44:31 +00:00
if ( constants . as _assistant . enabled ) {
if ( constants . as _assistant . require _key ) features . push ( "ASSISTANT_PRIVATE" )
else features . push ( "ASSISTANT_PUBLIC" )
}
2020-02-01 08:58:04 +00:00
const innerMap = new Map ( )
{
const addVersion = function ( shortVersion , block ) {
const coerced = semver . coerce ( shortVersion )
const previousVersion = semver . maxSatisfying ( [ ... innerMap . keys ( ) ] , coerced . major + ".x" )
let previous = { }
if ( previousVersion ) previous = innerMap . get ( previousVersion )
md ( block , previous )
md ( block , { version : shortVersion } )
innerMap . set ( coerced . version , block )
}
addVersion ( "1.0" , {
features
} )
addVersion ( "1.1" , {
availableVersions : [ ... versions . keys ( ) ] ,
history : history . export ( )
} )
addVersion ( "1.2" , {
settings : {
2020-05-01 02:37:27 +00:00
rssEnabled : constants . feeds . enabled
2020-02-01 08:58:04 +00:00
}
} )
2020-02-02 15:09:40 +00:00
addVersion ( "1.3" , {
settings : {
torAvailable : switcher . canUseTor ( ) // server.js holds on loading this file until tor state is known, so this is fine
}
} )
2020-02-01 08:58:04 +00:00
}
2020-01-12 12:50:21 +00:00
module . exports = [
2020-01-28 05:39:07 +00:00
{
route : "/.well-known/nodeinfo" , methods : [ "GET" ] , code : async ( { fill } ) => {
return reply ( 200 , {
link : [
{
rel : "http://nodeinfo.diaspora.software/ns/schema/2.0" ,
href : ` ${ constants . website _origin } /api/stats/2.0 `
}
]
} )
}
} ,
{
2020-01-30 12:51:59 +00:00
route : "/api/stats" , methods : [ "GET" ] , code : async ( ) => {
return redirect ( "/api/stats/2.0" , 302 )
}
} ,
{
route : "/api/stats/2.0" , methods : [ "GET" ] , code : async ( { url } ) => {
2020-02-01 08:58:04 +00:00
const selected = semver . maxSatisfying ( [ ... innerMap . keys ( ) ] , url . searchParams . get ( "bv" ) || "1.0" )
if ( ! selected ) return reply ( 400 , {
2020-01-30 12:51:59 +00:00
status : "fail" ,
fields : [ "q:bv" ] ,
2020-02-01 08:58:04 +00:00
message : "query parameter `bv` selects inner version, must be either missing or a semver query matching any of " + displayVersions . map ( v => "`" + v + "`" ) . join ( ", " ) + "."
2020-01-30 12:51:59 +00:00
} )
2020-02-01 08:58:04 +00:00
const inner = innerMap . get ( selected )
2020-01-28 05:39:07 +00:00
return reply ( 200 , {
version : "2.0" ,
software : {
name : "bibliogram" ,
2020-01-28 06:23:53 +00:00
version : "1.0.0" + commit
2020-01-28 05:39:07 +00:00
} ,
protocols : [ ] ,
services : {
inbound : [
"instagram"
] ,
outbound : [ ]
} ,
openRegistrations : false ,
usage : {
users : {
total : 0 ,
activeHalfyear : 0 ,
activeMonth : 0
}
} ,
metadata : {
2020-01-30 12:51:59 +00:00
bibliogram : inner
2020-01-28 05:39:07 +00:00
}
} )
}
}
2020-01-12 12:50:21 +00:00
]