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
2021-12-26 10:29:52 +00:00
def getMatrixAddress(actor_json: {}) -> str:
2019-12-17 15:25:34 +00:00
"""Returns matrix address for the given actor
"""
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
2019-12-17 15:25:34 +00:00
return ''
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['name'].lower().startswith('matrix'):
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('value'):
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
if '@' not in property_value['value']:
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['value'].startswith('@'):
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
if ':' not in property_value['value']:
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
if '"' in property_value['value']:
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
return property_value['value']
2019-12-17 15:25:34 +00:00
return ''
2020-04-03 16:47:12 +00:00
2021-12-26 10:29:52 +00:00
def setMatrixAddress(actor_json: {}, matrixAddress: str) -> None:
2019-12-17 15:25:34 +00:00
"""Sets an matrix address for the given actor
"""
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
actor_json['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
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2019-12-17 23:35:59 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2019-12-17 23:35:59 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['name'].lower().startswith('matrix'):
2019-12-17 23:35:59 +00:00
continue
2021-12-26 10:32:45 +00:00
propertyFound = property_value
2019-12-17 23:35:59 +00:00
break
if propertyFound:
2021-12-26 10:29:52 +00:00
actor_json['attachment'].remove(propertyFound)
2019-12-17 23:35:59 +00:00
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
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['name'].lower().startswith('matrix'):
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
2019-12-17 15:25:34 +00:00
continue
2021-12-26 10:32:45 +00:00
property_value['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
}
2021-12-26 10:29:52 +00:00
actor_json['attachment'].append(newMatrixAddress)