diff --git a/epicyon.py b/epicyon.py index 9e59ae71..ab65ac24 100644 --- a/epicyon.py +++ b/epicyon.py @@ -35,6 +35,7 @@ from follow import followPerson from follow import followerOfPerson from follow import unfollowPerson from follow import unfollowerOfPerson +from follow import getFollowersOfPerson from tests import testPostMessageBetweenServers from tests import runAllTests from config import setConfigParam @@ -273,5 +274,5 @@ if not os.path.isdir(baseDir+'/accounts/'+nickname+'@'+domain): if not os.path.isdir(baseDir+'/accounts/sharedinbox@'+domain): print('Creating shared inbox') createSharedInbox(baseDir,'sharedinbox',domain,port,httpPrefix) - + runDaemon(baseDir,domain,port,httpPrefix,federationList,useTor,debug) diff --git a/follow.py b/follow.py index 537e7742..7b2488b8 100644 --- a/follow.py +++ b/follow.py @@ -13,6 +13,29 @@ import sys from person import validNickname from utils import domainPermitted +def getFollowersOfPerson(baseDir: str,nickname: str,domain: str,followFile='following.txt') -> []: + """Returns a list containing the followers of the given person + Used by the shared inbox to know who to send incoming mail to + """ + followers=[] + handle=nickname.lower()+'@'+domain.lower() + if not os.path.isdir(baseDir+'/accounts/'+handle): + return followers + for subdir, dirs, files in os.walk(baseDir+'/accounts'): + for account in dirs: + filename = os.path.join(subdir, account)+'/'+followFile + if account == handle or account.startswith('sharedinbox@'): + continue + if not os.path.isfile(filename): + continue + with open(filename, 'r') as followingfile: + for followingHandle in followingfile: + if followingHandle.replace('\n','')==handle: + if account not in followers: + followers.append(account) + break + return followers + def followPerson(baseDir: str,nickname: str, domain: str, \ followNickname: str, followDomain: str, \ federationList: [], followFile='following.txt') -> bool: diff --git a/tests.py b/tests.py index d964776a..50838933 100644 --- a/tests.py +++ b/tests.py @@ -29,6 +29,7 @@ from follow import followPerson from follow import followerOfPerson from follow import unfollowPerson from follow import unfollowerOfPerson +from follow import getFollowersOfPerson from person import createPerson from person import setPreferredNickname from person import setBio @@ -230,6 +231,47 @@ def testPostMessageBetweenServers(): # queue item removed assert len([name for name in os.listdir(queuePath) if os.path.isfile(os.path.join(queuePath, name))])==0 + os.chdir(baseDir) + shutil.rmtree(aliceDir) + shutil.rmtree(bobDir) + +def testFollowersOfPerson(): + print('testFollowersOfPerson') + currDir=os.getcwd() + nickname='mxpop' + domain='diva.domain' + password='birb' + port=80 + httpPrefix='https' + federationList=[] + baseDir=currDir+'/.tests_followersofperson' + if os.path.isdir(baseDir): + shutil.rmtree(baseDir) + os.mkdir(baseDir) + os.chdir(baseDir) + createPerson(baseDir,nickname,domain,port,httpPrefix,True,password) + createPerson(baseDir,'maxboardroom',domain,port,httpPrefix,True,password) + createPerson(baseDir,'ultrapancake',domain,port,httpPrefix,True,password) + createPerson(baseDir,'drokk',domain,port,httpPrefix,True,password) + createPerson(baseDir,'sausagedog',domain,port,httpPrefix,True,password) + + clearFollows(baseDir,nickname,domain) + followPerson(baseDir,nickname,domain,'maxboardroom',domain,federationList) + followPerson(baseDir,'drokk',domain,'ultrapancake',domain,federationList) + # deliberate duplication + followPerson(baseDir,'drokk',domain,'ultrapancake',domain,federationList) + followPerson(baseDir,'sausagedog',domain,'ultrapancake',domain,federationList) + followPerson(baseDir,nickname,domain,'ultrapancake',domain,federationList) + followPerson(baseDir,nickname,domain,'someother','randodomain.net',federationList) + + followList=getFollowersOfPerson(baseDir,'ultrapancake',domain) + assert len(followList)==3 + assert 'mxpop@'+domain in followList + assert 'drokk@'+domain in followList + assert 'sausagedog@'+domain in followList + os.chdir(currDir) + shutil.rmtree(baseDir) + def testFollows(): print('testFollows') currDir=os.getcwd() @@ -359,5 +401,6 @@ def runAllTests(): testThreads() testCreatePerson() testAuthentication() - testFollows() + testFollowersOfPerson() + testFollows() print('Tests succeeded\n')