From b0be557047f243a0d20c5744634d30249c583e88 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 12 Oct 2019 13:45:53 +0100 Subject: [PATCH] Fix status number using sequence data --- tests.py | 12 ++++++++++++ utils.py | 7 ++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tests.py b/tests.py index 8c0f4065..b3b84682 100644 --- a/tests.py +++ b/tests.py @@ -41,6 +41,7 @@ from utils import followPerson from utils import getNicknameFromActor from utils import getDomainFromActor from utils import copytree +from utils import getStatusNumber from follow import followerOfPerson from follow import unfollowPerson from follow import unfollowerOfPerson @@ -1426,8 +1427,19 @@ def testAddEmoji(): os.chdir(baseDirOriginal) shutil.rmtree(baseDirOriginal+'/.tests') +def testGetStatusNumber(): + print('testGetStatusNumber') + prevStatusNumber=None + for i in range(1,20): + statusNumber,published = getStatusNumber() + if prevStatusNumber: + assert len(statusNumber) == 18 + assert int(statusNumber) > prevStatusNumber + prevStatusNumber=int(statusNumber) + def runAllTests(): print('Running tests...') + testGetStatusNumber() testAddEmoji() testWebLinks() testActorParsing() diff --git a/utils.py b/utils.py index 992d609f..05085a69 100644 --- a/utils.py +++ b/utils.py @@ -18,7 +18,12 @@ def getStatusNumber() -> (str,str): currTime=datetime.datetime.utcnow() daysSinceEpoch=(currTime - datetime.datetime(1970,1,1)).days # status is the number of seconds since epoch - statusNumber=str(((daysSinceEpoch*24*60*60) + (currTime.hour*60*60) + (currTime.minute*60) + currTime.second)*1000000 + currTime.microsecond) + statusNumber=str(((daysSinceEpoch*24*60*60) + (currTime.hour*60*60) + (currTime.minute*60) + currTime.second)*1000 + int(currTime.microsecond/1000)) + # See https://github.com/tootsuite/mastodon/blob/995f8b389a66ab76ec92d9a240de376f1fc13a38/lib/mastodon/snowflake.rb + # use the leftover microseconds as the sequence number + sequenceId=currTime.microsecond % 1000 + # shift by 16bits "sequence data" + statusNumber=str((int(statusNumber)<<16)+sequenceId) published=currTime.strftime("%Y-%m-%dT%H:%M:%SZ") return statusNumber,published