From 3469850199ff98fba01be0b6bef021fddd4b2cf4 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 1 Aug 2021 20:19:45 +0100 Subject: [PATCH] Fix removal of post interactions --- daemon.py | 2 +- posts.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- tests.py | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 7 deletions(-) diff --git a/daemon.py b/daemon.py index c427c61cc..543d365ff 100644 --- a/daemon.py +++ b/daemon.py @@ -9214,7 +9214,7 @@ class PubServer(BaseHTTPRequestHandler): proxyType: str, cookie: str, debug: str) -> bool: """Shows the outbox timeline - """ + """ # get outbox feed for a person outboxFeed = \ personBoxJson(self.server.recentPostsCache, diff --git a/posts.py b/posts.py index 84a819697..e3c3f63ec 100644 --- a/posts.py +++ b/posts.py @@ -1488,6 +1488,37 @@ def jsonPinPost(baseDir: str, httpPrefix: str, } +def regenerateIndexForBox(baseDir: str, + nickname: str, domain: str, boxName: str) -> None: + """Generates an index for the given box if it doesn't exist + Used by unit tests to artificially create an index + """ + boxDir = acctDir(baseDir, nickname, domain) + '/' + boxName + boxIndexFilename = boxDir + '.index' + + if not os.path.isdir(boxDir): + return + if os.path.isfile(boxIndexFilename): + return + + indexLines = [] + for subdir, dirs, files in os.walk(boxDir): + for f in files: + if ':##' not in f: + continue + indexLines.append(f) + break + + indexLines.sort(reverse=True) + + result = '' + with open(boxIndexFilename, 'w+') as fp: + for line in indexLines: + result += line + '\n' + fp.write(line + '\n') + print('Index generated for ' + boxName + '\n' + result) + + def createPublicPost(baseDir: str, nickname: str, domain: str, port: int, httpPrefix: str, content: str, followersOnly: bool, saveToFile: bool, @@ -3164,7 +3195,7 @@ def removePostInteractions(postJsonObject: {}, force: bool) -> bool: if not force: # If not authorized and it's a private post # then just don't show it within timelines - if not isPublicPost(postObj): + if not isPublicPost(postJsonObject): return False else: postObj = postJsonObject @@ -3267,6 +3298,7 @@ def _createBoxIndexed(recentPostsCache: {}, indexBoxName = boxname timelineNickname = 'news' + originalDomain = domain domain = getFullDomain(domain, port) boxActor = httpPrefix + '://' + domain + '/users/' + nickname @@ -3300,7 +3332,7 @@ def _createBoxIndexed(recentPostsCache: {}, postsInBox = [] indexFilename = \ - baseDir + '/accounts/' + timelineNickname + '@' + domain + \ + acctDir(baseDir, timelineNickname, originalDomain) + \ '/' + indexBoxName + '.index' totalPostsCount = 0 postsAddedToTimeline = 0 @@ -3342,22 +3374,29 @@ def _createBoxIndexed(recentPostsCache: {}, if postUrl in recentPostsCache['index']: if recentPostsCache['json'].get(postUrl): url = recentPostsCache['json'][postUrl] + print('Adding post to timeline: ' + + boxname + ' ' + url) if _addPostStringToTimeline(url, boxname, postsInBox, boxActor): totalPostsCount += 1 postsAddedToTimeline += 1 continue + else: + print('Post not added to timeline') + print('postsInBox: ' + str(len(postsInBox))) # read the post from file fullPostFilename = \ locatePost(baseDir, nickname, - domain, postUrl, False) + originalDomain, postUrl, False) if fullPostFilename: # has the post been rejected? if os.path.isfile(fullPostFilename + '.reject'): continue + print('Adding post to timeline: ' + + boxname + ' ' + fullPostFilename) if _addPostToTimeline(fullPostFilename, boxname, postsInBox, boxActor): postsAddedToTimeline += 1 @@ -3366,13 +3405,16 @@ def _createBoxIndexed(recentPostsCache: {}, print('WARN: Unable to add post ' + postUrl + ' nickname ' + nickname + ' timeline ' + boxname) + print('postsInBox: ' + str(len(postsInBox))) else: if timelineNickname != nickname: # if this is the features timeline fullPostFilename = \ locatePost(baseDir, timelineNickname, - domain, postUrl, False) + originalDomain, postUrl, False) if fullPostFilename: + print('Adding post to timeline: ' + + boxname + ' ' + fullPostFilename) if _addPostToTimeline(fullPostFilename, boxname, postsInBox, boxActor): postsAddedToTimeline += 1 @@ -3381,6 +3423,7 @@ def _createBoxIndexed(recentPostsCache: {}, print('WARN: Unable to add features post ' + postUrl + ' nickname ' + nickname + ' timeline ' + boxname) + print('postsInBox: ' + str(len(postsInBox))) else: print('WARN: features timeline. ' + 'Unable to locate post ' + postUrl) diff --git a/tests.py b/tests.py index 1dff6159e..49ab34cbf 100644 --- a/tests.py +++ b/tests.py @@ -24,6 +24,8 @@ from cache import getPersonFromCache from threads import threadWithTrace from daemon import runDaemon from session import createSession +from session import getJson +from posts import regenerateIndexForBox from posts import removePostInteractions from posts import getMentionedPeople from posts import validContentWarning @@ -547,6 +549,7 @@ def createServerAlice(path: str, domain: str, port: int, testSubject, testSchedulePost, testEventDate, testEventTime, testLocation, testIsArticle, systemLanguage) + regenerateIndexForBox(path, nickname, domain, 'outbox') global testServerAliceRunning testServerAliceRunning = True maxMentions = 10 @@ -675,6 +678,7 @@ def createServerBob(path: str, domain: str, port: int, testSubject, testSchedulePost, testEventDate, testEventTime, testLocation, testIsArticle, systemLanguage) + regenerateIndexForBox(path, nickname, domain, 'outbox') global testServerBobRunning testServerBobRunning = True maxMentions = 10 @@ -1343,7 +1347,7 @@ def testGroupFollow(): aliceDomain = '127.0.0.57' alicePort = 61927 aliceSendThreads = [] - # aliceAddress = aliceDomain + ':' + str(alicePort) + aliceAddress = aliceDomain + ':' + str(alicePort) bobDir = baseDir + '/.tests/bob' bobDomain = '127.0.0.59' @@ -1368,7 +1372,7 @@ def testGroupFollow(): threadWithTrace(target=createServerAlice, args=(aliceDir, aliceDomain, alicePort, testgroupAddress, - federationList, False, False, + federationList, False, True, aliceSendThreads), daemon=True) @@ -1422,6 +1426,35 @@ def testGroupFollow(): assert ctr <= 60 time.sleep(1) + print('*********************************************************') + print('Alice has some outbox posts') + aliceOutbox = 'http://' + aliceAddress + '/users/alice/outbox' + session = createSession(None) + profileStr = 'https://www.w3.org/ns/activitystreams' + asHeader = { + 'Accept': 'application/ld+json; profile="' + profileStr + '"' + } + outboxJson = getJson(session, aliceOutbox, asHeader, None, + True, __version__, 'http', None) + assert outboxJson + pprint(outboxJson) + assert outboxJson['type'] == 'OrderedCollection' + assert 'first' in outboxJson + firstPage = outboxJson['first'] + assert 'totalItems' in outboxJson + print('Alice outbox totalItems: ' + str(outboxJson['totalItems'])) + assert outboxJson['totalItems'] == 3 + + outboxJson = getJson(session, firstPage, asHeader, None, + True, __version__, 'http', None) + assert outboxJson + pprint(outboxJson) + assert 'orderedItems' in outboxJson + assert outboxJson['type'] == 'OrderedCollectionPage' + print('Alice outbox orderedItems: ' + + str(len(outboxJson['orderedItems']))) + assert len(outboxJson['orderedItems']) == 3 + queuePath = \ testgroupDir + '/accounts/testgroup@' + testgroupDomain + '/queue' @@ -1618,6 +1651,7 @@ def testGroupFollow(): assert aliceMessageArrived is True print('\n\n*********************************************************') print('Post from Alice to test group succeeded') + print('\n\n*********************************************************') print('Check that post was relayed from test group to bob') @@ -4267,6 +4301,7 @@ def _testRemovePostInteractions() -> None: assert postJsonObject['object']['shares'] == {} assert postJsonObject['object']['bookmarks'] == {} assert postJsonObject['object']['ignores'] == {} + postJsonObject['object']['to'] = ["some private address"] assert not removePostInteractions(postJsonObject, False)