Snake case

merge-requests/30/head
Bob Mottram 2022-01-03 13:18:36 +00:00
parent c20a208d61
commit d537827ecc
1 changed files with 112 additions and 113 deletions

225
outbox.py
View File

@ -66,7 +66,7 @@ def _person_receive_update_outbox(recent_posts_cache: {},
For example, setting the PGP key from the desktop client For example, setting the PGP key from the desktop client
""" """
# these attachments are updatable via c2s # these attachments are updatable via c2s
updatableAttachments = ('PGP', 'OpenPGP', 'Email') updatable_attachments = ('PGP', 'OpenPGP', 'Email')
if not message_json.get('type'): if not message_json.get('type'):
return return
@ -117,59 +117,58 @@ def _person_receive_update_outbox(recent_posts_cache: {},
print('DEBUG: c2s actor update - unexpected id ' + print('DEBUG: c2s actor update - unexpected id ' +
message_json['id']) message_json['id'])
return return
updatedActorJson = message_json['object'] updated_actor_json = message_json['object']
# load actor from file # load actor from file
actorFilename = acct_dir(base_dir, nickname, domain) + '.json' actor_filename = acct_dir(base_dir, nickname, domain) + '.json'
if not os.path.isfile(actorFilename): if not os.path.isfile(actor_filename):
print('actorFilename not found: ' + actorFilename) print('actor_filename not found: ' + actor_filename)
return return
actor_json = load_json(actorFilename) actor_json = load_json(actor_filename)
if not actor_json: if not actor_json:
return return
actorChanged = False actor_changed = False
# update fields within actor # update fields within actor
if 'attachment' in updatedActorJson: if 'attachment' in updated_actor_json:
for newPropertyValue in updatedActorJson['attachment']: for new_property_value in updated_actor_json['attachment']:
if not newPropertyValue.get('name'): if not new_property_value.get('name'):
continue continue
if newPropertyValue['name'] not in updatableAttachments: if new_property_value['name'] not in updatable_attachments:
continue continue
if not newPropertyValue.get('type'): if not new_property_value.get('type'):
continue continue
if not newPropertyValue.get('value'): if not new_property_value.get('value'):
continue continue
if newPropertyValue['type'] != 'PropertyValue': if new_property_value['type'] != 'PropertyValue':
continue continue
if 'attachment' not in actor_json: if 'attachment' not in actor_json:
continue continue
found = False found = False
for attachIdx in range(len(actor_json['attachment'])): for attach_idx in range(len(actor_json['attachment'])):
if actor_json['attachment'][attachIdx]['type'] != \ if actor_json['attachment'][attach_idx]['type'] != \
'PropertyValue': 'PropertyValue':
continue continue
if actor_json['attachment'][attachIdx]['name'] != \ if actor_json['attachment'][attach_idx]['name'] != \
newPropertyValue['name']: new_property_value['name']:
continue continue
else: if actor_json['attachment'][attach_idx]['value'] != \
if actor_json['attachment'][attachIdx]['value'] != \ new_property_value['value']:
newPropertyValue['value']: actor_json['attachment'][attach_idx]['value'] = \
actor_json['attachment'][attachIdx]['value'] = \ new_property_value['value']
newPropertyValue['value'] actor_changed = True
actorChanged = True found = True
found = True break
break
if not found: if not found:
actor_json['attachment'].append({ actor_json['attachment'].append({
"name": newPropertyValue['name'], "name": new_property_value['name'],
"type": "PropertyValue", "type": "PropertyValue",
"value": newPropertyValue['value'] "value": new_property_value['value']
}) })
actorChanged = True actor_changed = True
# save actor to file # save actor to file
if actorChanged: if actor_changed:
save_json(actor_json, actorFilename) save_json(actor_json, actor_filename)
if debug: if debug:
print('actor saved: ' + actorFilename) print('actor saved: ' + actor_filename)
if debug: if debug:
print('New attachment: ' + str(actor_json['attachment'])) print('New attachment: ' + str(actor_json['attachment']))
message_json['object'] = actor_json message_json['object'] = actor_json
@ -184,7 +183,7 @@ def post_message_to_outbox(session, translate: {},
onion_domain: str, i2p_domain: str, port: int, onion_domain: str, i2p_domain: str, port: int,
recent_posts_cache: {}, followers_threads: [], recent_posts_cache: {}, followers_threads: [],
federation_list: [], send_threads: [], federation_list: [], send_threads: [],
postLog: [], cached_webfingers: {}, post_log: [], cached_webfingers: {},
person_cache: {}, allow_deletion: bool, person_cache: {}, allow_deletion: bool,
proxy_type: str, version: str, debug: bool, proxy_type: str, version: str, debug: bool,
yt_replace_domain: str, yt_replace_domain: str,
@ -223,9 +222,9 @@ def post_message_to_outbox(session, translate: {},
# check that the outgoing post doesn't contain any markup # check that the outgoing post doesn't contain any markup
# which can be used to implement exploits # which can be used to implement exploits
if has_object_dict(message_json): if has_object_dict(message_json):
contentStr = get_base_content_from_post(message_json, system_language) content_str = get_base_content_from_post(message_json, system_language)
if contentStr: if content_str:
if dangerous_markup(contentStr, allow_local_network_access): if dangerous_markup(content_str, allow_local_network_access):
print('POST to outbox contains dangerous markup: ' + print('POST to outbox contains dangerous markup: ' +
str(message_json)) str(message_json))
return False return False
@ -274,14 +273,14 @@ def post_message_to_outbox(session, translate: {},
# sent by an actor on a local network address? # sent by an actor on a local network address?
if not allow_local_network_access: if not allow_local_network_access:
localNetworkPatternList = get_local_network_addresses() local_network_pattern_list = get_local_network_addresses()
for localNetworkPattern in localNetworkPatternList: for local_network_pattern in local_network_pattern_list:
if localNetworkPattern in message_json['actor']: if local_network_pattern in message_json['actor']:
return False return False
testDomain, testPort = get_domain_from_actor(message_json['actor']) test_domain, test_port = get_domain_from_actor(message_json['actor'])
testDomain = get_full_domain(testDomain, testPort) test_domain = get_full_domain(test_domain, test_port)
if is_blocked_domain(base_dir, testDomain): if is_blocked_domain(base_dir, test_domain):
if debug: if debug:
print('DEBUG: domain is blocked: ' + message_json['actor']) print('DEBUG: domain is blocked: ' + message_json['actor'])
return False return False
@ -294,11 +293,11 @@ def post_message_to_outbox(session, translate: {},
# https://www.w3.org/TR/activitypub/#create-activity-outbox # https://www.w3.org/TR/activitypub/#create-activity-outbox
message_json['object']['attributedTo'] = message_json['actor'] message_json['object']['attributedTo'] = message_json['actor']
if message_json['object'].get('attachment'): if message_json['object'].get('attachment'):
attachmentIndex = 0 attachment_index = 0
attach = message_json['object']['attachment'][attachmentIndex] attach = message_json['object']['attachment'][attachment_index]
if attach.get('mediaType'): if attach.get('mediaType'):
fileExtension = 'png' file_extension = 'png'
mediaTypeStr = \ media_type_str = \
attach['mediaType'] attach['mediaType']
extensions = { extensions = {
@ -313,39 +312,39 @@ def post_message_to_outbox(session, translate: {},
"webm": "webm", "webm": "webm",
"ogv": "ogv" "ogv": "ogv"
} }
for matchExt, ext in extensions.items(): for match_ext, ext in extensions.items():
if mediaTypeStr.endswith(matchExt): if media_type_str.endswith(match_ext):
fileExtension = ext file_extension = ext
break break
mediaDir = \ media_dir = \
base_dir + '/accounts/' + \ base_dir + '/accounts/' + \
post_to_nickname + '@' + domain post_to_nickname + '@' + domain
uploadMediaFilename = mediaDir + '/upload.' + fileExtension upload_media_filename = media_dir + '/upload.' + file_extension
if not os.path.isfile(uploadMediaFilename): if not os.path.isfile(upload_media_filename):
del message_json['object']['attachment'] del message_json['object']['attachment']
else: else:
# generate a path for the uploaded image # generate a path for the uploaded image
mPath = get_media_path() mpath = get_media_path()
mediaPath = mPath + '/' + \ media_path = mpath + '/' + \
create_password(16).lower() + '.' + fileExtension create_password(16).lower() + '.' + file_extension
create_media_dirs(base_dir, mPath) create_media_dirs(base_dir, mpath)
mediaFilename = base_dir + '/' + mediaPath media_filename = base_dir + '/' + media_path
# move the uploaded image to its new path # move the uploaded image to its new path
os.rename(uploadMediaFilename, mediaFilename) os.rename(upload_media_filename, media_filename)
# change the url of the attachment # change the url of the attachment
attach['url'] = \ attach['url'] = \
http_prefix + '://' + domain_full + '/' + mediaPath http_prefix + '://' + domain_full + '/' + media_path
attach['url'] = \ attach['url'] = \
attach['url'].replace('/media/', attach['url'].replace('/media/',
'/system/' + '/system/' +
'media_attachments/files/') 'media_attachments/files/')
permittedOutboxTypes = ( permitted_outbox_types = (
'Create', 'Announce', 'Like', 'EmojiReact', 'Follow', 'Undo', 'Create', 'Announce', 'Like', 'EmojiReact', 'Follow', 'Undo',
'Update', 'Add', 'Remove', 'Block', 'Delete', 'Skill', 'Ignore' 'Update', 'Add', 'Remove', 'Block', 'Delete', 'Skill', 'Ignore'
) )
if message_json['type'] not in permittedOutboxTypes: if message_json['type'] not in permitted_outbox_types:
if debug: if debug:
print('DEBUG: POST to outbox - ' + message_json['type'] + print('DEBUG: POST to outbox - ' + message_json['type'] +
' is not a permitted activity type') ' is not a permitted activity type')
@ -361,65 +360,65 @@ def post_message_to_outbox(session, translate: {},
if debug: if debug:
print('DEBUG: save_post_to_box') print('DEBUG: save_post_to_box')
if message_json['type'] != 'Upgrade': if message_json['type'] != 'Upgrade':
outboxName = 'outbox' outbox_name = 'outbox'
# if this is a blog post or an event then save to its own box # if this is a blog post or an event then save to its own box
if message_json['type'] == 'Create': if message_json['type'] == 'Create':
if has_object_dict(message_json): if has_object_dict(message_json):
if message_json['object'].get('type'): if message_json['object'].get('type'):
if message_json['object']['type'] == 'Article': if message_json['object']['type'] == 'Article':
outboxName = 'tlblogs' outbox_name = 'tlblogs'
savedFilename = \ saved_filename = \
save_post_to_box(base_dir, save_post_to_box(base_dir,
http_prefix, http_prefix,
post_id, post_id,
post_to_nickname, domain_full, post_to_nickname, domain_full,
message_json, outboxName) message_json, outbox_name)
if not savedFilename: if not saved_filename:
print('WARN: post not saved to outbox ' + outboxName) print('WARN: post not saved to outbox ' + outbox_name)
return False return False
# save all instance blogs to the news actor # save all instance blogs to the news actor
if post_to_nickname != 'news' and outboxName == 'tlblogs': if post_to_nickname != 'news' and outbox_name == 'tlblogs':
if '/' in savedFilename: if '/' in saved_filename:
if is_featured_writer(base_dir, post_to_nickname, domain): if is_featured_writer(base_dir, post_to_nickname, domain):
savedPostId = savedFilename.split('/')[-1] saved_post_id = saved_filename.split('/')[-1]
blogsDir = \ blogs_dir = \
base_dir + '/accounts/news@' + domain + '/tlblogs' base_dir + '/accounts/news@' + domain + '/tlblogs'
if not os.path.isdir(blogsDir): if not os.path.isdir(blogs_dir):
os.mkdir(blogsDir) os.mkdir(blogs_dir)
copyfile(savedFilename, blogsDir + '/' + savedPostId) copyfile(saved_filename, blogs_dir + '/' + saved_post_id)
inbox_update_index('tlblogs', base_dir, inbox_update_index('tlblogs', base_dir,
'news@' + domain, 'news@' + domain,
savedFilename, debug) saved_filename, debug)
# clear the citations file if it exists # clear the citations file if it exists
citationsFilename = \ citations_filename = \
base_dir + '/accounts/' + \ base_dir + '/accounts/' + \
post_to_nickname + '@' + domain + '/.citations.txt' post_to_nickname + '@' + domain + '/.citations.txt'
if os.path.isfile(citationsFilename): if os.path.isfile(citations_filename):
try: try:
os.remove(citationsFilename) os.remove(citations_filename)
except OSError: except OSError:
print('EX: post_message_to_outbox unable to delete ' + print('EX: post_message_to_outbox unable to delete ' +
citationsFilename) citations_filename)
# The following activity types get added to the index files # The following activity types get added to the index files
indexedActivities = ( indexed_activities = (
'Create', 'Question', 'Note', 'EncryptedMessage', 'Article', 'Create', 'Question', 'Note', 'EncryptedMessage', 'Article',
'Patch', 'Announce' 'Patch', 'Announce'
) )
if message_json['type'] in indexedActivities: if message_json['type'] in indexed_activities:
indexes = [outboxName, "inbox"] indexes = [outbox_name, "inbox"]
selfActor = \ self_actor = \
local_actor_url(http_prefix, post_to_nickname, domain_full) local_actor_url(http_prefix, post_to_nickname, domain_full)
for boxNameIndex in indexes: for box_name_index in indexes:
if not boxNameIndex: if not box_name_index:
continue continue
# should this also go to the media timeline? # should this also go to the media timeline?
if boxNameIndex == 'inbox': if box_name_index == 'inbox':
if is_image_media(session, base_dir, http_prefix, if is_image_media(session, base_dir, http_prefix,
post_to_nickname, domain, post_to_nickname, domain,
message_json, message_json,
@ -433,31 +432,31 @@ def post_message_to_outbox(session, translate: {},
signing_priv_key_pem): signing_priv_key_pem):
inbox_update_index('tlmedia', base_dir, inbox_update_index('tlmedia', base_dir,
post_to_nickname + '@' + domain, post_to_nickname + '@' + domain,
savedFilename, debug) saved_filename, debug)
if boxNameIndex == 'inbox' and outboxName == 'tlblogs': if box_name_index == 'inbox' and outbox_name == 'tlblogs':
continue continue
# avoid duplicates of the message if already going # avoid duplicates of the message if already going
# back to the inbox of the same account # back to the inbox of the same account
if selfActor not in message_json['to']: if self_actor not in message_json['to']:
# show sent post within the inbox, # show sent post within the inbox,
# as is the typical convention # as is the typical convention
inbox_update_index(boxNameIndex, base_dir, inbox_update_index(box_name_index, base_dir,
post_to_nickname + '@' + domain, post_to_nickname + '@' + domain,
savedFilename, debug) saved_filename, debug)
# regenerate the html # regenerate the html
useCacheOnly = False use_cache_only = False
pageNumber = 1 page_number = 1
showIndividualPostIcons = True show_individual_post_icons = True
manuallyApproveFollowers = \ manually_approve_followers = \
follower_approval_active(base_dir, follower_approval_active(base_dir,
post_to_nickname, domain) post_to_nickname, domain)
individual_post_as_html(signing_priv_key_pem, individual_post_as_html(signing_priv_key_pem,
False, recent_posts_cache, False, recent_posts_cache,
max_recent_posts, max_recent_posts,
translate, pageNumber, translate, page_number,
base_dir, session, base_dir, session,
cached_webfingers, cached_webfingers,
person_cache, person_cache,
@ -465,7 +464,7 @@ def post_message_to_outbox(session, translate: {},
message_json, None, True, message_json, None, True,
allow_deletion, allow_deletion,
http_prefix, __version__, http_prefix, __version__,
boxNameIndex, box_name_index,
yt_replace_domain, yt_replace_domain,
twitter_replacement_domain, twitter_replacement_domain,
show_published_date_only, show_published_date_only,
@ -473,10 +472,10 @@ def post_message_to_outbox(session, translate: {},
allow_local_network_access, allow_local_network_access,
theme, system_language, theme, system_language,
max_like_count, max_like_count,
boxNameIndex != 'dm', box_name_index != 'dm',
showIndividualPostIcons, show_individual_post_icons,
manuallyApproveFollowers, manually_approve_followers,
False, True, useCacheOnly, False, True, use_cache_only,
cw_lists, lists_enabled) cw_lists, lists_enabled)
if outbox_announce(recent_posts_cache, if outbox_announce(recent_posts_cache,
@ -493,12 +492,12 @@ def post_message_to_outbox(session, translate: {},
if debug: if debug:
print('DEBUG: sending c2s post to followers') print('DEBUG: sending c2s post to followers')
# remove inactive threads # remove inactive threads
inactiveFollowerThreads = [] inactive_follower_threads = []
for th in followers_threads: for thr in followers_threads:
if not th.is_alive(): if not thr.is_alive():
inactiveFollowerThreads.append(th) inactive_follower_threads.append(thr)
for th in inactiveFollowerThreads: for thr in inactive_follower_threads:
followers_threads.remove(th) followers_threads.remove(thr)
if debug: if debug:
print('DEBUG: ' + str(len(followers_threads)) + print('DEBUG: ' + str(len(followers_threads)) +
' followers threads active') ' followers threads active')
@ -510,7 +509,7 @@ def post_message_to_outbox(session, translate: {},
# remove it from the list # remove it from the list
followers_threads.pop(0) followers_threads.pop(0)
# create a thread to send the post to followers # create a thread to send the post to followers
followersThread = \ followers_thread = \
send_to_followers_thread(server.session, send_to_followers_thread(server.session,
base_dir, base_dir,
post_to_nickname, post_to_nickname,
@ -518,7 +517,7 @@ def post_message_to_outbox(session, translate: {},
port, http_prefix, port, http_prefix,
federation_list, federation_list,
send_threads, send_threads,
postLog, post_log,
cached_webfingers, cached_webfingers,
person_cache, person_cache,
message_json, debug, message_json, debug,
@ -526,7 +525,7 @@ def post_message_to_outbox(session, translate: {},
shared_items_federated_domains, shared_items_federated_domains,
shared_item_federation_tokens, shared_item_federation_tokens,
signing_priv_key_pem) signing_priv_key_pem)
followers_threads.append(followersThread) followers_threads.append(followers_thread)
if debug: if debug:
print('DEBUG: handle any unfollow requests') print('DEBUG: handle any unfollow requests')
@ -652,14 +651,14 @@ def post_message_to_outbox(session, translate: {},
else: else:
print('c2s sender: ' + print('c2s sender: ' +
post_to_nickname + '@' + domain + ':' + str(port)) post_to_nickname + '@' + domain + ':' + str(port))
namedAddressesThread = \ named_addresses_thread = \
send_to_named_addresses_thread(server.session, base_dir, send_to_named_addresses_thread(server.session, base_dir,
post_to_nickname, post_to_nickname,
domain, onion_domain, i2p_domain, port, domain, onion_domain, i2p_domain, port,
http_prefix, http_prefix,
federation_list, federation_list,
send_threads, send_threads,
postLog, post_log,
cached_webfingers, cached_webfingers,
person_cache, person_cache,
message_json, debug, message_json, debug,
@ -667,5 +666,5 @@ def post_message_to_outbox(session, translate: {},
shared_items_federated_domains, shared_items_federated_domains,
shared_item_federation_tokens, shared_item_federation_tokens,
signing_priv_key_pem) signing_priv_key_pem)
followers_threads.append(namedAddressesThread) followers_threads.append(named_addresses_thread)
return True return True