diff --git a/daemon.py b/daemon.py index 1c6c82ab2..1021e3444 100644 --- a/daemon.py +++ b/daemon.py @@ -641,6 +641,7 @@ class PubServer(BaseHTTPRequestHandler): self.server.GETbusy=False return + # announce/repeat from the web interface if authorized and '?repeat=' in self.path: repeatUrl=self.path.split('?repeat=')[1] actor=self.path.split('?repeat=')[0] @@ -667,7 +668,58 @@ class PubServer(BaseHTTPRequestHandler): self.server.GETbusy=False self._redirect_headers(actor+'/inbox',cookie) return - + + # like from the web interface icon + if authorized and '?like=' in self.path: + likeUrl=self.path.split('?like=')[1] + actor=self.path.split('?like=')[0] + self.postToNickname=getNicknameFromActor(actor) + if not self.server.session: + self.server.session= \ + createSession(self.server.domain,self.server.port,self.server.useTor) + likeActor=self.server.httpPrefix+'://'+self.server.fullDomain+'/users/'+self.postToNickname + likeJson= { + 'type': 'Like', + 'actor': likeActor, + 'object': likeUrl, + 'to': [likeActor+'/followers'], + 'cc': [] + } + if likeJson: + self._postToOutbox(likeJson) + self.server.GETbusy=False + self._redirect_headers(actor+'/inbox',cookie) + return + + # undo a like from the web interface icon + if authorized and '?unlike=' in self.path: + likeUrl=self.path.split('?unlike=')[1] + actor=self.path.split('?unlike=')[0] + self.postToNickname=getNicknameFromActor(actor) + if not self.server.session: + self.server.session= \ + createSession(self.server.domain,self.server.port,self.server.useTor) + undoActor=self.server.httpPrefix+'://'+self.server.fullDomain+'/users/'+self.postToNickname + undoLikeJson= { + 'type': 'Undo', + 'actor': undoActor, + 'object': { + 'type': 'Like', + 'actor': undoActor, + 'object': likeUrl, + 'to': [undoActor+'/followers'], + 'cc': [] + }, + 'to': [undoActor+'/followers'], + 'cc': [] + } + if undoLikeJson: + self._postToOutbox(undoLikeJson) + self.server.GETbusy=False + self._redirect_headers(actor+'/inbox',cookie) + return + + # reply from the web interface icon inReplyTo=None if authorized and '?replyto=' in self.path: inReplyTo=self.path.split('?replyto=')[1] diff --git a/like.py b/like.py index d0fe11702..27b3ef6da 100644 --- a/like.py +++ b/like.py @@ -59,6 +59,21 @@ def undoLikesCollectionEntry(postFilename: str,objectUrl: str, actor: str,debug: with open(postFilename, 'w') as fp: commentjson.dump(postJsonObject, fp, indent=4, sort_keys=True) +def likedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool: + """Returns True if the given post is liked by the given actor + """ + if not postJsonObject.get('object'): + return False + if not isinstance(postJsonObject['object'], dict): + return False + if not postJsonObject['object'].get('likes'): + return False + actorMatch=domain+'/users/'+nickname + for item in postJsonObject['object']['likes']['items']: + if item['actor'].endswith(actorMatch): + return True + return False + 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 7bdf5884e..9de9c2ca7 100644 --- a/webinterface.py +++ b/webinterface.py @@ -22,6 +22,7 @@ from posts import getUserUrl from posts import parseUserFeed from session import getJson from auth import createPassword +from like import likedByPerson def htmlGetLoginCredentials(loginParams: str,lastLoginTime: int) -> (str,str): """Receives login credentials via HTTPServer POST @@ -563,10 +564,15 @@ def individualPostAsHtml(baseDir: str, \ if fullDomain+'/users/'+nickname not in postJsonObject['actor']: announceStr= \ '' \ - '' + '' + likeIcon='like_inactive.png' + likeLink='like' + if likedByPerson(postJsonObject,nickname,fullDomain): + likeIcon='like.png' + likeLink='unlike' likeStr= \ - '' \ - '' + '' \ + '' if showIcons: footerStr='