Mastodon Id is a number

main
Bob Mottram 2021-01-22 13:32:37 +00:00
parent 7309a237ae
commit 0e4d504c50
3 changed files with 58 additions and 16 deletions

View File

@ -26,6 +26,8 @@ from webfinger import webfingerNodeInfo
from webfinger import webfingerLookup from webfinger import webfingerLookup
from webfinger import webfingerUpdate from webfinger import webfingerUpdate
from mastoapiv1 import getMastoApiV1Account from mastoapiv1 import getMastoApiV1Account
from mastoapiv1 import getMastApiV1Id
from mastoapiv1 import getNicknameFromMastoApiV1Id
from metadata import metaDataInstance from metadata import metaDataInstance
from metadata import metaDataNodeInfo from metadata import metaDataNodeInfo
from pgp import getEmailAddress from pgp import getEmailAddress
@ -805,22 +807,12 @@ class PubServer(BaseHTTPRequestHandler):
sendJsonStr = 'masto API account sent for ' + nickname sendJsonStr = 'masto API account sent for ' + nickname
# Parts of the api which don't need authorization # Parts of the api which don't need authorization
idStr = httpPrefix + '://' + domainFull + '/users/' mastoId = getMastApiV1Id(path)
idPath = '/api/v1/accounts/:' + idStr if mastoId is not None:
pathNickname = None pathNickname = getNicknameFromMastoApiV1Id(mastoId)
if path.startswith(idPath): if pathNickname:
pathNickname = path.replace(idPath, '')
if '/' in pathNickname:
pathNickname = pathNickname.split('/')[0]
if '?' in pathNickname:
pathNickname = pathNickname.split('?')[0]
sendJson = getMastoApiV1Account(baseDir, pathNickname, domain) sendJson = getMastoApiV1Account(baseDir, pathNickname, domain)
sendJsonStr = 'masto API account sent for ' + nickname 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)
sendJsonStr = 'masto API account sent for ' + nickname
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':

View File

@ -10,6 +10,42 @@ import os
from utils import loadJson 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) -> {}: def getMastoApiV1Account(baseDir: str, nickname: str, domain: str) -> {}:
"""See https://github.com/McKael/mastodon-documentation/ """See https://github.com/McKael/mastodon-documentation/
blob/master/Using-the-API/API.md#account blob/master/Using-the-API/API.md#account
@ -23,7 +59,7 @@ def getMastoApiV1Account(baseDir: str, nickname: str, domain: str) -> {}:
if not accountJson: if not accountJson:
return {} return {}
mastoAccountJson = { mastoAccountJson = {
"id": accountJson['id'], "id": getMastoApiV1IdFromNickname(nickname),
"username": nickname, "username": nickname,
"acct": nickname, "acct": nickname,
"display_name": accountJson['preferredUsername'], "display_name": accountJson['preferredUsername'],

View File

@ -92,6 +92,8 @@ from newsdaemon import hashtagRuleTree
from newsdaemon import hashtagRuleResolve from newsdaemon import hashtagRuleResolve
from newswire import getNewswireTags from newswire import getNewswireTags
from newswire import parseFeedDate from newswire import parseFeedDate
from mastoapiv1 import getMastoApiV1IdFromNickname
from mastoapiv1 import getNicknameFromMastoApiV1Id
testServerAliceRunning = False testServerAliceRunning = False
testServerBobRunning = False testServerBobRunning = False
@ -3046,9 +3048,21 @@ def testLinksWithinPost() -> None:
assert postJsonObject['object']['content'] == content 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(): def runAllTests():
print('Running tests...') print('Running tests...')
testFunctions() testFunctions()
testMastoApi()
testLinksWithinPost() testLinksWithinPost()
testReplyToPublicPost() testReplyToPublicPost()
testGetMentionedPeople() testGetMentionedPeople()