epicyon/ssb.py

124 lines
3.9 KiB
Python
Raw Normal View History

2020-04-04 11:40:47 +00:00
__filename__ = "ssb.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2023-01-21 23:03:30 +00:00
__version__ = "1.4.0"
2020-04-04 11:40:47 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-04 11:40:47 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "Profile Metadata"
2020-02-26 14:35:17 +00:00
from utils import get_attachment_property_value
from utils import remove_html
2021-12-28 17:33:54 +00:00
def get_ssb_address(actor_json: {}) -> str:
2020-02-26 14:35:17 +00:00
"""Returns ssb address for the given actor
"""
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
2020-02-26 14:35:17 +00:00
return ''
2023-11-29 10:58:48 +00:00
if not isinstance(actor_json['attachment'], list):
return ''
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
2022-05-11 16:10:38 +00:00
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:
2020-02-26 14:35:17 +00:00
continue
2022-05-11 16:10:38 +00:00
if not name_value.lower().startswith('ssb'):
2020-02-26 14:35:17 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2020-02-26 14:35:17 +00:00
continue
prop_value_name, _ = \
get_attachment_property_value(property_value)
if not prop_value_name:
2020-02-26 14:35:17 +00:00
continue
2022-05-11 16:16:34 +00:00
if not property_value['type'].endswith('PropertyValue'):
2020-02-26 14:35:17 +00:00
continue
property_value[prop_value_name] = \
property_value[prop_value_name].strip()
if not property_value[prop_value_name].startswith('@'):
2020-02-26 14:35:17 +00:00
continue
if '=.' not in property_value[prop_value_name]:
2020-02-26 14:35:17 +00:00
continue
if '"' in property_value[prop_value_name]:
2020-02-26 14:35:17 +00:00
continue
if ' ' in property_value[prop_value_name]:
2020-02-26 14:51:37 +00:00
continue
if ',' in property_value[prop_value_name]:
2020-02-26 14:51:37 +00:00
continue
return remove_html(property_value[prop_value_name])
2020-02-26 14:35:17 +00:00
return ''
2020-04-04 11:40:47 +00:00
def set_ssb_address(actor_json: {}, ssb_address: str) -> None:
2020-02-26 14:35:17 +00:00
"""Sets an ssb address for the given actor
"""
2022-01-03 18:37:14 +00:00
not_ssb_address = False
if not ssb_address.startswith('@'):
2022-01-03 18:37:14 +00:00
not_ssb_address = True
if '=.' not in ssb_address:
2022-01-03 18:37:14 +00:00
not_ssb_address = True
if '"' in ssb_address:
2022-01-03 18:37:14 +00:00
not_ssb_address = True
if ' ' in ssb_address:
2022-01-03 18:37:14 +00:00
not_ssb_address = True
if ',' in ssb_address:
2022-01-03 18:37:14 +00:00
not_ssb_address = True
if '<' in ssb_address:
2022-01-03 18:37:14 +00:00
not_ssb_address = True
2020-07-06 10:23:11 +00:00
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
actor_json['attachment'] = []
2020-02-26 14:35:17 +00:00
# remove any existing value
2022-01-03 10:27:55 +00:00
property_found = None
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
2022-05-11 16:10:38 +00:00
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:
2020-02-26 14:35:17 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2020-02-26 14:35:17 +00:00
continue
2022-05-11 16:10:38 +00:00
if not name_value.lower().startswith('ssb'):
2020-02-26 14:35:17 +00:00
continue
2022-01-03 10:27:55 +00:00
property_found = property_value
2020-02-26 14:35:17 +00:00
break
2022-01-03 10:27:55 +00:00
if property_found:
actor_json['attachment'].remove(property_found)
2022-01-03 18:37:14 +00:00
if not_ssb_address:
2020-02-26 14:35:17 +00:00
return
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
2022-05-11 16:10:38 +00:00
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:
2020-02-26 14:35:17 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2020-02-26 14:35:17 +00:00
continue
2022-05-11 16:10:38 +00:00
if not name_value.lower().startswith('ssb'):
2020-02-26 14:35:17 +00:00
continue
2022-05-11 16:16:34 +00:00
if not property_value['type'].endswith('PropertyValue'):
2020-02-26 14:35:17 +00:00
continue
prop_value_name, _ = \
get_attachment_property_value(property_value)
if not prop_value_name:
continue
property_value[prop_value_name] = ssb_address
2020-02-26 14:35:17 +00:00
return
2022-01-03 18:37:14 +00:00
new_ssb_address = {
2020-02-26 14:35:17 +00:00
"name": "SSB",
"type": "PropertyValue",
"value": ssb_address
2020-02-26 14:35:17 +00:00
}
2022-01-03 18:37:14 +00:00
actor_json['attachment'].append(new_ssb_address)