From 1b2e1befc3c466edd525265732a38b3f13ee6db7 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 6 Dec 2021 19:54:47 +0000 Subject: [PATCH] Improve consolidation of mentions during creation of new posts --- posts.py | 36 ++++++++++++++++++++++++++++++++++-- tests.py | 17 +++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/posts.py b/posts.py index 35944c002..21179a06e 100644 --- a/posts.py +++ b/posts.py @@ -32,6 +32,7 @@ from webfinger import webfingerHandle from httpsig import createSignedHeader from siteactive import siteIsActive from languages import understoodPostLanguage +from utils import getUserPaths from utils import invalidCiphertext from utils import hasObjectStringType from utils import removeIdEnding @@ -1259,6 +1260,29 @@ def _createPostPlaceAndTime(eventDate: str, endDate: str, return eventDateStr +def _consolidateActorsList(actorsList: []) -> None: + """ consolidate duplicated actors + https://domain/@nick gets merged with https://domain/users/nick + """ + possibleDuplicateActors = [] + for ccActor in actorsList: + if '/@' in ccActor: + if ccActor not in possibleDuplicateActors: + possibleDuplicateActors.append(ccActor) + if possibleDuplicateActors: + uPaths = getUserPaths() + removeActors = [] + for ccActor in possibleDuplicateActors: + for usrPath in uPaths: + ccActorFull = ccActor.replace('/@', usrPath) + if ccActorFull in actorsList: + if ccActor not in removeActors: + removeActors.append(ccActor) + break + for ccActor in removeActors: + actorsList.remove(ccActor) + + def _createPostMentions(ccUrl: str, newPost: {}, toRecipients: [], tags: []) -> None: """Updates mentions for a new post @@ -1267,9 +1291,10 @@ def _createPostMentions(ccUrl: str, newPost: {}, return if len(ccUrl) == 0: return - newPost['cc'] = [ccUrl] + if newPost.get('object'): - newPost['object']['cc'] = [ccUrl] + if ccUrl not in newPost['object']['cc']: + newPost['object']['cc'] = [ccUrl] + newPost['object']['cc'] # if this is a public post then include any mentions in cc toCC = newPost['object']['cc'] @@ -1283,6 +1308,13 @@ def _createPostMentions(ccUrl: str, newPost: {}, if tag['href'] not in toCC: newPost['object']['cc'].append(tag['href']) + _consolidateActorsList(newPost['object']['cc']) + newPost['cc'] = newPost['object']['cc'] + else: + if ccUrl not in newPost['cc']: + newPost['cc'] = [ccUrl] + newPost['cc'] + _consolidateActorsList(['cc']) + def _createPostModReport(baseDir: str, isModerationReport: bool, newPost: {}, diff --git a/tests.py b/tests.py index 998813595..34b3122cf 100644 --- a/tests.py +++ b/tests.py @@ -4240,6 +4240,7 @@ def _testReplyToPublicPost(baseDir: str) -> None: mediaType = None imageDescription = 'Some description' city = 'London, England' + testInReplyTo = postId testInReplyToAtomUri = None testSubject = None testSchedulePost = False @@ -4255,7 +4256,7 @@ def _testReplyToPublicPost(baseDir: str) -> None: content, followersOnly, saveToFile, clientToServer, commentsEnabled, attachImageFilename, mediaType, - imageDescription, city, postId, + imageDescription, city, testInReplyTo, testInReplyToAtomUri, testSubject, testSchedulePost, testEventDate, testEventTime, testLocation, @@ -4276,8 +4277,20 @@ def _testReplyToPublicPost(baseDir: str) -> None: assert len(reply['object']['cc']) >= 1 assert reply['object']['cc'][0].endswith(nickname + '/followers') assert len(reply['object']['tag']) == 1 + if len(reply['object']['cc']) != 2: + print('reply["object"]["cc"]: ' + str(reply['object']['cc'])) assert len(reply['object']['cc']) == 2 - assert reply['object']['cc'][1] == httpPrefix + '://rat.site/@ninjarodent' + assert reply['object']['cc'][1] == \ + httpPrefix + '://rat.site/users/ninjarodent' + + assert len(reply['to']) == 1 + assert reply['to'][0].endswith('#Public') + assert len(reply['cc']) >= 1 + assert reply['cc'][0].endswith(nickname + '/followers') + if len(reply['cc']) != 2: + print('reply["cc"]: ' + str(reply['cc'])) + assert len(reply['cc']) == 2 + assert reply['cc'][1] == httpPrefix + '://rat.site/users/ninjarodent' def _getFunctionCallArgs(name: str, lines: [], startLineCtr: int) -> []: