From a5dffaa5f7c06f71014908f2bbc46fd2acb84410 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 31 Dec 2019 09:23:41 +0000 Subject: [PATCH] Remember manual follow approvals --- follow.py | 26 +++++++++++++++++++++----- manualapprove.py | 17 +++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/follow.py b/follow.py index ff1e27a33..945b3834a 100644 --- a/follow.py +++ b/follow.py @@ -28,6 +28,19 @@ from auth import createBasicAuthHeader from auth import createPassword from session import postJson +def preApprovedFollower(baseDir: str, \ + nickname: str,domain: str, \ + approveHandle: str) -> bool: + """Is the given handle an already manually approved follower? + """ + handle=nickname+'@'+domain + accountDir=baseDir+'/accounts/'+handle + approvedFilename=accountDir+'/approved.txt' + if os.path.isfile(approvedFilename): + if approveHandle in open(approvedFilename).read(): + return True + return False + def removeFromFollowBase(baseDir: str, \ nickname: str,domain: str, \ acceptOrDenyHandle: str,followFile: str, \ @@ -331,9 +344,14 @@ def getFollowingFeed(baseDir: str,domain: str,port: int,path: str, \ return following def followApprovalRequired(baseDir: str,nicknameToFollow: str, \ - domainToFollow: str,debug: bool) -> bool: + domainToFollow: str,debug: bool, \ + followRequestHandle: str) -> bool: """ Returns the policy for follower approvals """ + if preApprovedFollower(baseDir,nicknameToFollow,domainToFollow, \ + followRequestHandle): + return False + manuallyApproveFollows=False if ':' in domainToFollow: domainToFollow=domainToFollow.split(':')[0] @@ -487,8 +505,9 @@ def receiveFollowRequest(session,baseDir: str,httpPrefix: str, \ return True # what is the followers policy? + approveHandle=nickname+'@'+domainFull if followApprovalRequired(baseDir,nicknameToFollow, \ - domainToFollow,debug): + domainToFollow,debug,approveHandle): print('Storing follow request for approval') return storeFollowRequest(baseDir, \ nicknameToFollow,domainToFollow,port, \ @@ -499,9 +518,6 @@ def receiveFollowRequest(session,baseDir: str,httpPrefix: str, \ # update the followers if os.path.isdir(baseDir+'/accounts/'+nicknameToFollow+'@'+domainToFollow): followersFilename=baseDir+'/accounts/'+nicknameToFollow+'@'+domainToFollow+'/followers.txt' - approveHandle=nickname+'@'+domain - if fromPort: - approveHandle=approveHandle+':'+str(fromPort) print('Updating followers file: '+followersFilename+' adding '+approveHandle) if os.path.isfile(followersFilename): if approveHandle not in open(followersFilename).read(): diff --git a/manualapprove.py b/manualapprove.py index 654cae4eb..8bc98e5eb 100644 --- a/manualapprove.py +++ b/manualapprove.py @@ -60,6 +60,21 @@ def manualDenyFollowRequest(session,baseDir: str, \ print('Follow request from '+denyHandle+' was denied.') +def approveFollowerHandle(accountDir: str,approveHandle: str) -> None: + """ Record manually approved handles so that if they unfollow and then + re-follow later then they don't need to be manually approved again + """ + approvedFilename=accountDir+'/approved.txt' + if os.path.isfile(approvedFilename): + if approveHandle not in open(approvedFilename).read(): + approvedFile=open(approvedFilename, "a+") + approvedFile.write(approveHandle+'\n') + approvedFile.close() + else: + approvedFile=open(approvedFilename, "w+") + approvedFile.write(approveHandle+'\n') + approvedFile.close() + def manualApproveFollowRequest(session,baseDir: str, \ httpPrefix: str, nickname: str,domain: str,port: int, \ @@ -146,6 +161,8 @@ def manualApproveFollowRequest(session,baseDir: str, \ # only update the follow requests file if the follow is confirmed to be # in followers.txt if approveHandle in open(followersFilename).read(): + # mark this handle as approved for following + approveFollowerHandle(accountDir,approveHandle) # update the follow requests with the handles not yet approved os.rename(approveFollowsFilename+'.new',approveFollowsFilename) # remove the .follow file