diff --git a/follow.py b/follow.py index 567187ee..b6973a97 100644 --- a/follow.py +++ b/follow.py @@ -505,9 +505,13 @@ def receiveFollowRequest(session,baseDir: str,httpPrefix: str, \ print('Updating followers file: '+followersFilename+' adding '+approveHandle) if os.path.isfile(followersFilename): if approveHandle not in open(followersFilename).read(): - followersFile=open(followersFilename, "a+") - followersFile.write(approveHandle+'\n') - followersFile.close() + try: + with open(followersFilename, 'r+') as followersFile: + content = followersFile.read() + followersFile.seek(0, 0) + followersFile.write(approveHandle+'\n'+content) + except Exception as e: + print('WARN: Failed to write entry to followers file '+str(e)) else: followersFile=open(followersFilename, "w+") followersFile.write(approveHandle+'\n') diff --git a/utils.py b/utils.py index b96b0585..53c25a53 100644 --- a/utils.py +++ b/utils.py @@ -219,11 +219,17 @@ def followPerson(baseDir: str,nickname: str, domain: str, \ if debug: print('DEBUG: follow already exists') return True - with open(filename, "a") as followfile: - followfile.write(followNickname+'@'+followDomain+'\n') - if debug: - print('DEBUG: follow added') - return True + # prepend to follow file + try: + with open(filename, 'r+') as followFile: + content = followFile.read() + followFile.seek(0, 0) + followFile.write(followNickname+'@'+followDomain+'\n'+content) + if debug: + print('DEBUG: follow added') + return True + except Exception as e: + print('WARN: Failed to write entry to follow file '+filename+' '+str(e)) if debug: print('DEBUG: creating new following file') with open(filename, "w") as followfile: