From 6f196d5fc30da37a994dde2d3831155489f007e4 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 19 Mar 2021 22:11:45 +0000 Subject: [PATCH] Undo bookmark request via c2s --- bookmarks.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++ desktop_client.py | 32 ++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/bookmarks.py b/bookmarks.py index eb68eb7f2..084b938cf 100644 --- a/bookmarks.py +++ b/bookmarks.py @@ -535,3 +535,85 @@ def sendBookmarkViaServer(baseDir: str, session, print('DEBUG: c2s POST bookmark success') return newBookmarkJson + + +def sendUndoBookmarkViaServer(baseDir: str, session, + nickname: str, password: str, + domain: str, fromPort: int, + httpPrefix: str, bookmarkUrl: str, + cachedWebfingers: {}, personCache: {}, + debug: bool, projectVersion: str) -> {}: + """Removes a bookmark via c2s + """ + if not session: + print('WARN: No session for sendUndoBookmarkViaServer') + return 6 + + domainFull = getFullDomain(domain, fromPort) + + actor = httpPrefix + '://' + domainFull + '/users/' + nickname + + newBookmarkJson = { + "@context": "https://www.w3.org/ns/activitystreams", + "type": "Remove", + "actor": actor, + "object": { + "type": "Document", + "url": bookmarkUrl + }, + "target": actor + "/tlbookmarks" + } + + handle = httpPrefix + '://' + domainFull + '/@' + nickname + + # lookup the inbox for the To handle + wfRequest = webfingerHandle(session, handle, httpPrefix, + cachedWebfingers, + domain, projectVersion, debug) + if not wfRequest: + if debug: + print('DEBUG: unbookmark webfinger failed for ' + handle) + return 1 + if not isinstance(wfRequest, dict): + print('WARN: unbookmark webfinger for ' + handle + + ' did not return a dict. ' + str(wfRequest)) + return 1 + + postToBox = 'outbox' + + # get the actor inbox for the To handle + (inboxUrl, pubKeyId, pubKey, fromPersonId, sharedInbox, + avatarUrl, displayName) = getPersonBox(baseDir, session, wfRequest, + personCache, + projectVersion, httpPrefix, + nickname, domain, + postToBox, 52594) + + if not inboxUrl: + if debug: + print('DEBUG: unbookmark no ' + postToBox + + ' was found for ' + handle) + return 3 + if not fromPersonId: + if debug: + print('DEBUG: unbookmark no actor was found for ' + handle) + return 4 + + authHeader = createBasicAuthHeader(nickname, password) + + headers = { + 'host': domain, + 'Content-type': 'application/json', + 'Authorization': authHeader + } + postResult = postJson(session, newBookmarkJson, [], inboxUrl, + headers, 30, True) + if not postResult: + if debug: + print('WARN: POST unbookmark failed for c2s to ' + inboxUrl) + return 5 + + if debug: + print('DEBUG: c2s POST unbookmark success') + + return newBookmarkJson diff --git a/desktop_client.py b/desktop_client.py index d15f5fdee..fc2ce2452 100644 --- a/desktop_client.py +++ b/desktop_client.py @@ -40,6 +40,7 @@ from pgp import pgpEncryptToActor from pgp import pgpPublicKeyUpload from like import noOfLikes from bookmarks import sendBookmarkViaServer +from bookmarks import sendUndoBookmarkViaServer def _desktopHelp() -> None: @@ -1207,6 +1208,37 @@ def runDesktopClient(baseDir: str, proxyType: str, httpPrefix: str, cachedWebfingers, personCache, False, __version__) print('') + elif (commandStr == 'undo bookmark' or + commandStr == 'undo bm' or + commandStr == 'unbookmark' or + commandStr == 'unbm' or + commandStr.startswith('unbookmark ') or + commandStr.startswith('unbm ')): + currIndex = 0 + if ' ' in commandStr: + postIndex = commandStr.split(' ')[-1].strip() + if postIndex.isdigit(): + currIndex = int(postIndex) + if currIndex > 0 and boxJson: + postJsonObject = \ + _desktopGetBoxPostObject(boxJson, currIndex) + if postJsonObject: + if postJsonObject.get('id'): + likeActor = postJsonObject['object']['attributedTo'] + sayStr = 'Unbookmarking post by ' + \ + getNicknameFromActor(likeActor) + _sayCommand(sayStr, sayStr, + screenreader, + systemLanguage, espeak) + sessionLike = createSession(proxyType) + sendUndoBookmarkViaServer(baseDir, sessionLike, + nickname, password, + domain, port, httpPrefix, + postJsonObject['id'], + cachedWebfingers, + personCache, + False, __version__) + print('') elif commandStr == 'unlike' or commandStr == 'undo like': currIndex = 0 if ' ' in commandStr: