2019-07-23 12:33:09 +00:00
|
|
|
__filename__ = "shares.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2019-08-29 13:35:29 +00:00
|
|
|
__version__ = "1.0.0"
|
2019-07-23 12:33:09 +00:00
|
|
|
__maintainer__ = "Bob Mottram"
|
|
|
|
__email__ = "bob@freedombone.net"
|
|
|
|
__status__ = "Production"
|
|
|
|
|
|
|
|
import json
|
|
|
|
import commentjson
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
from shutil import copyfile
|
|
|
|
from webfinger import webfingerHandle
|
|
|
|
from auth import createBasicAuthHeader
|
|
|
|
from posts import getPersonBox
|
|
|
|
from session import postJson
|
2019-07-27 22:48:34 +00:00
|
|
|
from utils import validNickname
|
2019-07-23 12:33:09 +00:00
|
|
|
from utils import getNicknameFromActor
|
|
|
|
from utils import getDomainFromActor
|
2019-07-24 13:21:47 +00:00
|
|
|
from media import removeMetaData
|
2019-07-23 12:33:09 +00:00
|
|
|
|
|
|
|
def removeShare(baseDir: str,nickname: str,domain: str, \
|
|
|
|
displayName: str) -> None:
|
|
|
|
"""Removes a share for a person
|
|
|
|
"""
|
|
|
|
sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json'
|
2019-09-30 22:39:02 +00:00
|
|
|
if os.path.isfile(sharesFilename):
|
2019-10-12 09:37:21 +00:00
|
|
|
tries=0
|
|
|
|
while tries<5:
|
|
|
|
try:
|
|
|
|
with open(sharesFilename, 'r') as fp:
|
|
|
|
sharesJson=commentjson.load(fp)
|
|
|
|
break
|
|
|
|
except Exception as e:
|
2019-10-17 10:26:56 +00:00
|
|
|
print('WARN: commentjson exception removeShare - '+str(e))
|
2019-10-12 09:37:21 +00:00
|
|
|
time.sleep(1)
|
|
|
|
tries+=1
|
2019-07-23 12:33:09 +00:00
|
|
|
|
|
|
|
itemID=displayName.replace(' ','')
|
|
|
|
if sharesJson.get(itemID):
|
|
|
|
# remove any image for the item
|
2019-07-24 09:02:38 +00:00
|
|
|
itemIDfile=baseDir+'/sharefiles/'+itemID
|
2019-07-23 12:33:09 +00:00
|
|
|
if sharesJson[itemID]['imageUrl']:
|
|
|
|
if sharesJson[itemID]['imageUrl'].endswith('.png'):
|
|
|
|
os.remove(itemIDfile+'.png')
|
|
|
|
if sharesJson[itemID]['imageUrl'].endswith('.jpg'):
|
|
|
|
os.remove(itemIDfile+'.jpg')
|
|
|
|
if sharesJson[itemID]['imageUrl'].endswith('.gif'):
|
|
|
|
os.remove(itemIDfile+'.gif')
|
|
|
|
# remove the item itself
|
|
|
|
del sharesJson[itemID]
|
2019-10-12 09:37:21 +00:00
|
|
|
tries=0
|
|
|
|
while tries<5:
|
|
|
|
try:
|
|
|
|
with open(sharesFilename, 'w') as fp:
|
|
|
|
commentjson.dump(sharesJson, fp, indent=4, sort_keys=False)
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
time.sleep(1)
|
|
|
|
tries+=1
|
2019-07-23 12:33:09 +00:00
|
|
|
|
2019-07-25 16:15:02 +00:00
|
|
|
def addShare(baseDir: str, \
|
|
|
|
httpPrefix: str,nickname: str,domain: str,port: int, \
|
2019-07-23 12:33:09 +00:00
|
|
|
displayName: str, \
|
|
|
|
summary: str, \
|
|
|
|
imageFilename: str, \
|
|
|
|
itemType: str, \
|
|
|
|
itemCategory: str, \
|
|
|
|
location: str, \
|
|
|
|
duration: str,
|
|
|
|
debug: bool) -> None:
|
|
|
|
"""Updates the likes collection within a post
|
|
|
|
"""
|
|
|
|
sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json'
|
|
|
|
sharesJson={}
|
2019-09-30 22:39:02 +00:00
|
|
|
if os.path.isfile(sharesFilename):
|
2019-10-12 09:37:21 +00:00
|
|
|
tries=0
|
|
|
|
while tries<5:
|
|
|
|
try:
|
|
|
|
with open(sharesFilename, 'r') as fp:
|
|
|
|
sharesJson=commentjson.load(fp)
|
|
|
|
break
|
|
|
|
except Exception as e:
|
2019-10-17 10:26:56 +00:00
|
|
|
print('WARN: commentjson exception addShare - '+str(e))
|
2019-10-12 09:37:21 +00:00
|
|
|
time.sleep(1)
|
|
|
|
tries+=1
|
2019-07-23 12:33:09 +00:00
|
|
|
|
|
|
|
duration=duration.lower()
|
|
|
|
durationSec=0
|
|
|
|
published=int(time.time())
|
|
|
|
if ' ' in duration:
|
|
|
|
durationList=duration.split(' ')
|
|
|
|
if durationList[0].isdigit():
|
|
|
|
if 'hour' in durationList[1]:
|
|
|
|
durationSec=published+(int(durationList[0])*60*60)
|
|
|
|
if 'day' in durationList[1]:
|
|
|
|
durationSec=published+(int(durationList[0])*60*60*24)
|
|
|
|
if 'week' in durationList[1]:
|
|
|
|
durationSec=published+(int(durationList[0])*60*60*24*7)
|
|
|
|
if 'month' in durationList[1]:
|
|
|
|
durationSec=published+(int(durationList[0])*60*60*24*30)
|
|
|
|
if 'year' in durationList[1]:
|
|
|
|
durationSec=published+(int(durationList[0])*60*60*24*365)
|
|
|
|
|
|
|
|
itemID=displayName.replace(' ','')
|
|
|
|
|
2019-07-23 19:02:26 +00:00
|
|
|
# has an image for this share been uploaded?
|
2019-07-23 12:33:09 +00:00
|
|
|
imageUrl=None
|
2019-07-23 19:02:26 +00:00
|
|
|
moveImage=False
|
|
|
|
if not imageFilename:
|
|
|
|
sharesImageFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/upload'
|
|
|
|
if os.path.isfile(sharesImageFilename+'.png'):
|
|
|
|
imageFilename=sharesImageFilename+'.png'
|
|
|
|
moveImage=True
|
|
|
|
elif os.path.isfile(sharesImageFilename+'.jpg'):
|
|
|
|
imageFilename=sharesImageFilename+'.jpg'
|
|
|
|
moveImage=True
|
|
|
|
elif os.path.isfile(sharesImageFilename+'.gif'):
|
|
|
|
imageFilename=sharesImageFilename+'.gif'
|
|
|
|
moveImage=True
|
|
|
|
|
|
|
|
# copy or move the image for the shared item to its destination
|
2019-07-23 12:33:09 +00:00
|
|
|
if imageFilename:
|
|
|
|
if os.path.isfile(imageFilename):
|
2019-07-25 16:15:02 +00:00
|
|
|
domainFull=domain
|
2019-08-16 20:35:11 +00:00
|
|
|
if port:
|
|
|
|
if port!=80 and port!=443:
|
|
|
|
if ':' not in domain:
|
|
|
|
domainFull=domain+':'+str(port)
|
2019-07-23 12:33:09 +00:00
|
|
|
if not os.path.isdir(baseDir+'/sharefiles'):
|
|
|
|
os.mkdir(baseDir+'/sharefiles')
|
2019-07-24 09:17:57 +00:00
|
|
|
if not os.path.isdir(baseDir+'/sharefiles/'+nickname):
|
|
|
|
os.mkdir(baseDir+'/sharefiles/'+nickname)
|
|
|
|
itemIDfile=baseDir+'/sharefiles/'+nickname+'/'+itemID
|
2019-07-23 22:12:19 +00:00
|
|
|
if imageFilename.endswith('.png'):
|
2019-07-24 13:21:47 +00:00
|
|
|
removeMetaData(imageFilename,itemIDfile+'.png')
|
2019-07-23 19:02:26 +00:00
|
|
|
if moveImage:
|
2019-07-24 13:21:47 +00:00
|
|
|
os.remove(imageFilename)
|
2019-07-25 16:15:02 +00:00
|
|
|
imageUrl=httpPrefix+'://'+domainFull+'/sharefiles/'+nickname+'/'+itemID+'.png'
|
2019-07-23 22:12:19 +00:00
|
|
|
if imageFilename.endswith('.jpg'):
|
2019-07-24 13:21:47 +00:00
|
|
|
removeMetaData(imageFilename,itemIDfile+'.jpg')
|
2019-07-23 19:02:26 +00:00
|
|
|
if moveImage:
|
2019-07-24 13:21:47 +00:00
|
|
|
os.remove(imageFilename)
|
2019-07-25 16:15:02 +00:00
|
|
|
imageUrl=httpPrefix+'://'+domainFull+'/sharefiles/'+nickname+'/'+itemID+'.jpg'
|
2019-07-23 22:12:19 +00:00
|
|
|
if imageFilename.endswith('.gif'):
|
2019-07-24 13:21:47 +00:00
|
|
|
removeMetaData(imageFilename,itemIDfile+'.gif')
|
2019-07-23 19:02:26 +00:00
|
|
|
if moveImage:
|
2019-07-24 13:21:47 +00:00
|
|
|
os.remove(imageFilename)
|
2019-07-25 16:15:02 +00:00
|
|
|
imageUrl=httpPrefix+'://'+domainFull+'/sharefiles/'+nickname+'/'+itemID+'.gif'
|
2019-07-23 12:33:09 +00:00
|
|
|
|
|
|
|
sharesJson[itemID] = {
|
|
|
|
"displayName": displayName,
|
|
|
|
"summary": summary,
|
|
|
|
"imageUrl": imageUrl,
|
2019-07-23 19:02:26 +00:00
|
|
|
"itemType": itemType,
|
2019-07-23 22:12:19 +00:00
|
|
|
"category": itemCategory,
|
2019-07-23 12:33:09 +00:00
|
|
|
"location": location,
|
|
|
|
"published": published,
|
|
|
|
"expire": durationSec
|
|
|
|
}
|
|
|
|
|
2019-10-12 09:37:21 +00:00
|
|
|
tries=0
|
|
|
|
while tries<5:
|
|
|
|
try:
|
|
|
|
with open(sharesFilename, 'w') as fp:
|
|
|
|
commentjson.dump(sharesJson, fp, indent=4, sort_keys=False)
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
time.sleep(1)
|
|
|
|
tries+=1
|
2019-07-23 12:33:09 +00:00
|
|
|
|
2019-10-17 09:58:30 +00:00
|
|
|
def expireShares(baseDir: str) -> None:
|
|
|
|
"""Removes expired items from shares
|
|
|
|
"""
|
|
|
|
for subdir,dirs,files in os.walk(baseDir+'/accounts'):
|
|
|
|
for account in dirs:
|
|
|
|
if '@' not in account:
|
|
|
|
continue
|
|
|
|
nickname=account.split('@')[0]
|
|
|
|
domain=account.split('@')[1]
|
|
|
|
expireSharesForAccount(baseDir,nickname,domain)
|
|
|
|
|
|
|
|
def expireSharesForAccount(baseDir: str,nickname: str,domain: str) -> None:
|
2019-07-23 12:33:09 +00:00
|
|
|
"""Removes expired items from shares
|
|
|
|
"""
|
|
|
|
handleDomain=domain
|
|
|
|
if ':' in handleDomain:
|
|
|
|
handleDomain=domain.split(':')[0]
|
|
|
|
handle=nickname+'@'+handleDomain
|
|
|
|
sharesFilename=baseDir+'/accounts/'+handle+'/shares.json'
|
|
|
|
if os.path.isfile(sharesFilename):
|
2019-09-30 22:39:02 +00:00
|
|
|
sharesJson=None
|
2019-10-12 09:37:21 +00:00
|
|
|
tries=0
|
|
|
|
while tries<5:
|
|
|
|
try:
|
|
|
|
with open(sharesFilename, 'r') as fp:
|
|
|
|
sharesJson=commentjson.load(fp)
|
|
|
|
break
|
|
|
|
except Exception as e:
|
2019-10-17 10:26:56 +00:00
|
|
|
print('WARN: commentjson exception expireSharesForAccount - '+str(e))
|
2019-10-12 09:37:21 +00:00
|
|
|
time.sleep(1)
|
|
|
|
tries+=1
|
2019-09-30 22:39:02 +00:00
|
|
|
if sharesJson:
|
2019-07-23 12:33:09 +00:00
|
|
|
currTime=int(time.time())
|
|
|
|
deleteItemID=[]
|
|
|
|
for itemID,item in sharesJson.items():
|
|
|
|
if currTime>item['expire']:
|
|
|
|
deleteItemID.append(itemID)
|
|
|
|
if deleteItemID:
|
|
|
|
for itemID in deleteItemID:
|
|
|
|
del sharesJson[itemID]
|
2019-07-24 09:17:57 +00:00
|
|
|
# remove any associated images
|
|
|
|
itemIDfile=baseDir+'/sharefiles/'+nickname+'/'+itemID
|
|
|
|
if os.path.isfile(itemIDfile+'.png'):
|
|
|
|
os.remove(itemIDfile+'.png')
|
|
|
|
if os.path.isfile(itemIDfile+'.jpg'):
|
|
|
|
os.remove(itemIDfile+'.jpg')
|
|
|
|
if os.path.isfile(itemIDfile+'.gif'):
|
|
|
|
os.remove(itemIDfile+'.gif')
|
2019-10-12 09:37:21 +00:00
|
|
|
tries=0
|
|
|
|
while tries<5:
|
|
|
|
try:
|
|
|
|
with open(sharesFilename, 'w') as fp:
|
|
|
|
commentjson.dump(sharesJson, fp, indent=4, sort_keys=False)
|
|
|
|
break
|
|
|
|
except Exception as e:
|
2019-10-17 10:26:56 +00:00
|
|
|
print('WARN: commentjson exception expireSharesForAccount 2 - '+str(e))
|
2019-10-12 09:37:21 +00:00
|
|
|
time.sleep(1)
|
|
|
|
tries+=1
|
2019-09-30 22:39:02 +00:00
|
|
|
|
2019-07-23 12:33:09 +00:00
|
|
|
def getSharesFeedForPerson(baseDir: str, \
|
2019-07-24 09:53:07 +00:00
|
|
|
domain: str,port: int, \
|
2019-07-23 12:33:09 +00:00
|
|
|
path: str,httpPrefix: str, \
|
|
|
|
sharesPerPage=12) -> {}:
|
|
|
|
"""Returns the shares for an account from GET requests
|
|
|
|
"""
|
|
|
|
if '/shares' not in path:
|
|
|
|
return None
|
|
|
|
# handle page numbers
|
|
|
|
headerOnly=True
|
|
|
|
pageNumber=None
|
|
|
|
if '?page=' in path:
|
|
|
|
pageNumber=path.split('?page=')[1]
|
|
|
|
if pageNumber=='true':
|
|
|
|
pageNumber=1
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
pageNumber=int(pageNumber)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
path=path.split('?page=')[0]
|
|
|
|
headerOnly=False
|
|
|
|
|
|
|
|
if not path.endswith('/shares'):
|
|
|
|
return None
|
|
|
|
nickname=None
|
|
|
|
if path.startswith('/users/'):
|
|
|
|
nickname=path.replace('/users/','',1).replace('/shares','')
|
|
|
|
if path.startswith('/@'):
|
|
|
|
nickname=path.replace('/@','',1).replace('/shares','')
|
|
|
|
if not nickname:
|
|
|
|
return None
|
2019-08-23 13:47:29 +00:00
|
|
|
if not validNickname(domain,nickname):
|
2019-07-23 12:33:09 +00:00
|
|
|
return None
|
2019-07-24 09:53:07 +00:00
|
|
|
|
2019-08-16 20:35:11 +00:00
|
|
|
if port:
|
|
|
|
if port!=80 and port!=443:
|
|
|
|
if ':' not in domain:
|
|
|
|
domain=domain+':'+str(port)
|
2019-07-23 12:33:09 +00:00
|
|
|
|
|
|
|
handleDomain=domain
|
|
|
|
if ':' in handleDomain:
|
|
|
|
handleDomain=domain.split(':')[0]
|
|
|
|
handle=nickname+'@'+handleDomain
|
|
|
|
sharesFilename=baseDir+'/accounts/'+handle+'/shares.json'
|
|
|
|
|
|
|
|
if headerOnly:
|
|
|
|
noOfShares=0
|
|
|
|
if os.path.isfile(sharesFilename):
|
2019-10-12 09:37:21 +00:00
|
|
|
tries=0
|
|
|
|
while tries<5:
|
|
|
|
try:
|
|
|
|
with open(sharesFilename, 'r') as fp:
|
|
|
|
sharesJson=commentjson.load(fp)
|
|
|
|
noOfShares=len(sharesJson.items())
|
|
|
|
break
|
|
|
|
except Exception as e:
|
2019-10-17 10:26:56 +00:00
|
|
|
print('WARN: commentjson exception getSharesFeedForPerson - '+str(e))
|
2019-10-12 09:37:21 +00:00
|
|
|
time.sleep(1)
|
|
|
|
tries+=1
|
2019-07-23 12:33:09 +00:00
|
|
|
shares = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'first': httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page=1',
|
|
|
|
'id': httpPrefix+'://'+domain+'/users/'+nickname+'/shares',
|
|
|
|
'totalItems': str(noOfShares),
|
|
|
|
'type': 'OrderedCollection'}
|
|
|
|
return shares
|
|
|
|
|
|
|
|
if not pageNumber:
|
|
|
|
pageNumber=1
|
|
|
|
|
|
|
|
nextPageNumber=int(pageNumber+1)
|
|
|
|
shares = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id': httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page='+str(pageNumber),
|
|
|
|
'orderedItems': [],
|
|
|
|
'partOf': httpPrefix+'://'+domain+'/users/'+nickname+'/shares',
|
|
|
|
'totalItems': 0,
|
|
|
|
'type': 'OrderedCollectionPage'}
|
|
|
|
|
|
|
|
if not os.path.isfile(sharesFilename):
|
2019-07-24 09:53:07 +00:00
|
|
|
print("test5")
|
2019-07-23 12:33:09 +00:00
|
|
|
return shares
|
|
|
|
currPage=1
|
|
|
|
pageCtr=0
|
|
|
|
totalCtr=0
|
|
|
|
|
2019-09-30 22:39:02 +00:00
|
|
|
sharesJson=None
|
2019-10-12 09:37:21 +00:00
|
|
|
tries=0
|
|
|
|
while tries<5:
|
|
|
|
try:
|
|
|
|
with open(sharesFilename, 'r') as fp:
|
|
|
|
sharesJson=commentjson.load(fp)
|
|
|
|
break
|
|
|
|
except Exception as e:
|
2019-10-17 10:26:56 +00:00
|
|
|
print('WARN: commentjson exception getSharesFeedForPerson 2 - '+str(e))
|
2019-10-12 09:37:21 +00:00
|
|
|
time.sleep(1)
|
|
|
|
tries+=1
|
2019-09-30 22:39:02 +00:00
|
|
|
|
|
|
|
if sharesJson:
|
2019-07-23 12:33:09 +00:00
|
|
|
for itemID,item in sharesJson.items():
|
|
|
|
pageCtr += 1
|
|
|
|
totalCtr += 1
|
|
|
|
if currPage==pageNumber:
|
|
|
|
shares['orderedItems'].append(item)
|
|
|
|
if pageCtr>=sharesPerPage:
|
|
|
|
pageCtr=0
|
|
|
|
currPage += 1
|
|
|
|
shares['totalItems']=totalCtr
|
|
|
|
lastPage=int(totalCtr/sharesPerPage)
|
|
|
|
if lastPage<1:
|
|
|
|
lastPage=1
|
|
|
|
if nextPageNumber>lastPage:
|
|
|
|
shares['next']=httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page='+str(lastPage)
|
|
|
|
return shares
|
2019-07-23 19:02:26 +00:00
|
|
|
|
2019-08-20 09:16:03 +00:00
|
|
|
def sendShareViaServer(baseDir,session, \
|
|
|
|
fromNickname: str,password: str, \
|
2019-07-23 19:02:26 +00:00
|
|
|
fromDomain: str,fromPort: int, \
|
|
|
|
httpPrefix: str, \
|
|
|
|
displayName: str, \
|
|
|
|
summary: str, \
|
|
|
|
imageFilename: str, \
|
|
|
|
itemType: str, \
|
|
|
|
itemCategory: str, \
|
|
|
|
location: str, \
|
|
|
|
duration: str, \
|
|
|
|
cachedWebfingers: {},personCache: {}, \
|
2019-08-14 20:12:27 +00:00
|
|
|
debug: bool, \
|
|
|
|
projectVersion: str) -> {}:
|
2019-07-23 19:02:26 +00:00
|
|
|
"""Creates an item share via c2s
|
|
|
|
"""
|
|
|
|
if not session:
|
|
|
|
print('WARN: No session for sendShareViaServer')
|
|
|
|
return 6
|
|
|
|
|
|
|
|
fromDomainFull=fromDomain
|
2019-08-16 20:35:11 +00:00
|
|
|
if fromPort:
|
|
|
|
if fromPort!=80 and fromPort!=443:
|
|
|
|
if ':' not in fromDomain:
|
|
|
|
fromDomainFull=fromDomain+':'+str(fromPort)
|
2019-07-23 19:02:26 +00:00
|
|
|
|
|
|
|
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
|
|
|
|
ccUrl = httpPrefix + '://'+fromDomainFull+'/users/'+fromNickname+'/followers'
|
|
|
|
|
|
|
|
newShareJson = {
|
2019-08-18 11:07:06 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
2019-07-23 19:02:26 +00:00
|
|
|
'type': 'Add',
|
|
|
|
'actor': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname,
|
|
|
|
'target': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/shares',
|
|
|
|
'object': {
|
|
|
|
"type": "Offer",
|
|
|
|
"displayName": displayName,
|
|
|
|
"summary": summary,
|
|
|
|
"itemType": itemType,
|
|
|
|
"category": category,
|
|
|
|
"location": location,
|
|
|
|
"duration": duration,
|
|
|
|
'to': [toUrl],
|
|
|
|
'cc': [ccUrl]
|
|
|
|
},
|
|
|
|
'to': [toUrl],
|
|
|
|
'cc': [ccUrl]
|
|
|
|
}
|
|
|
|
|
|
|
|
handle=httpPrefix+'://'+fromDomainFull+'/@'+fromNickname
|
|
|
|
|
|
|
|
# lookup the inbox for the To handle
|
2019-08-14 20:12:27 +00:00
|
|
|
wfRequest = webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
|
|
|
|
fromDomain,projectVersion)
|
2019-07-23 19:02:26 +00:00
|
|
|
if not wfRequest:
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: announce webfinger failed for '+handle)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
postToBox='outbox'
|
|
|
|
|
|
|
|
# get the actor inbox for the To handle
|
2019-08-22 18:36:07 +00:00
|
|
|
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName = \
|
2019-08-20 09:16:03 +00:00
|
|
|
getPersonBox(baseDir,session,wfRequest,personCache, \
|
2019-10-17 15:55:05 +00:00
|
|
|
projectVersion,httpPrefix, \
|
|
|
|
fromNickname,fromDomain,postToBox)
|
2019-07-23 19:02:26 +00:00
|
|
|
|
|
|
|
if not inboxUrl:
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: No '+postToBox+' was found for '+handle)
|
|
|
|
return 3
|
|
|
|
if not fromPersonId:
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: No actor was found for '+handle)
|
|
|
|
return 4
|
|
|
|
|
|
|
|
authHeader=createBasicAuthHeader(fromNickname,password)
|
|
|
|
|
|
|
|
if imageFilename:
|
|
|
|
headers = {'host': fromDomain, \
|
|
|
|
'Authorization': authHeader}
|
|
|
|
postResult = \
|
|
|
|
postImage(session,imageFilename,[],inboxUrl.replace('/'+postToBox,'/shares'),headers,"inbox:write")
|
|
|
|
|
|
|
|
headers = {'host': fromDomain, \
|
|
|
|
'Content-type': 'application/json', \
|
|
|
|
'Authorization': authHeader}
|
|
|
|
postResult = \
|
|
|
|
postJson(session,newShareJson,[],inboxUrl,headers,"inbox:write")
|
|
|
|
#if not postResult:
|
|
|
|
# if debug:
|
|
|
|
# print('DEBUG: POST announce failed for c2s to '+inboxUrl)
|
|
|
|
# return 5
|
|
|
|
|
|
|
|
if debug:
|
2019-07-23 21:14:16 +00:00
|
|
|
print('DEBUG: c2s POST share item success')
|
2019-07-23 19:02:26 +00:00
|
|
|
|
|
|
|
return newShareJson
|
2019-07-23 20:00:17 +00:00
|
|
|
|
2019-08-20 09:16:03 +00:00
|
|
|
def sendUndoShareViaServer(baseDir: str,session, \
|
|
|
|
fromNickname: str,password: str, \
|
2019-07-23 21:14:16 +00:00
|
|
|
fromDomain: str,fromPort: int, \
|
|
|
|
httpPrefix: str, \
|
|
|
|
displayName: str, \
|
|
|
|
cachedWebfingers: {},personCache: {}, \
|
2019-08-14 20:12:27 +00:00
|
|
|
debug: bool,projectVersion: str) -> {}:
|
2019-07-23 21:14:16 +00:00
|
|
|
"""Undoes a share via c2s
|
|
|
|
"""
|
|
|
|
if not session:
|
|
|
|
print('WARN: No session for sendUndoShareViaServer')
|
|
|
|
return 6
|
|
|
|
|
|
|
|
fromDomainFull=fromDomain
|
2019-08-16 20:35:11 +00:00
|
|
|
if fromPort:
|
|
|
|
if fromPort!=80 and fromPort!=443:
|
|
|
|
if ':' not in fromDomain:
|
|
|
|
fromDomainFull=fromDomain+':'+str(fromPort)
|
2019-07-23 21:14:16 +00:00
|
|
|
|
|
|
|
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
|
|
|
|
ccUrl = httpPrefix + '://'+fromDomainFull+'/users/'+fromNickname+'/followers'
|
|
|
|
|
|
|
|
undoShareJson = {
|
2019-08-18 11:07:06 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
2019-07-23 21:14:16 +00:00
|
|
|
'type': 'Remove',
|
|
|
|
'actor': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname,
|
|
|
|
'target': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/shares',
|
|
|
|
'object': {
|
|
|
|
"type": "Offer",
|
|
|
|
"displayName": displayName,
|
|
|
|
'to': [toUrl],
|
|
|
|
'cc': [ccUrl]
|
|
|
|
},
|
|
|
|
'to': [toUrl],
|
|
|
|
'cc': [ccUrl]
|
|
|
|
}
|
|
|
|
|
|
|
|
handle=httpPrefix+'://'+fromDomainFull+'/@'+fromNickname
|
|
|
|
|
|
|
|
# lookup the inbox for the To handle
|
2019-08-14 20:12:27 +00:00
|
|
|
wfRequest = webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
|
|
|
|
fromDomain,projectVersion)
|
2019-07-23 21:14:16 +00:00
|
|
|
if not wfRequest:
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: announce webfinger failed for '+handle)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
postToBox='outbox'
|
|
|
|
|
|
|
|
# get the actor inbox for the To handle
|
2019-08-22 18:36:07 +00:00
|
|
|
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName = \
|
2019-08-20 09:16:03 +00:00
|
|
|
getPersonBox(baseDir,session,wfRequest,personCache, \
|
2019-10-17 15:55:05 +00:00
|
|
|
projectVersion,httpPrefix, \
|
|
|
|
fromNickname,fromDomain,postToBox)
|
2019-07-23 21:14:16 +00:00
|
|
|
|
|
|
|
if not inboxUrl:
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: No '+postToBox+' was found for '+handle)
|
|
|
|
return 3
|
|
|
|
if not fromPersonId:
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: No actor was found for '+handle)
|
|
|
|
return 4
|
|
|
|
|
|
|
|
authHeader=createBasicAuthHeader(fromNickname,password)
|
|
|
|
|
|
|
|
headers = {'host': fromDomain, \
|
|
|
|
'Content-type': 'application/json', \
|
|
|
|
'Authorization': authHeader}
|
|
|
|
postResult = \
|
|
|
|
postJson(session,undoShareJson,[],inboxUrl,headers,"inbox:write")
|
|
|
|
#if not postResult:
|
|
|
|
# if debug:
|
|
|
|
# print('DEBUG: POST announce failed for c2s to '+inboxUrl)
|
|
|
|
# return 5
|
|
|
|
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: c2s POST undo share success')
|
|
|
|
|
|
|
|
return undoShareJson
|
|
|
|
|
2019-07-23 20:00:17 +00:00
|
|
|
def outboxShareUpload(baseDir: str,httpPrefix: str, \
|
|
|
|
nickname: str,domain: str,port: int, \
|
|
|
|
messageJson: {},debug: bool) -> None:
|
|
|
|
""" When a shared item is received by the outbox from c2s
|
|
|
|
"""
|
|
|
|
if not messageJson.get('type'):
|
|
|
|
return
|
|
|
|
if not messageJson['type']=='Add':
|
|
|
|
return
|
|
|
|
if not messageJson.get('object'):
|
|
|
|
return
|
|
|
|
if not isinstance(messageJson['object'], dict):
|
|
|
|
return
|
|
|
|
if not messageJson['object'].get('type'):
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: undo block - no type')
|
|
|
|
return
|
|
|
|
if not messageJson['object']['type']=='Offer':
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: not an Offer activity')
|
|
|
|
return
|
|
|
|
if not messageJson['object'].get('displayName'):
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: displayName missing from Offer')
|
|
|
|
return
|
|
|
|
if not messageJson['object'].get('summary'):
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: summary missing from Offer')
|
|
|
|
return
|
|
|
|
if not messageJson['object'].get('itemType'):
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: itemType missing from Offer')
|
|
|
|
return
|
|
|
|
if not messageJson['object'].get('category'):
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: category missing from Offer')
|
|
|
|
return
|
|
|
|
if not messageJson['object'].get('location'):
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: location missing from Offer')
|
|
|
|
return
|
|
|
|
if not messageJson['object'].get('duration'):
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: duration missing from Offer')
|
|
|
|
return
|
2019-07-25 16:15:02 +00:00
|
|
|
addShare(baseDir, \
|
|
|
|
httpPrefix,nickname,domain,port, \
|
2019-07-23 20:00:17 +00:00
|
|
|
messageJson['object']['displayName'], \
|
|
|
|
messageJson['object']['summary'], \
|
|
|
|
messageJson['object']['imageFilename'], \
|
|
|
|
messageJson['object']['itemType'], \
|
|
|
|
messageJson['object']['itemCategory'], \
|
|
|
|
messageJson['object']['location'], \
|
|
|
|
messageJson['object']['duration'], \
|
|
|
|
debug)
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: shared item received via c2s')
|
2019-07-23 21:14:16 +00:00
|
|
|
|
|
|
|
def outboxUndoShareUpload(baseDir: str,httpPrefix: str, \
|
|
|
|
nickname: str,domain: str,port: int, \
|
|
|
|
messageJson: {},debug: bool) -> None:
|
|
|
|
""" When a shared item is removed via c2s
|
|
|
|
"""
|
|
|
|
if not messageJson.get('type'):
|
|
|
|
return
|
|
|
|
if not messageJson['type']=='Remove':
|
|
|
|
return
|
|
|
|
if not messageJson.get('object'):
|
|
|
|
return
|
|
|
|
if not isinstance(messageJson['object'], dict):
|
|
|
|
return
|
|
|
|
if not messageJson['object'].get('type'):
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: undo block - no type')
|
|
|
|
return
|
|
|
|
if not messageJson['object']['type']=='Offer':
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: not an Offer activity')
|
|
|
|
return
|
|
|
|
if not messageJson['object'].get('displayName'):
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: displayName missing from Offer')
|
|
|
|
return
|
|
|
|
removeShare(baseDir,nickname,domain, \
|
|
|
|
messageJson['object']['displayName'])
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: shared item removed via c2s')
|