From 0e4d504c50abf25f8b694ba63e82067841c8d883 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 22 Jan 2021 13:32:37 +0000 Subject: [PATCH] Mastodon Id is a number --- daemon.py | 22 +++++++--------------- mastoapiv1.py | 38 +++++++++++++++++++++++++++++++++++++- tests.py | 14 ++++++++++++++ 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/daemon.py b/daemon.py index f2e7ee1e6..4e850f2a0 100644 --- a/daemon.py +++ b/daemon.py @@ -26,6 +26,8 @@ from webfinger import webfingerNodeInfo from webfinger import webfingerLookup from webfinger import webfingerUpdate from mastoapiv1 import getMastoApiV1Account +from mastoapiv1 import getMastApiV1Id +from mastoapiv1 import getNicknameFromMastoApiV1Id from metadata import metaDataInstance from metadata import metaDataNodeInfo from pgp import getEmailAddress @@ -805,21 +807,11 @@ class PubServer(BaseHTTPRequestHandler): sendJsonStr = 'masto API account sent for ' + nickname # Parts of the api which don't need authorization - idStr = httpPrefix + '://' + domainFull + '/users/' - idPath = '/api/v1/accounts/:' + idStr - pathNickname = None - if path.startswith(idPath): - pathNickname = path.replace(idPath, '') - if '/' in pathNickname: - pathNickname = pathNickname.split('/')[0] - if '?' in pathNickname: - pathNickname = pathNickname.split('?')[0] - sendJson = getMastoApiV1Account(baseDir, pathNickname, domain) - sendJsonStr = 'masto API account sent for ' + nickname - if nickname: - if path.startswith(idPath) or \ - path == '/api/v1/accounts/verify_credentials': - sendJson = getMastoApiV1Account(baseDir, nickname, domain) + mastoId = getMastApiV1Id(path) + if mastoId is not None: + pathNickname = getNicknameFromMastoApiV1Id(mastoId) + if pathNickname: + sendJson = getMastoApiV1Account(baseDir, pathNickname, domain) sendJsonStr = 'masto API account sent for ' + nickname adminNickname = getConfigParam(self.server.baseDir, 'admin') diff --git a/mastoapiv1.py b/mastoapiv1.py index 015548800..87881d92c 100644 --- a/mastoapiv1.py +++ b/mastoapiv1.py @@ -10,6 +10,42 @@ import os from utils import loadJson +def getMastApiV1Id(path: str) -> int: + """Extracts the mastodon Id number from the given path + """ + mastoId = None + idPath = '/api/v1/accounts/:' + if not path.startswith(idPath): + return None + mastoIdStr = path.replace(idPath, '') + if '/' in mastoIdStr: + mastoIdStr = mastoIdStr.split('/')[0] + if mastoIdStr.isdigit(): + mastoId = int(mastoIdStr) + return mastoId + return None + + +def getMastoApiV1IdFromNickname(nickname: str) -> int: + """Given an account nickname return the corresponding mastodon id + """ + return int.from_bytes(nickname.encode('utf-8'), 'little') + + +def _intToBytes(num: int) -> str: + if num == 0: + return b"" + else: + return _intToBytes(num // 256) + bytes([num % 256]) + + +def getNicknameFromMastoApiV1Id(mastoId: int) -> str: + """Given the mastodon Id return the nickname + """ + nickname = _intToBytes(mastoId).decode() + return nickname[::-1] + + def getMastoApiV1Account(baseDir: str, nickname: str, domain: str) -> {}: """See https://github.com/McKael/mastodon-documentation/ blob/master/Using-the-API/API.md#account @@ -23,7 +59,7 @@ def getMastoApiV1Account(baseDir: str, nickname: str, domain: str) -> {}: if not accountJson: return {} mastoAccountJson = { - "id": accountJson['id'], + "id": getMastoApiV1IdFromNickname(nickname), "username": nickname, "acct": nickname, "display_name": accountJson['preferredUsername'], diff --git a/tests.py b/tests.py index b9121b1ef..2bebb1f2c 100644 --- a/tests.py +++ b/tests.py @@ -92,6 +92,8 @@ from newsdaemon import hashtagRuleTree from newsdaemon import hashtagRuleResolve from newswire import getNewswireTags from newswire import parseFeedDate +from mastoapiv1 import getMastoApiV1IdFromNickname +from mastoapiv1 import getNicknameFromMastoApiV1Id testServerAliceRunning = False testServerBobRunning = False @@ -3046,9 +3048,21 @@ def testLinksWithinPost() -> None: assert postJsonObject['object']['content'] == content +def testMastoApi(): + print('testMastoApi') + nickname = 'ThisIsATestNickname' + mastoId = getMastoApiV1IdFromNickname(nickname) + assert(mastoId) + nickname2 = getNicknameFromMastoApiV1Id(mastoId) + if nickname2 != nickname: + print(nickname + ' != ' + nickname2) + assert nickname2 == nickname + + def runAllTests(): print('Running tests...') testFunctions() + testMastoApi() testLinksWithinPost() testReplyToPublicPost() testGetMentionedPeople()