Fix GIT_REV on dokku
[relay.git/.git] / relay / nodeinfo.py
1 import os
2 import subprocess
3 import urllib.parse
4
5 import aiohttp.web
6
7 from . import app
8 from .database import DATABASE
9
10
11 try:
12     commit_label = subprocess.check_output(["git", "rev-parse", "HEAD"]).strip().decode('ascii')
13 except:
14     commit_label = os.environ.get('GIT_REV')
15
16
17 nodeinfo_template = {
18     # XXX - is this valid for a relay?
19     'openRegistrations': True,
20     'protocols': ['activitypub'],
21     'services': {
22         'inbound': [],
23         'outbound': []
24     },
25     'software': {
26         'name': 'activityrelay',
27         'version': '0.1 {}'.format(commit_label)
28     },
29     'usage': {
30         'localPosts': 0,
31         'users': {
32             'total': 1
33         }
34     },
35     'version': '2.0'
36 }
37
38
39 def get_peers():
40     global DATABASE
41
42     return [urllib.parse.urlsplit(inbox).hostname for inbox in DATABASE.get('relay-list', [])]
43
44
45 async def nodeinfo_2_0(request):
46     data = nodeinfo_template.copy()
47     data['metadata'] = {
48         'peers': get_peers()
49     }
50     return aiohttp.web.json_response(data)
51
52
53 app.router.add_get('/nodeinfo/2.0.json', nodeinfo_2_0)
54
55
56 async def nodeinfo_wellknown(request):
57     data = {
58         'links': [
59              {
60                  'rel': 'http://nodeinfo.diaspora.software/ns/schema/2.0',
61                  'href': 'https://{}/nodeinfo/2.0.json'.format(request.host)
62              }
63         ]
64     }
65     return aiohttp.web.json_response(data)
66
67
68 app.router.add_get('/.well-known/nodeinfo', nodeinfo_wellknown)