From 21ec5ca9dea0e960c9538376aa2a2215e9849965 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 15 Oct 2021 10:13:04 +0100 Subject: [PATCH] Count likes on announces --- like.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/like.py b/like.py index ed074a574..848890998 100644 --- a/like.py +++ b/like.py @@ -41,7 +41,12 @@ def likedByPerson(postJsonObject: {}, nickname: str, domain: str) -> bool: if noOfLikes(postJsonObject) == 0: return False actorMatch = domain + '/users/' + nickname - for item in postJsonObject['object']['likes']['items']: + + obj = postJsonObject + if hasObjectDict(postJsonObject): + obj = postJsonObject['object'] + + for item in obj['likes']['items']: if item['actor'].endswith(actorMatch): return True return False @@ -50,16 +55,17 @@ def likedByPerson(postJsonObject: {}, nickname: str, domain: str) -> bool: def noOfLikes(postJsonObject: {}) -> int: """Returns the number of likes ona given post """ - if not hasObjectDict(postJsonObject): + obj = postJsonObject + if hasObjectDict(postJsonObject): + obj = postJsonObject['object'] + if not obj.get('likes'): return 0 - if not postJsonObject['object'].get('likes'): + if not isinstance(obj['likes'], dict): return 0 - if not isinstance(postJsonObject['object']['likes'], dict): - return 0 - if not postJsonObject['object']['likes'].get('items'): - postJsonObject['object']['likes']['items'] = [] - postJsonObject['object']['likes']['totalItems'] = 0 - return len(postJsonObject['object']['likes']['items']) + if not obj['likes'].get('items'): + obj['likes']['items'] = [] + obj['likes']['totalItems'] = 0 + return len(obj['likes']['items']) def _like(recentPostsCache: {}, @@ -428,6 +434,7 @@ def updateLikesCollection(recentPostsCache: {}, obj = postJsonObject if hasObjectDict(postJsonObject): obj = postJsonObject['object'] + if not objectUrl.endswith('/likes'): objectUrl = objectUrl + '/likes' if not obj.get('likes'):