Exception handling

main2
Bob Mottram 2019-09-30 23:39:02 +01:00
parent 358cd2ea40
commit 3c6122c56c
18 changed files with 411 additions and 146 deletions

View File

@ -67,8 +67,14 @@ def undoAnnounceCollectionEntry(postFilename: str,actor: str,debug: bool) -> Non
collection within a post. Note that the "shares" collection has no relation
to shared items in shares.py. It's shares of posts, not shares of physical objects.
"""
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
postJsonObject=None
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
except Exception as e:
print(e)
if postJsonObject:
if not postJsonObject.get('type'):
return
if postJsonObject['type']!='Create':
@ -103,16 +109,24 @@ def undoAnnounceCollectionEntry(postFilename: str,actor: str,debug: bool) -> Non
del postJsonObject['object']['shares']
else:
postJsonObject['object']['shares']['totalItems']=len(postJsonObject['object']['shares']['items'])
with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
try:
with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
def updateAnnounceCollection(postFilename: str,actor: str,debug: bool) -> None:
"""Updates the announcements collection within a post
Confusingly this is known as "shares", but isn't the same as shared items within shares.py
It's shares of posts, not shares of physical objects.
"""
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
postJsonObject=None
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
except Exception as e:
print(e)
if postJsonObject:
if not postJsonObject.get('object'):
if debug:
pprint(postJsonObject)
@ -154,8 +168,11 @@ def updateAnnounceCollection(postFilename: str,actor: str,debug: bool) -> None:
if debug:
print('DEBUG: saving post with shares (announcements) added')
pprint(postJsonObject)
with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
try:
with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
def announcedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool:
"""Returns True if the given post is announced by the given person
@ -218,8 +235,11 @@ def createAnnounce(session,baseDir: str,federationList: [], \
if saveToFile:
outboxDir = createOutboxDir(nickname,domain,baseDir)
filename=outboxDir+'/'+newAnnounceId.replace('/','#')+'.json'
with open(filename, 'w') as fp:
commentjson.dump(newAnnounce, fp, indent=4, sort_keys=False)
try:
with open(filename, 'w') as fp:
commentjson.dump(newAnnounce, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
announceNickname=None
announceDomain=None

View File

@ -26,11 +26,19 @@ def setAvailability(baseDir: str,nickname: str,domain: str, \
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if not os.path.isfile(actorFilename):
return False
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
actorJson=None
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
except Exception as e:
print(e)
if actorJson:
actorJson['availability']=status
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
return True
def getAvailability(baseDir: str,nickname: str,domain: str) -> str:
@ -39,8 +47,13 @@ def getAvailability(baseDir: str,nickname: str,domain: str) -> str:
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if not os.path.isfile(actorFilename):
return False
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
actorJson=None
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
except Exception as e:
print(e)
if actorJson:
if not actorJson.get('availability'):
return None
return actorJson['availability']

View File

@ -26,8 +26,11 @@ def storePersonInCache(baseDir: str,personUrl: str,personJson: {},personCache: {
if os.path.isdir(baseDir+'/cache/actors'):
cacheFilename=baseDir+'/cache/actors/'+personUrl.replace('/','#')+'.json'
if not os.path.isfile(cacheFilename):
with open(cacheFilename, 'w') as fp:
commentjson.dump(personJson, fp, indent=4, sort_keys=False)
try:
with open(cacheFilename, 'w') as fp:
commentjson.dump(personJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
def getPersonFromCache(baseDir: str,personUrl: str,personCache: {}) -> {}:
"""Get an actor from the cache

View File

@ -127,8 +127,11 @@ def capabilitiesAccept(baseDir: str,httpPrefix: str, \
# if the capability already exists then load it from file
if os.path.isfile(ocapFilename):
with open(ocapFilename, 'r') as fp:
ocapAccept=commentjson.load(fp)
try:
with open(ocapFilename, 'r') as fp:
ocapAccept=commentjson.load(fp)
except Exception as e:
print(e)
# otherwise create a new capability
if not ocapAccept:
acceptedActorNickname=getNicknameFromActor(acceptedActor)
@ -152,8 +155,11 @@ def capabilitiesAccept(baseDir: str,httpPrefix: str, \
ocapAccept['actor']=httpPrefix+"://"+fullDomain+'/users/'+nickname
if saveToFile:
with open(ocapFilename, 'w') as fp:
commentjson.dump(ocapAccept, fp, indent=4, sort_keys=False)
try:
with open(ocapFilename, 'w') as fp:
commentjson.dump(ocapAccept, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
return ocapAccept
def capabilitiesGrantedSave(baseDir :str,nickname :str,domain :str,ocap: {}) -> bool:
@ -165,8 +171,11 @@ def capabilitiesGrantedSave(baseDir :str,nickname :str,domain :str,ocap: {}) ->
ocapFilename=getOcapFilename(baseDir,nickname,domain,ocap['actor'],'granted')
if not ocapFilename:
return False
with open(ocapFilename, 'w') as fp:
commentjson.dump(ocap, fp, indent=4, sort_keys=False)
try:
with open(ocapFilename, 'w') as fp:
commentjson.dump(ocap, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
return True
def capabilitiesUpdate(baseDir: str,httpPrefix: str, \
@ -208,8 +217,11 @@ def capabilitiesUpdate(baseDir: str,httpPrefix: str, \
}
# read the existing capability
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
try:
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
except Exception as e:
print(e)
# set the new capabilities list. eg. ["inbox:write","objects:read"]
ocapJson['capability']=updateCaps
@ -228,9 +240,12 @@ def capabilitiesUpdate(baseDir: str,httpPrefix: str, \
ocapUpdate['object']=ocapJson
# save it again
with open(ocapFilename, 'w') as fp:
commentjson.dump(ocapJson, fp, indent=4, sort_keys=False)
try:
with open(ocapFilename, 'w') as fp:
commentjson.dump(ocapJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
return ocapUpdate
def capabilitiesReceiveUpdate(baseDir :str, \
@ -251,12 +266,21 @@ def capabilitiesReceiveUpdate(baseDir :str, \
print(ocapFilename)
return False
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
ocapJson=None
try:
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
except Exception as e:
print(e)
if ocapJson:
ocapJson['id']=newCapabilitiesId
ocapJson['capability']=capabilityList
with open(ocapFilename, 'w') as fp:
commentjson.dump(ocapJson, fp, indent=4, sort_keys=False)
return True
try:
with open(ocapFilename, 'w') as fp:
commentjson.dump(ocapJson, fp, indent=4, sort_keys=False)
return True
except Exception as e:
print(e)
return False

View File

@ -29,8 +29,11 @@ def setConfigParam(baseDir: str, variableName: str, variableValue) -> None:
"""
createConfig(baseDir)
configFilename=baseDir+'/config.json'
with open(configFilename, 'r') as fp:
configJson=commentjson.load(fp)
try:
with open(configFilename, 'r') as fp:
configJson=commentjson.load(fp)
except Exception as e:
print(e)
configJson[variableName]=variableValue
try:
with open(configFilename, 'w') as fp:

View File

@ -3014,13 +3014,19 @@ class PubServer(BaseHTTPRequestHandler):
os.remove(allowedInstancesFilename)
# save actor json file within accounts
if actorChanged:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
# also copy to the actors cache and personCache in memory
storePersonInCache(self.server.baseDir,actorJson['id'],actorJson,self.server.personCache)
actorCacheFilename=self.server.baseDir+'/cache/actors/'+actorJson['id'].replace('/','#')+'.json'
with open(actorCacheFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
try:
with open(actorCacheFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
# send actor update to followers
updateActorJson={
'type': 'Update',

View File

@ -319,8 +319,13 @@ def followApprovalRequired(baseDir: str,nicknameToFollow: str, \
domainToFollow=domainToFollow.split(':')[0]
actorFilename=baseDir+'/accounts/'+nicknameToFollow+'@'+domainToFollow+'.json'
if os.path.isfile(actorFilename):
with open(actorFilename, 'r') as fp:
actor=commentjson.load(fp)
actor=None
try:
with open(actorFilename, 'r') as fp:
actor=commentjson.load(fp)
except Exception as e:
print(e)
if actor:
if actor.get('manuallyApprovesFollowers'):
manuallyApproveFollows=actor['manuallyApprovesFollowers']
else:
@ -384,9 +389,12 @@ def storeFollowRequest(baseDir: str, \
if not os.path.isdir(requestsDir):
os.mkdir(requestsDir)
followActivityfilename=requestsDir+'/'+approveHandle+'.follow'
with open(followActivityfilename, 'w') as fp:
commentjson.dump(followJson, fp, indent=4, sort_keys=False)
return True
try:
with open(followActivityfilename, 'w') as fp:
commentjson.dump(followJson, fp, indent=4, sort_keys=False)
return True
except Exception as e:
print(e)
return False
def receiveFollowRequest(session,baseDir: str,httpPrefix: str, \
@ -828,9 +836,14 @@ def getFollowersOfActor(baseDir :str,actor :str,debug: bool) -> {}:
ocapFilename=baseDir+'/accounts/'+account+'/ocap/accept/'+httpPrefix+':##'+domain+':'+str(port)+'#users#'+nickname+'.json'
if debug:
print('DEBUG: checking capabilities of'+account)
if os.path.isfile(ocapFilename):
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
if os.path.isfile(ocapFilename):
ocapJson=None
try:
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
except Exception as e:
print(e)
if ocapJson:
if ocapJson.get('id'):
if debug:
print('DEBUG: capabilities id found for '+account)

View File

@ -282,9 +282,11 @@ def savePostToInboxQueue(baseDir: str,httpPrefix: str, \
if debug:
print('Inbox queue item created')
pprint(newQueueItem)
with open(filename, 'w') as fp:
commentjson.dump(newQueueItem, fp, indent=4, sort_keys=False)
try:
with open(filename, 'w') as fp:
commentjson.dump(newQueueItem, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
return filename
def inboxCheckCapabilities(baseDir :str,nickname :str,domain :str, \
@ -669,9 +671,12 @@ def personReceiveUpdate(baseDir: str, \
# save to cache in memory
storePersonInCache(baseDir,personJson['id'],personJson,personCache)
# save to cache on file
with open(actorFilename, 'w') as fp:
commentjson.dump(personJson, fp, indent=4, sort_keys=False)
print('actor updated for '+personJson['id'])
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(personJson, fp, indent=4, sort_keys=False)
print('actor updated for '+personJson['id'])
except Exception as e:
print(e)
# remove avatar if it exists so that it will be refreshed later
# when a timeline is constructed
@ -1263,13 +1268,19 @@ def inboxAfterCapabilities(session,keyId: str,handle: str,messageJson: {}, \
if messageJson.get('postNickname'):
if validPostContent(messageJson['post'],maxMentions):
obtainReplyToAvatar(baseDir,personCache,messageJson['post'])
with open(destinationFilename, 'w+') as fp:
commentjson.dump(messageJson['post'], fp, indent=4, sort_keys=False)
try:
with open(destinationFilename, 'w+') as fp:
commentjson.dump(messageJson['post'], fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
else:
if validPostContent(messageJson,maxMentions):
obtainReplyToAvatar(baseDir,personCache,messageJson)
with open(destinationFilename, 'w+') as fp:
commentjson.dump(messageJson, fp, indent=4, sort_keys=False)
try:
with open(destinationFilename, 'w+') as fp:
commentjson.dump(messageJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
if not os.path.isfile(destinationFilename):
return False
@ -1613,9 +1624,12 @@ def runInboxQueue(projectVersion: str, \
if len(recipientsDictFollowers)>0:
sharedInboxPostFilename=queueJson['destination'].replace(inboxHandle,inboxHandle)
if not os.path.isfile(sharedInboxPostFilename):
with open(sharedInboxPostFilename, 'w') as fp:
commentjson.dump(queueJson['post'],fp,indent=4, \
sort_keys=False)
try:
with open(sharedInboxPostFilename, 'w') as fp:
commentjson.dump(queueJson['post'],fp,indent=4, \
sort_keys=False)
except Exception as e:
print(e)
# for posts addressed to specific accounts
for handle,capsId in recipientsDict.items():

36
like.py
View File

@ -21,9 +21,15 @@ from posts import getPersonBox
def undoLikesCollectionEntry(postFilename: str,objectUrl: str,actor: str,debug: bool) -> None:
"""Undoes a like for a particular actor
"""
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
"""
postJsonObject=None
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
except Exception as e:
print(e)
if postJsonObject:
if not postJsonObject.get('type'):
return
if postJsonObject['type']!='Create':
@ -60,8 +66,11 @@ def undoLikesCollectionEntry(postFilename: str,objectUrl: str,actor: str,debug:
del postJsonObject['object']['likes']
else:
postJsonObject['object']['likes']['totalItems']=len(postJsonObject['likes']['items'])
with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
try:
with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
def likedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool:
"""Returns True if the given post is liked by the given person
@ -93,8 +102,14 @@ def noOfLikes(postJsonObject: {}) -> int:
def updateLikesCollection(postFilename: str,objectUrl: str, actor: str,debug: bool) -> None:
"""Updates the likes collection within a post
"""
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
postJsonObject=None
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
except Exception as e:
print(e)
if postJsonObject:
if not postJsonObject.get('object'):
if debug:
pprint(postJsonObject)
@ -133,8 +148,11 @@ def updateLikesCollection(postFilename: str,objectUrl: str, actor: str,debug: bo
if debug:
print('DEBUG: saving post with likes added')
pprint(postJsonObject)
with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
try:
with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
def like(session,baseDir: str,federationList: [],nickname: str,domain: str,port: int, \
ccList: [],httpPrefix: str,objectUrl: str,clientToServer: bool, \

View File

@ -97,8 +97,13 @@ def manualApproveFollowRequest(session,baseDir: str, \
requestsDir=accountsDir+'/requests'
followActivityfilename=requestsDir+'/'+handle+'.follow'
if os.path.isfile(followActivityfilename):
with open(followActivityfilename, 'r') as fp:
followJson=commentjson.load(fp)
followJson=None
try:
with open(followActivityfilename, 'r') as fp:
followJson=commentjson.load(fp)
except Exception as e:
print(e)
if followJson:
approveNickname=approveHandle.split('@')[0]
approveDomain=approveHandle.split('@')[1].replace('\n','')
approvePort=port2

View File

@ -90,12 +90,21 @@ def setProfileImage(baseDir: str,httpPrefix :str,nickname: str,domain: str, \
iconFilename=iconFilenameBase+'.gif'
profileFilename=baseDir+'/accounts/'+handle+'/'+iconFilename
with open(personFilename, 'r') as fp:
personJson=commentjson.load(fp)
personJson=None
try:
with open(personFilename, 'r') as fp:
personJson=commentjson.load(fp)
except Exception as e:
print(e)
if personJson:
personJson[iconFilenameBase]['mediaType']=mediaType
personJson[iconFilenameBase]['url']=httpPrefix+'://'+fullDomain+'/users/'+nickname+'/'+iconFilename
with open(personFilename, 'w') as fp:
commentjson.dump(personJson, fp, indent=4, sort_keys=False)
try:
with open(personFilename, 'w') as fp:
commentjson.dump(personJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
cmd = '/usr/bin/convert '+imageFilename+' -size '+resolution+' -quality 50 '+profileFilename
subprocess.call(cmd, shell=True)
@ -115,11 +124,21 @@ def setOrganizationScheme(baseDir: str,nickname: str,domain: str, \
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if not os.path.isfile(actorFilename):
return False
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
actorJson=None
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
except Exception as e:
print(e)
if actorJson:
actorJson['orgSchema']=schema
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
return True
def accountExists(baseDir: str,nickname: str,domain: str) -> bool:
@ -239,8 +258,11 @@ def createPersonBase(baseDir: str,nickname: str,domain: str,port: int, \
if not os.path.isdir(baseDir+peopleSubdir+'/'+handle+'/queue'):
os.mkdir(baseDir+peopleSubdir+'/'+handle+'/queue')
filename=baseDir+peopleSubdir+'/'+handle+'.json'
with open(filename, 'w') as fp:
commentjson.dump(newPerson, fp, indent=4, sort_keys=False)
try:
with open(filename, 'w') as fp:
commentjson.dump(newPerson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
# save to cache
if not os.path.isdir(baseDir+'/cache'):
@ -248,8 +270,11 @@ def createPersonBase(baseDir: str,nickname: str,domain: str,port: int, \
if not os.path.isdir(baseDir+'/cache/actors'):
os.mkdir(baseDir+'/cache/actors')
cacheFilename=baseDir+'/cache/actors/'+newPerson['id'].replace('/','#')+'.json'
with open(cacheFilename, 'w') as fp:
commentjson.dump(newPerson, fp, indent=4, sort_keys=False)
try:
with open(cacheFilename, 'w') as fp:
commentjson.dump(newPerson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
# save the private key
privateKeysSubdir='/keys/private'
@ -504,14 +529,22 @@ def setDisplayNickname(baseDir: str,nickname: str, domain: str, \
filename=baseDir+'/accounts/'+handle.lower()+'.json'
if not os.path.isfile(filename):
return False
personJson=None
with open(filename, 'r') as fp:
personJson=commentjson.load(fp)
try:
with open(filename, 'r') as fp:
personJson=commentjson.load(fp)
except Exception as e:
print(e)
if not personJson:
return False
personJson['name']=displayName
with open(filename, 'w') as fp:
commentjson.dump(personJson, fp, indent=4, sort_keys=False)
try:
with open(filename, 'w') as fp:
commentjson.dump(personJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
return True
def setBio(baseDir: str,nickname: str, domain: str, bio: str) -> bool:
@ -521,16 +554,26 @@ def setBio(baseDir: str,nickname: str, domain: str, bio: str) -> bool:
filename=baseDir+'/accounts/'+handle.lower()+'.json'
if not os.path.isfile(filename):
return False
personJson=None
with open(filename, 'r') as fp:
personJson=commentjson.load(fp)
try:
with open(filename, 'r') as fp:
personJson=commentjson.load(fp)
except Exception as e:
print(e)
if not personJson:
return False
if not personJson.get('summary'):
return False
personJson['summary']=bio
with open(filename, 'w') as fp:
commentjson.dump(personJson, fp, indent=4, sort_keys=False)
try:
with open(filename, 'w') as fp:
commentjson.dump(personJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
return True
def isSuspended(baseDir: str,nickname: str) -> bool:

View File

@ -413,8 +413,11 @@ def savePostToBox(baseDir: str,httpPrefix: str,postId: str, \
boxDir = createPersonDir(nickname,domain,baseDir,boxname)
filename=boxDir+'/'+postId.replace('/','#')+'.json'
with open(filename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
try:
with open(filename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
return filename
def updateHashtagsIndex(baseDir: str,tag: {},newPostId: str) -> None:
@ -2282,7 +2285,10 @@ def downloadAnnounce(session,baseDir: str,httpPrefix: str,nickname: str,domain:
rejectAnnounce(announceFilename)
return None
postJsonObject=announcedJson
with open(announceFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
return postJsonObject
try:
with open(announceFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False)
return postJsonObject
except Exception as e:
print(e)
return None

View File

@ -28,13 +28,22 @@ def clearModeratorStatus(baseDir: str) -> None:
if filename.endswith(".json") and '@' in filename:
filename=os.path.join(baseDir+'/accounts/', filename)
if '"moderator"' in open(filename).read():
with open(filename, 'r') as fp:
actorJson=commentjson.load(fp)
actorJson=None
try:
with open(filename, 'r') as fp:
actorJson=commentjson.load(fp)
except Exception as e:
print(e)
if actorJson:
if actorJson['roles'].get('instance'):
if 'moderator' in actorJson['roles']['instance']:
actorJson['roles']['instance'].remove('moderator')
with open(filename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
try:
with open(filename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
def addModerator(baseDir: str,nickname: str,domain: str) -> None:
"""Adds a moderator nickname to the file
@ -87,8 +96,15 @@ def setRole(baseDir: str,nickname: str,domain: str, \
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if not os.path.isfile(actorFilename):
return False
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
actorJson=None
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
except Exception as e:
print(e)
if actorJson:
if role:
# add the role
if project=='instance' and 'role'=='moderator':
@ -107,8 +123,11 @@ def setRole(baseDir: str,nickname: str,domain: str, \
# 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)
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
return True
def getRoles(baseDir: str,nickname: str,domain: str, \
@ -118,8 +137,15 @@ def getRoles(baseDir: str,nickname: str,domain: str, \
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if not os.path.isfile(actorFilename):
return False
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
actorJson=None
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
except Exception as e:
print(e)
if actorJson:
if not actorJson.get('roles'):
return None
if not actorJson['roles'].get(project):

View File

@ -25,9 +25,12 @@ def removeShare(baseDir: str,nickname: str,domain: str, \
"""Removes a share for a person
"""
sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json'
if os.path.isfile(sharesFilename):
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
if os.path.isfile(sharesFilename):
try:
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
except Exception as e:
print(e)
itemID=displayName.replace(' ','')
if sharesJson.get(itemID):
@ -42,8 +45,11 @@ def removeShare(baseDir: str,nickname: str,domain: str, \
os.remove(itemIDfile+'.gif')
# remove the item itself
del sharesJson[itemID]
with open(sharesFilename, 'w') as fp:
commentjson.dump(sharesJson, fp, indent=4, sort_keys=False)
try:
with open(sharesFilename, 'w') as fp:
commentjson.dump(sharesJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
def addShare(baseDir: str, \
httpPrefix: str,nickname: str,domain: str,port: int, \
@ -59,9 +65,12 @@ def addShare(baseDir: str, \
"""
sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json'
sharesJson={}
if os.path.isfile(sharesFilename):
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
if os.path.isfile(sharesFilename):
try:
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
except Exception as e:
print(e)
duration=duration.lower()
durationSec=0
@ -137,8 +146,11 @@ def addShare(baseDir: str, \
"expire": durationSec
}
with open(sharesFilename, 'w') as fp:
commentjson.dump(sharesJson, fp, indent=4, sort_keys=False)
try:
with open(sharesFilename, 'w') as fp:
commentjson.dump(sharesJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
def expireShares(baseDir: str,nickname: str,domain: str) -> None:
"""Removes expired items from shares
@ -149,8 +161,13 @@ def expireShares(baseDir: str,nickname: str,domain: str) -> None:
handle=nickname+'@'+handleDomain
sharesFilename=baseDir+'/accounts/'+handle+'/shares.json'
if os.path.isfile(sharesFilename):
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
sharesJson=None
try:
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
except Exception as e:
print(e)
if sharesJson:
currTime=int(time.time())
deleteItemID=[]
for itemID,item in sharesJson.items():
@ -167,9 +184,12 @@ def expireShares(baseDir: str,nickname: str,domain: str) -> None:
os.remove(itemIDfile+'.jpg')
if os.path.isfile(itemIDfile+'.gif'):
os.remove(itemIDfile+'.gif')
with open(sharesFilename, 'w') as fp:
commentjson.dump(sharesJson, fp, indent=4, sort_keys=False)
try:
with open(sharesFilename, 'w') as fp:
commentjson.dump(sharesJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
def getSharesFeedForPerson(baseDir: str, \
domain: str,port: int, \
path: str,httpPrefix: str, \
@ -219,9 +239,12 @@ def getSharesFeedForPerson(baseDir: str, \
if headerOnly:
noOfShares=0
if os.path.isfile(sharesFilename):
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
noOfShares=len(sharesJson.items())
try:
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
noOfShares=len(sharesJson.items())
except Exception as e:
print(e)
shares = {
'@context': 'https://www.w3.org/ns/activitystreams',
'first': httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page=1',
@ -249,8 +272,14 @@ def getSharesFeedForPerson(baseDir: str, \
pageCtr=0
totalCtr=0
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
sharesJson=None
try:
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
except Exception as e:
print(e)
if sharesJson:
for itemID,item in sharesJson.items():
pageCtr += 1
totalCtr += 1

View File

@ -26,27 +26,47 @@ def setSkillLevel(baseDir: str,nickname: str,domain: str, \
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if not os.path.isfile(actorFilename):
return False
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
actorJson=None
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
except Exception as e:
print(e)
if actorJson:
if not actorJson.get('skills'):
actorJson['skills']={}
if skillLevelPercent>0:
actorJson['skills'][skill]=skillLevelPercent
else:
del actorJson['skills'][skill]
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
return True
def setSkills(baseDir: str,nickname: str,domain: str,skills: {}) -> None:
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if not os.path.isfile(actorFilename):
return False
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
actorJson=None
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
except Exception as e:
print(e)
if actorJson:
actorJson['skills']=skills
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
def getSkills(baseDir: str,nickname: str,domain: str) -> []:
"""Returns the skills for a given person
@ -54,8 +74,15 @@ def getSkills(baseDir: str,nickname: str,domain: str) -> []:
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if not os.path.isfile(actorFilename):
return False
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
actorJson=None
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
except Exception as e:
print(e)
if actorJson:
if not actorJson.get('skills'):
return None
return actorJson['skills']

View File

@ -452,6 +452,7 @@ def testPostMessageBetweenServers():
with open(outboxPostFilename, 'r') as fp:
alicePostJson=commentjson.load(fp)
pprint(alicePostJson)
assert 'likes' in open(outboxPostFilename).read()
print('\n\n*******************************************************')

View File

@ -229,9 +229,14 @@ def removeModerationPostFromIndex(baseDir: str,postUrl: str,debug: bool) -> None
def deletePost(baseDir: str,httpPrefix: str,nickname: str,domain: str,postFilename: str,debug: bool):
"""Recursively deletes a post and its replies and attachments
"""
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
postJsonObject=None
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
except Exception as e:
print(e)
if postJsonObject:
# remove any attachment
removeAttachment(baseDir,httpPrefix,domain,postJsonObject)

View File

@ -91,13 +91,19 @@ def storeWebfingerEndpoint(nickname: str,domain: str,port: int,baseDir: str, \
if not os.path.isdir(baseDir+wfSubdir):
os.mkdir(baseDir+wfSubdir)
filename=baseDir+wfSubdir+'/'+handle.lower()+'.json'
with open(filename, 'w') as fp:
commentjson.dump(wfJson, fp, indent=4, sort_keys=False)
try:
with open(filename, 'w') as fp:
commentjson.dump(wfJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
if nickname=='inbox':
handle=originalDomain+'@'+domain
filename=baseDir+wfSubdir+'/'+handle.lower()+'.json'
with open(filename, 'w') as fp:
commentjson.dump(wfJson, fp, indent=4, sort_keys=False)
try:
with open(filename, 'w') as fp:
commentjson.dump(wfJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
return True
def createWebfingerEndpoint(nickname: str,domain: str,port: int, \
@ -216,6 +222,9 @@ def webfingerLookup(path: str,baseDir: str,port: int,debug: bool) -> {}:
print('DEBUG: WEBFINGER filename not found '+filename)
return None
wfJson={"nickname": "unknown"}
with open(filename, 'r') as fp:
wfJson=commentjson.load(fp)
try:
with open(filename, 'r') as fp:
wfJson=commentjson.load(fp)
except Exception as e:
print(e)
return wfJson