Less indentation

merge-requests/30/head
Bob Mottram 2022-06-09 10:44:21 +01:00
parent db95bcb2af
commit 9d06389788
1 changed files with 84 additions and 82 deletions

View File

@ -164,95 +164,97 @@ def update_bookmarks_collection(recent_posts_cache: {},
"""Updates the bookmarks collection within a post """Updates the bookmarks collection within a post
""" """
post_json_object = load_json(post_filename) post_json_object = load_json(post_filename)
if post_json_object: if not post_json_object:
# remove any cached version of this post so that the return
# bookmark icon is changed
nickname = get_nickname_from_actor(actor)
if not nickname:
return
cached_post_filename = \
get_cached_post_filename(base_dir, nickname,
domain, post_json_object)
if cached_post_filename:
if os.path.isfile(cached_post_filename):
try:
os.remove(cached_post_filename)
except OSError:
if debug:
print('EX: update_bookmarks_collection ' +
'unable to delete cached post ' +
str(cached_post_filename))
remove_post_from_cache(post_json_object, recent_posts_cache)
if not post_json_object.get('object'): # remove any cached version of this post so that the
if debug: # bookmark icon is changed
print('DEBUG: no object in bookmarked post ' + nickname = get_nickname_from_actor(actor)
str(post_json_object)) if not nickname:
return return
if not object_url.endswith('/bookmarks'): cached_post_filename = \
object_url = object_url + '/bookmarks' get_cached_post_filename(base_dir, nickname,
# does this post have bookmarks on it from differenent actors? domain, post_json_object)
if not post_json_object['object'].get('bookmarks'): if cached_post_filename:
if debug: if os.path.isfile(cached_post_filename):
print('DEBUG: Adding initial bookmarks to ' + object_url) try:
bookmarks_json = { os.remove(cached_post_filename)
"@context": "https://www.w3.org/ns/activitystreams", except OSError:
'id': object_url, if debug:
'type': 'Collection', print('EX: update_bookmarks_collection ' +
"totalItems": 1, 'unable to delete cached post ' +
'items': [{ str(cached_post_filename))
'type': 'Bookmark', remove_post_from_cache(post_json_object, recent_posts_cache)
'actor': actor
}] if not post_json_object.get('object'):
} if debug:
post_json_object['object']['bookmarks'] = bookmarks_json print('DEBUG: no object in bookmarked post ' +
else: str(post_json_object))
if not post_json_object['object']['bookmarks'].get('items'): return
post_json_object['object']['bookmarks']['items'] = [] if not object_url.endswith('/bookmarks'):
bm_items = post_json_object['object']['bookmarks']['items'] object_url = object_url + '/bookmarks'
for bookmark_item in bm_items: # does this post have bookmarks on it from differenent actors?
if bookmark_item.get('actor'): if not post_json_object['object'].get('bookmarks'):
if bookmark_item['actor'] == actor: if debug:
return print('DEBUG: Adding initial bookmarks to ' + object_url)
new_bookmark = { bookmarks_json = {
"@context": "https://www.w3.org/ns/activitystreams",
'id': object_url,
'type': 'Collection',
"totalItems": 1,
'items': [{
'type': 'Bookmark', 'type': 'Bookmark',
'actor': actor 'actor': actor
} }]
nbook = new_bookmark }
bm_it = len(post_json_object['object']['bookmarks']['items']) post_json_object['object']['bookmarks'] = bookmarks_json
post_json_object['object']['bookmarks']['items'].append(nbook) else:
post_json_object['object']['bookmarks']['totalItems'] = bm_it if not post_json_object['object']['bookmarks'].get('items'):
post_json_object['object']['bookmarks']['items'] = []
bm_items = post_json_object['object']['bookmarks']['items']
for bookmark_item in bm_items:
if bookmark_item.get('actor'):
if bookmark_item['actor'] == actor:
return
new_bookmark = {
'type': 'Bookmark',
'actor': actor
}
nbook = new_bookmark
bm_it = len(post_json_object['object']['bookmarks']['items'])
post_json_object['object']['bookmarks']['items'].append(nbook)
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')
pprint(post_json_object) pprint(post_json_object)
save_json(post_json_object, post_filename) save_json(post_json_object, post_filename)
# prepend to the index # prepend to the index
bookmarks_index_filename = \ bookmarks_index_filename = \
acct_dir(base_dir, nickname, domain) + '/bookmarks.index' acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
bookmark_index = post_filename.split('/')[-1] bookmark_index = post_filename.split('/')[-1]
if os.path.isfile(bookmarks_index_filename): if os.path.isfile(bookmarks_index_filename):
if bookmark_index not in open(bookmarks_index_filename).read(): if bookmark_index not in open(bookmarks_index_filename).read():
try:
with open(bookmarks_index_filename, 'r+') as bmi_file:
content = bmi_file.read()
if bookmark_index + '\n' not in content:
bmi_file.seek(0, 0)
bmi_file.write(bookmark_index + '\n' + content)
if debug:
print('DEBUG: bookmark added to index')
except OSError as ex:
print('WARN: Failed to write entry to bookmarks index ' +
bookmarks_index_filename + ' ' + str(ex))
else:
try: try:
with open(bookmarks_index_filename, 'w+') as bm_file: with open(bookmarks_index_filename, 'r+') as bmi_file:
bm_file.write(bookmark_index + '\n') content = bmi_file.read()
except OSError: if bookmark_index + '\n' not in content:
print('EX: unable to write bookmarks index ' + bmi_file.seek(0, 0)
bookmarks_index_filename) bmi_file.write(bookmark_index + '\n' + content)
if debug:
print('DEBUG: bookmark added to index')
except OSError as ex:
print('WARN: Failed to write entry to bookmarks index ' +
bookmarks_index_filename + ' ' + str(ex))
else:
try:
with open(bookmarks_index_filename, 'w+') as bm_file:
bm_file.write(bookmark_index + '\n')
except OSError:
print('EX: unable to write bookmarks index ' +
bookmarks_index_filename)
def bookmark_post(recent_posts_cache: {}, def bookmark_post(recent_posts_cache: {},