diff --git a/like.py b/like.py index 0e1c74078..ed074a574 100644 --- a/like.py +++ b/like.py @@ -425,14 +425,12 @@ def updateLikesCollection(recentPostsCache: {}, except BaseException: pass - if not hasObjectDict(postJsonObject): - if debug: - pprint(postJsonObject) - print('DEBUG: post ' + objectUrl + ' has no object') - return + obj = postJsonObject + if hasObjectDict(postJsonObject): + obj = postJsonObject['object'] if not objectUrl.endswith('/likes'): objectUrl = objectUrl + '/likes' - if not postJsonObject['object'].get('likes'): + if not obj.get('likes'): if debug: print('DEBUG: Adding initial like to ' + objectUrl) likesJson = { @@ -445,11 +443,11 @@ def updateLikesCollection(recentPostsCache: {}, 'actor': actor }] } - postJsonObject['object']['likes'] = likesJson + obj['likes'] = likesJson else: - if not postJsonObject['object']['likes'].get('items'): - postJsonObject['object']['likes']['items'] = [] - for likeItem in postJsonObject['object']['likes']['items']: + if not obj['likes'].get('items'): + obj['likes']['items'] = [] + for likeItem in obj['likes']['items']: if likeItem.get('actor'): if likeItem['actor'] == actor: # already liked @@ -458,9 +456,9 @@ def updateLikesCollection(recentPostsCache: {}, 'type': 'Like', 'actor': actor } - postJsonObject['object']['likes']['items'].append(newLike) - itlen = len(postJsonObject['object']['likes']['items']) - postJsonObject['object']['likes']['totalItems'] = itlen + obj['likes']['items'].append(newLike) + itlen = len(obj['likes']['items']) + obj['likes']['totalItems'] = itlen if debug: print('DEBUG: saving post with likes added') diff --git a/utils.py b/utils.py index 3afe85063..50007b2dd 100644 --- a/utils.py +++ b/utils.py @@ -2184,27 +2184,25 @@ def undoLikesCollectionEntry(recentPostsCache: {}, return if postJsonObject['type'] != 'Create': return - if not hasObjectDict(postJsonObject): - if debug: - pprint(postJsonObject) - print('DEBUG: post ' + objectUrl + ' has no object') + obj = postJsonObject + if hasObjectDict(postJsonObject): + obj = postJsonObject['object'] + if not obj.get('likes'): return - if not postJsonObject['object'].get('likes'): + if not isinstance(obj['likes'], dict): return - if not isinstance(postJsonObject['object']['likes'], dict): - return - if not postJsonObject['object']['likes'].get('items'): + if not obj['likes'].get('items'): return totalItems = 0 - if postJsonObject['object']['likes'].get('totalItems'): - totalItems = postJsonObject['object']['likes']['totalItems'] + if obj['likes'].get('totalItems'): + totalItems = obj['likes']['totalItems'] itemFound = False - for likeItem in postJsonObject['object']['likes']['items']: + for likeItem in obj['likes']['items']: if likeItem.get('actor'): if likeItem['actor'] == actor: if debug: print('DEBUG: like was removed for ' + actor) - postJsonObject['object']['likes']['items'].remove(likeItem) + obj['likes']['items'].remove(likeItem) itemFound = True break if not itemFound: @@ -2212,10 +2210,10 @@ def undoLikesCollectionEntry(recentPostsCache: {}, if totalItems == 1: if debug: print('DEBUG: likes was removed from post') - del postJsonObject['object']['likes'] + del obj['likes'] else: - itlen = len(postJsonObject['object']['likes']['items']) - postJsonObject['object']['likes']['totalItems'] = itlen + itlen = len(obj['likes']['items']) + obj['likes']['totalItems'] = itlen saveJson(postJsonObject, postFilename)