2020-04-04 11:35:55 +00:00
|
|
|
__filename__ = "skills.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2024-01-21 19:01:20 +00:00
|
|
|
__version__ = "1.5.0"
|
2020-04-04 11:35:55 +00:00
|
|
|
__maintainer__ = "Bob Mottram"
|
2021-09-10 16:14:50 +00:00
|
|
|
__email__ = "bob@libreserver.org"
|
2020-04-04 11:35:55 +00:00
|
|
|
__status__ = "Production"
|
2021-06-26 11:16:41 +00:00
|
|
|
__module_group__ = "Profile Metadata"
|
2020-04-04 11:35:55 +00:00
|
|
|
|
2019-07-19 10:01:24 +00:00
|
|
|
import os
|
2021-12-29 21:55:09 +00:00
|
|
|
from webfinger import webfinger_handle
|
2021-12-28 21:36:27 +00:00
|
|
|
from auth import create_basic_auth_header
|
2021-12-29 21:55:09 +00:00
|
|
|
from posts import get_person_box
|
|
|
|
from session import post_json
|
2021-12-26 17:12:07 +00:00
|
|
|
from utils import has_object_string
|
2021-12-26 12:45:03 +00:00
|
|
|
from utils import get_full_domain
|
2021-12-27 22:19:18 +00:00
|
|
|
from utils import get_nickname_from_actor
|
2021-12-27 19:05:25 +00:00
|
|
|
from utils import get_domain_from_actor
|
2021-12-26 15:13:34 +00:00
|
|
|
from utils import load_json
|
2021-12-26 19:01:36 +00:00
|
|
|
from utils import get_occupation_skills
|
2021-12-26 18:50:59 +00:00
|
|
|
from utils import set_occupation_skills_list
|
2021-12-26 12:02:29 +00:00
|
|
|
from utils import acct_dir
|
2021-12-26 10:19:59 +00:00
|
|
|
from utils import local_actor_url
|
2021-12-26 17:15:04 +00:00
|
|
|
from utils import has_actor
|
2024-01-09 16:59:23 +00:00
|
|
|
from utils import get_actor_from_post
|
2021-05-13 13:27:35 +00:00
|
|
|
|
|
|
|
|
2022-01-03 17:06:41 +00:00
|
|
|
def set_skills_from_dict(actor_json: {}, skills_dict: {}) -> []:
|
2021-05-16 15:10:39 +00:00
|
|
|
"""Converts a dict containing skills to a list
|
2021-05-13 14:13:27 +00:00
|
|
|
Returns the string version of the dictionary
|
2021-05-13 13:27:35 +00:00
|
|
|
"""
|
2022-01-03 17:06:41 +00:00
|
|
|
skills_list = []
|
|
|
|
for name, value in skills_dict.items():
|
|
|
|
skills_list.append(name + ':' + str(value))
|
|
|
|
set_occupation_skills_list(actor_json, skills_list)
|
|
|
|
return skills_list
|
2021-05-13 13:27:35 +00:00
|
|
|
|
|
|
|
|
2022-01-03 17:06:41 +00:00
|
|
|
def get_skills_from_list(skills_list: []) -> {}:
|
2021-05-14 20:02:55 +00:00
|
|
|
"""Returns a dict of skills from a list
|
2021-05-13 13:27:35 +00:00
|
|
|
"""
|
2022-01-03 17:06:41 +00:00
|
|
|
if isinstance(skills_list, list):
|
|
|
|
skills_list2 = skills_list
|
2021-05-14 17:41:05 +00:00
|
|
|
else:
|
2022-01-03 17:06:41 +00:00
|
|
|
skills_list2 = skills_list.split(',')
|
|
|
|
skills_dict = {}
|
|
|
|
for skill in skills_list2:
|
2021-05-13 13:27:35 +00:00
|
|
|
if ':' not in skill:
|
|
|
|
continue
|
|
|
|
name = skill.split(':')[0].strip().lower()
|
2022-01-03 17:06:41 +00:00
|
|
|
value_str = skill.split(':')[1]
|
|
|
|
if not value_str.isdigit():
|
2021-05-13 13:27:35 +00:00
|
|
|
continue
|
2022-01-03 17:06:41 +00:00
|
|
|
skills_dict[name] = int(value_str)
|
|
|
|
return skills_dict
|
2021-05-13 13:27:35 +00:00
|
|
|
|
|
|
|
|
2022-01-03 17:06:41 +00:00
|
|
|
def actor_skill_value(actor_json: {}, skill_name: str) -> int:
|
2021-05-13 13:27:35 +00:00
|
|
|
"""Returns The skill level from an actor
|
|
|
|
"""
|
2022-01-03 17:06:41 +00:00
|
|
|
oc_skills_list = get_occupation_skills(actor_json)
|
|
|
|
skills_dict = get_skills_from_list(oc_skills_list)
|
|
|
|
if not skills_dict:
|
2021-05-13 13:27:35 +00:00
|
|
|
return 0
|
2022-01-03 17:06:41 +00:00
|
|
|
skill_name = skill_name.lower()
|
|
|
|
if skills_dict.get(skill_name):
|
|
|
|
return skills_dict[skill_name]
|
2021-05-13 13:27:35 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2021-12-28 20:32:11 +00:00
|
|
|
def no_of_actor_skills(actor_json: {}) -> int:
|
2021-05-13 13:27:35 +00:00
|
|
|
"""Returns the number of skills that an actor has
|
|
|
|
"""
|
2021-12-26 10:29:52 +00:00
|
|
|
if actor_json.get('hasOccupation'):
|
2022-01-03 17:06:41 +00:00
|
|
|
skills_list = get_occupation_skills(actor_json)
|
|
|
|
return len(skills_list)
|
2021-05-13 13:27:35 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2021-12-28 20:32:11 +00:00
|
|
|
def set_actor_skill_level(actor_json: {},
|
2022-01-03 17:06:41 +00:00
|
|
|
skill: str, skill_level_percent: int) -> bool:
|
2021-05-13 13:27:35 +00:00
|
|
|
"""Set a skill level for a person
|
|
|
|
Setting skill level to zero removes it
|
|
|
|
"""
|
2022-01-03 17:06:41 +00:00
|
|
|
if skill_level_percent < 0 or skill_level_percent > 100:
|
2021-05-13 13:27:35 +00:00
|
|
|
return False
|
|
|
|
|
2021-12-26 10:29:52 +00:00
|
|
|
if not actor_json:
|
2021-05-13 14:13:27 +00:00
|
|
|
return True
|
2021-12-26 10:29:52 +00:00
|
|
|
if not actor_json.get('hasOccupation'):
|
|
|
|
actor_json['hasOccupation'] = [{
|
2021-07-04 19:59:08 +00:00
|
|
|
'@type': 'Occupation',
|
|
|
|
'name': '',
|
|
|
|
"occupationLocation": {
|
|
|
|
"@type": "City",
|
|
|
|
"name": "Fediverse"
|
|
|
|
},
|
|
|
|
'skills': []
|
|
|
|
}]
|
2022-01-03 17:06:41 +00:00
|
|
|
oc_skills_list = get_occupation_skills(actor_json)
|
|
|
|
skills_dict = get_skills_from_list(oc_skills_list)
|
|
|
|
if not skills_dict.get(skill):
|
|
|
|
if len(skills_dict.items()) >= 32:
|
2021-06-07 09:18:19 +00:00
|
|
|
print('WARN: Maximum number of skills reached for ' +
|
2021-12-26 10:29:52 +00:00
|
|
|
actor_json['id'])
|
2021-06-07 09:18:19 +00:00
|
|
|
return False
|
2022-01-03 17:06:41 +00:00
|
|
|
if skill_level_percent > 0:
|
|
|
|
skills_dict[skill] = skill_level_percent
|
2021-05-13 14:13:27 +00:00
|
|
|
else:
|
2022-01-03 17:06:41 +00:00
|
|
|
if skills_dict.get(skill):
|
|
|
|
del skills_dict[skill]
|
|
|
|
set_skills_from_dict(actor_json, skills_dict)
|
2021-05-13 13:27:35 +00:00
|
|
|
return True
|
2019-07-19 10:01:24 +00:00
|
|
|
|
2020-04-04 11:35:55 +00:00
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def set_skill_level(base_dir: str, nickname: str, domain: str,
|
2022-01-03 17:06:41 +00:00
|
|
|
skill: str, skill_level_percent: int) -> bool:
|
2019-07-19 10:01:24 +00:00
|
|
|
"""Set a skill level for a person
|
|
|
|
Setting skill level to zero removes it
|
|
|
|
"""
|
2022-01-03 17:06:41 +00:00
|
|
|
if skill_level_percent < 0 or skill_level_percent > 100:
|
2019-07-19 10:01:24 +00:00
|
|
|
return False
|
2022-01-03 17:06:41 +00:00
|
|
|
actor_filename = acct_dir(base_dir, nickname, domain) + '.json'
|
|
|
|
if not os.path.isfile(actor_filename):
|
2019-07-19 10:01:24 +00:00
|
|
|
return False
|
2019-09-30 22:39:02 +00:00
|
|
|
|
2022-01-03 17:06:41 +00:00
|
|
|
actor_json = load_json(actor_filename)
|
2021-12-28 20:32:11 +00:00
|
|
|
return set_actor_skill_level(actor_json,
|
2022-01-03 17:06:41 +00:00
|
|
|
skill, skill_level_percent)
|
2019-07-19 10:01:24 +00:00
|
|
|
|
2020-04-04 11:35:55 +00:00
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def get_skills(base_dir: str, nickname: str, domain: str) -> []:
|
2019-07-19 10:01:24 +00:00
|
|
|
"""Returns the skills for a given person
|
|
|
|
"""
|
2022-01-03 17:06:41 +00:00
|
|
|
actor_filename = acct_dir(base_dir, nickname, domain) + '.json'
|
|
|
|
if not os.path.isfile(actor_filename):
|
2019-07-19 10:01:24 +00:00
|
|
|
return False
|
2019-09-30 22:39:02 +00:00
|
|
|
|
2022-01-03 17:06:41 +00:00
|
|
|
actor_json = load_json(actor_filename)
|
2021-12-26 10:29:52 +00:00
|
|
|
if actor_json:
|
|
|
|
if not actor_json.get('hasOccupation'):
|
2019-07-19 10:01:24 +00:00
|
|
|
return None
|
2022-01-03 17:06:41 +00:00
|
|
|
oc_skills_list = get_occupation_skills(actor_json)
|
|
|
|
return get_skills_from_list(oc_skills_list)
|
2019-07-19 10:01:24 +00:00
|
|
|
return None
|
|
|
|
|
2020-04-04 11:35:55 +00:00
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def outbox_skills(base_dir: str, nickname: str, message_json: {},
|
|
|
|
debug: bool) -> bool:
|
2019-07-19 10:01:24 +00:00
|
|
|
"""Handles receiving a skills update
|
|
|
|
"""
|
2021-12-25 23:51:19 +00:00
|
|
|
if not message_json.get('type'):
|
2019-07-19 10:01:24 +00:00
|
|
|
return False
|
2021-12-25 23:51:19 +00:00
|
|
|
if not message_json['type'] == 'Skill':
|
2019-07-19 10:01:24 +00:00
|
|
|
return False
|
2021-12-26 17:15:04 +00:00
|
|
|
if not has_actor(message_json, debug):
|
2019-07-19 10:01:24 +00:00
|
|
|
return False
|
2021-12-26 17:12:07 +00:00
|
|
|
if not has_object_string(message_json, debug):
|
2019-07-19 10:01:24 +00:00
|
|
|
return False
|
|
|
|
|
2024-01-09 16:59:23 +00:00
|
|
|
actor_url = get_actor_from_post(message_json)
|
|
|
|
actor_nickname = get_nickname_from_actor(actor_url)
|
2022-03-23 23:59:29 +00:00
|
|
|
if not actor_nickname:
|
|
|
|
return False
|
2022-01-03 17:06:41 +00:00
|
|
|
if actor_nickname != nickname:
|
2019-07-19 10:01:24 +00:00
|
|
|
return False
|
2024-01-09 16:59:23 +00:00
|
|
|
domain, _ = get_domain_from_actor(actor_url)
|
2021-12-25 23:51:19 +00:00
|
|
|
skill = message_json['object'].replace('"', '').split(';')[0].strip()
|
2022-01-03 17:06:41 +00:00
|
|
|
skill_level_percent_str = \
|
2021-12-25 23:51:19 +00:00
|
|
|
message_json['object'].replace('"', '').split(';')[1].strip()
|
2022-01-03 17:06:41 +00:00
|
|
|
skill_level_percent = 50
|
|
|
|
if skill_level_percent_str.isdigit():
|
|
|
|
skill_level_percent = int(skill_level_percent_str)
|
2020-04-04 11:35:55 +00:00
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
return set_skill_level(base_dir, nickname, domain,
|
2022-01-03 17:06:41 +00:00
|
|
|
skill, skill_level_percent)
|
2019-07-19 10:01:24 +00:00
|
|
|
|
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def send_skill_via_server(base_dir: str, session, nickname: str, password: str,
|
|
|
|
domain: str, port: int,
|
|
|
|
http_prefix: str,
|
2022-01-03 17:06:41 +00:00
|
|
|
skill: str, skill_level_percent: int,
|
2021-12-29 21:55:09 +00:00
|
|
|
cached_webfingers: {}, person_cache: {},
|
|
|
|
debug: bool, project_version: str,
|
2023-10-25 19:55:40 +00:00
|
|
|
signing_priv_key_pem: str,
|
|
|
|
system_language: str) -> {}:
|
2019-07-19 10:01:24 +00:00
|
|
|
"""Sets a skill for a person via c2s
|
|
|
|
"""
|
|
|
|
if not session:
|
2021-12-29 21:55:09 +00:00
|
|
|
print('WARN: No session for send_skill_via_server')
|
2019-07-19 10:01:24 +00:00
|
|
|
return 6
|
|
|
|
|
2021-12-26 12:45:03 +00:00
|
|
|
domain_full = get_full_domain(domain, port)
|
2020-03-22 21:16:02 +00:00
|
|
|
|
2021-12-26 10:19:59 +00:00
|
|
|
actor = local_actor_url(http_prefix, nickname, domain_full)
|
2022-01-03 17:06:41 +00:00
|
|
|
to_url = actor
|
|
|
|
cc_url = actor + '/followers'
|
2019-07-19 10:01:24 +00:00
|
|
|
|
2022-01-03 17:06:41 +00:00
|
|
|
if skill_level_percent:
|
|
|
|
skill_str = skill + ';' + str(skill_level_percent)
|
2019-07-19 10:01:24 +00:00
|
|
|
else:
|
2022-01-03 17:06:41 +00:00
|
|
|
skill_str = skill + ';0'
|
2020-04-04 11:35:55 +00:00
|
|
|
|
2022-01-03 17:06:41 +00:00
|
|
|
new_skill_json = {
|
2019-07-19 10:01:24 +00:00
|
|
|
'type': 'Skill',
|
2020-04-04 11:35:55 +00:00
|
|
|
'actor': actor,
|
2022-01-03 17:06:41 +00:00
|
|
|
'object': '"' + skill_str + '"',
|
|
|
|
'to': [to_url],
|
|
|
|
'cc': [cc_url]
|
2019-07-19 10:01:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 10:00:46 +00:00
|
|
|
handle = http_prefix + '://' + domain_full + '/@' + nickname
|
2019-07-19 10:01:24 +00:00
|
|
|
|
|
|
|
# lookup the inbox for the To handle
|
2022-01-02 14:51:02 +00:00
|
|
|
wf_request = \
|
2021-12-29 21:55:09 +00:00
|
|
|
webfinger_handle(session, handle, http_prefix,
|
|
|
|
cached_webfingers,
|
|
|
|
domain, project_version, debug, False,
|
|
|
|
signing_priv_key_pem)
|
2022-01-02 14:51:02 +00:00
|
|
|
if not wf_request:
|
2019-07-19 10:01:24 +00:00
|
|
|
if debug:
|
2021-03-18 10:01:01 +00:00
|
|
|
print('DEBUG: skill webfinger failed for ' + handle)
|
2019-07-19 10:01:24 +00:00
|
|
|
return 1
|
2022-01-02 14:51:02 +00:00
|
|
|
if not isinstance(wf_request, dict):
|
2021-03-18 10:01:01 +00:00
|
|
|
print('WARN: skill webfinger for ' + handle +
|
2022-01-02 14:51:02 +00:00
|
|
|
' did not return a dict. ' + str(wf_request))
|
2020-06-23 10:41:12 +00:00
|
|
|
return 1
|
2019-07-19 10:01:24 +00:00
|
|
|
|
2022-01-03 17:06:41 +00:00
|
|
|
post_to_box = 'outbox'
|
2019-07-19 10:01:24 +00:00
|
|
|
|
|
|
|
# get the actor inbox for the To handle
|
2022-01-03 17:06:41 +00:00
|
|
|
origin_domain = domain
|
|
|
|
(inbox_url, _, _, from_person_id, _, _,
|
|
|
|
_, _) = get_person_box(signing_priv_key_pem,
|
|
|
|
origin_domain,
|
|
|
|
base_dir, session, wf_request,
|
|
|
|
person_cache, project_version,
|
|
|
|
http_prefix, nickname, domain,
|
2023-10-25 19:55:40 +00:00
|
|
|
post_to_box, 76121,
|
|
|
|
system_language)
|
2022-01-03 17:06:41 +00:00
|
|
|
|
|
|
|
if not inbox_url:
|
2019-07-19 10:01:24 +00:00
|
|
|
if debug:
|
2022-01-03 17:06:41 +00:00
|
|
|
print('DEBUG: skill no ' + post_to_box +
|
2021-03-18 10:01:01 +00:00
|
|
|
' was found for ' + handle)
|
2019-07-19 10:01:24 +00:00
|
|
|
return 3
|
2022-01-03 17:06:41 +00:00
|
|
|
if not from_person_id:
|
2019-07-19 10:01:24 +00:00
|
|
|
if debug:
|
2021-03-18 10:01:01 +00:00
|
|
|
print('DEBUG: skill no actor was found for ' + handle)
|
2019-07-19 10:01:24 +00:00
|
|
|
return 4
|
2020-03-22 21:16:02 +00:00
|
|
|
|
2022-01-03 17:06:41 +00:00
|
|
|
auth_header = create_basic_auth_header(nickname, password)
|
2020-03-22 21:16:02 +00:00
|
|
|
|
2020-04-04 11:35:55 +00:00
|
|
|
headers = {
|
|
|
|
'host': domain,
|
|
|
|
'Content-type': 'application/json',
|
2022-01-03 17:06:41 +00:00
|
|
|
'Authorization': auth_header
|
2020-03-22 20:36:19 +00:00
|
|
|
}
|
2022-01-03 17:06:41 +00:00
|
|
|
post_result = \
|
2021-12-29 21:55:09 +00:00
|
|
|
post_json(http_prefix, domain_full,
|
2022-01-03 17:06:41 +00:00
|
|
|
session, new_skill_json, [], inbox_url,
|
2021-12-29 21:55:09 +00:00
|
|
|
headers, 30, True)
|
2022-01-03 17:06:41 +00:00
|
|
|
if not post_result:
|
2020-04-04 11:35:55 +00:00
|
|
|
if debug:
|
2022-01-03 17:06:41 +00:00
|
|
|
print('DEBUG: POST skill failed for c2s to ' + inbox_url)
|
2020-04-04 11:35:55 +00:00
|
|
|
# return 5
|
2019-07-19 10:01:24 +00:00
|
|
|
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: c2s POST skill success')
|
|
|
|
|
2022-01-03 17:06:41 +00:00
|
|
|
return new_skill_json
|
2021-05-16 15:10:39 +00:00
|
|
|
|
|
|
|
|
2022-01-03 17:06:41 +00:00
|
|
|
def actor_has_skill(actor_json: {}, skill_name: str) -> bool:
|
2021-05-16 15:10:39 +00:00
|
|
|
"""Returns true if the given actor has the given skill
|
|
|
|
"""
|
2022-01-03 17:06:41 +00:00
|
|
|
oc_skills_list = get_occupation_skills(actor_json)
|
|
|
|
for skill_str in oc_skills_list:
|
|
|
|
if skill_name + ':' in skill_str:
|
2021-05-16 15:10:39 +00:00
|
|
|
return True
|
|
|
|
return False
|