Removing bookmarks from index

merge-requests/30/head
Bob Mottram 2019-11-18 15:21:35 +00:00
parent 3b83a30e41
commit 79789014d4
1 changed files with 69 additions and 59 deletions

View File

@ -29,68 +29,78 @@ def undoBookmarksCollectionEntry(baseDir: str,postFilename: str,objectUrl: str,
"""Undoes a bookmark for a particular actor """Undoes a bookmark for a particular actor
""" """
postJsonObject=loadJson(postFilename) postJsonObject=loadJson(postFilename)
if postJsonObject: if not postJsonObject:
# remove any cached version of this post so that the bookmark icon is changed return
nickname=getNicknameFromActor(actor)
cachedPostFilename= \
getCachedPostFilename(baseDir,nickname,domain,postJsonObject)
if os.path.isfile(cachedPostFilename):
os.remove(cachedPostFilename)
if not postJsonObject.get('type'): # remove any cached version of this post so that the bookmark icon is changed
return nickname=getNicknameFromActor(actor)
if postJsonObject['type']!='Create': cachedPostFilename= \
return getCachedPostFilename(baseDir,nickname,domain,postJsonObject)
if not postJsonObject.get('object'): if os.path.isfile(cachedPostFilename):
if debug: os.remove(cachedPostFilename)
pprint(postJsonObject)
print('DEBUG: post '+objectUrl+' has no object') if not postJsonObject.get('type'):
return return
if not isinstance(postJsonObject['object'], dict): if postJsonObject['type']!='Create':
return return
if not postJsonObject['object'].get('bookmarks'): if not postJsonObject.get('object'):
return if debug:
if not isinstance(postJsonObject['object']['bookmarks'], dict): pprint(postJsonObject)
return print('DEBUG: post '+objectUrl+' has no object')
if not postJsonObject['object']['bookmarks'].get('items'): return
return if not isinstance(postJsonObject['object'], dict):
totalItems=0 return
if postJsonObject['object']['bookmarks'].get('totalItems'): if not postJsonObject['object'].get('bookmarks'):
totalItems=postJsonObject['object']['bookmarks']['totalItems'] return
itemFound=False if not isinstance(postJsonObject['object']['bookmarks'], dict):
for bookmarkItem in postJsonObject['object']['bookmarks']['items']: return
if bookmarkItem.get('actor'): if not postJsonObject['object']['bookmarks'].get('items'):
if bookmarkItem['actor']==actor: return
if debug: totalItems=0
print('DEBUG: bookmark was removed for '+actor) if postJsonObject['object']['bookmarks'].get('totalItems'):
postJsonObject['object']['bookmarks']['items'].remove(bookmarkItem) totalItems=postJsonObject['object']['bookmarks']['totalItems']
itemFound=True itemFound=False
break for bookmarkItem in postJsonObject['object']['bookmarks']['items']:
if itemFound: if bookmarkItem.get('actor'):
if totalItems==1: if bookmarkItem['actor']==actor:
if debug: if debug:
print('DEBUG: bookmarks was removed from post') print('DEBUG: bookmark was removed for '+actor)
del postJsonObject['object']['bookmarks'] postJsonObject['object']['bookmarks']['items'].remove(bookmarkItem)
else: itemFound=True
postJsonObject['object']['bookmarks']['totalItems']= \ break
len(postJsonObject['bookmarks']['items'])
saveJson(postJsonObject,postFilename)
# remove from the index if not itemFound:
bookmarksIndexFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/bookmarks.index' return
if os.path.isfile(bookmarksIndexFilename):
bookmarkIndex=postFilename.split('/')[-1]+'\n' if totalItems==1:
if bookmarkIndex in open(bookmarksIndexFilename).read(): if debug:
indexStr='' print('DEBUG: bookmarks was removed from post')
indexStrChanged=False del postJsonObject['object']['bookmarks']
with open(bookmarksIndexFilename, 'r') as indexFile: else:
indexStr=indexFile.read().replace(bookmarkIndex,'') postJsonObject['object']['bookmarks']['totalItems']= \
indexStrChanged=True len(postJsonObject['bookmarks']['items'])
if indexStrChanged: saveJson(postJsonObject,postFilename)
bookmarksIndexFile=open(bookmarksIndexFilename,'w')
if bookmarksIndexFile: # remove from the index
bookmarksIndexFile.write(indexStr) bookmarksIndexFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/bookmarks.index'
bookmarksIndexFile.close() if not os.path.isfile(bookmarksIndexFilename):
return
if '/' in postFilename:
bookmarkIndex=postFilename.split('/')[-1].strip()
else:
bookmarkIndex=postFilename.strip()
if bookmarkIndex not in open(bookmarksIndexFilename).read():
return
indexStr=''
indexStrChanged=False
with open(bookmarksIndexFilename, 'r') as indexFile:
indexStr=indexFile.read().replace(bookmarkIndex+'\n','')
indexStrChanged=True
if indexStrChanged:
bookmarksIndexFile=open(bookmarksIndexFilename,'w')
if bookmarksIndexFile:
bookmarksIndexFile.write(indexStr)
bookmarksIndexFile.close()
def bookmarkedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool: def bookmarkedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool:
"""Returns True if the given post is bookmarked by the given person """Returns True if the given post is bookmarked by the given person