Tidying of masto api

merge-requests/30/head
Bob Mottram 2021-01-22 11:54:48 +00:00
parent 0ff65b483a
commit 97e6c68f0f
2 changed files with 25 additions and 54 deletions

View File

@ -793,24 +793,17 @@ class PubServer(BaseHTTPRequestHandler):
return False return False
if self.server.debug: if self.server.debug:
print('DEBUG: mastodon api v1 ' + path) print('DEBUG: mastodon api v1 ' + path)
sendJson = None
sendJsonStr = ''
# authorized parts of the api
if authorized and nickname: if authorized and nickname:
if path == '/api/v1/accounts/:id': if path == '/api/v1/accounts/:id':
acctJson = getMastoApiV1Account(baseDir, nickname, domain) sendJson = getMastoApiV1Account(baseDir, nickname, domain)
msg = json.dumps(instanceJson).encode('utf-8') sendJsonStr = 'masto API account sent for ' + nickname
msglen = len(msg)
if self._hasAccept(callingDomain): # Parts of the api which don't need authorization
if 'application/ld+json' in self.headers['Accept']:
self._set_headers('application/ld+json', msglen,
None, callingDomain)
else:
self._set_headers('application/json', msglen,
None, callingDomain)
else:
self._set_headers('application/ld+json', msglen,
None, callingDomain)
self._write(msg)
print('masto API account sent for ' + nickname)
return True
adminNickname = getConfigParam(self.server.baseDir, 'admin') adminNickname = getConfigParam(self.server.baseDir, 'admin')
if adminNickname and path == '/api/v1/instance': if adminNickname and path == '/api/v1/instance':
instanceDescriptionShort = \ instanceDescriptionShort = \
@ -823,7 +816,7 @@ class PubServer(BaseHTTPRequestHandler):
'instanceDescription') 'instanceDescription')
instanceTitle = getConfigParam(self.server.baseDir, instanceTitle = getConfigParam(self.server.baseDir,
'instanceTitle') 'instanceTitle')
instanceJson = \ sendJson = \
metaDataInstance(instanceTitle, metaDataInstance(instanceTitle,
instanceDescriptionShort, instanceDescriptionShort,
instanceDescription, instanceDescription,
@ -835,29 +828,21 @@ class PubServer(BaseHTTPRequestHandler):
self.server.registration, self.server.registration,
self.server.systemLanguage, self.server.systemLanguage,
self.server.projectVersion) self.server.projectVersion)
msg = json.dumps(instanceJson).encode('utf-8') sendJsonStr = 'masto API instance metadata sent'
msglen = len(msg) elif path.startswith('/api/v1/instance/peers'):
if self._hasAccept(callingDomain):
if 'application/ld+json' in self.headers['Accept']:
self._set_headers('application/ld+json', msglen,
None, callingDomain)
else:
self._set_headers('application/json', msglen,
None, callingDomain)
else:
self._set_headers('application/ld+json', msglen,
None, callingDomain)
self._write(msg)
print('instance metadata sent')
return True
if path.startswith('/api/v1/instance/peers'):
# This is just a dummy result. # This is just a dummy result.
# Showing the full list of peers would have privacy implications. # Showing the full list of peers would have privacy implications.
# On a large instance you are somewhat lost in the crowd, but on # On a large instance you are somewhat lost in the crowd, but on
# small instances a full list of peers would convey a lot of # small instances a full list of peers would convey a lot of
# information about the interests of a small number of accounts # information about the interests of a small number of accounts
msg = json.dumps(['mastodon.social', sendJson = ['mastodon.social', self.server.domainFull]
self.server.domainFull]).encode('utf-8') sendJsonStr = 'masto API peers metadata sent'
elif path.startswith('/api/v1/instance/activity'):
sendJson = []
sendJsonStr = 'masto API activity metadata sent'
if sendJson is not None:
msg = json.dumps(sendJson).encode('utf-8')
msglen = len(msg) msglen = len(msg)
if self._hasAccept(callingDomain): if self._hasAccept(callingDomain):
if 'application/ld+json' in self.headers['Accept']: if 'application/ld+json' in self.headers['Accept']:
@ -870,25 +855,11 @@ class PubServer(BaseHTTPRequestHandler):
self._set_headers('application/ld+json', msglen, self._set_headers('application/ld+json', msglen,
None, callingDomain) None, callingDomain)
self._write(msg) self._write(msg)
print('instance peers metadata sent') if sendJsonStr:
return True print(sendJsonStr)
if path.startswith('/api/v1/instance/activity'):
# This is just a dummy result.
msg = json.dumps([]).encode('utf-8')
msglen = len(msg)
if self._hasAccept(callingDomain):
if 'application/ld+json' in self.headers['Accept']:
self._set_headers('application/ld+json', msglen,
None, callingDomain)
else:
self._set_headers('application/json', msglen,
None, callingDomain)
else:
self._set_headers('application/ld+json', msglen,
None, callingDomain)
self._write(msg)
print('instance activity metadata sent')
return True return True
# no api endpoints were matched
self._404() self._404()
return True return True

View File

@ -28,7 +28,7 @@ def getMastoApiV1Account(baseDir: str, nickname: str, domain: str) -> {}:
"acct": nickname, "acct": nickname,
"display_name": accountJson['preferredUsername'], "display_name": accountJson['preferredUsername'],
"locked": accountJson['manuallyApprovesFollowers'], "locked": accountJson['manuallyApprovesFollowers'],
# "created_at": "", "created_at": "",
"followers_count": 0, "followers_count": 0,
"following_count": 0, "following_count": 0,
"statuses_count": 0, "statuses_count": 0,