epicyon/skills.py

276 lines
8.6 KiB
Python
Raw Normal View History

2020-04-04 11:35:55 +00:00
__filename__ = "skills.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.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
2021-12-29 21:55:09 +00:00
def set_skills_from_dict(actor_json: {}, skillsDict: {}) -> []:
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-14 17:41:05 +00:00
skillsList = []
2021-05-13 14:13:27 +00:00
for name, value in skillsDict.items():
2021-05-14 17:41:05 +00:00
skillsList.append(name + ':' + str(value))
2021-12-26 18:50:59 +00:00
set_occupation_skills_list(actor_json, skillsList)
2021-05-14 17:41:05 +00:00
return skillsList
2021-12-28 20:32:11 +00:00
def get_skills_from_list(skillsList: []) -> {}:
2021-05-14 20:02:55 +00:00
"""Returns a dict of skills from a list
"""
2021-05-14 20:02:55 +00:00
if isinstance(skillsList, list):
skillsList2 = skillsList
2021-05-14 17:41:05 +00:00
else:
2021-05-14 20:02:55 +00:00
skillsList2 = skillsList.split(',')
2021-05-13 14:13:27 +00:00
skillsDict = {}
2021-05-14 20:02:55 +00:00
for skill in skillsList2:
if ':' not in skill:
continue
name = skill.split(':')[0].strip().lower()
valueStr = skill.split(':')[1]
if not valueStr.isdigit():
continue
2021-05-13 14:13:27 +00:00
skillsDict[name] = int(valueStr)
return skillsDict
2021-12-28 20:32:11 +00:00
def actor_skill_value(actor_json: {}, skillName: str) -> int:
"""Returns The skill level from an actor
"""
2021-12-26 19:01:36 +00:00
ocSkillsList = get_occupation_skills(actor_json)
2021-12-28 20:32:11 +00:00
skillsDict = get_skills_from_list(ocSkillsList)
2021-05-13 14:13:27 +00:00
if not skillsDict:
return 0
skillName = skillName.lower()
2021-05-13 14:13:27 +00:00
if skillsDict.get(skillName):
return skillsDict[skillName]
return 0
2021-12-28 20:32:11 +00:00
def no_of_actor_skills(actor_json: {}) -> int:
"""Returns the number of skills that an actor has
"""
2021-12-26 10:29:52 +00:00
if actor_json.get('hasOccupation'):
2021-12-26 19:01:36 +00:00
skillsList = get_occupation_skills(actor_json)
2021-05-16 15:10:39 +00:00
return len(skillsList)
return 0
2021-12-28 20:32:11 +00:00
def set_actor_skill_level(actor_json: {},
skill: str, skillLevelPercent: int) -> bool:
"""Set a skill level for a person
Setting skill level to zero removes it
"""
if skillLevelPercent < 0 or skillLevelPercent > 100:
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': []
}]
2021-12-26 19:01:36 +00:00
ocSkillsList = get_occupation_skills(actor_json)
2021-12-28 20:32:11 +00:00
skillsDict = get_skills_from_list(ocSkillsList)
if not skillsDict.get(skill):
2021-06-07 09:19:18 +00:00
if len(skillsDict.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
2021-05-13 14:13:27 +00:00
if skillLevelPercent > 0:
skillsDict[skill] = skillLevelPercent
else:
if skillsDict.get(skill):
del skillsDict[skill]
2021-12-29 21:55:09 +00:00
set_skills_from_dict(actor_json, skillsDict)
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,
skill: str, skillLevelPercent: int) -> bool:
2019-07-19 10:01:24 +00:00
"""Set a skill level for a person
Setting skill level to zero removes it
"""
2020-04-04 11:35:55 +00:00
if skillLevelPercent < 0 or skillLevelPercent > 100:
2019-07-19 10:01:24 +00:00
return False
2021-12-26 12:02:29 +00:00
actorFilename = acct_dir(base_dir, nickname, domain) + '.json'
2019-07-19 10:01:24 +00:00
if not os.path.isfile(actorFilename):
return False
2019-09-30 22:39:02 +00:00
2021-12-26 15:13:34 +00:00
actor_json = load_json(actorFilename)
2021-12-28 20:32:11 +00:00
return set_actor_skill_level(actor_json,
skill, skillLevelPercent)
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
"""
2021-12-26 12:02:29 +00:00
actorFilename = acct_dir(base_dir, nickname, domain) + '.json'
2019-07-19 10:01:24 +00:00
if not os.path.isfile(actorFilename):
return False
2019-09-30 22:39:02 +00:00
2021-12-26 15:13:34 +00:00
actor_json = load_json(actorFilename)
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
2021-12-26 19:01:36 +00:00
ocSkillsList = get_occupation_skills(actor_json)
2021-12-28 20:32:11 +00:00
return get_skills_from_list(ocSkillsList)
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
2021-12-27 22:19:18 +00:00
actorNickname = get_nickname_from_actor(message_json['actor'])
2020-04-04 11:35:55 +00:00
if actorNickname != nickname:
2019-07-19 10:01:24 +00:00
return False
2021-12-27 19:05:25 +00:00
domain, port = get_domain_from_actor(message_json['actor'])
2021-12-25 23:51:19 +00:00
skill = message_json['object'].replace('"', '').split(';')[0].strip()
2020-04-04 11:35:55 +00:00
skillLevelPercentStr = \
2021-12-25 23:51:19 +00:00
message_json['object'].replace('"', '').split(';')[1].strip()
2020-04-04 11:35:55 +00:00
skillLevelPercent = 50
2020-03-01 10:18:08 +00:00
if skillLevelPercentStr.isdigit():
2020-04-04 11:35:55 +00:00
skillLevelPercent = int(skillLevelPercentStr)
2021-12-29 21:55:09 +00:00
return set_skill_level(base_dir, nickname, domain,
skill, skillLevelPercent)
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,
skill: str, skillLevelPercent: int,
cached_webfingers: {}, person_cache: {},
debug: bool, project_version: str,
signing_priv_key_pem: 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)
2020-04-04 11:35:55 +00:00
toUrl = actor
ccUrl = actor + '/followers'
2019-07-19 10:01:24 +00:00
if skillLevelPercent:
2020-04-04 11:35:55 +00:00
skillStr = skill + ';' + str(skillLevelPercent)
2019-07-19 10:01:24 +00:00
else:
2020-04-04 11:35:55 +00:00
skillStr = skill + ';0'
newSkillJson = {
2019-07-19 10:01:24 +00:00
'type': 'Skill',
2020-04-04 11:35:55 +00:00
'actor': actor,
'object': '"' + skillStr + '"',
2019-07-19 10:01:24 +00:00
'to': [toUrl],
'cc': [ccUrl]
}
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
2020-04-04 11:35:55 +00:00
wfRequest = \
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)
2019-07-19 10:01:24 +00:00
if not wfRequest:
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
2020-06-23 10:41:12 +00:00
if not isinstance(wfRequest, dict):
2021-03-18 10:01:01 +00:00
print('WARN: skill webfinger for ' + handle +
' did not return a dict. ' + str(wfRequest))
2020-06-23 10:41:12 +00:00
return 1
2019-07-19 10:01:24 +00:00
2020-04-04 11:35:55 +00:00
postToBox = 'outbox'
2019-07-19 10:01:24 +00:00
# get the actor inbox for the To handle
2021-09-15 14:05:08 +00:00
originDomain = domain
(inboxUrl, pubKeyId, pubKey, fromPersonId, sharedInbox, avatarUrl,
2021-12-29 21:55:09 +00:00
displayName, _) = get_person_box(signing_priv_key_pem,
originDomain,
base_dir, session, wfRequest,
person_cache, project_version,
http_prefix, nickname, domain,
postToBox, 76121)
2020-03-22 21:16:02 +00:00
2019-07-19 10:01:24 +00:00
if not inboxUrl:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: skill no ' + postToBox +
' was found for ' + handle)
2019-07-19 10:01:24 +00:00
return 3
if not fromPersonId:
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
2021-12-28 21:36:27 +00:00
authHeader = 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',
2020-03-22 20:36:19 +00:00
'Authorization': authHeader
}
2020-04-04 11:35:55 +00:00
postResult = \
2021-12-29 21:55:09 +00:00
post_json(http_prefix, domain_full,
session, newSkillJson, [], inboxUrl,
headers, 30, True)
2020-04-04 11:35:55 +00:00
if not postResult:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: POST skill failed for c2s to ' + inboxUrl)
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')
return newSkillJson
2021-05-16 15:10:39 +00:00
2021-12-28 20:32:11 +00:00
def actor_has_skill(actor_json: {}, skillName: str) -> bool:
2021-05-16 15:10:39 +00:00
"""Returns true if the given actor has the given skill
"""
2021-12-26 19:01:36 +00:00
ocSkillsList = get_occupation_skills(actor_json)
2021-05-16 15:10:39 +00:00
for skillStr in ocSkillsList:
if skillName + ':' in skillStr:
return True
return False