Undoing likes via the web interface

master
Bob Mottram 2019-08-01 10:05:09 +01:00
parent f9d86cf762
commit cfae58b92b
3 changed files with 77 additions and 4 deletions

View File

@ -641,6 +641,7 @@ class PubServer(BaseHTTPRequestHandler):
self.server.GETbusy=False self.server.GETbusy=False
return return
# announce/repeat from the web interface
if authorized and '?repeat=' in self.path: if authorized and '?repeat=' in self.path:
repeatUrl=self.path.split('?repeat=')[1] repeatUrl=self.path.split('?repeat=')[1]
actor=self.path.split('?repeat=')[0] actor=self.path.split('?repeat=')[0]
@ -667,7 +668,58 @@ class PubServer(BaseHTTPRequestHandler):
self.server.GETbusy=False self.server.GETbusy=False
self._redirect_headers(actor+'/inbox',cookie) self._redirect_headers(actor+'/inbox',cookie)
return 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 inReplyTo=None
if authorized and '?replyto=' in self.path: if authorized and '?replyto=' in self.path:
inReplyTo=self.path.split('?replyto=')[1] inReplyTo=self.path.split('?replyto=')[1]

15
like.py
View File

@ -59,6 +59,21 @@ def undoLikesCollectionEntry(postFilename: str,objectUrl: str, actor: str,debug:
with open(postFilename, 'w') as fp: with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=True) 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: def updateLikesCollection(postFilename: str,objectUrl: str, actor: str,debug: bool) -> None:
"""Updates the likes collection within a post """Updates the likes collection within a post
""" """

View File

@ -22,6 +22,7 @@ from posts import getUserUrl
from posts import parseUserFeed from posts import parseUserFeed
from session import getJson from session import getJson
from auth import createPassword from auth import createPassword
from like import likedByPerson
def htmlGetLoginCredentials(loginParams: str,lastLoginTime: int) -> (str,str): def htmlGetLoginCredentials(loginParams: str,lastLoginTime: int) -> (str,str):
"""Receives login credentials via HTTPServer POST """Receives login credentials via HTTPServer POST
@ -563,10 +564,15 @@ def individualPostAsHtml(baseDir: str, \
if fullDomain+'/users/'+nickname not in postJsonObject['actor']: if fullDomain+'/users/'+nickname not in postJsonObject['actor']:
announceStr= \ announceStr= \
'<a href="/users/'+nickname+'?repeat='+postJsonObject['object']['id']+'" title="Repeat this post">' \ '<a href="/users/'+nickname+'?repeat='+postJsonObject['object']['id']+'" title="Repeat this post">' \
'<img src="/icons/repeat_inactive.png"/></a>' '<img src="/icons/repeat_inactive.png"/></a>'
likeIcon='like_inactive.png'
likeLink='like'
if likedByPerson(postJsonObject,nickname,fullDomain):
likeIcon='like.png'
likeLink='unlike'
likeStr= \ likeStr= \
'<a href="/users/'+nickname+'?like='+postJsonObject['object']['id']+'" title="Like this post">' \ '<a href="/users/'+nickname+'?'+likeLink+'='+postJsonObject['object']['id']+'" title="Like this post">' \
'<img src="/icons/like_inactive.png"/></a>' '<img src="/icons/'+likeIcon+'"/></a>'
if showIcons: if showIcons:
footerStr='<div class="'+containerClassIcons+'">' footerStr='<div class="'+containerClassIcons+'">'