From ab62e047215d4c3a550c07dbc6e00eeb03a2b92d Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 17 Jul 2019 20:04:00 +0100 Subject: [PATCH] Undoing likes --- epicyon.py | 28 +++++++++++++++++++++ like.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/epicyon.py b/epicyon.py index 950e2000a..e286bb192 100644 --- a/epicyon.py +++ b/epicyon.py @@ -63,6 +63,8 @@ from utils import getDomainFromActor from utils import getNicknameFromActor from media import archiveMedia from delete import sendDeleteViaServer +from like import sendLikeViaServer +from like import sendUndoLikeViaServer import argparse def str2bool(v): @@ -171,6 +173,8 @@ parser.add_argument('--repeat','--announce', dest='announce', type=str,default=N help='Announce/repeat a url') parser.add_argument('--favorite','--like', dest='like', type=str,default=None, \ help='Like a url') +parser.add_argument('--undolike','--unlike', dest='undolike', type=str,default=None, \ + help='Undo a like of a url') parser.add_argument('--sendto', nargs='+',dest='sendto', \ help='List of post recipients') parser.add_argument('--attach', dest='attach', type=str,default=None, \ @@ -411,6 +415,30 @@ if args.like: time.sleep(1) sys.exit() +if args.undolike: + if not nickname: + print('Specify a nickname with the --nickname option') + sys.exit() + + if not args.password: + print('Specify a password with the --password option') + sys.exit() + + session = createSession(domain,port,useTor) + personCache={} + cachedWebfingers={} + print('Sending undo like of '+args.undolike) + + sendUndoLikeViaServer(session,nickname,args.password, + domain,port, \ + httpPrefix,args.undolike, \ + cachedWebfingers,personCache, \ + True) + for i in range(10): + # TODO detect send success/fail + time.sleep(1) + sys.exit() + if args.delete: if not nickname: print('Specify a nickname with the --nickname option') diff --git a/like.py b/like.py index c333453c7..8d766451a 100644 --- a/like.py +++ b/like.py @@ -289,7 +289,7 @@ def sendLikeViaServer(session,fromNickname: str,password: str, newLikeJson = { 'type': 'Like', - 'actor': httpPrefix+'://'+fullDomain+'/users/'+nickname, + 'actor': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname, 'object': likeUrl, 'to': [toUrl], 'cc': [ccUrl] @@ -335,3 +335,74 @@ def sendLikeViaServer(session,fromNickname: str,password: str, print('DEBUG: c2s POST like success') return newLikeJson + +def sendUndoLikeViaServer(session,fromNickname: str,password: str, + fromDomain: str,fromPort: int, \ + httpPrefix: str,likeUrl: str, \ + cachedWebfingers: {},personCache: {}, \ + debug: bool) -> {}: + """Undo a like via c2s + """ + if not session: + print('WARN: No session for sendUndoLikeViaServer') + return 6 + + fromDomainFull=fromDomain + if fromPort!=80 and fromPort!=443: + fromDomainFull=fromDomain+':'+str(fromPort) + + toUrl = 'https://www.w3.org/ns/activitystreams#Public' + ccUrl = httpPrefix + '://'+fromDomainFull+'/users/'+fromNickname+'/followers' + + newUndoLikeJson = { + 'type': 'Undo', + 'actor': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname, + 'object': { + 'type': 'Like', + 'actor': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname, + 'object': likeUrl, + 'to': [toUrl], + 'cc': [ccUrl] + } + } + + handle=httpPrefix+'://'+fromDomainFull+'/@'+fromNickname + + # lookup the inbox for the To handle + wfRequest = webfingerHandle(session,handle,httpPrefix,cachedWebfingers) + if not wfRequest: + if debug: + print('DEBUG: announce webfinger failed for '+handle) + return 1 + + postToBox='outbox' + + # get the actor inbox for the To handle + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + getPersonBox(session,wfRequest,personCache,postToBox) + + if not inboxUrl: + if debug: + print('DEBUG: No '+postToBox+' was found for '+handle) + return 3 + if not fromPersonId: + if debug: + print('DEBUG: No actor was found for '+handle) + return 4 + + authHeader=createBasicAuthHeader(fromNickname,password) + + headers = {'host': fromDomain, \ + 'Content-type': 'application/json', \ + 'Authorization': authHeader} + postResult = \ + postJson(session,newUndoLikeJson,[],inboxUrl,headers,"inbox:write") + #if not postResult: + # if debug: + # print('DEBUG: POST announce failed for c2s to '+inboxUrl) + # return 5 + + if debug: + print('DEBUG: c2s POST undo like success') + + return newUndoLikeJson