mirror of https://gitlab.com/bashrc2/epicyon
Snake case
parent
ba49308f23
commit
5789c20bf9
368
bookmarks.py
368
bookmarks.py
|
@ -34,7 +34,7 @@ from session import post_json
|
||||||
|
|
||||||
def undo_bookmarks_collection_entry(recent_posts_cache: {},
|
def undo_bookmarks_collection_entry(recent_posts_cache: {},
|
||||||
base_dir: str, post_filename: str,
|
base_dir: str, post_filename: str,
|
||||||
objectUrl: str,
|
object_url: str,
|
||||||
actor: str, domain: str,
|
actor: str, domain: str,
|
||||||
debug: bool) -> None:
|
debug: bool) -> None:
|
||||||
"""Undoes a bookmark for a particular actor
|
"""Undoes a bookmark for a particular actor
|
||||||
|
@ -46,45 +46,45 @@ def undo_bookmarks_collection_entry(recent_posts_cache: {},
|
||||||
# remove any cached version of this post so that the
|
# remove any cached version of this post so that the
|
||||||
# bookmark icon is changed
|
# bookmark icon is changed
|
||||||
nickname = get_nickname_from_actor(actor)
|
nickname = get_nickname_from_actor(actor)
|
||||||
cachedPostFilename = \
|
cached_post_filename = \
|
||||||
get_cached_post_filename(base_dir, nickname,
|
get_cached_post_filename(base_dir, nickname,
|
||||||
domain, post_json_object)
|
domain, post_json_object)
|
||||||
if cachedPostFilename:
|
if cached_post_filename:
|
||||||
if os.path.isfile(cachedPostFilename):
|
if os.path.isfile(cached_post_filename):
|
||||||
try:
|
try:
|
||||||
os.remove(cachedPostFilename)
|
os.remove(cached_post_filename)
|
||||||
except OSError:
|
except OSError:
|
||||||
if debug:
|
if debug:
|
||||||
print('EX: undo_bookmarks_collection_entry ' +
|
print('EX: undo_bookmarks_collection_entry ' +
|
||||||
'unable to delete cached post file ' +
|
'unable to delete cached post file ' +
|
||||||
str(cachedPostFilename))
|
str(cached_post_filename))
|
||||||
remove_post_from_cache(post_json_object, recent_posts_cache)
|
remove_post_from_cache(post_json_object, recent_posts_cache)
|
||||||
|
|
||||||
# remove from the index
|
# remove from the index
|
||||||
bookmarksIndexFilename = \
|
bookmarks_index_filename = \
|
||||||
acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
|
acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
|
||||||
if not os.path.isfile(bookmarksIndexFilename):
|
if not os.path.isfile(bookmarks_index_filename):
|
||||||
return
|
return
|
||||||
if '/' in post_filename:
|
if '/' in post_filename:
|
||||||
bookmarkIndex = post_filename.split('/')[-1].strip()
|
bookmark_index = post_filename.split('/')[-1].strip()
|
||||||
else:
|
else:
|
||||||
bookmarkIndex = post_filename.strip()
|
bookmark_index = post_filename.strip()
|
||||||
bookmarkIndex = bookmarkIndex.replace('\n', '').replace('\r', '')
|
bookmark_index = bookmark_index.replace('\n', '').replace('\r', '')
|
||||||
if bookmarkIndex not in open(bookmarksIndexFilename).read():
|
if bookmark_index not in open(bookmarks_index_filename).read():
|
||||||
return
|
return
|
||||||
indexStr = ''
|
index_str = ''
|
||||||
try:
|
try:
|
||||||
with open(bookmarksIndexFilename, 'r') as indexFile:
|
with open(bookmarks_index_filename, 'r') as index_file:
|
||||||
indexStr = indexFile.read().replace(bookmarkIndex + '\n', '')
|
index_str = index_file.read().replace(bookmark_index + '\n', '')
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: unable to read ' + bookmarksIndexFilename)
|
print('EX: unable to read ' + bookmarks_index_filename)
|
||||||
if indexStr:
|
if index_str:
|
||||||
try:
|
try:
|
||||||
with open(bookmarksIndexFilename, 'w+') as bookmarksIndexFile:
|
with open(bookmarks_index_filename, 'w+') as bmi_file:
|
||||||
bookmarksIndexFile.write(indexStr)
|
bmi_file.write(index_str)
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: unable to write bookmarks index ' +
|
print('EX: unable to write bookmarks index ' +
|
||||||
bookmarksIndexFilename)
|
bookmarks_index_filename)
|
||||||
if not post_json_object.get('type'):
|
if not post_json_object.get('type'):
|
||||||
return
|
return
|
||||||
if post_json_object['type'] != 'Create':
|
if post_json_object['type'] != 'Create':
|
||||||
|
@ -100,30 +100,30 @@ def undo_bookmarks_collection_entry(recent_posts_cache: {},
|
||||||
return
|
return
|
||||||
if not post_json_object['object']['bookmarks'].get('items'):
|
if not post_json_object['object']['bookmarks'].get('items'):
|
||||||
return
|
return
|
||||||
totalItems = 0
|
total_items = 0
|
||||||
if post_json_object['object']['bookmarks'].get('totalItems'):
|
if post_json_object['object']['bookmarks'].get('totalItems'):
|
||||||
totalItems = post_json_object['object']['bookmarks']['totalItems']
|
total_items = post_json_object['object']['bookmarks']['totalItems']
|
||||||
itemFound = False
|
item_found = False
|
||||||
for bookmarkItem in post_json_object['object']['bookmarks']['items']:
|
for bookmark_item in post_json_object['object']['bookmarks']['items']:
|
||||||
if bookmarkItem.get('actor'):
|
if bookmark_item.get('actor'):
|
||||||
if bookmarkItem['actor'] == actor:
|
if bookmark_item['actor'] == actor:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: bookmark was removed for ' + actor)
|
print('DEBUG: bookmark was removed for ' + actor)
|
||||||
bmIt = bookmarkItem
|
bm_it = bookmark_item
|
||||||
post_json_object['object']['bookmarks']['items'].remove(bmIt)
|
post_json_object['object']['bookmarks']['items'].remove(bm_it)
|
||||||
itemFound = True
|
item_found = True
|
||||||
break
|
break
|
||||||
|
|
||||||
if not itemFound:
|
if not item_found:
|
||||||
return
|
return
|
||||||
|
|
||||||
if totalItems == 1:
|
if total_items == 1:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: bookmarks was removed from post')
|
print('DEBUG: bookmarks was removed from post')
|
||||||
del post_json_object['object']['bookmarks']
|
del post_json_object['object']['bookmarks']
|
||||||
else:
|
else:
|
||||||
bmItLen = len(post_json_object['object']['bookmarks']['items'])
|
bm_it_len = len(post_json_object['object']['bookmarks']['items'])
|
||||||
post_json_object['object']['bookmarks']['totalItems'] = bmItLen
|
post_json_object['object']['bookmarks']['totalItems'] = bm_it_len
|
||||||
save_json(post_json_object, post_filename)
|
save_json(post_json_object, post_filename)
|
||||||
|
|
||||||
|
|
||||||
|
@ -133,9 +133,9 @@ def bookmarked_by_person(post_json_object: {},
|
||||||
"""
|
"""
|
||||||
if _no_of_bookmarks(post_json_object) == 0:
|
if _no_of_bookmarks(post_json_object) == 0:
|
||||||
return False
|
return False
|
||||||
actorMatch = domain + '/users/' + nickname
|
actor_match = domain + '/users/' + nickname
|
||||||
for item in post_json_object['object']['bookmarks']['items']:
|
for item in post_json_object['object']['bookmarks']['items']:
|
||||||
if item['actor'].endswith(actorMatch):
|
if item['actor'].endswith(actor_match):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -157,7 +157,7 @@ def _no_of_bookmarks(post_json_object: {}) -> int:
|
||||||
|
|
||||||
def update_bookmarks_collection(recent_posts_cache: {},
|
def update_bookmarks_collection(recent_posts_cache: {},
|
||||||
base_dir: str, post_filename: str,
|
base_dir: str, post_filename: str,
|
||||||
objectUrl: str,
|
object_url: str,
|
||||||
actor: str, domain: str, debug: bool) -> None:
|
actor: str, domain: str, debug: bool) -> None:
|
||||||
"""Updates the bookmarks collection within a post
|
"""Updates the bookmarks collection within a post
|
||||||
"""
|
"""
|
||||||
|
@ -166,18 +166,18 @@ def update_bookmarks_collection(recent_posts_cache: {},
|
||||||
# remove any cached version of this post so that the
|
# remove any cached version of this post so that the
|
||||||
# bookmark icon is changed
|
# bookmark icon is changed
|
||||||
nickname = get_nickname_from_actor(actor)
|
nickname = get_nickname_from_actor(actor)
|
||||||
cachedPostFilename = \
|
cached_post_filename = \
|
||||||
get_cached_post_filename(base_dir, nickname,
|
get_cached_post_filename(base_dir, nickname,
|
||||||
domain, post_json_object)
|
domain, post_json_object)
|
||||||
if cachedPostFilename:
|
if cached_post_filename:
|
||||||
if os.path.isfile(cachedPostFilename):
|
if os.path.isfile(cached_post_filename):
|
||||||
try:
|
try:
|
||||||
os.remove(cachedPostFilename)
|
os.remove(cached_post_filename)
|
||||||
except OSError:
|
except OSError:
|
||||||
if debug:
|
if debug:
|
||||||
print('EX: update_bookmarks_collection ' +
|
print('EX: update_bookmarks_collection ' +
|
||||||
'unable to delete cached post ' +
|
'unable to delete cached post ' +
|
||||||
str(cachedPostFilename))
|
str(cached_post_filename))
|
||||||
remove_post_from_cache(post_json_object, recent_posts_cache)
|
remove_post_from_cache(post_json_object, recent_posts_cache)
|
||||||
|
|
||||||
if not post_json_object.get('object'):
|
if not post_json_object.get('object'):
|
||||||
|
@ -185,15 +185,15 @@ def update_bookmarks_collection(recent_posts_cache: {},
|
||||||
print('DEBUG: no object in bookmarked post ' +
|
print('DEBUG: no object in bookmarked post ' +
|
||||||
str(post_json_object))
|
str(post_json_object))
|
||||||
return
|
return
|
||||||
if not objectUrl.endswith('/bookmarks'):
|
if not object_url.endswith('/bookmarks'):
|
||||||
objectUrl = objectUrl + '/bookmarks'
|
object_url = object_url + '/bookmarks'
|
||||||
# does this post have bookmarks on it from differenent actors?
|
# does this post have bookmarks on it from differenent actors?
|
||||||
if not post_json_object['object'].get('bookmarks'):
|
if not post_json_object['object'].get('bookmarks'):
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: Adding initial bookmarks to ' + objectUrl)
|
print('DEBUG: Adding initial bookmarks to ' + object_url)
|
||||||
bookmarksJson = {
|
bookmarks_json = {
|
||||||
"@context": "https://www.w3.org/ns/activitystreams",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
'id': objectUrl,
|
'id': object_url,
|
||||||
'type': 'Collection',
|
'type': 'Collection',
|
||||||
"totalItems": 1,
|
"totalItems": 1,
|
||||||
'items': [{
|
'items': [{
|
||||||
|
@ -201,23 +201,23 @@ def update_bookmarks_collection(recent_posts_cache: {},
|
||||||
'actor': actor
|
'actor': actor
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
post_json_object['object']['bookmarks'] = bookmarksJson
|
post_json_object['object']['bookmarks'] = bookmarks_json
|
||||||
else:
|
else:
|
||||||
if not post_json_object['object']['bookmarks'].get('items'):
|
if not post_json_object['object']['bookmarks'].get('items'):
|
||||||
post_json_object['object']['bookmarks']['items'] = []
|
post_json_object['object']['bookmarks']['items'] = []
|
||||||
bm_items = post_json_object['object']['bookmarks']['items']
|
bm_items = post_json_object['object']['bookmarks']['items']
|
||||||
for bookmarkItem in bm_items:
|
for bookmark_item in bm_items:
|
||||||
if bookmarkItem.get('actor'):
|
if bookmark_item.get('actor'):
|
||||||
if bookmarkItem['actor'] == actor:
|
if bookmark_item['actor'] == actor:
|
||||||
return
|
return
|
||||||
newBookmark = {
|
new_bookmark = {
|
||||||
'type': 'Bookmark',
|
'type': 'Bookmark',
|
||||||
'actor': actor
|
'actor': actor
|
||||||
}
|
}
|
||||||
nb = newBookmark
|
nbook = new_bookmark
|
||||||
bmIt = len(post_json_object['object']['bookmarks']['items'])
|
bm_it = len(post_json_object['object']['bookmarks']['items'])
|
||||||
post_json_object['object']['bookmarks']['items'].append(nb)
|
post_json_object['object']['bookmarks']['items'].append(nbook)
|
||||||
post_json_object['object']['bookmarks']['totalItems'] = bmIt
|
post_json_object['object']['bookmarks']['totalItems'] = bm_it
|
||||||
|
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: saving post with bookmarks added')
|
print('DEBUG: saving post with bookmarks added')
|
||||||
|
@ -226,38 +226,38 @@ def update_bookmarks_collection(recent_posts_cache: {},
|
||||||
save_json(post_json_object, post_filename)
|
save_json(post_json_object, post_filename)
|
||||||
|
|
||||||
# prepend to the index
|
# prepend to the index
|
||||||
bookmarksIndexFilename = \
|
bookmarks_index_filename = \
|
||||||
acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
|
acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
|
||||||
bookmarkIndex = post_filename.split('/')[-1]
|
bookmark_index = post_filename.split('/')[-1]
|
||||||
if os.path.isfile(bookmarksIndexFilename):
|
if os.path.isfile(bookmarks_index_filename):
|
||||||
if bookmarkIndex not in open(bookmarksIndexFilename).read():
|
if bookmark_index not in open(bookmarks_index_filename).read():
|
||||||
try:
|
try:
|
||||||
with open(bookmarksIndexFilename, 'r+') as bmIndexFile:
|
with open(bookmarks_index_filename, 'r+') as bmi_file:
|
||||||
content = bmIndexFile.read()
|
content = bmi_file.read()
|
||||||
if bookmarkIndex + '\n' not in content:
|
if bookmark_index + '\n' not in content:
|
||||||
bmIndexFile.seek(0, 0)
|
bmi_file.seek(0, 0)
|
||||||
bmIndexFile.write(bookmarkIndex + '\n' + content)
|
bmi_file.write(bookmark_index + '\n' + content)
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: bookmark added to index')
|
print('DEBUG: bookmark added to index')
|
||||||
except Exception as ex:
|
except OSError as ex:
|
||||||
print('WARN: Failed to write entry to bookmarks index ' +
|
print('WARN: Failed to write entry to bookmarks index ' +
|
||||||
bookmarksIndexFilename + ' ' + str(ex))
|
bookmarks_index_filename + ' ' + str(ex))
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
with open(bookmarksIndexFilename, 'w+') as bookmarksIndexFile:
|
with open(bookmarks_index_filename, 'w+') as bm_file:
|
||||||
bookmarksIndexFile.write(bookmarkIndex + '\n')
|
bm_file.write(bookmark_index + '\n')
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: unable to write bookmarks index ' +
|
print('EX: unable to write bookmarks index ' +
|
||||||
bookmarksIndexFilename)
|
bookmarks_index_filename)
|
||||||
|
|
||||||
|
|
||||||
def bookmark_post(recent_posts_cache: {},
|
def bookmark_post(recent_posts_cache: {},
|
||||||
session, base_dir: str, federation_list: [],
|
session, base_dir: str, federation_list: [],
|
||||||
nickname: str, domain: str, port: int,
|
nickname: str, domain: str, port: int,
|
||||||
ccList: [], http_prefix: str,
|
ccList: [], http_prefix: str,
|
||||||
objectUrl: str, actorBookmarked: str,
|
object_url: str, actorBookmarked: str,
|
||||||
client_to_server: bool,
|
client_to_server: bool,
|
||||||
send_threads: [], postLog: [],
|
send_threads: [], post_log: [],
|
||||||
person_cache: {}, cached_webfingers: {},
|
person_cache: {}, cached_webfingers: {},
|
||||||
debug: bool, project_version: str) -> {}:
|
debug: bool, project_version: str) -> {}:
|
||||||
"""Creates a bookmark
|
"""Creates a bookmark
|
||||||
|
@ -265,59 +265,56 @@ def bookmark_post(recent_posts_cache: {},
|
||||||
'to' might be a specific person (actor) whose post was bookmarked
|
'to' might be a specific person (actor) whose post was bookmarked
|
||||||
object is typically the url of the message which was bookmarked
|
object is typically the url of the message which was bookmarked
|
||||||
"""
|
"""
|
||||||
if not url_permitted(objectUrl, federation_list):
|
if not url_permitted(object_url, federation_list):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
fullDomain = get_full_domain(domain, port)
|
full_domain = get_full_domain(domain, port)
|
||||||
|
|
||||||
newBookmarkJson = {
|
new_bookmark_json = {
|
||||||
"@context": "https://www.w3.org/ns/activitystreams",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
'type': 'Bookmark',
|
'type': 'Bookmark',
|
||||||
'actor': local_actor_url(http_prefix, nickname, fullDomain),
|
'actor': local_actor_url(http_prefix, nickname, full_domain),
|
||||||
'object': objectUrl
|
'object': object_url
|
||||||
}
|
}
|
||||||
if ccList:
|
if ccList:
|
||||||
if len(ccList) > 0:
|
if len(ccList) > 0:
|
||||||
newBookmarkJson['cc'] = ccList
|
new_bookmark_json['cc'] = ccList
|
||||||
|
|
||||||
# Extract the domain and nickname from a statuses link
|
# Extract the domain and nickname from a statuses link
|
||||||
bookmarkedPostNickname = None
|
bookmarked_post_nickname = None
|
||||||
bookmarkedPostDomain = None
|
|
||||||
bookmarkedPostPort = None
|
|
||||||
if actorBookmarked:
|
if actorBookmarked:
|
||||||
acBm = actorBookmarked
|
ac_bm = actorBookmarked
|
||||||
bookmarkedPostNickname = get_nickname_from_actor(acBm)
|
bookmarked_post_nickname = get_nickname_from_actor(ac_bm)
|
||||||
bookmarkedPostDomain, bookmarkedPostPort = get_domain_from_actor(acBm)
|
_, _ = get_domain_from_actor(ac_bm)
|
||||||
else:
|
else:
|
||||||
if has_users_path(objectUrl):
|
if has_users_path(object_url):
|
||||||
ou = objectUrl
|
ourl = object_url
|
||||||
bookmarkedPostNickname = get_nickname_from_actor(ou)
|
bookmarked_post_nickname = get_nickname_from_actor(ourl)
|
||||||
bookmarkedPostDomain, bookmarkedPostPort = \
|
_, _ = get_domain_from_actor(ourl)
|
||||||
get_domain_from_actor(ou)
|
|
||||||
|
|
||||||
if bookmarkedPostNickname:
|
if bookmarked_post_nickname:
|
||||||
post_filename = locate_post(base_dir, nickname, domain, objectUrl)
|
post_filename = locate_post(base_dir, nickname, domain, object_url)
|
||||||
if not post_filename:
|
if not post_filename:
|
||||||
print('DEBUG: bookmark base_dir: ' + base_dir)
|
print('DEBUG: bookmark base_dir: ' + base_dir)
|
||||||
print('DEBUG: bookmark nickname: ' + nickname)
|
print('DEBUG: bookmark nickname: ' + nickname)
|
||||||
print('DEBUG: bookmark domain: ' + domain)
|
print('DEBUG: bookmark domain: ' + domain)
|
||||||
print('DEBUG: bookmark objectUrl: ' + objectUrl)
|
print('DEBUG: bookmark object_url: ' + object_url)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
update_bookmarks_collection(recent_posts_cache,
|
update_bookmarks_collection(recent_posts_cache,
|
||||||
base_dir, post_filename, objectUrl,
|
base_dir, post_filename, object_url,
|
||||||
newBookmarkJson['actor'], domain, debug)
|
new_bookmark_json['actor'], domain, debug)
|
||||||
|
|
||||||
return newBookmarkJson
|
return new_bookmark_json
|
||||||
|
|
||||||
|
|
||||||
def undo_bookmark_post(recent_posts_cache: {},
|
def undo_bookmark_post(recent_posts_cache: {},
|
||||||
session, base_dir: str, federation_list: [],
|
session, base_dir: str, federation_list: [],
|
||||||
nickname: str, domain: str, port: int,
|
nickname: str, domain: str, port: int,
|
||||||
ccList: [], http_prefix: str,
|
ccList: [], http_prefix: str,
|
||||||
objectUrl: str, actorBookmarked: str,
|
object_url: str, actorBookmarked: str,
|
||||||
client_to_server: bool,
|
client_to_server: bool,
|
||||||
send_threads: [], postLog: [],
|
send_threads: [], post_log: [],
|
||||||
person_cache: {}, cached_webfingers: {},
|
person_cache: {}, cached_webfingers: {},
|
||||||
debug: bool, project_version: str) -> {}:
|
debug: bool, project_version: str) -> {}:
|
||||||
"""Removes a bookmark
|
"""Removes a bookmark
|
||||||
|
@ -325,54 +322,51 @@ def undo_bookmark_post(recent_posts_cache: {},
|
||||||
'to' might be a specific person (actor) whose post was bookmarked
|
'to' might be a specific person (actor) whose post was bookmarked
|
||||||
object is typically the url of the message which was bookmarked
|
object is typically the url of the message which was bookmarked
|
||||||
"""
|
"""
|
||||||
if not url_permitted(objectUrl, federation_list):
|
if not url_permitted(object_url, federation_list):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
fullDomain = get_full_domain(domain, port)
|
full_domain = get_full_domain(domain, port)
|
||||||
|
|
||||||
newUndoBookmarkJson = {
|
new_undo_bookmark_json = {
|
||||||
"@context": "https://www.w3.org/ns/activitystreams",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
'type': 'Undo',
|
'type': 'Undo',
|
||||||
'actor': local_actor_url(http_prefix, nickname, fullDomain),
|
'actor': local_actor_url(http_prefix, nickname, full_domain),
|
||||||
'object': {
|
'object': {
|
||||||
'type': 'Bookmark',
|
'type': 'Bookmark',
|
||||||
'actor': local_actor_url(http_prefix, nickname, fullDomain),
|
'actor': local_actor_url(http_prefix, nickname, full_domain),
|
||||||
'object': objectUrl
|
'object': object_url
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ccList:
|
if ccList:
|
||||||
if len(ccList) > 0:
|
if len(ccList) > 0:
|
||||||
newUndoBookmarkJson['cc'] = ccList
|
new_undo_bookmark_json['cc'] = ccList
|
||||||
newUndoBookmarkJson['object']['cc'] = ccList
|
new_undo_bookmark_json['object']['cc'] = ccList
|
||||||
|
|
||||||
# Extract the domain and nickname from a statuses link
|
# Extract the domain and nickname from a statuses link
|
||||||
bookmarkedPostNickname = None
|
bookmarked_post_nickname = None
|
||||||
bookmarkedPostDomain = None
|
|
||||||
bookmarkedPostPort = None
|
|
||||||
if actorBookmarked:
|
if actorBookmarked:
|
||||||
acBm = actorBookmarked
|
ac_bm = actorBookmarked
|
||||||
bookmarkedPostNickname = get_nickname_from_actor(acBm)
|
bookmarked_post_nickname = get_nickname_from_actor(ac_bm)
|
||||||
bookmarkedPostDomain, bookmarkedPostPort = get_domain_from_actor(acBm)
|
_, _ = get_domain_from_actor(ac_bm)
|
||||||
else:
|
else:
|
||||||
if has_users_path(objectUrl):
|
if has_users_path(object_url):
|
||||||
ou = objectUrl
|
ourl = object_url
|
||||||
bookmarkedPostNickname = get_nickname_from_actor(ou)
|
bookmarked_post_nickname = get_nickname_from_actor(ourl)
|
||||||
bookmarkedPostDomain, bookmarkedPostPort = \
|
_, _ = get_domain_from_actor(ourl)
|
||||||
get_domain_from_actor(ou)
|
|
||||||
|
|
||||||
if bookmarkedPostNickname:
|
if bookmarked_post_nickname:
|
||||||
post_filename = locate_post(base_dir, nickname, domain, objectUrl)
|
post_filename = locate_post(base_dir, nickname, domain, object_url)
|
||||||
if not post_filename:
|
if not post_filename:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
undo_bookmarks_collection_entry(recent_posts_cache,
|
undo_bookmarks_collection_entry(recent_posts_cache,
|
||||||
base_dir, post_filename, objectUrl,
|
base_dir, post_filename, object_url,
|
||||||
newUndoBookmarkJson['actor'],
|
new_undo_bookmark_json['actor'],
|
||||||
domain, debug)
|
domain, debug)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return newUndoBookmarkJson
|
return new_undo_bookmark_json
|
||||||
|
|
||||||
|
|
||||||
def send_bookmark_via_server(base_dir: str, session,
|
def send_bookmark_via_server(base_dir: str, session,
|
||||||
|
@ -392,7 +386,7 @@ def send_bookmark_via_server(base_dir: str, session,
|
||||||
|
|
||||||
actor = local_actor_url(http_prefix, nickname, domain_full)
|
actor = local_actor_url(http_prefix, nickname, domain_full)
|
||||||
|
|
||||||
newBookmarkJson = {
|
new_bookmark_json = {
|
||||||
"@context": "https://www.w3.org/ns/activitystreams",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
"type": "Add",
|
"type": "Add",
|
||||||
"actor": actor,
|
"actor": actor,
|
||||||
|
@ -408,61 +402,62 @@ def send_bookmark_via_server(base_dir: str, session,
|
||||||
handle = http_prefix + '://' + domain_full + '/@' + nickname
|
handle = http_prefix + '://' + domain_full + '/@' + nickname
|
||||||
|
|
||||||
# lookup the inbox for the To handle
|
# lookup the inbox for the To handle
|
||||||
wfRequest = webfinger_handle(session, handle, http_prefix,
|
wf_request = \
|
||||||
cached_webfingers,
|
webfinger_handle(session, handle, http_prefix,
|
||||||
domain, project_version, debug, False,
|
cached_webfingers,
|
||||||
signing_priv_key_pem)
|
domain, project_version, debug, False,
|
||||||
if not wfRequest:
|
signing_priv_key_pem)
|
||||||
|
if not wf_request:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: bookmark webfinger failed for ' + handle)
|
print('DEBUG: bookmark webfinger failed for ' + handle)
|
||||||
return 1
|
return 1
|
||||||
if not isinstance(wfRequest, dict):
|
if not isinstance(wf_request, dict):
|
||||||
print('WARN: bookmark webfinger for ' + handle +
|
print('WARN: bookmark webfinger for ' + handle +
|
||||||
' did not return a dict. ' + str(wfRequest))
|
' did not return a dict. ' + str(wf_request))
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
postToBox = 'outbox'
|
post_to_box = 'outbox'
|
||||||
|
|
||||||
# get the actor inbox for the To handle
|
# get the actor inbox for the To handle
|
||||||
originDomain = domain
|
origin_domain = domain
|
||||||
(inboxUrl, pubKeyId, pubKey, fromPersonId, sharedInbox, avatarUrl,
|
(inbox_url, _, _, from_person_id, _, _,
|
||||||
displayName, _) = get_person_box(signing_priv_key_pem,
|
_, _) = get_person_box(signing_priv_key_pem,
|
||||||
originDomain,
|
origin_domain,
|
||||||
base_dir, session, wfRequest,
|
base_dir, session, wf_request,
|
||||||
person_cache,
|
person_cache,
|
||||||
project_version, http_prefix,
|
project_version, http_prefix,
|
||||||
nickname, domain,
|
nickname, domain,
|
||||||
postToBox, 58391)
|
post_to_box, 58391)
|
||||||
|
|
||||||
if not inboxUrl:
|
if not inbox_url:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: bookmark no ' + postToBox +
|
print('DEBUG: bookmark no ' + post_to_box +
|
||||||
' was found for ' + handle)
|
' was found for ' + handle)
|
||||||
return 3
|
return 3
|
||||||
if not fromPersonId:
|
if not from_person_id:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: bookmark no actor was found for ' + handle)
|
print('DEBUG: bookmark no actor was found for ' + handle)
|
||||||
return 4
|
return 4
|
||||||
|
|
||||||
authHeader = create_basic_auth_header(nickname, password)
|
auth_header = create_basic_auth_header(nickname, password)
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
'host': domain,
|
'host': domain,
|
||||||
'Content-type': 'application/json',
|
'Content-type': 'application/json',
|
||||||
'Authorization': authHeader
|
'Authorization': auth_header
|
||||||
}
|
}
|
||||||
postResult = post_json(http_prefix, domain_full,
|
post_result = post_json(http_prefix, domain_full,
|
||||||
session, newBookmarkJson, [], inboxUrl,
|
session, new_bookmark_json, [], inbox_url,
|
||||||
headers, 3, True)
|
headers, 3, True)
|
||||||
if not postResult:
|
if not post_result:
|
||||||
if debug:
|
if debug:
|
||||||
print('WARN: POST bookmark failed for c2s to ' + inboxUrl)
|
print('WARN: POST bookmark failed for c2s to ' + inbox_url)
|
||||||
return 5
|
return 5
|
||||||
|
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s POST bookmark success')
|
print('DEBUG: c2s POST bookmark success')
|
||||||
|
|
||||||
return newBookmarkJson
|
return new_bookmark_json
|
||||||
|
|
||||||
|
|
||||||
def send_undo_bookmark_via_server(base_dir: str, session,
|
def send_undo_bookmark_via_server(base_dir: str, session,
|
||||||
|
@ -482,7 +477,7 @@ def send_undo_bookmark_via_server(base_dir: str, session,
|
||||||
|
|
||||||
actor = local_actor_url(http_prefix, nickname, domain_full)
|
actor = local_actor_url(http_prefix, nickname, domain_full)
|
||||||
|
|
||||||
newBookmarkJson = {
|
new_bookmark_json = {
|
||||||
"@context": "https://www.w3.org/ns/activitystreams",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
"type": "Remove",
|
"type": "Remove",
|
||||||
"actor": actor,
|
"actor": actor,
|
||||||
|
@ -498,61 +493,62 @@ def send_undo_bookmark_via_server(base_dir: str, session,
|
||||||
handle = http_prefix + '://' + domain_full + '/@' + nickname
|
handle = http_prefix + '://' + domain_full + '/@' + nickname
|
||||||
|
|
||||||
# lookup the inbox for the To handle
|
# lookup the inbox for the To handle
|
||||||
wfRequest = webfinger_handle(session, handle, http_prefix,
|
wf_request = \
|
||||||
cached_webfingers,
|
webfinger_handle(session, handle, http_prefix,
|
||||||
domain, project_version, debug, False,
|
cached_webfingers,
|
||||||
signing_priv_key_pem)
|
domain, project_version, debug, False,
|
||||||
if not wfRequest:
|
signing_priv_key_pem)
|
||||||
|
if not wf_request:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: unbookmark webfinger failed for ' + handle)
|
print('DEBUG: unbookmark webfinger failed for ' + handle)
|
||||||
return 1
|
return 1
|
||||||
if not isinstance(wfRequest, dict):
|
if not isinstance(wf_request, dict):
|
||||||
print('WARN: unbookmark webfinger for ' + handle +
|
print('WARN: unbookmark webfinger for ' + handle +
|
||||||
' did not return a dict. ' + str(wfRequest))
|
' did not return a dict. ' + str(wf_request))
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
postToBox = 'outbox'
|
post_to_box = 'outbox'
|
||||||
|
|
||||||
# get the actor inbox for the To handle
|
# get the actor inbox for the To handle
|
||||||
originDomain = domain
|
origin_domain = domain
|
||||||
(inboxUrl, pubKeyId, pubKey, fromPersonId, sharedInbox, avatarUrl,
|
(inbox_url, _, _, from_person_id, _, _,
|
||||||
displayName, _) = get_person_box(signing_priv_key_pem,
|
_, _) = get_person_box(signing_priv_key_pem,
|
||||||
originDomain,
|
origin_domain,
|
||||||
base_dir, session, wfRequest,
|
base_dir, session, wf_request,
|
||||||
person_cache,
|
person_cache,
|
||||||
project_version, http_prefix,
|
project_version, http_prefix,
|
||||||
nickname, domain,
|
nickname, domain,
|
||||||
postToBox, 52594)
|
post_to_box, 52594)
|
||||||
|
|
||||||
if not inboxUrl:
|
if not inbox_url:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: unbookmark no ' + postToBox +
|
print('DEBUG: unbookmark no ' + post_to_box +
|
||||||
' was found for ' + handle)
|
' was found for ' + handle)
|
||||||
return 3
|
return 3
|
||||||
if not fromPersonId:
|
if not from_person_id:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: unbookmark no actor was found for ' + handle)
|
print('DEBUG: unbookmark no actor was found for ' + handle)
|
||||||
return 4
|
return 4
|
||||||
|
|
||||||
authHeader = create_basic_auth_header(nickname, password)
|
auth_header = create_basic_auth_header(nickname, password)
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
'host': domain,
|
'host': domain,
|
||||||
'Content-type': 'application/json',
|
'Content-type': 'application/json',
|
||||||
'Authorization': authHeader
|
'Authorization': auth_header
|
||||||
}
|
}
|
||||||
postResult = post_json(http_prefix, domain_full,
|
post_result = post_json(http_prefix, domain_full,
|
||||||
session, newBookmarkJson, [], inboxUrl,
|
session, new_bookmark_json, [], inbox_url,
|
||||||
headers, 3, True)
|
headers, 3, True)
|
||||||
if not postResult:
|
if not post_result:
|
||||||
if debug:
|
if debug:
|
||||||
print('WARN: POST unbookmark failed for c2s to ' + inboxUrl)
|
print('WARN: POST unbookmark failed for c2s to ' + inbox_url)
|
||||||
return 5
|
return 5
|
||||||
|
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s POST unbookmark success')
|
print('DEBUG: c2s POST unbookmark success')
|
||||||
|
|
||||||
return newBookmarkJson
|
return new_bookmark_json
|
||||||
|
|
||||||
|
|
||||||
def outbox_bookmark(recent_posts_cache: {},
|
def outbox_bookmark(recent_posts_cache: {},
|
||||||
|
@ -596,16 +592,16 @@ def outbox_bookmark(recent_posts_cache: {},
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s bookmark Add request arrived in outbox')
|
print('DEBUG: c2s bookmark Add request arrived in outbox')
|
||||||
|
|
||||||
messageUrl = remove_id_ending(message_json['object']['url'])
|
message_url = remove_id_ending(message_json['object']['url'])
|
||||||
domain = remove_domain_port(domain)
|
domain = remove_domain_port(domain)
|
||||||
post_filename = locate_post(base_dir, nickname, domain, messageUrl)
|
post_filename = locate_post(base_dir, nickname, domain, message_url)
|
||||||
if not post_filename:
|
if not post_filename:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s like post not found in inbox or outbox')
|
print('DEBUG: c2s like post not found in inbox or outbox')
|
||||||
print(messageUrl)
|
print(message_url)
|
||||||
return True
|
return True
|
||||||
update_bookmarks_collection(recent_posts_cache,
|
update_bookmarks_collection(recent_posts_cache,
|
||||||
base_dir, post_filename, messageUrl,
|
base_dir, post_filename, message_url,
|
||||||
message_json['actor'], domain, debug)
|
message_json['actor'], domain, debug)
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: post bookmarked via c2s - ' + post_filename)
|
print('DEBUG: post bookmarked via c2s - ' + post_filename)
|
||||||
|
@ -652,16 +648,16 @@ def outbox_undo_bookmark(recent_posts_cache: {},
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s unbookmark Remove request arrived in outbox')
|
print('DEBUG: c2s unbookmark Remove request arrived in outbox')
|
||||||
|
|
||||||
messageUrl = remove_id_ending(message_json['object']['url'])
|
message_url = remove_id_ending(message_json['object']['url'])
|
||||||
domain = remove_domain_port(domain)
|
domain = remove_domain_port(domain)
|
||||||
post_filename = locate_post(base_dir, nickname, domain, messageUrl)
|
post_filename = locate_post(base_dir, nickname, domain, message_url)
|
||||||
if not post_filename:
|
if not post_filename:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s unbookmark post not found in inbox or outbox')
|
print('DEBUG: c2s unbookmark post not found in inbox or outbox')
|
||||||
print(messageUrl)
|
print(message_url)
|
||||||
return True
|
return True
|
||||||
update_bookmarks_collection(recent_posts_cache,
|
update_bookmarks_collection(recent_posts_cache,
|
||||||
base_dir, post_filename, messageUrl,
|
base_dir, post_filename, message_url,
|
||||||
message_json['actor'], domain, debug)
|
message_json['actor'], domain, debug)
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: post unbookmarked via c2s - ' + post_filename)
|
print('DEBUG: post unbookmarked via c2s - ' + post_filename)
|
||||||
|
|
Loading…
Reference in New Issue