epicyon/enigma.py

102 lines
3.1 KiB
Python

__filename__ = "enigma.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.4.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Profile Metadata"
from utils import get_attachment_property_value
from utils import remove_html
def get_enigma_pub_key(actor_json: {}) -> str:
"""Returns Enigma public key for the given actor
"""
if not actor_json.get('attachment'):
return ''
if not isinstance(actor_json['attachment'], list):
return ''
for property_value in actor_json['attachment']:
name_value = None
if property_value.get('name'):
name_value = property_value['name']
elif property_value.get('schema:name'):
name_value = property_value['schema:name']
if not name_value:
continue
if not name_value.lower().startswith('enigma'):
continue
if not property_value.get('type'):
continue
prop_value_name, _ = \
get_attachment_property_value(property_value)
if not prop_value_name:
continue
if not property_value['type'].endswith('PropertyValue'):
continue
return remove_html(property_value[prop_value_name])
return ''
def set_enigma_pub_key(actor_json: {}, enigma_pub_key: str) -> None:
"""Sets a Enigma public key for the given actor
"""
remove_key = False
if not enigma_pub_key:
remove_key = True
if not actor_json.get('attachment'):
actor_json['attachment'] = []
# remove any existing value
property_found = None
for property_value in actor_json['attachment']:
name_value = None
if property_value.get('name'):
name_value = property_value['name']
elif property_value.get('schema:name'):
name_value = property_value['schema:name']
if not name_value:
continue
if not property_value.get('type'):
continue
if not name_value.lower().startswith('enigma'):
continue
property_found = property_value
break
if property_found:
actor_json['attachment'].remove(property_found)
if remove_key:
return
for property_value in actor_json['attachment']:
name_value = None
if property_value.get('name'):
name_value = property_value['name']
elif property_value.get('schema:name'):
name_value = property_value['schema:name']
if not name_value:
continue
if not property_value.get('type'):
continue
if not name_value.lower().startswith('enigma'):
continue
if not property_value['type'].endswith('PropertyValue'):
continue
prop_value_name, _ = \
get_attachment_property_value(property_value)
if not prop_value_name:
continue
property_value[prop_value_name] = enigma_pub_key
return
new_enigma_pub_key = {
"name": "Enigma",
"type": "PropertyValue",
"value": enigma_pub_key
}
actor_json['attachment'].append(new_enigma_pub_key)