diff --git a/person.py b/person.py index c5accdc06..2a5c30d1f 100644 --- a/person.py +++ b/person.py @@ -105,6 +105,35 @@ def setSkillLevel(baseDir: str,nickname: str,domain: str, \ commentjson.dump(actorJson, fp, indent=4, sort_keys=False) return True +def setRole(baseDir: str,nickname: str,domain: str, \ + project: str,role: str) -> bool: + """Set a person's role within a project + Setting the role to an empty string or None will remove it + """ + # avoid giant strings + if len(role)>128 or len(project)>128: + return False + actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json' + if not os.path.isfile(actorFilename): + return False + with open(actorFilename, 'r') as fp: + actorJson=commentjson.load(fp) + if role: + if actorJson['roles'].get(project): + if role not in actorJson['roles'][project]: + actorJson['roles'][project].append(role) + else: + actorJson['roles'][project]=[role] + else: + if actorJson['roles'].get(project): + actorJson['roles'][project].remove(role) + # if the project contains no roles then remove it + if len(actorJson['roles'][project])==0: + del actorJson['roles'][project] + with open(actorFilename, 'w') as fp: + commentjson.dump(actorJson, fp, indent=4, sort_keys=False) + return True + def createPersonBase(baseDir: str,nickname: str,domain: str,port: int, \ httpPrefix: str, saveToFile: bool,password=None) -> (str,str,{},{}): """Returns the private key, public key, actor and webfinger endpoint diff --git a/tests.py b/tests.py index ff5cb01b0..30f592557 100644 --- a/tests.py +++ b/tests.py @@ -41,6 +41,7 @@ from person import createPerson from person import setPreferredNickname from person import setBio from person import setSkillLevel +from person import setRole from auth import createBasicAuthHeader from auth import authorizeBasic from auth import storeBasicCredentials @@ -149,6 +150,7 @@ def createServerAlice(path: str,domain: str,port: int,federationList: [], \ deleteAllPosts(path,nickname,domain,'inbox') deleteAllPosts(path,nickname,domain,'outbox') assert setSkillLevel(path,nickname,domain,'hacking',90) + assert setRole(path,nickname,domain,'someproject','guru') if hasFollows: followPerson(path,nickname,domain,'bob','127.0.0.100:61936', \ federationList,True) @@ -194,6 +196,8 @@ def createServerBob(path: str,domain: str,port: int,federationList: [], \ createPerson(path,nickname,domain,port,httpPrefix,True,password) deleteAllPosts(path,nickname,domain,'inbox') deleteAllPosts(path,nickname,domain,'outbox') + assert setRole(path,nickname,domain,'bandname','bass player') + assert setRole(path,nickname,domain,'bandname','publicist') if hasFollows: followPerson(path,nickname,domain, \ 'alice','127.0.0.50:61935',federationList,True)