Check post recency before creating speaker endpoint

merge-requests/30/head
Bob Mottram 2021-03-03 20:16:53 +00:00
parent 19a9fe0970
commit a9f5831db9
3 changed files with 58 additions and 22 deletions

View File

@ -11,6 +11,7 @@ import os
import datetime import datetime
import time import time
from linked_data_sig import verifyJsonSignature from linked_data_sig import verifyJsonSignature
from utils import isRecentPost
from utils import getConfigParam from utils import getConfigParam
from utils import hasUsersPath from utils import hasUsersPath
from utils import validPostDate from utils import validPostDate
@ -1404,14 +1405,15 @@ def _receiveAnnounce(recentPostsCache: {},
if '/statuses/' in lookupActor: if '/statuses/' in lookupActor:
lookupActor = lookupActor.split('/statuses/')[0] lookupActor = lookupActor.split('/statuses/')[0]
if not os.path.isfile(postFilename + '.tts'): if isRecentPost(postJsonObject):
updateSpeaker(baseDir, nickname, domain, if not os.path.isfile(postFilename + '.tts'):
postJsonObject, personCache, updateSpeaker(baseDir, nickname, domain,
translate, lookupActor) postJsonObject, personCache,
ttsFile = open(postFilename + '.tts', "w+") translate, lookupActor)
if ttsFile: ttsFile = open(postFilename + '.tts', "w+")
ttsFile.write('\n') if ttsFile:
ttsFile.close() ttsFile.write('\n')
ttsFile.close()
if debug: if debug:
print('DEBUG: Obtaining actor for announce post ' + print('DEBUG: Obtaining actor for announce post ' +
@ -2481,9 +2483,10 @@ def _inboxAfterInitial(recentPostsCache: {}, maxRecentPosts: int,
print('ERROR: unable to update ' + boxname + ' index') print('ERROR: unable to update ' + boxname + ' index')
else: else:
if boxname == 'inbox': if boxname == 'inbox':
updateSpeaker(baseDir, nickname, domain, if isRecentPost(postJsonObject):
postJsonObject, personCache, updateSpeaker(baseDir, nickname, domain,
translate, None) postJsonObject, personCache,
translate, None)
if not unitTest: if not unitTest:
if debug: if debug:
print('Saving inbox post as html to cache') print('Saving inbox post as html to cache')

View File

@ -1990,3 +1990,33 @@ def mediaFileMimeType(filename: str) -> str:
if not extensions.get(fileExt): if not extensions.get(fileExt):
return 'image/png' return 'image/png'
return extensions[fileExt] return extensions[fileExt]
def isRecentPost(postJsonObject: {}, maxDays=3) -> bool:
""" Is the given post recent?
"""
if not postJsonObject.get('object'):
return False
if not isinstance(postJsonObject['object'], dict):
return False
if not postJsonObject['object'].get('published'):
return False
if not isinstance(postJsonObject['object']['published'], str):
return False
currTime = datetime.datetime.utcnow()
daysSinceEpoch = (currTime - datetime.datetime(1970, 1, 1)).days
recently = daysSinceEpoch - maxDays
publishedDateStr = postJsonObject['object']['published']
try:
publishedDate = \
datetime.datetime.strptime(publishedDateStr,
"%Y-%m-%dT%H:%M:%SZ")
except BaseException:
return False
publishedDaysSinceEpoch = \
(publishedDate - datetime.datetime(1970, 1, 1)).days
if publishedDaysSinceEpoch < recently:
return False
return True

View File

@ -22,6 +22,7 @@ from posts import getPersonBox
from posts import isDM from posts import isDM
from posts import downloadAnnounce from posts import downloadAnnounce
from posts import populateRepliesJson from posts import populateRepliesJson
from utils import isRecentPost
from utils import getConfigParam from utils import getConfigParam
from utils import getFullDomain from utils import getFullDomain
from utils import isEditor from utils import isEditor
@ -1292,17 +1293,19 @@ def individualPostAsHtml(allowDownloads: bool,
return '' return ''
postJsonObject = postJsonAnnounce postJsonObject = postJsonAnnounce
announceFilename = \ if isRecentPost(postJsonObject):
locatePost(baseDir, nickname, domain, postJsonObject['id']) announceFilename = \
if announceFilename and postJsonObject.get('actor'): locatePost(baseDir, nickname, domain,
if not os.path.isfile(announceFilename + '.tts'): postJsonObject['id'])
updateSpeaker(baseDir, nickname, domain, if announceFilename and postJsonObject.get('actor'):
postJsonObject, personCache, if not os.path.isfile(announceFilename + '.tts'):
translate, postJsonObject['actor']) updateSpeaker(baseDir, nickname, domain,
ttsFile = open(announceFilename + '.tts', "w+") postJsonObject, personCache,
if ttsFile: translate, postJsonObject['actor'])
ttsFile.write('\n') ttsFile = open(announceFilename + '.tts', "w+")
ttsFile.close() if ttsFile:
ttsFile.write('\n')
ttsFile.close()
isAnnounced = True isAnnounced = True