From 825fb7606623b37c0759ef81cd85da0867a179f6 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 16 Dec 2020 11:19:16 +0000 Subject: [PATCH] Tidying --- roles.py | 8 ++------ shares.py | 24 +++++------------------- skills.py | 7 ++----- tests.py | 7 ++----- webapp_confirm.py | 11 +++-------- webapp_person_options.py | 11 +++-------- webapp_post.py | 16 ++++------------ webapp_profile.py | 18 ++++-------------- webapp_search.py | 11 +++-------- webapp_timeline.py | 16 ++++------------ webfinger.py | 16 ++++------------ 11 files changed, 36 insertions(+), 109 deletions(-) diff --git a/roles.py b/roles.py index 22db327b..ecbf237b 100644 --- a/roles.py +++ b/roles.py @@ -11,6 +11,7 @@ from webfinger import webfingerHandle from auth import createBasicAuthHeader from posts import getPersonBox from session import postJson +from utils import getFullDomain from utils import getNicknameFromActor from utils import getDomainFromActor from utils import loadJson @@ -259,12 +260,7 @@ def sendRoleViaServer(baseDir: str, session, print('WARN: No session for sendRoleViaServer') return 6 - delegatorDomainFull = delegatorDomain - if delegatorPort: - if delegatorPort != 80 and delegatorPort != 443: - if ':' not in delegatorDomain: - delegatorDomainFull = \ - delegatorDomain + ':' + str(delegatorPort) + delegatorDomainFull = getFullDomain(delegatorDomain, delegatorPort) toUrl = \ httpPrefix + '://' + delegatorDomainFull + '/users/' + nickname diff --git a/shares.py b/shares.py index d4b19b36..1926395a 100644 --- a/shares.py +++ b/shares.py @@ -13,6 +13,7 @@ from auth import createBasicAuthHeader from posts import getPersonBox from session import postJson from session import postImage +from utils import getFullDomain from utils import validNickname from utils import loadJson from utils import saveJson @@ -115,11 +116,7 @@ def addShare(baseDir: str, imageFilename = sharesImageFilename + '.' + ext moveImage = True - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) # copy or move the image for the shared item to its destination if imageFilename: @@ -247,10 +244,7 @@ def getSharesFeedForPerson(baseDir: str, if not validNickname(domain, nickname): return None - if port: - if port != 80 and port != 443: - if ':' not in domain: - domain = domain + ':' + str(port) + domain = getFullDomain(domain, port) handleDomain = domain if ':' in handleDomain: @@ -331,11 +325,7 @@ def sendShareViaServer(baseDir, session, print('WARN: No session for sendShareViaServer') return 6 - fromDomainFull = fromDomain - if fromPort: - if fromPort != 80 and fromPort != 443: - if ':' not in fromDomain: - fromDomainFull = fromDomain + ':' + str(fromPort) + fromDomainFull = getFullDomain(fromDomain, fromPort) toUrl = 'https://www.w3.org/ns/activitystreams#Public' ccUrl = httpPrefix + '://' + fromDomainFull + \ @@ -439,11 +429,7 @@ def sendUndoShareViaServer(baseDir: str, session, print('WARN: No session for sendUndoShareViaServer') return 6 - fromDomainFull = fromDomain - if fromPort: - if fromPort != 80 and fromPort != 443: - if ':' not in fromDomain: - fromDomainFull = fromDomain + ':' + str(fromPort) + fromDomainFull = getFullDomain(fromDomain, fromPort) toUrl = 'https://www.w3.org/ns/activitystreams#Public' ccUrl = httpPrefix + '://' + fromDomainFull + \ diff --git a/skills.py b/skills.py index 175e6ab0..2b4930ff 100644 --- a/skills.py +++ b/skills.py @@ -11,6 +11,7 @@ from webfinger import webfingerHandle from auth import createBasicAuthHeader from posts import getPersonBox from session import postJson +from utils import getFullDomain from utils import getNicknameFromActor from utils import getDomainFromActor from utils import loadJson @@ -108,11 +109,7 @@ def sendSkillViaServer(baseDir: str, session, nickname: str, password: str, print('WARN: No session for sendSkillViaServer') return 6 - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) actor = httpPrefix + '://' + domainFull + '/users/' + nickname toUrl = actor diff --git a/tests.py b/tests.py index d23a0869..654f977b 100644 --- a/tests.py +++ b/tests.py @@ -33,6 +33,7 @@ from follow import clearFollows from follow import clearFollowers from follow import sendFollowRequestViaServer from follow import sendUnfollowRequestViaServer +from utils import getFullDomain from utils import validNickname from utils import firstParagraphFromString from utils import removeIdEnding @@ -127,11 +128,7 @@ def testHttpsigBase(withDigest): } messageBodyJsonStr = json.dumps(messageBodyJson) - headersDomain = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - headersDomain = domain + ':' + str(port) + headersDomain = getFullDomain(domain, port) dateStr = strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime()) boxpath = '/inbox' diff --git a/webapp_confirm.py b/webapp_confirm.py index a7396f9d..a0f87f74 100644 --- a/webapp_confirm.py +++ b/webapp_confirm.py @@ -8,6 +8,7 @@ __status__ = "Production" import os from shutil import copyfile +from utils import getFullDomain from utils import getNicknameFromActor from utils import getDomainFromActor from utils import locatePost @@ -35,10 +36,7 @@ def htmlConfirmDelete(cssCache: {}, actor = messageId.split('/statuses/')[0] nickname = getNicknameFromActor(actor) domain, port = getDomainFromActor(actor) - domainFull = domain - if port: - if port != 80 and port != 443: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) postFilename = locatePost(baseDir, nickname, domain, messageId) if not postFilename: @@ -103,10 +101,7 @@ def htmlConfirmRemoveSharedItem(cssCache: {}, translate: {}, baseDir: str, itemID = getValidSharedItemID(shareName) nickname = getNicknameFromActor(actor) domain, port = getDomainFromActor(actor) - domainFull = domain - if port: - if port != 80 and port != 443: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) sharesFile = baseDir + '/accounts/' + \ nickname + '@' + domain + '/shares.json' if not os.path.isfile(sharesFile): diff --git a/webapp_person_options.py b/webapp_person_options.py index b18d5460..e27189b5 100644 --- a/webapp_person_options.py +++ b/webapp_person_options.py @@ -11,6 +11,7 @@ from shutil import copyfile from petnames import getPetName from person import isPersonSnoozed from posts import isModerator +from utils import getFullDomain from utils import isDormant from utils import removeHtml from utils import getDomainFromActor @@ -45,10 +46,7 @@ def htmlPersonOptions(defaultTimeline: str, """Show options for a person: view/follow/block/report """ optionsDomain, optionsPort = getDomainFromActor(optionsActor) - optionsDomainFull = optionsDomain - if optionsPort: - if optionsPort != 80 and optionsPort != 443: - optionsDomainFull = optionsDomain + ':' + str(optionsPort) + optionsDomainFull = getFullDomain(optionsDomain, optionsPort) if os.path.isfile(baseDir + '/accounts/options-background-custom.jpg'): if not os.path.isfile(baseDir + '/accounts/options-background.jpg'): @@ -74,10 +72,7 @@ def htmlPersonOptions(defaultTimeline: str, dormantMonths) optionsNickname = getNicknameFromActor(optionsActor) - optionsDomainFull = optionsDomain - if optionsPort: - if optionsPort != 80 and optionsPort != 443: - optionsDomainFull = optionsDomain + ':' + str(optionsPort) + optionsDomainFull = getFullDomain(optionsDomain, optionsPort) if isBlocked(baseDir, nickname, domain, optionsNickname, optionsDomainFull): blockStr = 'Block' diff --git a/webapp_post.py b/webapp_post.py index e788ac6e..5ad02932 100644 --- a/webapp_post.py +++ b/webapp_post.py @@ -22,6 +22,7 @@ from posts import getPersonBox from posts import isDM from posts import downloadAnnounce from posts import populateRepliesJson +from utils import getFullDomain from utils import isEditor from utils import locatePost from utils import loadJson @@ -1135,11 +1136,7 @@ def individualPostAsHtml(allowDownloads: bool, if messageId: messageIdStr = ';' + messageId - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) pageNumberParam = '' if pageNumber: @@ -1604,19 +1601,14 @@ def htmlIndividualPost(cssCache: {}, if likedBy: likedByNickname = getNicknameFromActor(likedBy) likedByDomain, likedByPort = getDomainFromActor(likedBy) - if likedByPort: - if likedByPort != 80 and likedByPort != 443: - likedByDomain += ':' + str(likedByPort) + likedByDomain = getFullDomain(likedByDomain, likedByPort) likedByHandle = likedByNickname + '@' + likedByDomain postStr += \ '

' + translate['Liked by'] + \ ' @' + \ likedByHandle + '\n' - domainFull = domain - if port: - if port != 80 and port != 443: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) actor = '/users/' + nickname followStr = '

\n' diff --git a/webapp_profile.py b/webapp_profile.py index 21fdbc4e..c18c8794 100644 --- a/webapp_profile.py +++ b/webapp_profile.py @@ -8,6 +8,7 @@ __status__ = "Production" import os from pprint import pprint +from utils import getFullDomain from utils import isDormant from utils import getNicknameFromActor from utils import getDomainFromActor @@ -97,11 +98,7 @@ def htmlProfileAfterSearch(cssCache: {}, print('DEBUG: No domain found in ' + profileHandle) return None - searchDomainFull = searchDomain - if searchPort: - if searchPort != 80 and searchPort != 443: - if ':' not in searchDomain: - searchDomainFull = searchDomain + ':' + str(searchPort) + searchDomainFull = getFullDomain(searchDomain, searchPort) profileStr = '' cssFilename = baseDir + '/epicyon-profile.css' @@ -225,10 +222,7 @@ def htmlProfileAfterSearch(cssCache: {}, profileDescriptionShort, avatarUrl, imageUrl) - domainFull = domain - if port: - if port != 80 and port != 443: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) followIsPermitted = True if searchNickname == 'news' and searchDomainFull == domainFull: @@ -834,11 +828,7 @@ def htmlEditProfile(cssCache: {}, translate: {}, baseDir: str, path: str, nickname = getNicknameFromActor(path) if not nickname: return '' - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) actorFilename = \ baseDir + '/accounts/' + nickname + '@' + domain + '.json' diff --git a/webapp_search.py b/webapp_search.py index 13d8ea87..05b2fc7f 100644 --- a/webapp_search.py +++ b/webapp_search.py @@ -10,6 +10,7 @@ import os from shutil import copyfile import urllib.parse from datetime import datetime +from utils import getFullDomain from utils import isEditor from utils import loadJson from utils import getDomainFromActor @@ -544,10 +545,7 @@ def htmlHistorySearch(cssCache: {}, translate: {}, baseDir: str, htmlHeaderWithExternalStyle(cssFilename) # add the page title - domainFull = domain - if port: - if port != 80 and port != 443: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) actor = httpPrefix + '://' + domainFull + '/users/' + nickname historySearchForm += \ '

' + \ @@ -823,10 +821,7 @@ def rssHashtagSearch(nickname: str, domain: str, port: int, if not lines: return None - domainFull = domain - if port: - if port != 80 and port != 443: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) maxFeedLength = 10 hashtagFeed = \ diff --git a/webapp_timeline.py b/webapp_timeline.py index ca71c10d..84066b51 100644 --- a/webapp_timeline.py +++ b/webapp_timeline.py @@ -8,6 +8,7 @@ __status__ = "Production" import os import time +from utils import getFullDomain from utils import isEditor from utils import removeIdEnding from follow import followerApprovalActive @@ -191,10 +192,7 @@ def htmlTimeline(cssCache: {}, defaultTimeline: str, eventsButton = 'buttonselected' # get the full domain, including any port number - fullDomain = domain - if port != 80 and port != 443: - if ':' not in domain: - fullDomain = domain + ':' + str(port) + fullDomain = getFullDomain(domain, port) usersPath = '/users/' + nickname actor = httpPrefix + '://' + fullDomain + usersPath @@ -399,10 +397,7 @@ def htmlTimeline(cssCache: {}, defaultTimeline: str, tlStr += ' \n' tlStr += ' \n' - domainFull = domain - if port: - if port != 80 and port != 443: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) # left column leftColumnStr = \ @@ -655,10 +650,7 @@ def htmlSharesTimeline(translate: {}, pageNumber: int, itemsPerPage: int, sharesJson, lastPage = \ sharesTimelineJson(actor, pageNumber, itemsPerPage, baseDir, maxSharesPerAccount) - domainFull = domain - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) actor = httpPrefix + '://' + domainFull + '/users/' + nickname timelineStr = '' diff --git a/webfinger.py b/webfinger.py index 65b5666f..e95c8c7e 100644 --- a/webfinger.py +++ b/webfinger.py @@ -18,6 +18,7 @@ import urllib.parse from session import getJson from cache import storeWebfingerInCache from cache import getWebfingerFromCache +from utils import getFullDomain from utils import loadJson from utils import loadJsonOnionify from utils import saveJson @@ -113,10 +114,7 @@ def storeWebfingerEndpoint(nickname: str, domain: str, port: int, """Stores webfinger endpoint for a user to a file """ originalDomain = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domain = domain + ':' + str(port) + domain = getFullDomain(domain, port) handle = nickname + '@' + domain wfSubdir = '/wfendpoints' if not os.path.isdir(baseDir + wfSubdir): @@ -135,10 +133,7 @@ def createWebfingerEndpoint(nickname: str, domain: str, port: int, """Creates a webfinger endpoint for a user """ originalDomain = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domain = domain + ':' + str(port) + domain = getFullDomain(domain, port) personName = nickname personId = httpPrefix + "://" + domain + "/users/" + personName @@ -245,10 +240,7 @@ def webfingerLookup(path: str, baseDir: str, if debug: print('DEBUG: WEBFINGER no @ in handle ' + handle) return None - if port: - if port != 80 and port != 443: - if ':' not in handle: - handle = handle + ':' + str(port) + handle = getFullDomain(handle, port) # convert @domain@domain to inbox@domain if '@' in handle: handleDomain = handle.split('@')[1]