From 77e2ec39bc5477fdea8e610d397352ec762a431a Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 8 Jul 2019 17:49:12 +0100 Subject: [PATCH] Function for getting the followers of an actor --- follow.py | 28 ++++++++++++++++++++++++++++ tests.py | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/follow.py b/follow.py index 6f852c366..f00d8e9e1 100644 --- a/follow.py +++ b/follow.py @@ -348,3 +348,31 @@ def sendFollowRequest(session,baseDir: str, \ sendThreads,postLog,cachedWebfingers,personCache, debug) return newFollowJson + +def getFollowersOfActor(baseDir :str,actor :str) -> []: + """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 + """ + result=[] + nickname=getNicknameFromActor(actor) + print("nickname: "+nickname) + if not nickname: + return result + domain,port=getDomainFromActor(actor) + print("domain: "+domain) + if not domain: + return result + actorHandle=nickname+'@'+domain + # for each of the accounts + for subdir, dirs, files in os.walk(baseDir+'/accounts'): + for account in dirs: + if '@' in account and not account.startswith('inbox@'): + print("account: "+account) + followingFilename = os.path.join(subdir, account)+'/following.txt' + if os.path.isfile(followingFilename): + print("followingFilename: "+followingFilename) + print("actorHandle: "+actorHandle) + # does this account follow the given actor? + if actorHandle in open(followingFilename).read(): + result.append(account) + return result diff --git a/tests.py b/tests.py index dad93c833..aa4d085e6 100644 --- a/tests.py +++ b/tests.py @@ -33,6 +33,7 @@ from follow import unfollowPerson from follow import unfollowerOfPerson from follow import getFollowersOfPerson from follow import sendFollowRequest +from follow import getFollowersOfActor from person import createPerson from person import setPreferredNickname from person import setBio @@ -360,6 +361,11 @@ def testFollowBetweenServers(): break time.sleep(1) + actorFollows=getFollowersOfActor(aliceDir,httpPrefix+'://'+bobDomain+':'+str(bobPort)+'/users/bob') + assert len(actorFollows)==1 + print("actorFollows: "+actorFollows[0]) + assert actorFollows[0]=='alice@'+aliceDomain + print('\n\nEve tries to send to Bob') sessionEve = createSession(eveDomain,evePort,useTor) eveSendThreads = []