Mastodon Id is a number

merge-requests/9/merge
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 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')

View File

@ -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'],

View File

@ -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()