epicyon/skills.py

271 lines
8.2 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"
__email__ = "bob@freedombone.net"
__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
from webfinger import webfingerHandle
from auth import createBasicAuthHeader
from posts import getPersonBox
from session import postJson
2020-12-16 11:19:16 +00:00
from utils import getFullDomain
2019-07-19 10:01:24 +00:00
from utils import getNicknameFromActor
from utils import getDomainFromActor
2019-10-22 11:55:06 +00:00
from utils import loadJson
2021-05-16 15:10:39 +00:00
from utils import getOccupationSkills
from utils import setOccupationSkillsList
2021-07-13 21:59:53 +00:00
from utils import acctDir
2021-05-14 17:41:05 +00:00
def setSkillsFromDict(actorJson: {}, 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-05-16 15:10:39 +00:00
setOccupationSkillsList(actorJson, skillsList)
2021-05-14 17:41:05 +00:00
return skillsList
2021-05-14 20:02:55 +00:00
def getSkillsFromList(skillsList: []) -> {}:
"""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
def actorSkillValue(actorJson: {}, skillName: str) -> int:
"""Returns The skill level from an actor
"""
2021-05-16 15:10:39 +00:00
ocSkillsList = getOccupationSkills(actorJson)
skillsDict = getSkillsFromList(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
def noOfActorSkills(actorJson: {}) -> int:
"""Returns the number of skills that an actor has
"""
if actorJson.get('hasOccupation'):
2021-05-16 15:10:39 +00:00
skillsList = getOccupationSkills(actorJson)
return len(skillsList)
return 0
def setActorSkillLevel(actorJson: {},
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-05-13 14:13:27 +00:00
if not actorJson:
return True
if not actorJson.get('hasOccupation'):
2021-07-04 19:59:08 +00:00
actorJson['hasOccupation'] = [{
'@type': 'Occupation',
'name': '',
"occupationLocation": {
"@type": "City",
"name": "Fediverse"
},
'skills': []
}]
2021-05-16 15:10:39 +00:00
ocSkillsList = getOccupationSkills(actorJson)
skillsDict = getSkillsFromList(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 ' +
actorJson['id'])
return False
2021-05-13 14:13:27 +00:00
if skillLevelPercent > 0:
skillsDict[skill] = skillLevelPercent
else:
if skillsDict.get(skill):
del skillsDict[skill]
setSkillsFromDict(actorJson, skillsDict)
return True
2019-07-19 10:01:24 +00:00
2020-04-04 11:35:55 +00:00
def setSkillLevel(baseDir: 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-07-13 21:59:53 +00:00
actorFilename = acctDir(baseDir, 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
2020-04-04 11:35:55 +00:00
actorJson = loadJson(actorFilename)
return setActorSkillLevel(actorJson,
skill, skillLevelPercent)
2019-07-19 10:01:24 +00:00
2020-04-04 11:35:55 +00:00
def getSkills(baseDir: str, nickname: str, domain: str) -> []:
2019-07-19 10:01:24 +00:00
"""Returns the skills for a given person
"""
2021-07-13 21:59:53 +00:00
actorFilename = acctDir(baseDir, 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
2020-04-04 11:35:55 +00:00
actorJson = loadJson(actorFilename)
2019-09-30 22:39:02 +00:00
if actorJson:
if not actorJson.get('hasOccupation'):
2019-07-19 10:01:24 +00:00
return None
2021-05-16 15:10:39 +00:00
ocSkillsList = getOccupationSkills(actorJson)
return getSkillsFromList(ocSkillsList)
2019-07-19 10:01:24 +00:00
return None
2020-04-04 11:35:55 +00:00
def outboxSkills(baseDir: str, nickname: str, messageJson: {},
debug: bool) -> bool:
2019-07-19 10:01:24 +00:00
"""Handles receiving a skills update
"""
if not messageJson.get('type'):
return False
2020-04-04 11:35:55 +00:00
if not messageJson['type'] == 'Skill':
2019-07-19 10:01:24 +00:00
return False
if not messageJson.get('actor'):
return False
if not messageJson.get('object'):
return False
if not isinstance(messageJson['object'], str):
return False
2020-04-04 11:35:55 +00:00
actorNickname = getNicknameFromActor(messageJson['actor'])
if actorNickname != nickname:
2019-07-19 10:01:24 +00:00
return False
2020-04-04 11:35:55 +00:00
domain, port = getDomainFromActor(messageJson['actor'])
skill = messageJson['object'].replace('"', '').split(';')[0].strip()
skillLevelPercentStr = \
messageJson['object'].replace('"', '').split(';')[1].strip()
skillLevelPercent = 50
2020-03-01 10:18:08 +00:00
if skillLevelPercentStr.isdigit():
2020-04-04 11:35:55 +00:00
skillLevelPercent = int(skillLevelPercentStr)
return setSkillLevel(baseDir, nickname, domain,
skill, skillLevelPercent)
2019-07-19 10:01:24 +00:00
2020-04-04 11:35:55 +00:00
def sendSkillViaServer(baseDir: str, session, nickname: str, password: str,
domain: str, port: int,
httpPrefix: str,
skill: str, skillLevelPercent: int,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str) -> {}:
2019-07-19 10:01:24 +00:00
"""Sets a skill for a person via c2s
"""
if not session:
print('WARN: No session for sendSkillViaServer')
return 6
2020-12-16 11:19:16 +00:00
domainFull = getFullDomain(domain, port)
2020-03-22 21:16:02 +00:00
2020-04-04 11:35:55 +00:00
actor = httpPrefix + '://' + domainFull + '/users/' + nickname
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]
}
2020-04-04 11:35:55 +00:00
handle = httpPrefix + '://' + domainFull + '/@' + 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 = \
webfingerHandle(session, handle, httpPrefix,
cachedWebfingers,
2021-03-14 19:22:58 +00:00
domain, projectVersion, debug)
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
2020-04-04 11:35:55 +00:00
(inboxUrl, pubKeyId, pubKey,
fromPersonId, sharedInbox,
avatarUrl, displayName) = getPersonBox(baseDir, session, wfRequest,
personCache, projectVersion,
httpPrefix, nickname, domain,
2020-12-18 17:49:17 +00:00
postToBox, 86725)
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
2020-04-04 11:35:55 +00:00
authHeader = createBasicAuthHeader(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-06-20 13:39:53 +00:00
postJson(httpPrefix, domainFull,
session, newSkillJson, [], inboxUrl,
2021-03-10 19:24:52 +00:00
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
def actorHasSkill(actorJson: {}, skillName: str) -> bool:
"""Returns true if the given actor has the given skill
"""
ocSkillsList = getOccupationSkills(actorJson)
for skillStr in ocSkillsList:
if skillName + ':' in skillStr:
return True
return False