From 838d853a59b0c1d53624f1f1486ea986c2a83218 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 23 Dec 2020 10:57:44 +0000 Subject: [PATCH] Tidying of users path detection --- acceptreject.py | 6 ++--- announce.py | 6 ++--- auth.py | 6 ++--- blocking.py | 11 +++------- bookmarks.py | 11 +++------- daemon.py | 8 +++---- delete.py | 6 ++--- epicyon.py | 11 +++------- follow.py | 21 +++++------------- inbox.py | 56 ++++++++++------------------------------------- like.py | 6 ++--- posts.py | 6 ++--- utils.py | 10 +++++++++ webapp_profile.py | 7 ++---- 14 files changed, 53 insertions(+), 118 deletions(-) diff --git a/acceptreject.py b/acceptreject.py index 6aa4fc3d0..8db41534c 100644 --- a/acceptreject.py +++ b/acceptreject.py @@ -7,6 +7,7 @@ __email__ = "bob@freedombone.net" __status__ = "Production" import os +from utils import hasUsersPath from utils import getFullDomain from utils import urlPermitted from utils import getDomainFromActor @@ -182,10 +183,7 @@ def receiveAcceptReject(session, baseDir: str, if debug: print('DEBUG: ' + messageJson['type'] + ' has no actor') return False - if '/users/' not in messageJson['actor'] and \ - '/accounts/' not in messageJson['actor'] and \ - '/channel/' not in messageJson['actor'] and \ - '/profile/' not in messageJson['actor']: + if not hasUsersPath(messageJson['actor']): if debug: print('DEBUG: "users" or "profile" missing from actor in ' + messageJson['type'] + '. Assuming single user instance.') diff --git a/announce.py b/announce.py index 5364ba483..08b1bdd20 100644 --- a/announce.py +++ b/announce.py @@ -6,6 +6,7 @@ __maintainer__ = "Bob Mottram" __email__ = "bob@freedombone.net" __status__ = "Production" +from utils import hasUsersPath from utils import getFullDomain from utils import getStatusNumber from utils import createOutboxDir @@ -143,10 +144,7 @@ def createAnnounce(session, baseDir: str, federationList: [], announceNickname = None announceDomain = None announcePort = None - if '/users/' in objectUrl or \ - '/accounts/' in objectUrl or \ - '/channel/' in objectUrl or \ - '/profile/' in objectUrl: + if hasUsersPath(objectUrl): announceNickname = getNicknameFromActor(objectUrl) announceDomain, announcePort = getDomainFromActor(objectUrl) diff --git a/auth.py b/auth.py index 841141279..e0690a983 100644 --- a/auth.py +++ b/auth.py @@ -12,6 +12,7 @@ import binascii import os import secrets from utils import isSystemAccount +from utils import hasUsersPath def _hashPassword(password: str) -> str: @@ -89,10 +90,7 @@ def authorizeBasic(baseDir: str, path: str, authHeader: str, print('DEBUG: basic auth - Authorixation header does not ' + 'contain a space character') return False - if '/users/' not in path and \ - '/accounts/' not in path and \ - '/channel/' not in path and \ - '/profile/' not in path: + if not hasUsersPath(path): if debug: print('DEBUG: basic auth - ' + 'path for Authorization does not contain a user') diff --git a/blocking.py b/blocking.py index c34641fc2..6fa7af3dd 100644 --- a/blocking.py +++ b/blocking.py @@ -7,6 +7,7 @@ __email__ = "bob@freedombone.net" __status__ = "Production" import os +from utils import hasUsersPath from utils import getFullDomain from utils import removeIdEnding from utils import isEvil @@ -246,10 +247,7 @@ def outboxBlock(baseDir: str, httpPrefix: str, if debug: print('DEBUG: c2s block object is not a status') return - if '/users/' not in messageId and \ - '/accounts/' not in messageId and \ - '/channel/' not in messageId and \ - '/profile/' not in messageId: + if not hasUsersPath(messageId): if debug: print('DEBUG: c2s block object has no nickname') return @@ -321,10 +319,7 @@ def outboxUndoBlock(baseDir: str, httpPrefix: str, if debug: print('DEBUG: c2s undo block object is not a status') return - if '/users/' not in messageId and \ - '/accounts/' not in messageId and \ - '/channel/' not in messageId and \ - '/profile/' not in messageId: + if not hasUsersPath(messageId): if debug: print('DEBUG: c2s undo block object has no nickname') return diff --git a/bookmarks.py b/bookmarks.py index a9bf0c457..d6f9817c0 100644 --- a/bookmarks.py +++ b/bookmarks.py @@ -8,6 +8,7 @@ __status__ = "Production" import os from pprint import pprint +from utils import hasUsersPath from utils import getFullDomain from utils import removeIdEnding from utils import removePostFromCache @@ -255,10 +256,7 @@ def bookmark(recentPostsCache: {}, bookmarkedPostNickname = getNicknameFromActor(acBm) bookmarkedPostDomain, bookmarkedPostPort = getDomainFromActor(acBm) else: - if '/users/' in objectUrl or \ - '/accounts/' in objectUrl or \ - '/channel/' in objectUrl or \ - '/profile/' in objectUrl: + if hasUsersPath(objectUrl): ou = objectUrl bookmarkedPostNickname = getNicknameFromActor(ou) bookmarkedPostDomain, bookmarkedPostPort = getDomainFromActor(ou) @@ -322,10 +320,7 @@ def undoBookmark(recentPostsCache: {}, bookmarkedPostNickname = getNicknameFromActor(acBm) bookmarkedPostDomain, bookmarkedPostPort = getDomainFromActor(acBm) else: - if '/users/' in objectUrl or \ - '/accounts/' in objectUrl or \ - '/channel/' in objectUrl or \ - '/profile/' in objectUrl: + if hasUsersPath(objectUrl): ou = objectUrl bookmarkedPostNickname = getNicknameFromActor(ou) bookmarkedPostDomain, bookmarkedPostPort = getDomainFromActor(ou) diff --git a/daemon.py b/daemon.py index 8537ea5b5..15bdd70db 100644 --- a/daemon.py +++ b/daemon.py @@ -171,9 +171,10 @@ from shares import getSharesFeedForPerson from shares import addShare from shares import removeShare from shares import expireShares +from categories import setHashtagCategory +from utils import hasUsersPath from utils import getFullDomain from utils import removeHtml -from categories import setHashtagCategory from utils import isEditor from utils import getImageExtensions from utils import mediaFileMimeType @@ -2523,10 +2524,7 @@ class PubServer(BaseHTTPRequestHandler): return elif ('@' in searchStr or ('://' in searchStr and - ('/users/' in searchStr or - '/profile/' in searchStr or - '/accounts/' in searchStr or - '/channel/' in searchStr))): + hasUsersPath(searchStr))): # profile search nickname = getNicknameFromActor(actorStr) if not self.server.session: diff --git a/delete.py b/delete.py index 97b1aaa1c..240845a89 100644 --- a/delete.py +++ b/delete.py @@ -8,6 +8,7 @@ __status__ = "Production" import os from datetime import datetime +from utils import hasUsersPath from utils import getFullDomain from utils import removeIdEnding from utils import getNicknameFromActor @@ -139,10 +140,7 @@ def outboxDelete(baseDir: str, httpPrefix: str, if debug: print('DEBUG: c2s delete object is not a status') return - if '/users/' not in messageId and \ - '/accounts/' not in messageId and \ - '/channel/' not in messageId and \ - '/profile/' not in messageId: + if not hasUsersPath(messageId): if debug: print('DEBUG: c2s delete object has no nickname') return diff --git a/epicyon.py b/epicyon.py index 6b06ef495..3c111e4bf 100644 --- a/epicyon.py +++ b/epicyon.py @@ -47,6 +47,7 @@ from tests import testClientToServer from tests import runAllTests from auth import storeBasicCredentials from auth import createPassword +from utils import hasUsersPath from utils import getFullDomain from utils import setConfigParam from utils import getConfigParam @@ -1318,10 +1319,7 @@ if args.actor: for prefix in prefixes: args.actor = args.actor.replace(prefix, '') args.actor = args.actor.replace('/@', '/users/') - if '/users/' not in args.actor and \ - '/accounts/' not in args.actor and \ - '/channel/' not in args.actor and \ - '/profile/' not in args.actor: + if not hasUsersPath(args.actor): print('Expected actor format: ' + 'https://domain/@nick or https://domain/users/nick') sys.exit() @@ -1391,10 +1389,7 @@ if args.actor: personUrl = None if wfRequest.get('errors'): print('wfRequest error: ' + str(wfRequest['errors'])) - if '/users/' in args.actor or \ - '/accounts/' in args.actor or \ - '/profile/' in args.actor or \ - '/channel/' in args.actor: + if hasUsersPath(args.actor): personUrl = originalActor else: sys.exit() diff --git a/follow.py b/follow.py index 04a6a7db4..f3c209670 100644 --- a/follow.py +++ b/follow.py @@ -8,6 +8,7 @@ __status__ = "Production" from pprint import pprint import os +from utils import hasUsersPath from utils import getFullDomain from utils import isSystemAccount from utils import getFollowersList @@ -316,10 +317,7 @@ def _getNoOfFollows(baseDir: str, nickname: str, domain: str, ctr += 1 elif ((line.startswith('http') or line.startswith('dat')) and - ('/users/' in line or - '/profile/' in line or - '/accounts/' in line or - '/channel/' in line)): + hasUsersPath(line)): ctr += 1 return ctr @@ -438,10 +436,7 @@ def getFollowingFeed(baseDir: str, domain: str, port: int, path: str, following['orderedItems'].append(url) elif ((line.startswith('http') or line.startswith('dat')) and - ('/users/' in line or - '/profile/' in line or - '/accounts/' in line or - '/channel/' in line)): + hasUsersPath(line)): # https://domain/users/nickname pageCtr += 1 totalCtr += 1 @@ -616,10 +611,7 @@ def receiveFollowRequest(session, baseDir: str, httpPrefix: str, if debug: print('DEBUG: follow request has no actor') return False - if '/users/' not in messageJson['actor'] and \ - '/accounts/' not in messageJson['actor'] and \ - '/channel/' not in messageJson['actor'] and \ - '/profile/' not in messageJson['actor']: + if not hasUsersPath(messageJson['actor']): if debug: print('DEBUG: users/profile/accounts/channel missing from actor') return False @@ -641,10 +633,7 @@ def receiveFollowRequest(session, baseDir: str, httpPrefix: str, 'nickname. Assuming single user instance.') if not messageJson.get('to'): messageJson['to'] = messageJson['object'] - if '/users/' not in messageJson['object'] and \ - '/accounts/' not in messageJson['object'] and \ - '/channel/' not in messageJson['object'] and \ - '/profile/' not in messageJson['object']: + if not hasUsersPath(messageJson['object']): if debug: print('DEBUG: users/profile/channel/accounts ' + 'not found within object') diff --git a/inbox.py b/inbox.py index 680132308..96d78c4a1 100644 --- a/inbox.py +++ b/inbox.py @@ -10,6 +10,7 @@ import json import os import datetime import time +from utils import hasUsersPath from utils import validPostDate from utils import getFullDomain from utils import isEventPost @@ -604,10 +605,7 @@ def _receiveUndoFollow(session, baseDir: str, httpPrefix: str, if debug: print('DEBUG: follow request has no actor within object') return False - if '/users/' not in messageJson['object']['actor'] and \ - '/accounts/' not in messageJson['object']['actor'] and \ - '/channel/' not in messageJson['object']['actor'] and \ - '/profile/' not in messageJson['object']['actor']: + if not hasUsersPath(messageJson['object']['actor']): if debug: print('DEBUG: "users" or "profile" missing ' + 'from actor within object') @@ -668,10 +666,7 @@ def _receiveUndo(session, baseDir: str, httpPrefix: str, if debug: print('DEBUG: follow request has no actor') return False - if '/users/' not in messageJson['actor'] and \ - '/accounts/' not in messageJson['actor'] and \ - '/channel/' not in messageJson['actor'] and \ - '/profile/' not in messageJson['actor']: + if not hasUsersPath(messageJson['actor']): if debug: print('DEBUG: "users" or "profile" missing from actor') return False @@ -859,10 +854,7 @@ def _receiveUpdate(recentPostsCache: {}, session, baseDir: str, if debug: print('DEBUG: ' + messageJson['type'] + ' object has no type') return False - if '/users/' not in messageJson['actor'] and \ - '/accounts/' not in messageJson['actor'] and \ - '/channel/' not in messageJson['actor'] and \ - '/profile/' not in messageJson['actor']: + if not hasUsersPath(messageJson['actor']): if debug: print('DEBUG: "users" or "profile" missing from actor in ' + messageJson['type']) @@ -943,10 +935,7 @@ def _receiveLike(recentPostsCache: {}, if debug: print('DEBUG: ' + messageJson['type'] + ' has no "to" list') return False - if '/users/' not in messageJson['actor'] and \ - '/accounts/' not in messageJson['actor'] and \ - '/channel/' not in messageJson['actor'] and \ - '/profile/' not in messageJson['actor']: + if not hasUsersPath(messageJson['actor']): if debug: print('DEBUG: "users" or "profile" missing from actor in ' + messageJson['type']) @@ -1014,10 +1003,7 @@ def _receiveUndoLike(recentPostsCache: {}, print('DEBUG: ' + messageJson['type'] + ' like object is not a string') return False - if '/users/' not in messageJson['actor'] and \ - '/accounts/' not in messageJson['actor'] and \ - '/channel/' not in messageJson['actor'] and \ - '/profile/' not in messageJson['actor']: + if not hasUsersPath(messageJson['actor']): if debug: print('DEBUG: "users" or "profile" missing from actor in ' + messageJson['type'] + ' like') @@ -1219,10 +1205,7 @@ def _receiveDelete(session, handle: str, isGroup: bool, baseDir: str, if debug: print('DEBUG: ' + messageJson['type'] + ' has no "to" list') return False - if '/users/' not in messageJson['actor'] and \ - '/accounts/' not in messageJson['actor'] and \ - '/channel/' not in messageJson['actor'] and \ - '/profile/' not in messageJson['actor']: + if not hasUsersPath(messageJson['actor']): if debug: print('DEBUG: ' + '"users" or "profile" missing from actor in ' + @@ -1303,19 +1286,13 @@ def _receiveAnnounce(recentPostsCache: {}, if debug: print('DEBUG: ' + messageJson['type'] + ' has no "to" list') return False - if '/users/' not in messageJson['actor'] and \ - '/accounts/' not in messageJson['actor'] and \ - '/channel/' not in messageJson['actor'] and \ - '/profile/' not in messageJson['actor']: + if not hasUsersPath(messageJson['actor']): if debug: print('DEBUG: ' + '"users" or "profile" missing from actor in ' + messageJson['type']) return False - if '/users/' not in messageJson['object'] and \ - '/accounts/' not in messageJson['object'] and \ - '/channel/' not in messageJson['object'] and \ - '/profile/' not in messageJson['object']: + if not hasUsersPath(messageJson['object']): if debug: print('DEBUG: ' + '"users", "channel" or "profile" missing in ' + @@ -1387,10 +1364,7 @@ def _receiveAnnounce(recentPostsCache: {}, if isinstance(attrib, str): lookupActor = attrib if lookupActor: - if '/users/' in lookupActor or \ - '/accounts/' in lookupActor or \ - '/channel/' in lookupActor or \ - '/profile/' in lookupActor: + if hasUsersPath(lookupActor): if '/statuses/' in lookupActor: lookupActor = lookupActor.split('/statuses/')[0] @@ -1439,10 +1413,7 @@ def _receiveUndoAnnounce(recentPostsCache: {}, return False if messageJson['object']['type'] != 'Announce': return False - if '/users/' not in messageJson['actor'] and \ - '/accounts/' not in messageJson['actor'] and \ - '/channel/' not in messageJson['actor'] and \ - '/profile/' not in messageJson['actor']: + if not hasUsersPath(messageJson['actor']): if debug: print('DEBUG: "users" or "profile" missing from actor in ' + messageJson['type'] + ' announce') @@ -1688,10 +1659,7 @@ def _obtainAvatarForReplyPost(session, baseDir: str, httpPrefix: str, if not isinstance(lookupActor, str): return - if not ('/users/' in lookupActor or - '/accounts/' in lookupActor or - '/channel/' in lookupActor or - '/profile/' in lookupActor): + if not hasUsersPath(lookupActor): return if '/statuses/' in lookupActor: diff --git a/like.py b/like.py index 29efeda3f..289c930c9 100644 --- a/like.py +++ b/like.py @@ -6,6 +6,7 @@ __maintainer__ = "Bob Mottram" __email__ = "bob@freedombone.net" __status__ = "Production" +from utils import hasUsersPath from utils import getFullDomain from utils import removeIdEnding from utils import urlPermitted @@ -87,10 +88,7 @@ def _like(recentPostsCache: {}, likedPostNickname = getNicknameFromActor(actorLiked) likedPostDomain, likedPostPort = getDomainFromActor(actorLiked) else: - if '/users/' in objectUrl or \ - '/accounts/' in objectUrl or \ - '/channel/' in objectUrl or \ - '/profile/' in objectUrl: + if hasUsersPath(objectUrl): likedPostNickname = getNicknameFromActor(objectUrl) likedPostDomain, likedPostPort = getDomainFromActor(objectUrl) diff --git a/posts.py b/posts.py index 895c50c7e..c402e883c 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 hasUsersPath from utils import validPostDate from utils import getFullDomain from utils import getFollowersList @@ -155,10 +156,7 @@ def getUserUrl(wfRequest: {}, sourceId=0) -> str: continue if link['type'] != 'application/activity+json': continue - if not ('/users/' in link['href'] or - '/accounts/' in link['href'] or - '/profile/' in link['href'] or - '/channel/' in link['href']): + if not hasUsersPath(link['href']): print('getUserUrl webfinger activity+json ' + 'contains single user instance actor ' + str(sourceId) + ' ' + str(link)) diff --git a/utils.py b/utils.py index ff89afd02..2bdcbff74 100644 --- a/utils.py +++ b/utils.py @@ -19,6 +19,16 @@ from calendar import monthrange from followingCalendar import addPersonToCalendar +def hasUsersPath(pathStr: str) -> bool: + """Whether there is a /users/ path (or equivalent) in the given string + """ + usersList = ('users', 'accounts', 'channel', 'profile') + for usersStr in usersList: + if '/' + usersStr + '/' in pathStr: + return True + return False + + def validPostDate(published: str, maxAgeDays=7) -> bool: """Returns true if the published date is recent and is not in the future """ diff --git a/webapp_profile.py b/webapp_profile.py index 5cc8866db..e89c4de32 100644 --- a/webapp_profile.py +++ b/webapp_profile.py @@ -8,6 +8,7 @@ __status__ = "Production" import os from pprint import pprint +from utils import hasUsersPath from utils import getFullDomain from utils import isDormant from utils import getNicknameFromActor @@ -61,11 +62,7 @@ def htmlProfileAfterSearch(cssCache: {}, defaultTimeline: str) -> str: """Show a profile page after a search for a fediverse address """ - if '/users/' in profileHandle or \ - '/accounts/' in profileHandle or \ - '/channel/' in profileHandle or \ - '/profile/' in profileHandle or \ - '/@' in profileHandle: + if hasUsersPath(profileHandle) or '/@' in profileHandle: searchNickname = getNicknameFromActor(profileHandle) searchDomain, searchPort = getDomainFromActor(profileHandle) else: