flake8 format

merge-requests/30/head
Bob Mottram 2020-04-04 12:27:51 +01:00
parent d652485f8a
commit b7fc2b948d
1 changed files with 291 additions and 257 deletions

548
shares.py
View File

@ -1,148 +1,160 @@
__filename__="shares.py" __filename__ = "shares.py"
__author__="Bob Mottram" __author__ = "Bob Mottram"
__license__="AGPL3+" __license__ = "AGPL3+"
__version__="1.1.0" __version__ = "1.1.0"
__maintainer__="Bob Mottram" __maintainer__ = "Bob Mottram"
__email__="bob@freedombone.net" __email__ = "bob@freedombone.net"
__status__="Production" __status__ = "Production"
import json
import os import os
import time import time
from shutil import copyfile
from webfinger import webfingerHandle from webfinger import webfingerHandle
from auth import createBasicAuthHeader from auth import createBasicAuthHeader
from posts import getPersonBox from posts import getPersonBox
from session import postJson from session import postJson
from session import postImage
from utils import validNickname from utils import validNickname
from utils import getNicknameFromActor
from utils import getDomainFromActor
from utils import loadJson from utils import loadJson
from utils import saveJson from utils import saveJson
from media import removeMetaData from media import removeMetaData
def getValidSharedItemID(displayName: str) -> str: def getValidSharedItemID(displayName: str) -> str:
"""Removes any invalid characters from the display name to """Removes any invalid characters from the display name to
produce an item ID produce an item ID
""" """
return displayName.replace(' ','').replace('+','-').replace('/','-').replace('\\','-').replace('.','_').replace('?','-').replace('\n','').replace("","'").replace('&','-') displayName = displayName.replace(' ', '').replace('+', '-')
displayName = displayName.replace('/', '-').replace('\\', '-')
displayName = displayName.replace('.', '_').replace('?', '-')
displayName = displayName.replace('\n', '').replace("", "'")
return displayName.replace('&', '-')
def removeShare(baseDir: str,nickname: str,domain: str, \
def removeShare(baseDir: str, nickname: str, domain: str,
displayName: str) -> None: displayName: str) -> None:
"""Removes a share for a person """Removes a share for a person
""" """
sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json' sharesFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/shares.json'
if not os.path.isfile(sharesFilename): if not os.path.isfile(sharesFilename):
print('ERROR: missing shares.json '+sharesFilename) print('ERROR: missing shares.json ' + sharesFilename)
return return
sharesJson=loadJson(sharesFilename) sharesJson = loadJson(sharesFilename)
if not sharesJson: if not sharesJson:
print('ERROR: shares.json could not be loaded from '+sharesFilename) print('ERROR: shares.json could not be loaded from ' + sharesFilename)
return return
itemID=getValidSharedItemID(displayName) itemID = getValidSharedItemID(displayName)
if sharesJson.get(itemID): if sharesJson.get(itemID):
# remove any image for the item # remove any image for the item
itemIDfile=baseDir+'/sharefiles/'+nickname+'/'+itemID itemIDfile = baseDir + '/sharefiles/' + nickname + '/' + itemID
if sharesJson[itemID]['imageUrl']: if sharesJson[itemID]['imageUrl']:
if sharesJson[itemID]['imageUrl'].endswith('.png'): if sharesJson[itemID]['imageUrl'].endswith('.png'):
if os.path.isfile(itemIDfile+'.png'): if os.path.isfile(itemIDfile + '.png'):
os.remove(itemIDfile+'.png') os.remove(itemIDfile + '.png')
if sharesJson[itemID]['imageUrl'].endswith('.jpg'): if sharesJson[itemID]['imageUrl'].endswith('.jpg'):
if os.path.isfile(itemIDfile+'.jpg'): if os.path.isfile(itemIDfile + '.jpg'):
os.remove(itemIDfile+'.jpg') os.remove(itemIDfile + '.jpg')
if sharesJson[itemID]['imageUrl'].endswith('.gif'): if sharesJson[itemID]['imageUrl'].endswith('.gif'):
if os.path.isfile(itemIDfile+'.gif'): if os.path.isfile(itemIDfile + '.gif'):
os.remove(itemIDfile+'.gif') os.remove(itemIDfile + '.gif')
# remove the item itself # remove the item itself
del sharesJson[itemID] del sharesJson[itemID]
saveJson(sharesJson,sharesFilename) saveJson(sharesJson, sharesFilename)
else: else:
print('ERROR: share index "'+itemID+'" does not exist in '+sharesFilename) print('ERROR: share index "' + itemID +
'" does not exist in ' + sharesFilename)
def addShare(baseDir: str, \
httpPrefix: str,nickname: str,domain: str,port: int, \ def addShare(baseDir: str,
displayName: str, \ httpPrefix: str, nickname: str, domain: str, port: int,
summary: str, \ displayName: str, summary: str, imageFilename: str,
imageFilename: str, \ itemType: str, itemCategory: str, location: str,
itemType: str, \ duration: str, debug: bool) -> None:
itemCategory: str, \
location: str, \
duration: str,
debug: bool) -> None:
"""Updates the likes collection within a post """Updates the likes collection within a post
""" """
sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json' sharesFilename = baseDir + '/accounts/' + \
sharesJson={} nickname + '@' + domain + '/shares.json'
sharesJson = {}
if os.path.isfile(sharesFilename): if os.path.isfile(sharesFilename):
sharesJson=loadJson(sharesFilename) sharesJson = loadJson(sharesFilename)
duration=duration.lower() duration = duration.lower()
durationSec=0 durationSec = 0
published=int(time.time()) published = int(time.time())
if ' ' in duration: if ' ' in duration:
durationList=duration.split(' ') durationList = duration.split(' ')
if durationList[0].isdigit(): if durationList[0].isdigit():
if 'hour' in durationList[1]: if 'hour' in durationList[1]:
durationSec=published+(int(durationList[0])*60*60) durationSec = published + (int(durationList[0]) * 60 * 60)
if 'day' in durationList[1]: if 'day' in durationList[1]:
durationSec=published+(int(durationList[0])*60*60*24) durationSec = published + (int(durationList[0]) * 60 * 60 * 24)
if 'week' in durationList[1]: if 'week' in durationList[1]:
durationSec=published+(int(durationList[0])*60*60*24*7) durationSec = \
published + (int(durationList[0]) * 60 * 60 * 24 * 7)
if 'month' in durationList[1]: if 'month' in durationList[1]:
durationSec=published+(int(durationList[0])*60*60*24*30) durationSec = \
published + (int(durationList[0]) * 60 * 60 * 24 * 30)
if 'year' in durationList[1]: if 'year' in durationList[1]:
durationSec=published+(int(durationList[0])*60*60*24*365) durationSec = \
published + (int(durationList[0]) * 60 * 60 * 24 * 365)
itemID=getValidSharedItemID(displayName) itemID = getValidSharedItemID(displayName)
# has an image for this share been uploaded? # has an image for this share been uploaded?
imageUrl=None imageUrl = None
moveImage=False moveImage = False
if not imageFilename: if not imageFilename:
sharesImageFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/upload' sharesImageFilename = \
if os.path.isfile(sharesImageFilename+'.png'): baseDir + '/accounts/' + nickname + '@' + domain + '/upload'
imageFilename=sharesImageFilename+'.png' if os.path.isfile(sharesImageFilename + '.png'):
moveImage=True imageFilename = sharesImageFilename + '.png'
elif os.path.isfile(sharesImageFilename+'.jpg'): moveImage = True
imageFilename=sharesImageFilename+'.jpg' elif os.path.isfile(sharesImageFilename + '.jpg'):
moveImage=True imageFilename = sharesImageFilename + '.jpg'
elif os.path.isfile(sharesImageFilename+'.gif'): moveImage = True
imageFilename=sharesImageFilename+'.gif' elif os.path.isfile(sharesImageFilename + '.gif'):
moveImage=True imageFilename = sharesImageFilename + '.gif'
moveImage = True
domainFull=domain domainFull = domain
if port: if port:
if port!=80 and port!=443: if port != 80 and port != 443:
if ':' not in domain: if ':' not in domain:
domainFull=domain+':'+str(port) domainFull = domain + ':' + str(port)
# copy or move the image for the shared item to its destination # copy or move the image for the shared item to its destination
if imageFilename: if imageFilename:
if os.path.isfile(imageFilename): if os.path.isfile(imageFilename):
if not os.path.isdir(baseDir+'/sharefiles'): if not os.path.isdir(baseDir + '/sharefiles'):
os.mkdir(baseDir+'/sharefiles') os.mkdir(baseDir + '/sharefiles')
if not os.path.isdir(baseDir+'/sharefiles/'+nickname): if not os.path.isdir(baseDir + '/sharefiles/' + nickname):
os.mkdir(baseDir+'/sharefiles/'+nickname) os.mkdir(baseDir + '/sharefiles/' + nickname)
itemIDfile=baseDir+'/sharefiles/'+nickname+'/'+itemID itemIDfile = baseDir + '/sharefiles/' + nickname + '/' + itemID
if imageFilename.endswith('.png'): if imageFilename.endswith('.png'):
removeMetaData(imageFilename,itemIDfile+'.png') removeMetaData(imageFilename, itemIDfile + '.png')
if moveImage: if moveImage:
os.remove(imageFilename) os.remove(imageFilename)
imageUrl=httpPrefix+'://'+domainFull+'/sharefiles/'+nickname+'/'+itemID+'.png' imageUrl = \
httpPrefix + '://' + domainFull + \
'/sharefiles/' + nickname + '/' + itemID + '.png'
if imageFilename.endswith('.jpg'): if imageFilename.endswith('.jpg'):
removeMetaData(imageFilename,itemIDfile+'.jpg') removeMetaData(imageFilename, itemIDfile + '.jpg')
if moveImage: if moveImage:
os.remove(imageFilename) os.remove(imageFilename)
imageUrl=httpPrefix+'://'+domainFull+'/sharefiles/'+nickname+'/'+itemID+'.jpg' imageUrl = \
httpPrefix + '://' + domainFull + \
'/sharefiles/' + nickname + '/' + itemID + '.jpg'
if imageFilename.endswith('.gif'): if imageFilename.endswith('.gif'):
removeMetaData(imageFilename,itemIDfile+'.gif') removeMetaData(imageFilename, itemIDfile + '.gif')
if moveImage: if moveImage:
os.remove(imageFilename) os.remove(imageFilename)
imageUrl=httpPrefix+'://'+domainFull+'/sharefiles/'+nickname+'/'+itemID+'.gif' imageUrl = \
httpPrefix + '://' + domainFull + \
'/sharefiles/' + nickname + '/' + itemID + '.gif'
sharesJson[itemID]={ sharesJson[itemID] = {
"displayName": displayName, "displayName": displayName,
"summary": summary, "summary": summary,
"imageUrl": imageUrl, "imageUrl": imageUrl,
@ -153,132 +165,139 @@ def addShare(baseDir: str, \
"expire": durationSec "expire": durationSec
} }
saveJson(sharesJson,sharesFilename) saveJson(sharesJson, sharesFilename)
# indicate that a new share is available # indicate that a new share is available
for subdir, dirs, files in os.walk(baseDir+'/accounts'): for subdir, dirs, files in os.walk(baseDir + '/accounts'):
for handle in dirs: for handle in dirs:
if '@' in handle: if '@' in handle:
accountDir=baseDir+'/accounts/'+handle accountDir = baseDir + '/accounts/' + handle
newShareFile=accountDir+'/.newShare' newShareFile = accountDir + '/.newShare'
if not os.path.isfile(newShareFile): if not os.path.isfile(newShareFile):
nickname=handle.split('@')[0] nickname = handle.split('@')[0]
try: try:
with open(newShareFile, 'w') as fp: with open(newShareFile, 'w') as fp:
fp.write(httpPrefix+'://'+domainFull+'/users/'+nickname+'/tlshares') fp.write(httpPrefix + '://' + domainFull +
except: '/users/' + nickname + '/tlshares')
except BaseException:
pass pass
def expireShares(baseDir: str) -> None: def expireShares(baseDir: str) -> None:
"""Removes expired items from shares """Removes expired items from shares
""" """
for subdir,dirs,files in os.walk(baseDir+'/accounts'): for subdir, dirs, files in os.walk(baseDir + '/accounts'):
for account in dirs: for account in dirs:
if '@' not in account: if '@' not in account:
continue continue
nickname=account.split('@')[0] nickname = account.split('@')[0]
domain=account.split('@')[1] domain = account.split('@')[1]
expireSharesForAccount(baseDir,nickname,domain) expireSharesForAccount(baseDir, nickname, domain)
def expireSharesForAccount(baseDir: str,nickname: str,domain: str) -> None:
def expireSharesForAccount(baseDir: str, nickname: str, domain: str) -> None:
"""Removes expired items from shares """Removes expired items from shares
""" """
handleDomain=domain handleDomain = domain
if ':' in handleDomain: if ':' in handleDomain:
handleDomain=domain.split(':')[0] handleDomain = domain.split(':')[0]
handle=nickname+'@'+handleDomain handle = nickname + '@' + handleDomain
sharesFilename=baseDir+'/accounts/'+handle+'/shares.json' sharesFilename = baseDir + '/accounts/' + handle + '/shares.json'
if os.path.isfile(sharesFilename): if os.path.isfile(sharesFilename):
sharesJson=loadJson(sharesFilename) sharesJson = loadJson(sharesFilename)
if sharesJson: if sharesJson:
currTime=int(time.time()) currTime = int(time.time())
deleteItemID=[] deleteItemID = []
for itemID,item in sharesJson.items(): for itemID, item in sharesJson.items():
if currTime>item['expire']: if currTime > item['expire']:
deleteItemID.append(itemID) deleteItemID.append(itemID)
if deleteItemID: if deleteItemID:
for itemID in deleteItemID: for itemID in deleteItemID:
del sharesJson[itemID] del sharesJson[itemID]
# remove any associated images # remove any associated images
itemIDfile=baseDir+'/sharefiles/'+nickname+'/'+itemID itemIDfile = \
if os.path.isfile(itemIDfile+'.png'): baseDir + '/sharefiles/' + nickname + '/' + itemID
os.remove(itemIDfile+'.png') if os.path.isfile(itemIDfile + '.png'):
if os.path.isfile(itemIDfile+'.jpg'): os.remove(itemIDfile + '.png')
os.remove(itemIDfile+'.jpg') if os.path.isfile(itemIDfile + '.jpg'):
if os.path.isfile(itemIDfile+'.gif'): os.remove(itemIDfile + '.jpg')
os.remove(itemIDfile+'.gif') if os.path.isfile(itemIDfile + '.gif'):
saveJson(sharesJson,sharesFilename) os.remove(itemIDfile + '.gif')
saveJson(sharesJson, sharesFilename)
def getSharesFeedForPerson(baseDir: str, \
domain: str,port: int, \ def getSharesFeedForPerson(baseDir: str,
path: str,httpPrefix: str, \ domain: str, port: int,
path: str, httpPrefix: str,
sharesPerPage=12) -> {}: sharesPerPage=12) -> {}:
"""Returns the shares for an account from GET requests """Returns the shares for an account from GET requests
""" """
if '/shares' not in path: if '/shares' not in path:
return None return None
# handle page numbers # handle page numbers
headerOnly=True headerOnly = True
pageNumber=None pageNumber = None
if '?page=' in path: if '?page=' in path:
pageNumber=path.split('?page=')[1] pageNumber = path.split('?page=')[1]
if pageNumber=='true': if pageNumber == 'true':
pageNumber=1 pageNumber = 1
else: else:
try: try:
pageNumber=int(pageNumber) pageNumber = int(pageNumber)
except: except BaseException:
pass pass
path=path.split('?page=')[0] path = path.split('?page=')[0]
headerOnly=False headerOnly = False
if not path.endswith('/shares'): if not path.endswith('/shares'):
return None return None
nickname=None nickname = None
if path.startswith('/users/'): if path.startswith('/users/'):
nickname=path.replace('/users/','',1).replace('/shares','') nickname = path.replace('/users/', '', 1).replace('/shares', '')
if path.startswith('/@'): if path.startswith('/@'):
nickname=path.replace('/@','',1).replace('/shares','') nickname = path.replace('/@', '', 1).replace('/shares', '')
if not nickname: if not nickname:
return None return None
if not validNickname(domain,nickname): if not validNickname(domain, nickname):
return None return None
if port: if port:
if port!=80 and port!=443: if port != 80 and port != 443:
if ':' not in domain: if ':' not in domain:
domain=domain+':'+str(port) domain = domain + ':' + str(port)
handleDomain=domain handleDomain = domain
if ':' in handleDomain: if ':' in handleDomain:
handleDomain=domain.split(':')[0] handleDomain = domain.split(':')[0]
handle=nickname+'@'+handleDomain handle = nickname + '@' + handleDomain
sharesFilename=baseDir+'/accounts/'+handle+'/shares.json' sharesFilename = baseDir + '/accounts/' + handle + '/shares.json'
if headerOnly: if headerOnly:
noOfShares=0 noOfShares = 0
if os.path.isfile(sharesFilename): if os.path.isfile(sharesFilename):
sharesJson=loadJson(sharesFilename) sharesJson = loadJson(sharesFilename)
if sharesJson: if sharesJson:
noOfShares=len(sharesJson.items()) noOfShares = len(sharesJson.items())
shares={ idStr = httpPrefix + '://' + domain + '/users/' + nickname
shares = {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
'first': httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page=1', 'first': idStr+'/shares?page=1',
'id': httpPrefix+'://'+domain+'/users/'+nickname+'/shares', 'id': idStr+'/shares',
'totalItems': str(noOfShares), 'totalItems': str(noOfShares),
'type': 'OrderedCollection' 'type': 'OrderedCollection'
} }
return shares return shares
if not pageNumber: if not pageNumber:
pageNumber=1 pageNumber = 1
nextPageNumber=int(pageNumber+1) nextPageNumber = int(pageNumber + 1)
shares={ idStr = httpPrefix + '://' + domain + '/users/' + nickname
shares = {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
'id': httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page='+str(pageNumber), 'id': idStr+'/shares?page='+str(pageNumber),
'orderedItems': [], 'orderedItems': [],
'partOf': httpPrefix+'://'+domain+'/users/'+nickname+'/shares', 'partOf': idStr+'/shares',
'totalItems': 0, 'totalItems': 0,
'type': 'OrderedCollectionPage' 'type': 'OrderedCollectionPage'
} }
@ -286,68 +305,68 @@ def getSharesFeedForPerson(baseDir: str, \
if not os.path.isfile(sharesFilename): if not os.path.isfile(sharesFilename):
print("test5") print("test5")
return shares return shares
currPage=1 currPage = 1
pageCtr=0 pageCtr = 0
totalCtr=0 totalCtr = 0
sharesJson=loadJson(sharesFilename) sharesJson = loadJson(sharesFilename)
if sharesJson: if sharesJson:
for itemID,item in sharesJson.items(): for itemID, item in sharesJson.items():
pageCtr += 1 pageCtr += 1
totalCtr += 1 totalCtr += 1
if currPage==pageNumber: if currPage == pageNumber:
shares['orderedItems'].append(item) shares['orderedItems'].append(item)
if pageCtr>=sharesPerPage: if pageCtr >= sharesPerPage:
pageCtr=0 pageCtr = 0
currPage += 1 currPage += 1
shares['totalItems']=totalCtr shares['totalItems'] = totalCtr
lastPage=int(totalCtr/sharesPerPage) lastPage = int(totalCtr / sharesPerPage)
if lastPage<1: if lastPage < 1:
lastPage=1 lastPage = 1
if nextPageNumber>lastPage: if nextPageNumber > lastPage:
shares['next']=httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page='+str(lastPage) shares['next'] = \
httpPrefix + '://' + domain + '/users/' + nickname + \
'/shares?page=' + str(lastPage)
return shares return shares
def sendShareViaServer(baseDir,session, \
fromNickname: str,password: str, \ def sendShareViaServer(baseDir, session,
fromDomain: str,fromPort: int, \ fromNickname: str, password: str,
httpPrefix: str, \ fromDomain: str, fromPort: int,
displayName: str, \ httpPrefix: str, displayName: str,
summary: str, \ summary: str, imageFilename: str,
imageFilename: str, \ itemType: str, itemCategory: str,
itemType: str, \ location: str, duration: str,
itemCategory: str, \ cachedWebfingers: {}, personCache: {},
location: str, \ debug: bool, projectVersion: str) -> {}:
duration: str, \
cachedWebfingers: {},personCache: {}, \
debug: bool, \
projectVersion: str) -> {}:
"""Creates an item share via c2s """Creates an item share via c2s
""" """
if not session: if not session:
print('WARN: No session for sendShareViaServer') print('WARN: No session for sendShareViaServer')
return 6 return 6
fromDomainFull=fromDomain fromDomainFull = fromDomain
if fromPort: if fromPort:
if fromPort!=80 and fromPort!=443: if fromPort != 80 and fromPort != 443:
if ':' not in fromDomain: if ':' not in fromDomain:
fromDomainFull=fromDomain+':'+str(fromPort) fromDomainFull = fromDomain + ':' + str(fromPort)
toUrl='https://www.w3.org/ns/activitystreams#Public' toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl=httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers' ccUrl = httpPrefix + '://' + fromDomainFull + \
'/users/' + fromNickname + '/followers'
newShareJson={ actor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
newShareJson = {
"@context": "https://www.w3.org/ns/activitystreams", "@context": "https://www.w3.org/ns/activitystreams",
'type': 'Add', 'type': 'Add',
'actor': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname, 'actor': actor,
'target': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/shares', 'target': actor+'/shares',
'object': { 'object': {
"type": "Offer", "type": "Offer",
"displayName": displayName, "displayName": displayName,
"summary": summary, "summary": summary,
"itemType": itemType, "itemType": itemType,
"category": category, "category": itemCategory,
"location": location, "location": location,
"duration": duration, "duration": duration,
'to': [toUrl], 'to': [toUrl],
@ -357,87 +376,96 @@ def sendShareViaServer(baseDir,session, \
'cc': [ccUrl] 'cc': [ccUrl]
} }
handle=httpPrefix+'://'+fromDomainFull+'/@'+fromNickname handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
# lookup the inbox for the To handle # lookup the inbox for the To handle
wfRequest=webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \ wfRequest = \
fromDomain,projectVersion) webfingerHandle(session, handle, httpPrefix,
cachedWebfingers,
fromDomain, projectVersion)
if not wfRequest: if not wfRequest:
if debug: if debug:
print('DEBUG: announce webfinger failed for '+handle) print('DEBUG: announce webfinger failed for ' + handle)
return 1 return 1
postToBox='outbox' postToBox = 'outbox'
# get the actor inbox for the To handle # get the actor inbox for the To handle
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \ (inboxUrl, pubKeyId, pubKey,
getPersonBox(baseDir,session,wfRequest,personCache, \ fromPersonId, sharedInbox,
projectVersion,httpPrefix, \ capabilityAcquisition,
fromNickname,fromDomain,postToBox) avatarUrl, displayName) = getPersonBox(baseDir, session, wfRequest,
personCache, projectVersion,
httpPrefix, fromNickname,
fromDomain, postToBox)
if not inboxUrl: if not inboxUrl:
if debug: if debug:
print('DEBUG: No '+postToBox+' was found for '+handle) print('DEBUG: No ' + postToBox + ' was found for ' + handle)
return 3 return 3
if not fromPersonId: if not fromPersonId:
if debug: if debug:
print('DEBUG: No actor was found for '+handle) print('DEBUG: No actor was found for ' + handle)
return 4 return 4
authHeader=createBasicAuthHeader(fromNickname,password) authHeader = createBasicAuthHeader(fromNickname, password)
if imageFilename: if imageFilename:
headers={ headers = {
'host': fromDomain, \ 'host': fromDomain,
'Authorization': authHeader 'Authorization': authHeader
} }
postResult= \ postResult = \
postImage(session,imageFilename,[],inboxUrl.replace('/'+postToBox,'/shares'),headers,"inbox:write") postImage(session, imageFilename, [],
inboxUrl.replace('/' + postToBox, '/shares'),
headers, "inbox:write")
headers={ headers = {
'host': fromDomain, \ 'host': fromDomain,
'Content-type': 'application/json', \ 'Content-type': 'application/json',
'Authorization': authHeader 'Authorization': authHeader
} }
postResult= \ postResult = \
postJson(session,newShareJson,[],inboxUrl,headers,"inbox:write") postJson(session, newShareJson, [], inboxUrl, headers, "inbox:write")
#if not postResult: if not postResult:
# if debug: if debug:
# print('DEBUG: POST announce failed for c2s to '+inboxUrl) print('DEBUG: POST announce failed for c2s to ' + inboxUrl)
# return 5 # return 5
if debug: if debug:
print('DEBUG: c2s POST share item success') print('DEBUG: c2s POST share item success')
return newShareJson return newShareJson
def sendUndoShareViaServer(baseDir: str,session, \
fromNickname: str,password: str, \ def sendUndoShareViaServer(baseDir: str, session,
fromDomain: str,fromPort: int, \ fromNickname: str, password: str,
httpPrefix: str, \ fromDomain: str, fromPort: int,
displayName: str, \ httpPrefix: str, displayName: str,
cachedWebfingers: {},personCache: {}, \ cachedWebfingers: {}, personCache: {},
debug: bool,projectVersion: str) -> {}: debug: bool, projectVersion: str) -> {}:
"""Undoes a share via c2s """Undoes a share via c2s
""" """
if not session: if not session:
print('WARN: No session for sendUndoShareViaServer') print('WARN: No session for sendUndoShareViaServer')
return 6 return 6
fromDomainFull=fromDomain fromDomainFull = fromDomain
if fromPort: if fromPort:
if fromPort!=80 and fromPort!=443: if fromPort != 80 and fromPort != 443:
if ':' not in fromDomain: if ':' not in fromDomain:
fromDomainFull=fromDomain+':'+str(fromPort) fromDomainFull = fromDomain + ':' + str(fromPort)
toUrl='https://www.w3.org/ns/activitystreams#Public' toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl=httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers' ccUrl = httpPrefix + '://' + fromDomainFull + \
'/users/' + fromNickname + '/followers'
undoShareJson={ actor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
undoShareJson = {
"@context": "https://www.w3.org/ns/activitystreams", "@context": "https://www.w3.org/ns/activitystreams",
'type': 'Remove', 'type': 'Remove',
'actor': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname, 'actor': actor,
'target': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/shares', 'target': actor + '/shares',
'object': { 'object': {
"type": "Offer", "type": "Offer",
"displayName": displayName, "displayName": displayName,
@ -448,60 +476,65 @@ def sendUndoShareViaServer(baseDir: str,session, \
'cc': [ccUrl] 'cc': [ccUrl]
} }
handle=httpPrefix+'://'+fromDomainFull+'/@'+fromNickname handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
# lookup the inbox for the To handle # lookup the inbox for the To handle
wfRequest=webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \ wfRequest = \
fromDomain,projectVersion) webfingerHandle(session, handle, httpPrefix, cachedWebfingers,
fromDomain, projectVersion)
if not wfRequest: if not wfRequest:
if debug: if debug:
print('DEBUG: announce webfinger failed for '+handle) print('DEBUG: announce webfinger failed for ' + handle)
return 1 return 1
postToBox='outbox' postToBox = 'outbox'
# get the actor inbox for the To handle # get the actor inbox for the To handle
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \ (inboxUrl, pubKeyId, pubKey,
getPersonBox(baseDir,session,wfRequest,personCache, \ fromPersonId, sharedInbox,
projectVersion,httpPrefix, \ capabilityAcquisition,
fromNickname,fromDomain,postToBox) avatarUrl, displayName) = getPersonBox(baseDir, session, wfRequest,
personCache, projectVersion,
httpPrefix, fromNickname,
fromDomain, postToBox)
if not inboxUrl: if not inboxUrl:
if debug: if debug:
print('DEBUG: No '+postToBox+' was found for '+handle) print('DEBUG: No '+postToBox+' was found for ' + handle)
return 3 return 3
if not fromPersonId: if not fromPersonId:
if debug: if debug:
print('DEBUG: No actor was found for '+handle) print('DEBUG: No actor was found for ' + handle)
return 4 return 4
authHeader=createBasicAuthHeader(fromNickname,password) authHeader = createBasicAuthHeader(fromNickname, password)
headers={ headers = {
'host': fromDomain, \ 'host': fromDomain,
'Content-type': 'application/json', \ 'Content-type': 'application/json',
'Authorization': authHeader 'Authorization': authHeader
} }
postResult= \ postResult = \
postJson(session,undoShareJson,[],inboxUrl,headers,"inbox:write") postJson(session, undoShareJson, [], inboxUrl, headers, "inbox:write")
#if not postResult: if not postResult:
# if debug: if debug:
# print('DEBUG: POST announce failed for c2s to '+inboxUrl) print('DEBUG: POST announce failed for c2s to ' + inboxUrl)
# return 5 # return 5
if debug: if debug:
print('DEBUG: c2s POST undo share success') print('DEBUG: c2s POST undo share success')
return undoShareJson return undoShareJson
def outboxShareUpload(baseDir: str,httpPrefix: str, \
nickname: str,domain: str,port: int, \ def outboxShareUpload(baseDir: str, httpPrefix: str,
messageJson: {},debug: bool) -> None: nickname: str, domain: str, port: int,
messageJson: {}, debug: bool) -> None:
""" When a shared item is received by the outbox from c2s """ When a shared item is received by the outbox from c2s
""" """
if not messageJson.get('type'): if not messageJson.get('type'):
return return
if not messageJson['type']=='Add': if not messageJson['type'] == 'Add':
return return
if not messageJson.get('object'): if not messageJson.get('object'):
return return
@ -511,7 +544,7 @@ def outboxShareUpload(baseDir: str,httpPrefix: str, \
if debug: if debug:
print('DEBUG: undo block - no type') print('DEBUG: undo block - no type')
return return
if not messageJson['object']['type']=='Offer': if not messageJson['object']['type'] == 'Offer':
if debug: if debug:
print('DEBUG: not an Offer activity') print('DEBUG: not an Offer activity')
return return
@ -539,27 +572,28 @@ def outboxShareUpload(baseDir: str,httpPrefix: str, \
if debug: if debug:
print('DEBUG: duration missing from Offer') print('DEBUG: duration missing from Offer')
return return
addShare(baseDir, \ addShare(baseDir,
httpPrefix,nickname,domain,port, \ httpPrefix, nickname, domain, port,
messageJson['object']['displayName'], \ messageJson['object']['displayName'],
messageJson['object']['summary'], \ messageJson['object']['summary'],
messageJson['object']['imageFilename'], \ messageJson['object']['imageFilename'],
messageJson['object']['itemType'], \ messageJson['object']['itemType'],
messageJson['object']['itemCategory'], \ messageJson['object']['itemCategory'],
messageJson['object']['location'], \ messageJson['object']['location'],
messageJson['object']['duration'], \ messageJson['object']['duration'],
debug) debug)
if debug: if debug:
print('DEBUG: shared item received via c2s') print('DEBUG: shared item received via c2s')
def outboxUndoShareUpload(baseDir: str,httpPrefix: str, \
nickname: str,domain: str,port: int, \ def outboxUndoShareUpload(baseDir: str, httpPrefix: str,
messageJson: {},debug: bool) -> None: nickname: str, domain: str, port: int,
messageJson: {}, debug: bool) -> None:
""" When a shared item is removed via c2s """ When a shared item is removed via c2s
""" """
if not messageJson.get('type'): if not messageJson.get('type'):
return return
if not messageJson['type']=='Remove': if not messageJson['type'] == 'Remove':
return return
if not messageJson.get('object'): if not messageJson.get('object'):
return return
@ -569,7 +603,7 @@ def outboxUndoShareUpload(baseDir: str,httpPrefix: str, \
if debug: if debug:
print('DEBUG: undo block - no type') print('DEBUG: undo block - no type')
return return
if not messageJson['object']['type']=='Offer': if not messageJson['object']['type'] == 'Offer':
if debug: if debug:
print('DEBUG: not an Offer activity') print('DEBUG: not an Offer activity')
return return
@ -577,7 +611,7 @@ def outboxUndoShareUpload(baseDir: str,httpPrefix: str, \
if debug: if debug:
print('DEBUG: displayName missing from Offer') print('DEBUG: displayName missing from Offer')
return return
removeShare(baseDir,nickname,domain, \ removeShare(baseDir, nickname, domain,
messageJson['object']['displayName']) messageJson['object']['displayName'])
if debug: if debug:
print('DEBUG: shared item removed via c2s') print('DEBUG: shared item removed via c2s')