From 76fba0e5ddb42786c3804f546bc51fad2d8ac36f Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 24 Mar 2021 15:07:17 +0000 Subject: [PATCH] Getting follow requests collection via c2s --- daemon.py | 11 ++++++++ epicyon.py | 35 +++++++++++++++++++++++++ follow.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) diff --git a/daemon.py b/daemon.py index ea7d66939..d37f5f32b 100644 --- a/daemon.py +++ b/daemon.py @@ -10616,6 +10616,17 @@ class PubServer(BaseHTTPRequestHandler): self.server.followingItemsPerPage, self.server.debug, 'followers') return + elif '/followrequests?page=' in self.path: + self._getFollowingJson(self.server.baseDir, + self.path, + callingDomain, + self.server.httpPrefix, + self.server.domain, + self.server.port, + self.server.followingItemsPerPage, + self.server.debug, + 'followrequests') + return # authorized endpoint used for TTS of posts # arriving in your inbox diff --git a/epicyon.py b/epicyon.py index 7a1437404..a6d265f25 100644 --- a/epicyon.py +++ b/epicyon.py @@ -46,6 +46,7 @@ from filters import addFilter from filters import removeFilter from pprint import pprint from daemon import runDaemon +from follow import getFollowRequestsViaServer from follow import getFollowingViaServer from follow import getFollowersViaServer from follow import clearFollows @@ -262,6 +263,12 @@ parser.add_argument("--followersList", const=True, default=False, help="Get the followers list. Use nickname and " + "domain options to specify the account") +parser.add_argument("--followRequestsList", + dest='followRequestsList', + type=str2bool, nargs='?', + const=True, default=False, + help="Get the follow requests list. Use nickname and " + + "domain options to specify the account") parser.add_argument("--repliesEnabled", "--commentsEnabled", dest='commentsEnabled', type=str2bool, nargs='?', @@ -1554,6 +1561,34 @@ if args.followersList: pprint(followersJson) sys.exit() +if args.followRequestsList: + # follow requests list via c2s protocol + if not args.nickname: + print('Please specify the nickname for the account with --nickname') + sys.exit() + if not args.password: + args.password = getpass.getpass('Password: ') + if not args.password: + print('Specify a password with the --password option') + sys.exit() + args.password = args.password.replace('\n', '') + + session = createSession(proxyType) + personCache = {} + cachedWebfingers = {} + followHttpPrefix = httpPrefix + + followRequestsJson = \ + getFollowRequestsViaServer(baseDir, session, + args.nickname, args.password, + domain, port, + httpPrefix, args.pageNumber, + cachedWebfingers, personCache, + debug, __version__) + if followRequestsJson: + pprint(followRequestsJson) + sys.exit() + nickname = 'admin' if args.domain: domain = args.domain diff --git a/follow.py b/follow.py index a39e2f803..87cccca78 100644 --- a/follow.py +++ b/follow.py @@ -1281,6 +1281,82 @@ def getFollowersViaServer(baseDir: str, session, return followersJson +def getFollowRequestsViaServer(baseDir: str, session, + nickname: str, password: str, + domain: str, port: int, + httpPrefix: str, pageNumber: int, + cachedWebfingers: {}, personCache: {}, + debug: bool, projectVersion: str) -> {}: + """Gets a page from the follow requests collection as json + """ + if not session: + print('WARN: No session for getFollowRequestsViaServer') + return 6 + + domainFull = getFullDomain(domain, port) + + followActor = httpPrefix + '://' + domainFull + '/users/' + nickname + handle = httpPrefix + '://' + domainFull + '/@' + nickname + + # lookup the inbox for the To handle + wfRequest = \ + webfingerHandle(session, handle, httpPrefix, cachedWebfingers, + domain, projectVersion, debug) + if not wfRequest: + if debug: + print('DEBUG: follow requests list webfinger failed for ' + + handle) + return 1 + if not isinstance(wfRequest, dict): + print('WARN: follow requests list Webfinger for ' + handle + + ' did not return a dict. ' + str(wfRequest)) + return 1 + + postToBox = 'outbox' + + # get the actor inbox for the To handle + (inboxUrl, pubKeyId, pubKey, + fromPersonId, sharedInbox, avatarUrl, + displayName) = getPersonBox(baseDir, session, wfRequest, personCache, + projectVersion, httpPrefix, nickname, + domain, postToBox, 42759) + + if not inboxUrl: + if debug: + print('DEBUG: follow requests list no ' + postToBox + + ' was found for ' + handle) + return 3 + if not fromPersonId: + if debug: + print('DEBUG: follow requests list no actor was found for ' + + handle) + return 4 + + authHeader = createBasicAuthHeader(nickname, password) + + headers = { + 'host': domain, + 'Content-type': 'application/json', + 'Authorization': authHeader + } + + if pageNumber < 1: + pageNumber = 1 + url = followActor + '/followrequests?page=' + str(pageNumber) + followersJson = \ + getJson(session, url, headers, {}, debug, + __version__, httpPrefix, domain, 10, True) + if not followersJson: + if debug: + print('DEBUG: GET follow requests list failed for c2s to ' + url) + return 5 + + if debug: + print('DEBUG: c2s GET follow requests list request success') + + return followersJson + + def getFollowersOfActor(baseDir: str, actor: str, debug: bool) -> {}: """In a shared inbox if we receive a post we know who it's from and if it's addressed to followers then we need to get a list of those.