From adbddb95c3002c44598ad77ed5aac9eb944c593a Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 29 Dec 2020 20:22:28 +0000 Subject: [PATCH] Check before adding new entries to indexes --- bookmarks.py | 9 +++++---- follow.py | 6 ++++-- happening.py | 5 +++-- inbox.py | 5 +++-- manualapprove.py | 6 ++++-- newsdaemon.py | 7 ++++--- posts.py | 12 +++++++----- utils.py | 7 ++++--- 8 files changed, 34 insertions(+), 23 deletions(-) diff --git a/bookmarks.py b/bookmarks.py index d6f9817c0..e4a43d70d 100644 --- a/bookmarks.py +++ b/bookmarks.py @@ -204,10 +204,11 @@ def updateBookmarksCollection(recentPostsCache: {}, try: with open(bookmarksIndexFilename, 'r+') as bmIndexFile: content = bmIndexFile.read() - bmIndexFile.seek(0, 0) - bmIndexFile.write(bookmarkIndex + '\n' + content) - if debug: - print('DEBUG: bookmark added to index') + if bookmarkIndex + '\n' not in content: + bmIndexFile.seek(0, 0) + bmIndexFile.write(bookmarkIndex + '\n' + content) + if debug: + print('DEBUG: bookmark added to index') except Exception as e: print('WARN: Failed to write entry to bookmarks index ' + bookmarksIndexFilename + ' ' + str(e)) diff --git a/follow.py b/follow.py index 0c9e2598a..ca7fb1637 100644 --- a/follow.py +++ b/follow.py @@ -725,8 +725,10 @@ def receiveFollowRequest(session, baseDir: str, httpPrefix: str, try: with open(followersFilename, 'r+') as followersFile: content = followersFile.read() - followersFile.seek(0, 0) - followersFile.write(approveHandle + '\n' + content) + if approveHandle + '\n' not in content: + followersFile.seek(0, 0) + followersFile.write(approveHandle + '\n' + + content) except Exception as e: print('WARN: ' + 'Failed to write entry to followers file ' + diff --git a/happening.py b/happening.py index 137adb138..69a878747 100644 --- a/happening.py +++ b/happening.py @@ -96,8 +96,9 @@ def saveEventPost(baseDir: str, handle: str, postId: str, try: with open(tlEventsFilename, 'r+') as tlEventsFile: content = tlEventsFile.read() - tlEventsFile.seek(0, 0) - tlEventsFile.write(eventId + '\n' + content) + if eventId + '\n' not in content: + tlEventsFile.seek(0, 0) + tlEventsFile.write(eventId + '\n' + content) except Exception as e: print('WARN: Failed to write entry to events file ' + tlEventsFilename + ' ' + str(e)) diff --git a/inbox.py b/inbox.py index dba441288..7f5a4aa3d 100644 --- a/inbox.py +++ b/inbox.py @@ -123,8 +123,9 @@ def storeHashTags(baseDir: str, nickname: str, postJsonObject: {}) -> None: try: with open(tagsFilename, 'r+') as tagsFile: content = tagsFile.read() - tagsFile.seek(0, 0) - tagsFile.write(tagline + content) + if tagline not in content: + tagsFile.seek(0, 0) + tagsFile.write(tagline + content) except Exception as e: print('WARN: Failed to write entry to tags file ' + tagsFilename + ' ' + str(e)) diff --git a/manualapprove.py b/manualapprove.py index 7c0eb9ce1..f442c8e5c 100644 --- a/manualapprove.py +++ b/manualapprove.py @@ -184,8 +184,10 @@ def manualApproveFollowRequest(session, baseDir: str, try: with open(followersFilename, 'r+') as followersFile: content = followersFile.read() - followersFile.seek(0, 0) - followersFile.write(approveHandleFull + '\n' + content) + if approveHandleFull + '\n' not in content: + followersFile.seek(0, 0) + followersFile.write(approveHandleFull + '\n' + + content) except Exception as e: print('WARN: Manual follow accept. ' + 'Failed to write entry to followers file ' + str(e)) diff --git a/newsdaemon.py b/newsdaemon.py index ff3952926..737b4ac9c 100644 --- a/newsdaemon.py +++ b/newsdaemon.py @@ -46,9 +46,10 @@ def _updateFeedsOutboxIndex(baseDir: str, domain: str, postId: str) -> None: try: with open(indexFilename, 'r+') as feedsFile: content = feedsFile.read() - feedsFile.seek(0, 0) - feedsFile.write(postId + '\n' + content) - print('DEBUG: feeds post added to index') + if postId + '\n' not in content: + feedsFile.seek(0, 0) + feedsFile.write(postId + '\n' + content) + print('DEBUG: feeds post added to index') except Exception as e: print('WARN: Failed to write entry to feeds posts index ' + indexFilename + ' ' + str(e)) diff --git a/posts.py b/posts.py index c402e883c..cd205b49b 100644 --- a/posts.py +++ b/posts.py @@ -668,8 +668,9 @@ def _updateHashtagsIndex(baseDir: str, tag: {}, newPostId: str) -> None: try: with open(tagsFilename, 'r+') as tagsFile: content = tagsFile.read() - tagsFile.seek(0, 0) - tagsFile.write(tagline+content) + if tagline not in content: + tagsFile.seek(0, 0) + tagsFile.write(tagline + content) except Exception as e: print('WARN: Failed to write entry to tags file ' + tagsFilename + ' ' + str(e)) @@ -688,9 +689,10 @@ def _addSchedulePost(baseDir: str, nickname: str, domain: str, try: with open(scheduleIndexFilename, 'r+') as scheduleFile: content = scheduleFile.read() - scheduleFile.seek(0, 0) - scheduleFile.write(indexStr + '\n' + content) - print('DEBUG: scheduled post added to index') + if indexStr + '\n' not in content: + scheduleFile.seek(0, 0) + scheduleFile.write(indexStr + '\n' + content) + print('DEBUG: scheduled post added to index') except Exception as e: print('WARN: Failed to write entry to scheduled posts index ' + scheduleIndexFilename + ' ' + str(e)) diff --git a/utils.py b/utils.py index 2bdcbff74..99d8a8637 100644 --- a/utils.py +++ b/utils.py @@ -708,9 +708,10 @@ def followPerson(baseDir: str, nickname: str, domain: str, try: with open(filename, 'r+') as f: content = f.read() - f.seek(0, 0) - f.write(handleToFollow + '\n' + content) - print('DEBUG: follow added') + if handleToFollow + '\n' not in content: + f.seek(0, 0) + f.write(handleToFollow + '\n' + content) + print('DEBUG: follow added') except Exception as e: print('WARN: Failed to write entry to follow file ' + filename + ' ' + str(e))