From 6ec956e3129f42893c9b842b10e46f06580b7162 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 16 Dec 2020 11:04:46 +0000 Subject: [PATCH] Tidying --- like.py | 31 +++---------- media.py | 6 +-- newsdaemon.py | 11 ++--- outbox.py | 5 +- person.py | 29 +++--------- posts.py | 125 ++++++++++---------------------------------------- 6 files changed, 42 insertions(+), 165 deletions(-) diff --git a/like.py b/like.py index 4a16d0fe..6076c729 100644 --- a/like.py +++ b/like.py @@ -6,6 +6,7 @@ __maintainer__ = "Bob Mottram" __email__ = "bob@freedombone.net" __status__ = "Production" +from utils import getFullDomain from utils import removeIdEnding from utils import urlPermitted from utils import getNicknameFromActor @@ -66,11 +67,7 @@ def like(recentPostsCache: {}, if not urlPermitted(objectUrl, federationList): return None - fullDomain = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - fullDomain = domain + ':' + str(port) + fullDomain = getFullDomain(domain, port) newLikeJson = { "@context": "https://www.w3.org/ns/activitystreams", @@ -132,11 +129,7 @@ def likePost(recentPostsCache: {}, debug: bool, projectVersion: str) -> {}: """Likes a given status post. This is only used by unit tests """ - likeDomain = likeDomain - if likePort: - if likePort != 80 and likePort != 443: - if ':' not in likeDomain: - likeDomain = likeDomain + ':' + str(likePort) + likeDomain = getFullDomain(likeDomain, likePort) actorLiked = httpPrefix + '://' + likeDomain + '/users/' + likeNickname objectUrl = actorLiked + '/statuses/' + str(likeStatusNumber) @@ -165,11 +158,7 @@ def undolike(recentPostsCache: {}, if not urlPermitted(objectUrl, federationList): return None - fullDomain = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - fullDomain = domain + ':' + str(port) + fullDomain = getFullDomain(domain, port) newUndoLikeJson = { "@context": "https://www.w3.org/ns/activitystreams", @@ -234,11 +223,7 @@ def sendLikeViaServer(baseDir: str, session, print('WARN: No session for sendLikeViaServer') return 6 - fromDomainFull = fromDomain - if fromPort: - if fromPort != 80 and fromPort != 443: - if ':' not in fromDomain: - fromDomainFull = fromDomain + ':' + str(fromPort) + fromDomainFull = getFullDomain(fromDomain, fromPort) actor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname @@ -313,11 +298,7 @@ def sendUndoLikeViaServer(baseDir: str, session, print('WARN: No session for sendUndoLikeViaServer') return 6 - fromDomainFull = fromDomain - if fromPort: - if fromPort != 80 and fromPort != 443: - if ':' not in fromDomain: - fromDomainFull = fromDomain + ':' + str(fromPort) + fromDomainFull = getFullDomain(fromDomain, fromPort) actor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname diff --git a/media.py b/media.py index df6409f7..b7c0b849 100644 --- a/media.py +++ b/media.py @@ -13,6 +13,7 @@ import os import datetime from hashlib import sha1 from auth import createPassword +from utils import getFullDomain from utils import getImageExtensions from utils import getVideoExtensions from utils import getAudioExtensions @@ -163,10 +164,7 @@ def attachMedia(baseDir: str, httpPrefix: str, domain: str, port: int, if mediaType == 'audio/mpeg': fileExtension = 'mp3' - if port: - if port != 80 and port != 443: - if ':' not in domain: - domain = domain + ':' + str(port) + domain = getFullDomain(domain, port) mPath = getMediaPath() mediaPath = mPath + '/' + createPassword(32) + '.' + fileExtension diff --git a/newsdaemon.py b/newsdaemon.py index 74ee3382..42a69994 100644 --- a/newsdaemon.py +++ b/newsdaemon.py @@ -26,6 +26,7 @@ from posts import archivePostsForPerson from content import removeHtmlTag from content import dangerousMarkup from content import validHashTag +from utils import getFullDomain from utils import loadJson from utils import saveJson from utils import getStatusNumber @@ -245,10 +246,7 @@ def newswireHashtagProcessing(session, baseDir: str, postJsonObject: {}, with open(rulesFilename, "r") as f: rules = f.readlines() - domainFull = domain - if port: - if port != 80 and port != 443: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) # get the full text content of the post content = '' @@ -584,10 +582,7 @@ def convertRSStoActivityPub(baseDir: str, httpPrefix: str, blog['object']['content'] = rssDescription blog['object']['contentMap']['en'] = rssDescription - domainFull = domain - if port: - if port != 80 and port != 443: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) hashtags = item[6] diff --git a/outbox.py b/outbox.py index 141745bb..8da5d9b3 100644 --- a/outbox.py +++ b/outbox.py @@ -14,6 +14,7 @@ from posts import outboxMessageCreateWrap from posts import savePostToBox from posts import sendToFollowersThread from posts import sendToNamedAddresses +from utils import getFullDomain from utils import removeIdEnding from utils import getDomainFromActor from blocking import isBlockedDomain @@ -113,9 +114,7 @@ def postMessageToOutbox(messageJson: {}, postToNickname: str, str(messageJson)) return False testDomain, testPort = getDomainFromActor(messageJson['actor']) - if testPort: - if testPort != 80 and testPort != 443: - testDomain = testDomain + ':' + str(testPort) + testDomain = getFullDomain(testDomain, testPort) if isBlockedDomain(baseDir, testDomain): if debug: print('DEBUG: domain is blocked: ' + messageJson['actor']) diff --git a/person.py b/person.py index 86f0fe6e..216ae096 100644 --- a/person.py +++ b/person.py @@ -35,6 +35,7 @@ from auth import storeBasicCredentials from auth import removePassword from roles import setRole from media import removeMetaData +from utils import getFullDomain from utils import validNickname from utils import loadJson from utils import saveJson @@ -68,11 +69,7 @@ def setProfileImage(baseDir: str, httpPrefix: str, nickname: str, domain: str, if ':' in domain: domain = domain.split(':')[0] - fullDomain = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - fullDomain = domain + ':' + str(port) + fullDomain = getFullDomain(domain, port) handle = nickname + '@' + domain personFilename = baseDir + '/accounts/' + handle + '.json' @@ -213,10 +210,7 @@ def createPersonBase(baseDir: str, nickname: str, domain: str, port: int, handle = nickname + '@' + domain originalDomain = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domain = domain + ':' + str(port) + domain = getFullDomain(domain, port) personType = 'Person' # Enable follower approval by default @@ -418,10 +412,7 @@ def savePersonQrcode(baseDir: str, nickname + '@' + domain + '/qrcode.png' if os.path.isfile(qrcodeFilename): return - handle = '@' + nickname + '@' + domain - if port: - if port != 80 and port != 443: - handle = handle + ':' + str(port) + handle = getFullDomain('@' + nickname + '@' + domain, port) url = pyqrcode.create(handle) url.png(qrcodeFilename, scale) @@ -868,11 +859,7 @@ def canRemovePost(baseDir: str, nickname: str, if '/statuses/' not in postId: return False - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) # is the post by the admin? adminNickname = getConfigParam(baseDir, 'admin') @@ -898,11 +885,7 @@ def removeTagsForNickname(baseDir: str, nickname: str, """ if not os.path.isdir(baseDir + '/tags'): return - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) matchStr = domainFull + '/users/' + nickname + '/' directory = os.fsencode(baseDir + '/tags/') for f in os.scandir(directory): diff --git a/posts.py b/posts.py index b35c0505..27baf1eb 100644 --- a/posts.py +++ b/posts.py @@ -30,6 +30,7 @@ from session import postJsonString from session import postImage from webfinger import webfingerHandle from httpsig import createSignedHeader +from utils import getFullDomain from utils import getFollowersList from utils import isEvil from utils import removeIdEnding @@ -740,10 +741,7 @@ def createPostBase(baseDir: str, nickname: str, domain: str, port: int, tags = [] hashtagsDict = {} - if port: - if port != 80 and port != 443: - if ':' not in domain: - domain = domain + ':' + str(port) + domain = getFullDomain(domain, port) # add tags if nickname != 'news': @@ -1070,10 +1068,7 @@ def outboxMessageCreateWrap(httpPrefix: str, https://www.w3.org/TR/activitypub/#object-without-create """ - if port: - if port != 80 and port != 443: - if ':' not in domain: - domain = domain + ':' + str(port) + domain = getFullDomain(domain, port) statusNumber, published = getStatusNumber() if messageJson.get('published'): published = messageJson['published'] @@ -1108,10 +1103,7 @@ def postIsAddressedToFollowers(baseDir: str, postJsonObject: {}) -> bool: """Returns true if the given post is addressed to followers of the nickname """ - if port: - if port != 80 and port != 443: - if ':' not in domain: - domain = domain + ':' + str(port) + domain = getFullDomain(domain, port) if not postJsonObject.get('object'): return False @@ -1174,11 +1166,7 @@ def createPublicPost(baseDir: str, eventDate=None, eventTime=None, location=None) -> {}: """Public post """ - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) return createPostBase(baseDir, nickname, domain, port, 'https://www.w3.org/ns/activitystreams#Public', httpPrefix + '://' + domainFull + '/users/' + @@ -1278,11 +1266,7 @@ def createQuestionPost(baseDir: str, subject: str, durationDays: int) -> {}: """Question post with multiple choice options """ - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) messageJson = \ createPostBase(baseDir, nickname, domain, port, 'https://www.w3.org/ns/activitystreams#Public', @@ -1328,11 +1312,7 @@ def createUnlistedPost(baseDir: str, eventDate=None, eventTime=None, location=None) -> {}: """Unlisted post. This has the #Public and followers links inverted. """ - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) return createPostBase(baseDir, nickname, domain, port, httpPrefix + '://' + domainFull + '/users/' + nickname + '/followers', @@ -1361,11 +1341,7 @@ def createFollowersOnlyPost(baseDir: str, location=None) -> {}: """Followers only post """ - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) return createPostBase(baseDir, nickname, domain, port, httpPrefix + '://' + domainFull + '/users/' + nickname + '/followers', @@ -1404,11 +1380,7 @@ def createEventPost(baseDir: str, if not category: print('Event has no category') return None - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) # create event uuid eventUUID = str(uuid.uuid1()) @@ -1524,11 +1496,7 @@ def createReportPost(baseDir: str, debug: bool, subject=None) -> {}: """Send a report to moderators """ - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) # add a title to distinguish moderation reports from other posts reportTitle = 'Moderation Report' @@ -1698,10 +1666,7 @@ def sendPost(projectVersion: str, # shared inbox actor on @domain@domain toNickname = toDomain - if toPort: - if toPort != 80 and toPort != 443: - if ':' not in toDomain: - toDomain = toDomain + ':' + str(toPort) + toDomain = getFullDomain(toDomain, toPort) handle = httpPrefix + '://' + toDomain + '/@' + toNickname @@ -1863,11 +1828,7 @@ def sendPostViaServer(projectVersion: str, clientToServer = True if toDomain.lower().endswith('public'): toPersonId = 'https://www.w3.org/ns/activitystreams#Public' - fromDomainFull = fromDomain - if fromPort: - if fromPort != 80 and fromPort != 443: - if ':' not in fromDomain: - fromDomainFull = fromDomain + ':' + str(fromPort) + fromDomainFull = getFullDomain(fromDomain, fromPort) cc = httpPrefix + '://' + fromDomainFull + '/users/' + \ fromNickname + '/followers' else: @@ -1877,11 +1838,7 @@ def sendPostViaServer(projectVersion: str, httpPrefix + '://' + \ fromDomainFull + '/users/' + fromNickname + '/followers' else: - toDomainFull = toDomain - if toPort: - if toPort != 80 and toPort != 443: - if ':' not in toDomain: - toDomainFull = toDomain + ':' + str(toPort) + toDomainFull = getFullDomain(toDomain, toPort) toPersonId = httpPrefix + '://' + toDomainFull + \ '/users/' + toNickname @@ -2010,10 +1967,7 @@ def sendSignedJson(postJsonObject: {}, session, baseDir: str, toNickname = toDomain # sharedInbox = True - if toPort: - if toPort != 80 and toPort != 443: - if ':' not in toDomain: - toDomain = toDomain + ':' + str(toPort) + toDomain = getFullDomain(toDomain, toPort) toDomainUrl = httpPrefix + '://' + toDomain if not siteIsActive(toDomainUrl): @@ -2290,16 +2244,8 @@ def sendToNamedAddresses(session, baseDir: str, if not toDomain: continue if debug: - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) - toDomainFull = toDomain - if toPort: - if toPort != 80 and toPort != 443: - if ':' not in toDomain: - toDomainFull = toDomain + ':' + str(toPort) + domainFull = getFullDomain(domain, port) + toDomainFull = getFullDomain(toDomain, toPort) print('DEBUG: Post sending s2s: ' + nickname + '@' + domainFull + ' to ' + toNickname + '@' + toDomainFull) @@ -2618,10 +2564,7 @@ def createModeration(baseDir: str, nickname: str, domain: str, port: int, boxDir = createPersonDir(nickname, domain, baseDir, 'inbox') boxname = 'moderation' - if port: - if port != 80 and port != 443: - if ':' not in domain: - domain = domain + ':' + str(port) + domain = getFullDomain(domain, port) if not pageNumber: pageNumber = 1 @@ -2939,10 +2882,7 @@ def createBoxIndexed(recentPostsCache: {}, indexBoxName = boxname timelineNickname = 'news' - if port: - if port != 80 and port != 443: - if ':' not in domain: - domain = domain + ':' + str(port) + domain = getFullDomain(domain, port) boxActor = httpPrefix + '://' + domain + '/users/' + nickname @@ -3332,11 +3272,7 @@ def getPublicPostsOfPerson(baseDir: str, nickname: str, domain: str, cachedWebfingers = {} federationList = [] - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) handle = httpPrefix + "://" + domainFull + "/@" + nickname wfRequest = \ webfingerHandle(session, handle, httpPrefix, cachedWebfingers, @@ -3377,11 +3313,7 @@ def getPublicPostDomains(session, baseDir: str, nickname: str, domain: str, cachedWebfingers = {} federationList = [] - domainFull = domain - if port: - if port != 80 and port != 443: - if ':' not in domain: - domainFull = domain + ':' + str(port) + domainFull = getFullDomain(domain, port) handle = httpPrefix + "://" + domainFull + "/@" + nickname wfRequest = \ webfingerHandle(session, handle, httpPrefix, cachedWebfingers, @@ -3740,10 +3672,7 @@ def downloadAnnounce(session, baseDir: str, httpPrefix: str, attributedDomain, attributedPort = \ getDomainFromActor(announcedJson['object']['id']) if attributedNickname and attributedDomain: - if attributedPort: - if attributedPort != 80 and attributedPort != 443: - attributedDomain = \ - attributedDomain + ':' + str(attributedPort) + attributedDomain = getFullDomain(attributedDomain, attributedPort) if isBlocked(baseDir, nickname, domain, attributedNickname, attributedDomain): rejectAnnounce(announceFilename) @@ -3859,11 +3788,7 @@ def sendBlockViaServer(baseDir: str, session, print('WARN: No session for sendBlockViaServer') 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 + '/users/' + \ @@ -3942,11 +3867,7 @@ def sendUndoBlockViaServer(baseDir: str, session, print('WARN: No session for sendBlockViaServer') 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 + '/users/' + \