epicyon/enigma.py

75 lines
2.1 KiB
Python
Raw Normal View History

2021-12-11 10:53:38 +00:00
__filename__ = "enigma.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.2.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Profile Metadata"
2021-12-26 10:29:52 +00:00
def getEnigmaPubKey(actor_json: {}) -> str:
2021-12-11 10:53:38 +00:00
"""Returns Enigma public key for the given actor
"""
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
2021-12-11 10:53:38 +00:00
return ''
2021-12-26 10:29:52 +00:00
for propertyValue in actor_json['attachment']:
2021-12-11 10:53:38 +00:00
if not propertyValue.get('name'):
continue
if not propertyValue['name'].lower().startswith('enigma'):
continue
if not propertyValue.get('type'):
continue
if not propertyValue.get('value'):
continue
if propertyValue['type'] != 'PropertyValue':
continue
return propertyValue['value']
return ''
2021-12-26 10:29:52 +00:00
def setEnigmaPubKey(actor_json: {}, enigmaPubKey: str) -> None:
2021-12-11 10:53:38 +00:00
"""Sets a Enigma public key for the given actor
"""
removeKey = False
if not enigmaPubKey:
removeKey = True
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
actor_json['attachment'] = []
2021-12-11 10:53:38 +00:00
# remove any existing value
propertyFound = None
2021-12-26 10:29:52 +00:00
for propertyValue in actor_json['attachment']:
2021-12-11 10:53:38 +00:00
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
if not propertyValue['name'].lower().startswith('enigma'):
continue
propertyFound = propertyValue
break
if propertyFound:
2021-12-26 10:29:52 +00:00
actor_json['attachment'].remove(propertyValue)
2021-12-11 10:53:38 +00:00
if removeKey:
return
2021-12-26 10:29:52 +00:00
for propertyValue in actor_json['attachment']:
2021-12-11 10:53:38 +00:00
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
if not propertyValue['name'].lower().startswith('enigma'):
continue
if propertyValue['type'] != 'PropertyValue':
continue
propertyValue['value'] = enigmaPubKey
return
newenigmaPubKey = {
"name": "Enigma",
"type": "PropertyValue",
"value": enigmaPubKey
}
2021-12-26 10:29:52 +00:00
actor_json['attachment'].append(newenigmaPubKey)