diff --git a/follow.py b/follow.py index 0559a9fb..17454798 100644 --- a/follow.py +++ b/follow.py @@ -62,9 +62,12 @@ def followerOfPerson(baseDir: str,nickname: str, domain: str, \ def unfollowPerson(baseDir: str,nickname: str, domain: str, \ followNickname: str, followDomain: str, \ - followFile='following.txt') -> bool: + followFile='following.txt', \ + debug=False) -> bool: """Removes a person to the follow list """ + if ':' in domain: + domain=domain.split(':')[0] handle=nickname.lower()+'@'+domain.lower() handleToUnfollow=followNickname.lower()+'@'+followDomain.lower() if not os.path.isdir(baseDir+'/accounts'): @@ -73,8 +76,12 @@ def unfollowPerson(baseDir: str,nickname: str, domain: str, \ os.mkdir(baseDir+'/accounts/'+handle) filename=baseDir+'/accounts/'+handle+'/'+followFile if not os.path.isfile(filename): + if debug: + print('DEBUG: follow file '+filename+' was not found') return False if handleToUnfollow not in open(filename).read(): + if debug: + print('DEBUG: handle to unfollow '+handleToUnfollow+' is not in '+filename) return with open(filename, "r") as f: lines = f.readlines() @@ -84,11 +91,13 @@ def unfollowPerson(baseDir: str,nickname: str, domain: str, \ f.write(line) def unfollowerOfPerson(baseDir: str,nickname: str,domain: str, \ - followerNickname: str,followerDomain: str) -> bool: + followerNickname: str,followerDomain: str, \ + debug=False) -> bool: """Remove a follower of a person """ return unfollowPerson(baseDir,nickname,domain, \ - followerNickname,followerDomain,'followers.txt') + followerNickname,followerDomain, \ + 'followers.txt',debug) def clearFollows(baseDir: str,nickname: str,domain: str, \ followFile='following.txt') -> None: diff --git a/inbox.py b/inbox.py index 943ad491..3e0e67ed 100644 --- a/inbox.py +++ b/inbox.py @@ -27,6 +27,7 @@ from session import createSession from session import getJson from follow import receiveFollowRequest from follow import getFollowersOfActor +from follow import unfollowerOfPerson from pprint import pprint from cache import getPersonFromCache from cache import storePersonInCache @@ -74,7 +75,7 @@ def inboxMessageHasParams(messageJson: {}) -> bool: if not messageJson.get(param): return False if not messageJson.get('to'): - allowedWithoutToParam=['Follow','Request','Capability'] + allowedWithoutToParam=['Follow','Request','Capability','Undo'] if messageJson['type'] not in allowedWithoutToParam: return False return True @@ -410,8 +411,17 @@ def receiveUndoFollow(session,baseDir: str,httpPrefix: str, \ if portFollowing!=80 and portFollowing!=443: domainFollowingFull=domainFollowing+':'+str(portFollowing) - return unfollowerOfPerson(baseDir,nicknameFollower,domainFollowerFull, \ - nicknameFollowing,domainFollowingFull) + if unfollowerOfPerson(baseDir, \ + nicknameFollowing,domainFollowingFull, \ + nicknameFollower,domainFollowerFull, \ + debug): + if debug: + print('DEBUG: Follower '+nicknameFollower+'@'+domainFollowerFull+' was removed') + return True + + if debug: + print('DEBUG: Follower '+nicknameFollower+'@'+domainFollowerFull+' was not removed') + return False def receiveUndo(session,baseDir: str,httpPrefix: str, \ port: int,sendThreads: [],postLog: [], \ diff --git a/tests.py b/tests.py index 6212567e..b551e5d3 100644 --- a/tests.py +++ b/tests.py @@ -33,6 +33,7 @@ from posts import sendPostViaServer from follow import clearFollows from follow import clearFollowers from follow import sendFollowRequestViaServer +from follow import sendUnfollowRequestViaServer from utils import followPerson from follow import followerOfPerson from follow import unfollowPerson @@ -1113,7 +1114,27 @@ def testClientToServer(): assert len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==1 assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==1 print('Post repeated') - + + + print('\n\nAlice unfollows Bob') + password='alicepass' + sendUnfollowRequestViaServer(sessionAlice,'alice',password, \ + aliceDomain,alicePort, \ + 'bob',bobDomain,bobPort, \ + httpPrefix, \ + cachedWebfingers,personCache, \ + True) + for t in range(10): + if 'alice@'+aliceDomain+':'+str(alicePort) not in open(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt').read(): + if 'bob@'+bobDomain+':'+str(bobPort) not in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read(): + break + time.sleep(1) + + assert os.path.isfile(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt') + assert os.path.isfile(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt') + assert 'alice@'+aliceDomain+':'+str(alicePort) not in open(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt').read() + assert 'bob@'+bobDomain+':'+str(bobPort) not in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read() + # stop the servers thrAlice.kill() thrAlice.join()