Replace nickname in cached html

merge-requests/10/merge
Bob Mottram 2021-02-02 21:08:33 +00:00
parent 9f21353a7c
commit 1af83ed1aa
4 changed files with 63 additions and 4 deletions

View File

@ -1831,11 +1831,16 @@ def threadSendPost(session, postJsonStr: str, federationList: [],
for attempt in range(20):
postResult = None
unauthorized = False
if debug:
print('Getting postJsonString for ' + inboxUrl)
try:
postResult, unauthorized = \
postJsonString(session, postJsonStr, federationList,
inboxUrl, signatureHeaderJson,
debug)
if debug:
print('Obtained postJsonString for ' + inboxUrl +
' unauthorized: ' + str(unauthorized))
except Exception as e:
print('ERROR: postJsonString failed ' + str(e))
if unauthorized:

View File

@ -95,6 +95,7 @@ from newswire import getNewswireTags
from newswire import parseFeedDate
from mastoapiv1 import getMastoApiV1IdFromNickname
from mastoapiv1 import getNicknameFromMastoApiV1Id
from webapp_post import prepareHtmlPostNickname
testServerAliceRunning = False
testServerBobRunning = False
@ -3072,9 +3073,25 @@ def testDomainHandling():
assert decodedHost(testDomain) == "españa.icom.museum"
def testPrepareHtmlPostNickname():
print('testPrepareHtmlPostNickname')
postHtml = '<a class="imageAnchor" href="/users/bob?replyfollowers='
postHtml += '<a class="imageAnchor" href="/users/bob?repeatprivate='
result = prepareHtmlPostNickname('alice', postHtml)
assert result == postHtml.replace('/bob?', '/alice?')
postHtml = '<a class="imageAnchor" href="/users/bob?replyfollowers='
postHtml += '<a class="imageAnchor" href="/users/bob;repeatprivate='
expectedHtml = '<a class="imageAnchor" href="/users/alice?replyfollowers='
expectedHtml += '<a class="imageAnchor" href="/users/bob;repeatprivate='
result = prepareHtmlPostNickname('alice', postHtml)
assert result == expectedHtml
def runAllTests():
print('Running tests...')
testFunctions()
testPrepareHtmlPostNickname()
testDomainHandling()
testMastoApi()
testLinksWithinPost()

View File

@ -75,7 +75,42 @@ def _logPostTiming(enableTimingLog: bool, postStartTime, debugId: str) -> None:
print('TIMING INDIV ' + debugId + ' = ' + str(timeDiff))
def preparePostFromHtmlCache(postHtml: str, boxName: str,
def prepareHtmlPostNickname(nickname: str, postHtml: str) -> str:
"""Replaces links on html post icons, such as Reply, Repeat
relative to the given nickname
href="/users/sally? becomes href="/users/nickname?
"""
# replace the nickname
usersStr = ' href="/users/'
if usersStr not in postHtml:
return postHtml
userFound = True
postStr = postHtml
newPostStr = ''
while userFound:
if usersStr not in postStr:
newPostStr += postStr
break
# the next part, after href="/users/nickname?
nextStr = postStr.split(usersStr, 1)[1]
if '?' in nextStr:
nextStr = nextStr.split('?', 1)[1]
else:
newPostStr += postStr
break
# append the previous text to the result
newPostStr += postStr.split(usersStr)[0]
newPostStr += usersStr + nickname + '?'
# post is now the next part
postStr = nextStr
return newPostStr
def preparePostFromHtmlCache(nickname: str, postHtml: str, boxName: str,
pageNumber: int) -> str:
"""Sets the page number on a cached html post
"""
@ -91,7 +126,7 @@ def preparePostFromHtmlCache(postHtml: str, boxName: str,
withPageNumber = postHtml.replace(';-999;', ';' + str(pageNumber) + ';')
withPageNumber = withPageNumber.replace('?page=-999',
'?page=' + str(pageNumber))
return withPageNumber
return prepareHtmlPostNickname(nickname, withPageNumber)
def _saveIndividualPostAsHtmlToCache(baseDir: str,
@ -173,7 +208,8 @@ def _getPostFromRecentCache(session,
if not postHtml:
return None
postHtml = preparePostFromHtmlCache(postHtml, boxName, pageNumber)
postHtml = \
preparePostFromHtmlCache(nickname, postHtml, boxName, pageNumber)
updateRecentPostsCache(recentPostsCache, maxRecentPosts,
postJsonObject, postHtml)
_logPostTiming(enableTimingLog, postStartTime, '3')

View File

@ -563,7 +563,8 @@ def htmlTimeline(cssCache: {}, defaultTimeline: str,
if recentPostsCache['html'].get(postId):
currTlStr = recentPostsCache['html'][postId]
currTlStr = \
preparePostFromHtmlCache(currTlStr,
preparePostFromHtmlCache(nickname,
currTlStr,
boxName,
pageNumber)
_logTimelineTiming(enableTimingLog,