Devices endpoint

main
Bob Mottram 2020-08-05 13:05:39 +01:00
parent c682c759ba
commit a5bcf1a356
2 changed files with 58 additions and 0 deletions

View File

@ -192,6 +192,7 @@ from bookmarks import undoBookmark
from petnames import setPetName from petnames import setPetName
from followingCalendar import addPersonToCalendar from followingCalendar import addPersonToCalendar
from followingCalendar import removePersonFromCalendar from followingCalendar import removePersonFromCalendar
from devices import devicesCollection
import os import os
@ -1536,6 +1537,24 @@ class PubServer(BaseHTTPRequestHandler):
self._404() self._404()
return return
# list of registered devices for e2ee
# see https://github.com/tootsuite/mastodon/pull/13820
if not htmlGET and authorized and '/users/' in self.path:
if self.path.endswith('/collections/devices'):
nickname = self.path.split('/users/')
if '/' in nickname:
nickname = nickname.split('/')[0]
devJson = devicesCollection(self.server.baseDir,
nickname, self.server.domain,
self.server.domainFull,
self.server.httpPrefix)
msg = json.dumps(devJson,
ensure_ascii=False).encode('utf-8')
self._set_headers('application/json',
len(msg),
None, callingDomain)
self._write(msg)
if htmlGET and '/users/' in self.path: if htmlGET and '/users/' in self.path:
# show the person options screen with view/follow/block/report # show the person options screen with view/follow/block/report
if '?options=' in self.path: if '?options=' in self.path:

39
devices.py 100644
View File

@ -0,0 +1,39 @@
__filename__ = "devices.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
import os
from utils import loadJson
def devicesCollection(baseDir: str, nickname: str, domain: str,
domainFull: str, httpPrefix: str) -> {}:
"""Returns a list of registered devices
"""
personDir = baseDir + '/accounts/' + nickname + '@' + domain
if not os.path.isdir(personDir):
return {}
personId = httpPrefix + '://' + domainFull + '/users/' + nickname
if not os.path.isdir(personDir + '/devices'):
os.mkdir(personDir + '/devices')
deviceList = []
for subdir, dirs, files in os.walk(personDir + '/devices/'):
for dev in files:
if not dev.endswith('.json'):
continue
deviceFilename = os.path.join(personDir + '/devices', dev)
devJson = loadJson(deviceFilename)
if devJson:
deviceList.append(devJson)
devicesDict = {
'id': personId + '/collections/devices',
'type': 'Collection',
'totalItems': len(deviceList),
'items': deviceList
}
return devicesDict