Unique share ID for the instance

merge-requests/30/head
Bob Mottram 2021-07-25 10:33:59 +01:00
parent bba4e461e4
commit 5421facdb3
3 changed files with 62 additions and 31 deletions

View File

@ -205,7 +205,7 @@ from webapp_welcome_profile import htmlWelcomeProfile
from webapp_welcome_final import htmlWelcomeFinal from webapp_welcome_final import htmlWelcomeFinal
from shares import getSharesFeedForPerson from shares import getSharesFeedForPerson
from shares import addShare from shares import addShare
from shares import removeShare from shares import removeSharedItem
from shares import expireShares from shares import expireShares
from categories import setHashtagCategory from categories import setHashtagCategory
from languages import getActorLanguages from languages import getActorLanguages
@ -3332,8 +3332,9 @@ class PubServer(BaseHTTPRequestHandler):
shareNickname = getNicknameFromActor(shareActor) shareNickname = getNicknameFromActor(shareActor)
if shareNickname: if shareNickname:
shareDomain, sharePort = getDomainFromActor(shareActor) shareDomain, sharePort = getDomainFromActor(shareActor)
removeShare(baseDir, removeSharedItem(baseDir,
shareNickname, shareDomain, shareName) shareNickname, shareDomain, shareName,
httpPrefix, domainFull)
if callingDomain.endswith('.onion') and onionDomain: if callingDomain.endswith('.onion') and onionDomain:
originPathStr = 'http://' + onionDomain + usersPath originPathStr = 'http://' + onionDomain + usersPath

View File

@ -72,7 +72,7 @@ def _loadDfcIds(baseDir: str, systemLanguage: str) -> {}:
return dfcIds return dfcIds
def getValidSharedItemID(displayName: str) -> str: def getValidSharedItemID(actor: str, 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
""" """
@ -84,11 +84,12 @@ def getValidSharedItemID(displayName: str) -> str:
displayName = displayName.replace(ch, '-') displayName = displayName.replace(ch, '-')
displayName = displayName.replace('.', '_') displayName = displayName.replace('.', '_')
displayName = displayName.replace("", "'") displayName = displayName.replace("", "'")
return displayName return actor + '/item/' + displayName
def removeShare(baseDir: str, nickname: str, domain: str, def removeSharedItem(baseDir: str, nickname: str, domain: str,
displayName: str) -> None: displayName: str,
httpPrefix: str, domainFull: str) -> None:
"""Removes a share for a person """Removes a share for a person
""" """
sharesFilename = acctDir(baseDir, nickname, domain) + '/shares.json' sharesFilename = acctDir(baseDir, nickname, domain) + '/shares.json'
@ -101,7 +102,8 @@ def removeShare(baseDir: str, nickname: str, domain: str,
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) actor = httpPrefix + '://' + domainFull + '/users/' + nickname
itemID = getValidSharedItemID(actor, 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
@ -176,6 +178,28 @@ def _getshareDfcId(baseDir: str, systemLanguage: str,
return matchId return matchId
def _indicateNewShareAvailable(baseDir: str, httpPrefix: str,
domainFull: str) -> None:
"""Indicate to each account that a new share is available
"""
for subdir, dirs, files in os.walk(baseDir + '/accounts'):
for handle in dirs:
if not isAccountDir(handle):
continue
accountDir = baseDir + '/accounts/' + handle
newShareFile = accountDir + '/.newShare'
if os.path.isfile(newShareFile):
continue
nickname = handle.split('@')[0]
try:
with open(newShareFile, 'w+') as fp:
fp.write(httpPrefix + '://' + domainFull +
'/users/' + nickname + '/tlshares')
except BaseException:
pass
break
def addShare(baseDir: str, def addShare(baseDir: str,
httpPrefix: str, nickname: str, domain: str, port: int, httpPrefix: str, nickname: str, domain: str, port: int,
displayName: str, summary: str, imageFilename: str, displayName: str, summary: str, imageFilename: str,
@ -194,7 +218,9 @@ def addShare(baseDir: str,
published = int(time.time()) published = int(time.time())
durationSec = _addShareDurationSec(duration, published) durationSec = _addShareDurationSec(duration, published)
itemID = getValidSharedItemID(displayName) domainFull = getFullDomain(domain, port)
actor = httpPrefix + '://' + domainFull + '/users/' + nickname
itemID = getValidSharedItemID(actor, displayName)
dfcId = _getshareDfcId(baseDir, systemLanguage, dfcId = _getshareDfcId(baseDir, systemLanguage,
itemType, itemCategory, translate) itemType, itemCategory, translate)
@ -244,28 +270,13 @@ def addShare(baseDir: str,
"location": location, "location": location,
"published": published, "published": published,
"expire": durationSec, "expire": durationSec,
"price": "0", "price": price,
"currency": "" "currency": currency
} }
saveJson(sharesJson, sharesFilename) saveJson(sharesJson, sharesFilename)
# indicate that a new share is available _indicateNewShareAvailable(baseDir, httpPrefix, domainFull)
for subdir, dirs, files in os.walk(baseDir + '/accounts'):
for handle in dirs:
if not isAccountDir(handle):
continue
accountDir = baseDir + '/accounts/' + handle
newShareFile = accountDir + '/.newShare'
if not os.path.isfile(newShareFile):
nickname = handle.split('@')[0]
try:
with open(newShareFile, 'w+') as fp:
fp.write(httpPrefix + '://' + domainFull +
'/users/' + nickname + '/tlshares')
except BaseException:
pass
break
def expireShares(baseDir: str) -> None: def expireShares(baseDir: str) -> None:
@ -703,8 +714,10 @@ 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, domainFull = getFullDomain(domain, port)
messageJson['object']['displayName']) removeSharedItem(baseDir, nickname, domain,
messageJson['object']['displayName'],
httpPrefix, domainFull)
if debug: if debug:
print('DEBUG: shared item removed via c2s') print('DEBUG: shared item removed via c2s')
@ -718,11 +731,14 @@ def sharesCatalogAccountEndpoint(baseDir: str, httpPrefix: str,
""" """
dfcUrl = \ dfcUrl = \
"http://static.datafoodconsortium.org/ontologies/DFC_FullModel.owl#" "http://static.datafoodconsortium.org/ontologies/DFC_FullModel.owl#"
dfcPtUrl = \
"http://static.datafoodconsortium.org/data/productTypes.rdf#"
owner = httpPrefix + '://' + domainFull + '/users/' + nickname owner = httpPrefix + '://' + domainFull + '/users/' + nickname
dfcInstanceId = owner + '/catalog' dfcInstanceId = owner + '/catalog'
endpoint = { endpoint = {
"@context": { "@context": {
"DFC": dfcUrl, "DFC": dfcUrl,
"dfc-pt": dfcPtUrl,
"@base": "http://maPlateformeNationale" "@base": "http://maPlateformeNationale"
}, },
"@id": dfcInstanceId, "@id": dfcInstanceId,
@ -740,12 +756,17 @@ def sharesCatalogAccountEndpoint(baseDir: str, httpPrefix: str,
for itemID, item in sharesJson.items(): for itemID, item in sharesJson.items():
if not item.get('dfcId'): if not item.get('dfcId'):
continue continue
if '#' not in item['dfcId']:
continue
expireDate = datetime.datetime.fromtimestamp(item['durationSec']) expireDate = datetime.datetime.fromtimestamp(item['durationSec'])
expireDateStr = expireDate.strftime("%Y-%m-%dT%H:%M:%SZ") expireDateStr = expireDate.strftime("%Y-%m-%dT%H:%M:%SZ")
dfcId = item['dfcId'].split('#')[1]
catalogItem = { catalogItem = {
"@id": item['dfcId'], "@id": item['dfcId'],
"@type": "DFC:SuppliedProduct",
"DFC:hasType": "dfc-pt:" + dfcId,
"DFC:offeredThrough": owner, "DFC:offeredThrough": owner,
"DFC:startDate": item['published'], "DFC:startDate": item['published'],
"DFC:expiryDate": expireDateStr, "DFC:expiryDate": expireDateStr,
@ -768,10 +789,13 @@ def sharesCatalogEndpoint(baseDir: str, httpPrefix: str,
""" """
dfcUrl = \ dfcUrl = \
"http://static.datafoodconsortium.org/ontologies/DFC_FullModel.owl#" "http://static.datafoodconsortium.org/ontologies/DFC_FullModel.owl#"
dfcPtUrl = \
"http://static.datafoodconsortium.org/data/productTypes.rdf#"
dfcInstanceId = httpPrefix + '://' + domainFull + '/catalog' dfcInstanceId = httpPrefix + '://' + domainFull + '/catalog'
endpoint = { endpoint = {
"@context": { "@context": {
"DFC": dfcUrl, "DFC": dfcUrl,
"dfc-pt": dfcPtUrl,
"@base": "http://maPlateformeNationale" "@base": "http://maPlateformeNationale"
}, },
"@id": dfcInstanceId, "@id": dfcInstanceId,
@ -798,14 +822,20 @@ def sharesCatalogEndpoint(baseDir: str, httpPrefix: str,
for itemID, item in sharesJson.items(): for itemID, item in sharesJson.items():
if not item.get('dfcId'): if not item.get('dfcId'):
continue continue
if '#' not in item['dfcId']:
continue
expireDate = \ expireDate = \
datetime.datetime.fromtimestamp(item['durationSec']) datetime.datetime.fromtimestamp(item['durationSec'])
expireDateStr = expireDate.strftime("%Y-%m-%dT%H:%M:%SZ") expireDateStr = expireDate.strftime("%Y-%m-%dT%H:%M:%SZ")
description = item['displayName'] + ': ' + item['summary'] description = item['displayName'] + ': ' + item['summary']
shareId = getValidSharedItemID(owner, item['displayName'])
dfcId = item['dfcId'].split('#')[1]
catalogItem = { catalogItem = {
"@id": item['dfcId'], "@id": shareId,
"@type": "DFC:SuppliedProduct",
"DFC:hasType": "dfc-pt:" + dfcId,
"DFC:offeredThrough": owner, "DFC:offeredThrough": owner,
"DFC:startDate": item['published'], "DFC:startDate": item['published'],
"DFC:expiryDate": expireDateStr, "DFC:expiryDate": expireDateStr,

View File

@ -108,7 +108,7 @@ def htmlConfirmRemoveSharedItem(cssCache: {}, translate: {}, baseDir: str,
callingDomain: str) -> str: callingDomain: str) -> str:
"""Shows a screen asking to confirm the removal of a shared item """Shows a screen asking to confirm the removal of a shared item
""" """
itemID = getValidSharedItemID(shareName) itemID = getValidSharedItemID(actor, shareName)
nickname = getNicknameFromActor(actor) nickname = getNicknameFromActor(actor)
domain, port = getDomainFromActor(actor) domain, port = getDomainFromActor(actor)
domainFull = getFullDomain(domain, port) domainFull = getFullDomain(domain, port)