epicyon/bookmarks.py

667 lines
24 KiB
Python
Raw Normal View History

2020-04-01 22:22:51 +00:00
__filename__ = "bookmarks.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-04-01 22:22:51 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-01 22:22:51 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "Timeline"
2019-11-17 14:01:49 +00:00
import os
from pprint import pprint
2021-03-19 22:04:57 +00:00
from webfinger import webfingerHandle
from auth import createBasicAuthHeader
2021-12-26 18:17:37 +00:00
from utils import remove_domain_port
2021-12-26 12:19:00 +00:00
from utils import has_users_path
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
2021-12-27 11:20:57 +00:00
from utils import remove_id_ending
2021-12-27 11:05:24 +00:00
from utils import remove_post_from_cache
2019-11-17 14:01:49 +00:00
from utils import urlPermitted
from utils import getNicknameFromActor
2021-12-27 19:05:25 +00:00
from utils import get_domain_from_actor
2021-12-26 20:36:08 +00:00
from utils import locate_post
2021-12-26 23:41:34 +00:00
from utils import get_cached_post_filename
2021-12-26 15:13:34 +00:00
from utils import load_json
2021-12-26 14:47:21 +00:00
from utils import save_json
2021-12-26 10:57:03 +00:00
from utils import has_object_dict
2021-12-26 12:02:29 +00:00
from utils import acct_dir
2021-12-26 10:19:59 +00:00
from utils import local_actor_url
2021-12-26 17:15:04 +00:00
from utils import has_actor
2021-12-26 17:12:07 +00:00
from utils import has_object_stringType
2021-03-19 22:04:57 +00:00
from posts import getPersonBox
from session import postJson
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
2021-12-26 20:01:37 +00:00
def undoBookmarksCollectionEntry(recent_posts_cache: {},
2021-12-26 23:41:34 +00:00
base_dir: str, post_filename: str,
2020-04-01 22:22:51 +00:00
objectUrl: str,
actor: str, domain: str, debug: bool) -> None:
2019-11-17 14:01:49 +00:00
"""Undoes a bookmark for a particular actor
"""
2021-12-26 23:41:34 +00:00
post_json_object = load_json(post_filename)
2021-12-25 22:09:19 +00:00
if not post_json_object:
2019-11-18 15:21:35 +00:00
return
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
# remove any cached version of this post so that the
# bookmark icon is changed
nickname = getNicknameFromActor(actor)
2021-12-26 23:41:34 +00:00
cachedPostFilename = \
get_cached_post_filename(base_dir, nickname,
domain, post_json_object)
2019-11-29 23:04:37 +00:00
if cachedPostFilename:
if os.path.isfile(cachedPostFilename):
try:
os.remove(cachedPostFilename)
2021-11-25 18:42:38 +00:00
except OSError:
2021-10-29 16:31:20 +00:00
if debug:
print('EX: undoBookmarksCollectionEntry ' +
'unable to delete cached post file ' +
str(cachedPostFilename))
2021-12-27 11:05:24 +00:00
remove_post_from_cache(post_json_object, recent_posts_cache)
2019-11-18 15:21:35 +00:00
2020-05-21 22:12:31 +00:00
# remove from the index
2021-07-13 21:59:53 +00:00
bookmarksIndexFilename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
2020-05-21 22:12:31 +00:00
if not os.path.isfile(bookmarksIndexFilename):
return
2021-12-26 23:41:34 +00:00
if '/' in post_filename:
bookmarkIndex = post_filename.split('/')[-1].strip()
2020-05-21 22:12:31 +00:00
else:
2021-12-26 23:41:34 +00:00
bookmarkIndex = post_filename.strip()
2020-05-22 11:32:38 +00:00
bookmarkIndex = bookmarkIndex.replace('\n', '').replace('\r', '')
2020-05-21 22:12:31 +00:00
if bookmarkIndex not in open(bookmarksIndexFilename).read():
return
indexStr = ''
2021-11-26 12:28:20 +00:00
try:
with open(bookmarksIndexFilename, 'r') as indexFile:
indexStr = indexFile.read().replace(bookmarkIndex + '\n', '')
except OSError:
print('EX: unable to read ' + bookmarksIndexFilename)
if indexStr:
2021-11-25 18:42:38 +00:00
try:
with open(bookmarksIndexFilename, 'w+') as bookmarksIndexFile:
bookmarksIndexFile.write(indexStr)
except OSError:
2021-11-25 22:22:54 +00:00
print('EX: unable to write bookmarks index ' +
2021-11-25 18:42:38 +00:00
bookmarksIndexFilename)
2021-12-25 22:09:19 +00:00
if not post_json_object.get('type'):
2019-11-18 15:21:35 +00:00
return
2021-12-25 22:09:19 +00:00
if post_json_object['type'] != 'Create':
2019-11-18 15:21:35 +00:00
return
2021-12-26 10:57:03 +00:00
if not has_object_dict(post_json_object):
2019-11-18 15:21:35 +00:00
if debug:
2021-03-20 10:13:59 +00:00
print('DEBUG: bookmarked post has no object ' +
2021-12-25 22:09:19 +00:00
str(post_json_object))
2019-11-18 15:21:35 +00:00
return
2021-12-25 22:09:19 +00:00
if not post_json_object['object'].get('bookmarks'):
2019-11-18 15:21:35 +00:00
return
2021-12-25 22:09:19 +00:00
if not isinstance(post_json_object['object']['bookmarks'], dict):
2019-11-18 15:21:35 +00:00
return
2021-12-25 22:09:19 +00:00
if not post_json_object['object']['bookmarks'].get('items'):
2019-11-18 15:21:35 +00:00
return
2020-04-01 22:22:51 +00:00
totalItems = 0
2021-12-25 22:09:19 +00:00
if post_json_object['object']['bookmarks'].get('totalItems'):
totalItems = post_json_object['object']['bookmarks']['totalItems']
2020-04-01 22:22:51 +00:00
itemFound = False
2021-12-25 22:09:19 +00:00
for bookmarkItem in post_json_object['object']['bookmarks']['items']:
2019-11-18 15:21:35 +00:00
if bookmarkItem.get('actor'):
2020-04-01 22:22:51 +00:00
if bookmarkItem['actor'] == actor:
2019-11-17 14:01:49 +00:00
if debug:
2020-04-01 22:22:51 +00:00
print('DEBUG: bookmark was removed for ' + actor)
bmIt = bookmarkItem
2021-12-25 22:09:19 +00:00
post_json_object['object']['bookmarks']['items'].remove(bmIt)
2020-04-01 22:22:51 +00:00
itemFound = True
2019-11-18 15:21:35 +00:00
break
if not itemFound:
return
2020-04-01 22:22:51 +00:00
if totalItems == 1:
2019-11-18 15:21:35 +00:00
if debug:
print('DEBUG: bookmarks was removed from post')
2021-12-25 22:09:19 +00:00
del post_json_object['object']['bookmarks']
2019-11-18 15:21:35 +00:00
else:
2021-12-25 22:09:19 +00:00
bmItLen = len(post_json_object['object']['bookmarks']['items'])
post_json_object['object']['bookmarks']['totalItems'] = bmItLen
2021-12-26 23:41:34 +00:00
save_json(post_json_object, post_filename)
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
2021-12-25 22:09:19 +00:00
def bookmarkedByPerson(post_json_object: {},
nickname: str, domain: str) -> bool:
2019-11-17 14:01:49 +00:00
"""Returns True if the given post is bookmarked by the given person
"""
2021-12-25 22:09:19 +00:00
if _noOfBookmarks(post_json_object) == 0:
2019-11-17 14:01:49 +00:00
return False
2020-04-01 22:22:51 +00:00
actorMatch = domain + '/users/' + nickname
2021-12-25 22:09:19 +00:00
for item in post_json_object['object']['bookmarks']['items']:
2019-11-17 14:01:49 +00:00
if item['actor'].endswith(actorMatch):
return True
return False
2020-04-01 22:22:51 +00:00
2021-12-25 22:09:19 +00:00
def _noOfBookmarks(post_json_object: {}) -> int:
2019-11-17 14:01:49 +00:00
"""Returns the number of bookmarks ona given post
"""
2021-12-26 10:57:03 +00:00
if not has_object_dict(post_json_object):
2019-11-17 14:01:49 +00:00
return 0
2021-12-25 22:09:19 +00:00
if not post_json_object['object'].get('bookmarks'):
2019-11-17 14:01:49 +00:00
return 0
2021-12-25 22:09:19 +00:00
if not isinstance(post_json_object['object']['bookmarks'], dict):
2019-11-17 14:01:49 +00:00
return 0
2021-12-25 22:09:19 +00:00
if not post_json_object['object']['bookmarks'].get('items'):
post_json_object['object']['bookmarks']['items'] = []
post_json_object['object']['bookmarks']['totalItems'] = 0
return len(post_json_object['object']['bookmarks']['items'])
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
2021-12-26 20:01:37 +00:00
def updateBookmarksCollection(recent_posts_cache: {},
2021-12-26 23:41:34 +00:00
base_dir: str, post_filename: str,
2020-04-01 22:22:51 +00:00
objectUrl: str,
actor: str, domain: str, debug: bool) -> None:
2019-11-17 14:01:49 +00:00
"""Updates the bookmarks collection within a post
"""
2021-12-26 23:41:34 +00:00
post_json_object = load_json(post_filename)
2021-12-25 22:09:19 +00:00
if post_json_object:
2020-04-01 22:22:51 +00:00
# remove any cached version of this post so that the
# bookmark icon is changed
nickname = getNicknameFromActor(actor)
2021-12-26 23:41:34 +00:00
cachedPostFilename = \
get_cached_post_filename(base_dir, nickname,
domain, post_json_object)
2019-11-29 23:04:37 +00:00
if cachedPostFilename:
if os.path.isfile(cachedPostFilename):
try:
os.remove(cachedPostFilename)
2021-11-25 18:42:38 +00:00
except OSError:
2021-10-29 16:31:20 +00:00
if debug:
print('EX: updateBookmarksCollection ' +
'unable to delete cached post ' +
str(cachedPostFilename))
2021-12-27 11:05:24 +00:00
remove_post_from_cache(post_json_object, recent_posts_cache)
2019-11-17 14:01:49 +00:00
2021-12-25 22:09:19 +00:00
if not post_json_object.get('object'):
2019-11-17 14:01:49 +00:00
if debug:
2021-03-20 10:13:59 +00:00
print('DEBUG: no object in bookmarked post ' +
2021-12-25 22:09:19 +00:00
str(post_json_object))
2019-11-17 14:01:49 +00:00
return
if not objectUrl.endswith('/bookmarks'):
2020-04-01 22:22:51 +00:00
objectUrl = objectUrl + '/bookmarks'
2021-03-20 10:13:59 +00:00
# does this post have bookmarks on it from differenent actors?
2021-12-25 22:09:19 +00:00
if not post_json_object['object'].get('bookmarks'):
2019-11-17 14:01:49 +00:00
if debug:
2020-04-01 22:22:51 +00:00
print('DEBUG: Adding initial bookmarks to ' + objectUrl)
bookmarksJson = {
2019-11-18 16:03:54 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
'id': objectUrl,
'type': 'Collection',
"totalItems": 1,
'items': [{
'type': 'Bookmark',
'actor': actor
2020-03-22 21:16:02 +00:00
}]
2019-11-18 16:03:54 +00:00
}
2021-12-25 22:09:19 +00:00
post_json_object['object']['bookmarks'] = bookmarksJson
2019-11-17 14:01:49 +00:00
else:
2021-12-25 22:09:19 +00:00
if not post_json_object['object']['bookmarks'].get('items'):
post_json_object['object']['bookmarks']['items'] = []
bm_items = post_json_object['object']['bookmarks']['items']
for bookmarkItem in bm_items:
2019-11-17 14:01:49 +00:00
if bookmarkItem.get('actor'):
2020-04-01 22:22:51 +00:00
if bookmarkItem['actor'] == actor:
2019-11-17 14:01:49 +00:00
return
newBookmark = {
'type': 'Bookmark',
'actor': actor
}
nb = newBookmark
2021-12-25 22:09:19 +00:00
bmIt = len(post_json_object['object']['bookmarks']['items'])
post_json_object['object']['bookmarks']['items'].append(nb)
post_json_object['object']['bookmarks']['totalItems'] = bmIt
2019-11-17 14:01:49 +00:00
if debug:
print('DEBUG: saving post with bookmarks added')
2021-12-25 22:09:19 +00:00
pprint(post_json_object)
2019-11-17 14:01:49 +00:00
2021-12-26 23:41:34 +00:00
save_json(post_json_object, post_filename)
2019-11-17 14:01:49 +00:00
# prepend to the index
2021-07-13 21:59:53 +00:00
bookmarksIndexFilename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
2021-12-26 23:41:34 +00:00
bookmarkIndex = post_filename.split('/')[-1]
2019-11-17 14:01:49 +00:00
if os.path.isfile(bookmarksIndexFilename):
if bookmarkIndex not in open(bookmarksIndexFilename).read():
try:
2020-04-01 22:22:51 +00:00
with open(bookmarksIndexFilename, 'r+') as bmIndexFile:
content = bmIndexFile.read()
if bookmarkIndex + '\n' not in content:
bmIndexFile.seek(0, 0)
bmIndexFile.write(bookmarkIndex + '\n' + content)
if debug:
print('DEBUG: bookmark added to index')
2021-12-25 15:28:52 +00:00
except Exception as ex:
2020-04-01 22:22:51 +00:00
print('WARN: Failed to write entry to bookmarks index ' +
2021-12-25 15:28:52 +00:00
bookmarksIndexFilename + ' ' + str(ex))
2019-11-17 14:01:49 +00:00
else:
2021-11-25 18:42:38 +00:00
try:
with open(bookmarksIndexFilename, 'w+') as bookmarksIndexFile:
bookmarksIndexFile.write(bookmarkIndex + '\n')
except OSError:
2021-11-25 22:22:54 +00:00
print('EX: unable to write bookmarks index ' +
2021-11-25 18:42:38 +00:00
bookmarksIndexFilename)
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
2021-12-26 20:01:37 +00:00
def bookmark(recent_posts_cache: {},
2021-12-25 23:45:30 +00:00
session, base_dir: str, federation_list: [],
2020-04-01 22:22:51 +00:00
nickname: str, domain: str, port: int,
2021-12-25 17:09:22 +00:00
ccList: [], http_prefix: str,
2020-04-01 22:22:51 +00:00
objectUrl: str, actorBookmarked: str,
2021-12-25 20:39:35 +00:00
client_to_server: bool,
2021-12-25 21:37:41 +00:00
send_threads: [], postLog: [],
2021-12-25 22:28:18 +00:00
person_cache: {}, cached_webfingers: {},
2021-12-25 20:34:38 +00:00
debug: bool, project_version: str) -> {}:
2019-11-17 14:01:49 +00:00
"""Creates a bookmark
actor is the person doing the bookmarking
'to' might be a specific person (actor) whose post was bookmarked
object is typically the url of the message which was bookmarked
"""
2021-12-25 23:45:30 +00:00
if not urlPermitted(objectUrl, federation_list):
2019-11-17 14:01:49 +00:00
return None
2021-12-26 12:45:03 +00:00
fullDomain = get_full_domain(domain, port)
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
newBookmarkJson = {
2019-11-17 14:01:49 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
'type': 'Bookmark',
2021-12-26 10:19:59 +00:00
'actor': local_actor_url(http_prefix, nickname, fullDomain),
2019-11-17 14:01:49 +00:00
'object': objectUrl
}
if ccList:
2020-04-01 22:22:51 +00:00
if len(ccList) > 0:
newBookmarkJson['cc'] = ccList
2019-11-17 14:01:49 +00:00
# Extract the domain and nickname from a statuses link
2020-04-01 22:22:51 +00:00
bookmarkedPostNickname = None
bookmarkedPostDomain = None
bookmarkedPostPort = None
2019-11-17 14:01:49 +00:00
if actorBookmarked:
2020-04-01 22:22:51 +00:00
acBm = actorBookmarked
bookmarkedPostNickname = getNicknameFromActor(acBm)
2021-12-27 19:05:25 +00:00
bookmarkedPostDomain, bookmarkedPostPort = get_domain_from_actor(acBm)
2019-11-17 14:01:49 +00:00
else:
2021-12-26 12:19:00 +00:00
if has_users_path(objectUrl):
2020-04-01 22:22:51 +00:00
ou = objectUrl
bookmarkedPostNickname = getNicknameFromActor(ou)
2021-12-27 19:05:25 +00:00
bookmarkedPostDomain, bookmarkedPostPort = \
get_domain_from_actor(ou)
2019-11-17 14:01:49 +00:00
if bookmarkedPostNickname:
2021-12-26 23:41:34 +00:00
post_filename = locate_post(base_dir, nickname, domain, objectUrl)
if not post_filename:
2021-12-25 16:17:53 +00:00
print('DEBUG: bookmark base_dir: ' + base_dir)
2020-04-01 22:22:51 +00:00
print('DEBUG: bookmark nickname: ' + nickname)
print('DEBUG: bookmark domain: ' + domain)
print('DEBUG: bookmark objectUrl: ' + objectUrl)
2019-11-17 14:01:49 +00:00
return None
2020-03-22 21:16:02 +00:00
2021-12-26 20:01:37 +00:00
updateBookmarksCollection(recent_posts_cache,
2021-12-26 23:41:34 +00:00
base_dir, post_filename, objectUrl,
2020-04-01 22:22:51 +00:00
newBookmarkJson['actor'], domain, debug)
2019-11-17 14:01:49 +00:00
return newBookmarkJson
2020-04-01 22:22:51 +00:00
2021-12-26 20:01:37 +00:00
def undoBookmark(recent_posts_cache: {},
2021-12-25 23:45:30 +00:00
session, base_dir: str, federation_list: [],
2020-04-01 22:22:51 +00:00
nickname: str, domain: str, port: int,
2021-12-25 17:09:22 +00:00
ccList: [], http_prefix: str,
2020-04-01 22:22:51 +00:00
objectUrl: str, actorBookmarked: str,
2021-12-25 20:39:35 +00:00
client_to_server: bool,
2021-12-25 21:37:41 +00:00
send_threads: [], postLog: [],
2021-12-25 22:28:18 +00:00
person_cache: {}, cached_webfingers: {},
2021-12-25 20:34:38 +00:00
debug: bool, project_version: str) -> {}:
2019-11-17 14:01:49 +00:00
"""Removes a bookmark
actor is the person doing the bookmarking
'to' might be a specific person (actor) whose post was bookmarked
object is typically the url of the message which was bookmarked
"""
2021-12-25 23:45:30 +00:00
if not urlPermitted(objectUrl, federation_list):
2019-11-17 14:01:49 +00:00
return None
2021-12-26 12:45:03 +00:00
fullDomain = get_full_domain(domain, port)
2019-11-17 14:01:49 +00:00
2020-04-01 22:22:51 +00:00
newUndoBookmarkJson = {
2019-11-17 14:01:49 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
'type': 'Undo',
2021-12-26 10:19:59 +00:00
'actor': local_actor_url(http_prefix, nickname, fullDomain),
2019-11-17 14:01:49 +00:00
'object': {
'type': 'Bookmark',
2021-12-26 10:19:59 +00:00
'actor': local_actor_url(http_prefix, nickname, fullDomain),
2019-11-17 14:01:49 +00:00
'object': objectUrl
}
}
if ccList:
2020-04-01 22:22:51 +00:00
if len(ccList) > 0:
newUndoBookmarkJson['cc'] = ccList
newUndoBookmarkJson['object']['cc'] = ccList
2019-11-17 14:01:49 +00:00
# Extract the domain and nickname from a statuses link
2020-04-01 22:22:51 +00:00
bookmarkedPostNickname = None
bookmarkedPostDomain = None
bookmarkedPostPort = None
2019-11-17 14:01:49 +00:00
if actorBookmarked:
2020-04-01 22:22:51 +00:00
acBm = actorBookmarked
bookmarkedPostNickname = getNicknameFromActor(acBm)
2021-12-27 19:05:25 +00:00
bookmarkedPostDomain, bookmarkedPostPort = get_domain_from_actor(acBm)
2019-11-17 14:01:49 +00:00
else:
2021-12-26 12:19:00 +00:00
if has_users_path(objectUrl):
2020-04-01 22:22:51 +00:00
ou = objectUrl
bookmarkedPostNickname = getNicknameFromActor(ou)
2021-12-27 19:05:25 +00:00
bookmarkedPostDomain, bookmarkedPostPort = \
get_domain_from_actor(ou)
2019-11-17 14:01:49 +00:00
if bookmarkedPostNickname:
2021-12-26 23:41:34 +00:00
post_filename = locate_post(base_dir, nickname, domain, objectUrl)
if not post_filename:
2019-11-17 14:01:49 +00:00
return None
2021-12-26 20:01:37 +00:00
undoBookmarksCollectionEntry(recent_posts_cache,
2021-12-26 23:41:34 +00:00
base_dir, post_filename, objectUrl,
2020-04-01 22:22:51 +00:00
newUndoBookmarkJson['actor'],
domain, debug)
2019-11-17 14:01:49 +00:00
else:
return None
return newUndoBookmarkJson
2020-04-01 22:22:51 +00:00
2021-12-25 16:17:53 +00:00
def sendBookmarkViaServer(base_dir: str, session,
2021-03-19 22:04:57 +00:00
nickname: str, password: str,
domain: str, fromPort: int,
2021-12-25 17:09:22 +00:00
http_prefix: str, bookmarkUrl: str,
2021-12-25 22:28:18 +00:00
cached_webfingers: {}, person_cache: {},
2021-12-25 20:34:38 +00:00
debug: bool, project_version: str,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem: str) -> {}:
2021-03-19 22:04:57 +00:00
"""Creates a bookmark via c2s
"""
if not session:
print('WARN: No session for sendBookmarkViaServer')
return 6
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, fromPort)
2021-03-19 22:04:57 +00:00
2021-12-26 10:19:59 +00:00
actor = local_actor_url(http_prefix, nickname, domain_full)
2021-03-19 22:04:57 +00:00
newBookmarkJson = {
"@context": "https://www.w3.org/ns/activitystreams",
"type": "Add",
"actor": actor,
2021-03-20 12:20:17 +00:00
"to": [actor],
2021-03-19 22:04:57 +00:00
"object": {
"type": "Document",
2021-03-20 12:20:17 +00:00
"url": bookmarkUrl,
"to": [actor]
2021-03-19 22:04:57 +00:00
},
"target": actor + "/tlbookmarks"
}
2021-12-26 10:00:46 +00:00
handle = http_prefix + '://' + domain_full + '/@' + nickname
2021-03-19 22:04:57 +00:00
# lookup the inbox for the To handle
2021-12-25 17:09:22 +00:00
wfRequest = webfingerHandle(session, handle, http_prefix,
2021-12-25 22:28:18 +00:00
cached_webfingers,
2021-12-25 20:34:38 +00:00
domain, project_version, debug, False,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem)
2021-03-19 22:04:57 +00:00
if not wfRequest:
if debug:
print('DEBUG: bookmark webfinger failed for ' + handle)
return 1
if not isinstance(wfRequest, dict):
print('WARN: bookmark webfinger for ' + handle +
' did not return a dict. ' + str(wfRequest))
return 1
postToBox = 'outbox'
# get the actor inbox for the To handle
2021-09-15 14:05:08 +00:00
originDomain = domain
(inboxUrl, pubKeyId, pubKey, fromPersonId, sharedInbox, avatarUrl,
2021-12-25 23:03:28 +00:00
displayName, _) = getPersonBox(signing_priv_key_pem,
originDomain,
2021-12-25 16:17:53 +00:00
base_dir, session, wfRequest,
2021-12-25 22:17:49 +00:00
person_cache,
2021-12-25 20:34:38 +00:00
project_version, http_prefix,
nickname, domain,
postToBox, 58391)
2021-03-19 22:04:57 +00:00
if not inboxUrl:
if debug:
print('DEBUG: bookmark no ' + postToBox +
' was found for ' + handle)
return 3
if not fromPersonId:
if debug:
print('DEBUG: bookmark no actor was found for ' + handle)
return 4
authHeader = createBasicAuthHeader(nickname, password)
headers = {
'host': domain,
'Content-type': 'application/json',
'Authorization': authHeader
}
2021-12-26 10:00:46 +00:00
postResult = postJson(http_prefix, domain_full,
2021-06-20 13:39:53 +00:00
session, newBookmarkJson, [], inboxUrl,
2021-03-21 13:17:59 +00:00
headers, 3, True)
2021-03-19 22:04:57 +00:00
if not postResult:
if debug:
print('WARN: POST bookmark failed for c2s to ' + inboxUrl)
return 5
if debug:
print('DEBUG: c2s POST bookmark success')
return newBookmarkJson
2021-03-19 22:11:45 +00:00
2021-12-25 16:17:53 +00:00
def sendUndoBookmarkViaServer(base_dir: str, session,
2021-03-19 22:11:45 +00:00
nickname: str, password: str,
domain: str, fromPort: int,
2021-12-25 17:09:22 +00:00
http_prefix: str, bookmarkUrl: str,
2021-12-25 22:28:18 +00:00
cached_webfingers: {}, person_cache: {},
2021-12-25 20:34:38 +00:00
debug: bool, project_version: str,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem: str) -> {}:
2021-03-19 22:11:45 +00:00
"""Removes a bookmark via c2s
"""
if not session:
print('WARN: No session for sendUndoBookmarkViaServer')
return 6
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, fromPort)
2021-03-19 22:11:45 +00:00
2021-12-26 10:19:59 +00:00
actor = local_actor_url(http_prefix, nickname, domain_full)
2021-03-19 22:11:45 +00:00
newBookmarkJson = {
"@context": "https://www.w3.org/ns/activitystreams",
"type": "Remove",
"actor": actor,
2021-03-20 14:32:25 +00:00
"to": [actor],
2021-03-19 22:11:45 +00:00
"object": {
"type": "Document",
2021-03-20 14:32:25 +00:00
"url": bookmarkUrl,
"to": [actor]
2021-03-19 22:11:45 +00:00
},
"target": actor + "/tlbookmarks"
}
2021-12-26 10:00:46 +00:00
handle = http_prefix + '://' + domain_full + '/@' + nickname
2021-03-19 22:11:45 +00:00
# lookup the inbox for the To handle
2021-12-25 17:09:22 +00:00
wfRequest = webfingerHandle(session, handle, http_prefix,
2021-12-25 22:28:18 +00:00
cached_webfingers,
2021-12-25 20:34:38 +00:00
domain, project_version, debug, False,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem)
2021-03-19 22:11:45 +00:00
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
2021-09-15 14:05:08 +00:00
originDomain = domain
(inboxUrl, pubKeyId, pubKey, fromPersonId, sharedInbox, avatarUrl,
2021-12-25 23:03:28 +00:00
displayName, _) = getPersonBox(signing_priv_key_pem,
originDomain,
2021-12-25 16:17:53 +00:00
base_dir, session, wfRequest,
2021-12-25 22:17:49 +00:00
person_cache,
2021-12-25 20:34:38 +00:00
project_version, http_prefix,
nickname, domain,
postToBox, 52594)
2021-03-19 22:11:45 +00:00
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
}
2021-12-26 10:00:46 +00:00
postResult = postJson(http_prefix, domain_full,
2021-06-20 13:39:53 +00:00
session, newBookmarkJson, [], inboxUrl,
2021-03-21 13:17:59 +00:00
headers, 3, True)
2021-03-19 22:11:45 +00:00
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
2021-03-20 09:49:43 +00:00
2021-12-26 20:01:37 +00:00
def outboxBookmark(recent_posts_cache: {},
2021-12-25 17:09:22 +00:00
base_dir: str, http_prefix: str,
2021-03-20 09:49:43 +00:00
nickname: str, domain: str, port: int,
2021-12-25 23:51:19 +00:00
message_json: {}, debug: bool) -> None:
2021-03-20 09:49:43 +00:00
""" When a bookmark request is received by the outbox from c2s
"""
2021-12-25 23:51:19 +00:00
if not message_json.get('type'):
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if message_json['type'] != 'Add':
2021-03-20 09:49:43 +00:00
return
2021-12-26 17:15:04 +00:00
if not has_actor(message_json, debug):
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if not message_json.get('target'):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: no target in bookmark Add')
return
2021-12-26 17:12:07 +00:00
if not has_object_stringType(message_json, debug):
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if not isinstance(message_json['target'], str):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: bookmark Add target is not string')
return
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2021-12-26 10:00:46 +00:00
if not message_json['target'].endswith('://' + domain_full +
2021-12-25 23:51:19 +00:00
'/users/' + nickname +
'/tlbookmarks'):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: bookmark Add target invalid ' +
2021-12-25 23:51:19 +00:00
message_json['target'])
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if message_json['object']['type'] != 'Document':
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: bookmark Add type is not Document')
return
2021-12-25 23:51:19 +00:00
if not message_json['object'].get('url'):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: bookmark Add missing url')
return
if debug:
print('DEBUG: c2s bookmark Add request arrived in outbox')
2021-12-27 11:20:57 +00:00
messageUrl = remove_id_ending(message_json['object']['url'])
2021-12-26 18:17:37 +00:00
domain = remove_domain_port(domain)
2021-12-26 23:41:34 +00:00
post_filename = locate_post(base_dir, nickname, domain, messageUrl)
if not post_filename:
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: c2s like post not found in inbox or outbox')
print(messageUrl)
return True
2021-12-26 20:01:37 +00:00
updateBookmarksCollection(recent_posts_cache,
2021-12-26 23:41:34 +00:00
base_dir, post_filename, messageUrl,
2021-12-25 23:51:19 +00:00
message_json['actor'], domain, debug)
2021-03-20 09:49:43 +00:00
if debug:
2021-12-26 23:41:34 +00:00
print('DEBUG: post bookmarked via c2s - ' + post_filename)
2021-03-20 09:49:43 +00:00
2021-12-26 20:01:37 +00:00
def outboxUndoBookmark(recent_posts_cache: {},
2021-12-25 17:09:22 +00:00
base_dir: str, http_prefix: str,
2021-03-20 09:49:43 +00:00
nickname: str, domain: str, port: int,
2021-12-25 23:51:19 +00:00
message_json: {}, debug: bool) -> None:
2021-03-20 09:49:43 +00:00
""" When an undo bookmark request is received by the outbox from c2s
"""
2021-12-25 23:51:19 +00:00
if not message_json.get('type'):
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if message_json['type'] != 'Remove':
2021-03-20 09:49:43 +00:00
return
2021-12-26 17:15:04 +00:00
if not has_actor(message_json, debug):
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if not message_json.get('target'):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: no target in unbookmark Remove')
return
2021-12-26 17:12:07 +00:00
if not has_object_stringType(message_json, debug):
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if not isinstance(message_json['target'], str):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: unbookmark Remove target is not string')
return
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2021-12-26 10:00:46 +00:00
if not message_json['target'].endswith('://' + domain_full +
2021-12-25 23:51:19 +00:00
'/users/' + nickname +
'/tlbookmarks'):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: unbookmark Remove target invalid ' +
2021-12-25 23:51:19 +00:00
message_json['target'])
2021-03-20 09:49:43 +00:00
return
2021-12-25 23:51:19 +00:00
if message_json['object']['type'] != 'Document':
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: unbookmark Remove type is not Document')
return
2021-12-25 23:51:19 +00:00
if not message_json['object'].get('url'):
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: unbookmark Remove missing url')
return
if debug:
print('DEBUG: c2s unbookmark Remove request arrived in outbox')
2021-12-27 11:20:57 +00:00
messageUrl = remove_id_ending(message_json['object']['url'])
2021-12-26 18:17:37 +00:00
domain = remove_domain_port(domain)
2021-12-26 23:41:34 +00:00
post_filename = locate_post(base_dir, nickname, domain, messageUrl)
if not post_filename:
2021-03-20 09:49:43 +00:00
if debug:
print('DEBUG: c2s unbookmark post not found in inbox or outbox')
print(messageUrl)
return True
2021-12-26 20:01:37 +00:00
updateBookmarksCollection(recent_posts_cache,
2021-12-26 23:41:34 +00:00
base_dir, post_filename, messageUrl,
2021-12-25 23:51:19 +00:00
message_json['actor'], domain, debug)
2021-03-20 09:49:43 +00:00
if debug:
2021-12-26 23:41:34 +00:00
print('DEBUG: post unbookmarked via c2s - ' + post_filename)