diff --git a/like.py b/like.py index 2d7a6505..cc305b69 100644 --- a/like.py +++ b/like.py @@ -64,21 +64,28 @@ def undoLikesCollectionEntry(postFilename: str,objectUrl: str,actor: str,debug: def likedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool: """Returns True if the given post is liked by the given person """ - if not postJsonObject.get('object'): + if noOfLikes(postJsonObject)==0: return False - if not isinstance(postJsonObject['object'], dict): - return False - if not postJsonObject['object'].get('likes'): - return False - if not postJsonObject['object']['likes'].get('items'): - postJsonObject['object']['likes']['items']=[] - postJsonObject['object']['likes']['totalItems']=0 actorMatch=domain+'/users/'+nickname for item in postJsonObject['object']['likes']['items']: if item['actor'].endswith(actorMatch): return True return False +def noOfLikes(postJsonObject: {}) -> int: + """Returns the number of likes ona given post + """ + if not postJsonObject.get('object'): + return 0 + if not isinstance(postJsonObject['object'], dict): + return 0 + if not postJsonObject['object'].get('likes'): + return 0 + if not postJsonObject['object']['likes'].get('items'): + postJsonObject['object']['likes']['items']=[] + postJsonObject['object']['likes']['totalItems']=0 + return len(postJsonObject['object']['likes']['items']) + def updateLikesCollection(postFilename: str,objectUrl: str, actor: str,debug: bool) -> None: """Updates the likes collection within a post """ diff --git a/webinterface.py b/webinterface.py index 2a524907..616ce67a 100644 --- a/webinterface.py +++ b/webinterface.py @@ -33,6 +33,7 @@ from posts import outboxMessageCreateWrap from session import getJson from auth import createPassword from like import likedByPerson +from like import noOfLikes from announce import announcedByPerson from blocking import isBlocked from content import getMentionsFromHtml @@ -1549,7 +1550,7 @@ def individualPostAsHtml(baseDir: str, \ likeIcon='like_inactive.png' likeLink='like' likeTitle='Like this post' - if likedByPerson(postJsonObject,nickname,fullDomain): + if noOfLikes(postJsonObject)>0: likeIcon='like.png' likeLink='unlike' likeTitle='Undo the like of this post'