Tidying of role functions

main
Bob Mottram 2021-03-08 21:07:28 +00:00
parent 724da883e6
commit c90fc15c12
1 changed files with 60 additions and 51 deletions

111
roles.py
View File

@ -18,30 +18,10 @@ from utils import loadJson
from utils import saveJson from utils import saveJson
def clearModeratorStatus(baseDir: str) -> None: def _clearRoleStatus(baseDir: str, role: str) -> None:
"""Removes moderator status from all accounts """Removes role status from all accounts
This could be slow if there are many users, but only happens This could be slow if there are many users, but only happens
rarely when moderators are appointed or removed rarely when roles are appointed or removed
"""
directory = os.fsencode(baseDir + '/accounts/')
for f in os.scandir(directory):
f = f.name
filename = os.fsdecode(f)
if filename.endswith(".json") and '@' in filename:
filename = os.path.join(baseDir + '/accounts/', filename)
if '"moderator"' in open(filename).read():
actorJson = loadJson(filename)
if actorJson:
if actorJson['roles'].get('instance'):
if 'moderator' in actorJson['roles']['instance']:
actorJson['roles']['instance'].remove('moderator')
saveJson(actorJson, filename)
def clearEditorStatus(baseDir: str) -> None:
"""Removes editor status from all accounts
This could be slow if there are many users, but only happens
rarely when editors are appointed or removed
""" """
directory = os.fsencode(baseDir + '/accounts/') directory = os.fsencode(baseDir + '/accounts/')
for f in os.scandir(directory): for f in os.scandir(directory):
@ -52,59 +32,88 @@ def clearEditorStatus(baseDir: str) -> None:
if not filename.endswith(".json"): if not filename.endswith(".json"):
continue continue
filename = os.path.join(baseDir + '/accounts/', filename) filename = os.path.join(baseDir + '/accounts/', filename)
if '"editor"' not in open(filename).read(): if '"' + role + '"' not in open(filename).read():
continue continue
actorJson = loadJson(filename) actorJson = loadJson(filename)
if not actorJson: if not actorJson:
continue continue
if actorJson['roles'].get('instance'): if actorJson['roles'].get('instance'):
if 'editor' in actorJson['roles']['instance']: if role in actorJson['roles']['instance']:
actorJson['roles']['instance'].remove('editor') actorJson['roles']['instance'].remove(role)
saveJson(actorJson, filename) saveJson(actorJson, filename)
def _addModerator(baseDir: str, nickname: str, domain: str) -> None: def clearEditorStatus(baseDir: str) -> None:
"""Adds a moderator nickname to the file """Removes editor status from all accounts
This could be slow if there are many users, but only happens
rarely when editors are appointed or removed
"""
_clearRoleStatus(baseDir, 'editor')
def clearModeratorStatus(baseDir: str) -> None:
"""Removes moderator status from all accounts
This could be slow if there are many users, but only happens
rarely when moderators are appointed or removed
"""
_clearRoleStatus(baseDir, 'moderator')
def _addRole(baseDir: str, nickname: str, domain: str,
roleFilename: str) -> None:
"""Adds a role nickname to the file
""" """
if ':' in domain: if ':' in domain:
domain = domain.split(':')[0] domain = domain.split(':')[0]
moderatorsFile = baseDir + '/accounts/moderators.txt' roleFile = baseDir + '/accounts/' + roleFilename
if os.path.isfile(moderatorsFile): if os.path.isfile(roleFile):
# is this nickname already in the file? # is this nickname already in the file?
with open(moderatorsFile, "r") as f: with open(roleFile, "r") as f:
lines = f.readlines() lines = f.readlines()
for moderator in lines: for roleNickname in lines:
moderator = moderator.strip('\n').strip('\r') roleNickname = roleNickname.strip('\n').strip('\r')
if moderator == nickname: if roleNickname == nickname:
return return
lines.append(nickname) lines.append(nickname)
with open(moderatorsFile, 'w+') as f: with open(roleFile, 'w+') as f:
for moderator in lines: for roleNickname in lines:
moderator = moderator.strip('\n').strip('\r') roleNickname = roleNickname.strip('\n').strip('\r')
if len(moderator) > 1: if len(roleNickname) > 1:
if os.path.isdir(baseDir + '/accounts/' + if os.path.isdir(baseDir + '/accounts/' +
moderator + '@' + domain): roleNickname + '@' + domain):
f.write(moderator + '\n') f.write(roleNickname + '\n')
else: else:
with open(moderatorsFile, "w+") as f: with open(roleFile, "w+") as f:
if os.path.isdir(baseDir + '/accounts/' + if os.path.isdir(baseDir + '/accounts/' +
nickname + '@' + domain): nickname + '@' + domain):
f.write(nickname + '\n') f.write(nickname + '\n')
def _removeModerator(baseDir: str, nickname: str): def _addModerator(baseDir: str, nickname: str, domain: str) -> None:
"""Removes a moderator nickname from the file """Adds a moderator nickname to the file
""" """
moderatorsFile = baseDir + '/accounts/moderators.txt' _addRole(baseDir, nickname, domain, 'moderators.txt')
if not os.path.isfile(moderatorsFile):
def _removeRole(baseDir: str, nickname: str, roleFilename: str) -> None:
"""Removes a role nickname from the file
"""
roleFile = baseDir + '/accounts/' + roleFilename
if not os.path.isfile(roleFile):
return return
with open(moderatorsFile, "r") as f: with open(roleFile, "r") as f:
lines = f.readlines() lines = f.readlines()
with open(moderatorsFile, 'w+') as f: with open(roleFile, 'w+') as f:
for moderator in lines: for roleNickname in lines:
moderator = moderator.strip('\n').strip('\r') roleNickname = roleNickname.strip('\n').strip('\r')
if len(moderator) > 1 and moderator != nickname: if len(roleNickname) > 1 and roleNickname != nickname:
f.write(moderator + '\n') f.write(roleNickname + '\n')
def _removeModerator(baseDir: str, nickname: str) -> None:
"""Adds a moderator nickname to the file
"""
_removeRole(baseDir, nickname, 'moderators.txt')
def setRole(baseDir: str, nickname: str, domain: str, def setRole(baseDir: str, nickname: str, domain: str,