epicyon/matrix.py

90 lines
2.6 KiB
Python
Raw Normal View History

2020-04-03 16:47:12 +00:00
__filename__ = "matrix.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-04-03 16:47:12 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-03 16:47:12 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "Profile Metadata"
2019-12-17 15:25:34 +00:00
def getMatrixAddress(actorJson: {}) -> str:
"""Returns matrix address for the given actor
"""
if not actorJson.get('attachment'):
return ''
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if not propertyValue['name'].lower().startswith('matrix'):
continue
if not propertyValue.get('type'):
continue
if not propertyValue.get('value'):
continue
2020-04-03 16:47:12 +00:00
if propertyValue['type'] != 'PropertyValue':
2019-12-17 15:25:34 +00:00
continue
if '@' not in propertyValue['value']:
continue
if not propertyValue['value'].startswith('@'):
continue
if ':' not in propertyValue['value']:
continue
if '"' in propertyValue['value']:
continue
return propertyValue['value']
return ''
2020-04-03 16:47:12 +00:00
def setMatrixAddress(actorJson: {}, matrixAddress: str) -> None:
2019-12-17 15:25:34 +00:00
"""Sets an matrix address for the given actor
"""
if not actorJson.get('attachment'):
2020-04-03 16:47:12 +00:00
actorJson['attachment'] = []
2019-12-17 15:25:34 +00:00
2019-12-17 23:35:59 +00:00
# remove any existing value
2020-04-03 16:47:12 +00:00
propertyFound = None
2019-12-17 23:35:59 +00:00
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
if not propertyValue['name'].lower().startswith('matrix'):
continue
2020-04-03 16:47:12 +00:00
propertyFound = propertyValue
2019-12-17 23:35:59 +00:00
break
if propertyFound:
actorJson['attachment'].remove(propertyFound)
2019-12-17 15:25:34 +00:00
if '@' not in matrixAddress:
return
if not matrixAddress.startswith('@'):
return
if '.' not in matrixAddress:
return
if '"' in matrixAddress:
return
if '<' in matrixAddress:
return
2019-12-17 15:25:34 +00:00
if ':' not in matrixAddress:
return
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
if not propertyValue['name'].lower().startswith('matrix'):
continue
2020-04-03 16:47:12 +00:00
if propertyValue['type'] != 'PropertyValue':
2019-12-17 15:25:34 +00:00
continue
2020-04-03 16:47:12 +00:00
propertyValue['value'] = matrixAddress
2019-12-17 15:25:34 +00:00
return
2020-04-03 16:47:12 +00:00
newMatrixAddress = {
2019-12-17 15:25:34 +00:00
"name": "Matrix",
"type": "PropertyValue",
"value": matrixAddress
}
actorJson['attachment'].append(newMatrixAddress)