Fix status number using sequence data

main2
Bob Mottram 2019-10-12 13:45:53 +01:00
parent 78bb91505a
commit b0be557047
2 changed files with 18 additions and 1 deletions

View File

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

View File

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