From 0907932a0a67e611cb17da075227b20a7a15c874 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 28 Sep 2019 17:10:45 +0100 Subject: [PATCH] Move function --- posts.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ webinterface.py | 85 +------------------------------------------------ 2 files changed, 85 insertions(+), 84 deletions(-) diff --git a/posts.py b/posts.py index 229593c0e..5ba10582e 100644 --- a/posts.py +++ b/posts.py @@ -2187,3 +2187,87 @@ def populateRepliesJson(baseDir: str,nickname: str,domain: str, \ if authorized or \ 'https://www.w3.org/ns/activitystreams#Public' in postJsonObject['object']['to']: repliesJson['orderedItems'].append(postJsonObject) + +def downloadAnnounce(session,baseDir: str,httpPrefix: str,nickname: str,domain: str,postJsonObject: {},projectVersion: str) -> {}: + """Download the post referenced by an announce + """ + if not postJsonObject.get('object'): + return None + if not isinstance(postJsonObject['object'], str): + return None + + # get the announced post + announceCacheDir=baseDir+'/cache/announce/'+nickname + if not os.path.isdir(announceCacheDir): + os.mkdir(announceCacheDir) + announceFilename=announceCacheDir+'/'+postJsonObject['object'].replace('/','#')+'.json' + print('announceFilename: '+announceFilename) + + if os.path.isfile(announceFilename+'.reject'): + return None + + if os.path.isfile(announceFilename): + print('Reading cached Announce content for '+postJsonObject['object']) + try: + with open(announceFilename, 'r') as fp: + postJsonObject=commentjson.load(fp) + return postJsonObject + except Exception as e: + print(e) + else: + print('Downloading Announce content for '+postJsonObject['object']) + asHeader={'Accept': 'application/activity+json; profile="https://www.w3.org/ns/activitystreams"'} + actorNickname=getNicknameFromActor(postJsonObject['actor']) + actorDomain,actorPort=getDomainFromActor(postJsonObject['actor']) + announcedJson=getJson(session,postJsonObject['object'],asHeader,None,projectVersion,httpPrefix,domain) + + if not announcedJson: + return None + + if not announcedJson.get('id'): + rejectAnnounce(announceFilename) + pprint(announcedJson) + return None + if '/statuses/' not in announcedJson['id']: + rejectAnnounce(announceFilename) + return None + if '/users/' not in announcedJson['id'] and '/profile/' not in announcedJson['id']: + rejectAnnounce(announceFilename) + return None + if not announcedJson.get('type'): + rejectAnnounce(announceFilename) + pprint(announcedJson) + return None + if announcedJson['type']!='Note': + rejectAnnounce(announceFilename) + pprint(announcedJson) + return None + + # wrap in create to be consistent with other posts + announcedJson= \ + outboxMessageCreateWrap(httpPrefix, \ + actorNickname,actorDomain,actorPort, \ + announcedJson) + if announcedJson['type']!='Create': + rejectAnnounce(announceFilename) + pprint(announcedJson) + return None + + # set the id to the original status + announcedJson['id']=postJsonObject['object'] + announcedJson['object']['id']=postJsonObject['object'] + # check that the repeat isn't for a blocked account + attributedNickname=getNicknameFromActor(announcedJson['object']['id']) + attributedDomain,attributedPort=getDomainFromActor(announcedJson['object']['id']) + if attributedNickname and attributedDomain: + if attributedPort: + if attributedPort!=80 and attributedPort!=443: + attributedDomain=attributedDomain+':'+str(attributedPort) + if isBlocked(baseDir,nickname,domain,attributedNickname,attributedDomain): + rejectAnnounce(announceFilename) + return None + postJsonObject=announcedJson + with open(announceFilename, 'w') as fp: + commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) + return postJsonObject + return None diff --git a/webinterface.py b/webinterface.py index 887478f17..dfe2c451a 100644 --- a/webinterface.py +++ b/webinterface.py @@ -31,6 +31,7 @@ from posts import parseUserFeed from posts import populateRepliesJson from posts import isModerator from posts import outboxMessageCreateWrap +from posts import downloadAnnounce from session import getJson from auth import createPassword from like import likedByPerson @@ -1642,90 +1643,6 @@ def addEmojiToDisplayName(baseDir: str,httpPrefix: str, \ displayName=htmlReplaceEmojiFromTags(displayName,emojiTagsList,'profile') return displayName -def downloadAnnounce(session,baseDir: str,httpPrefix: str,nickname: str,domain: str,postJsonObject: {},projectVersion: str) -> {}: - """Download the post referenced by an announce - """ - if not postJsonObject.get('object'): - return None - if not isinstance(postJsonObject['object'], str): - return None - - # get the announced post - announceCacheDir=baseDir+'/cache/announce/'+nickname - if not os.path.isdir(announceCacheDir): - os.mkdir(announceCacheDir) - announceFilename=announceCacheDir+'/'+postJsonObject['object'].replace('/','#')+'.json' - print('announceFilename: '+announceFilename) - - if os.path.isfile(announceFilename+'.reject'): - return None - - if os.path.isfile(announceFilename): - print('Reading cached Announce content for '+postJsonObject['object']) - try: - with open(announceFilename, 'r') as fp: - postJsonObject=commentjson.load(fp) - return postJsonObject - except Exception as e: - print(e) - else: - print('Downloading Announce content for '+postJsonObject['object']) - asHeader={'Accept': 'application/activity+json; profile="https://www.w3.org/ns/activitystreams"'} - actorNickname=getNicknameFromActor(postJsonObject['actor']) - actorDomain,actorPort=getDomainFromActor(postJsonObject['actor']) - announcedJson=getJson(session,postJsonObject['object'],asHeader,None,projectVersion,httpPrefix,domain) - - if not announcedJson: - return None - - if not announcedJson.get('id'): - rejectAnnounce(announceFilename) - pprint(announcedJson) - return None - if '/statuses/' not in announcedJson['id']: - rejectAnnounce(announceFilename) - return None - if '/users/' not in announcedJson['id'] and '/profile/' not in announcedJson['id']: - rejectAnnounce(announceFilename) - return None - if not announcedJson.get('type'): - rejectAnnounce(announceFilename) - pprint(announcedJson) - return None - if announcedJson['type']!='Note': - rejectAnnounce(announceFilename) - pprint(announcedJson) - return None - - # wrap in create to be consistent with other posts - announcedJson= \ - outboxMessageCreateWrap(httpPrefix, \ - actorNickname,actorDomain,actorPort, \ - announcedJson) - if announcedJson['type']!='Create': - rejectAnnounce(announceFilename) - pprint(announcedJson) - return None - - # set the id to the original status - announcedJson['id']=postJsonObject['object'] - announcedJson['object']['id']=postJsonObject['object'] - # check that the repeat isn't for a blocked account - attributedNickname=getNicknameFromActor(announcedJson['object']['id']) - attributedDomain,attributedPort=getDomainFromActor(announcedJson['object']['id']) - if attributedNickname and attributedDomain: - if attributedPort: - if attributedPort!=80 and attributedPort!=443: - attributedDomain=attributedDomain+':'+str(attributedPort) - if isBlocked(baseDir,nickname,domain,attributedNickname,attributedDomain): - rejectAnnounce(announceFilename) - return None - postJsonObject=announcedJson - with open(announceFilename, 'w') as fp: - commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) - return postJsonObject - return None - def individualPostAsHtml(iconsDir: str,translate: {}, \ pageNumber: int,baseDir: str, \ session,wfRequest: {},personCache: {}, \