From fc141708a1e5ad300dd835b3410bae7f17f9a5b2 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 4 May 2020 12:28:43 +0100 Subject: [PATCH] Show blog address from actor tags --- blog.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ daemon.py | 27 ++++++++++++++-- webinterface.py | 15 +++++++++ 3 files changed, 124 insertions(+), 2 deletions(-) diff --git a/blog.py b/blog.py index 458076ce5..1b72ef2ff 100644 --- a/blog.py +++ b/blog.py @@ -690,3 +690,87 @@ def htmlEditBlog(mediaInstance: bool, translate: {}, editBlogForm += htmlFooter() return editBlogForm + + +def getBlogAddress(actorJson: {}) -> str: + """Returns blog address for the given actor + """ + if not actorJson.get('attachment'): + return '' + for propertyValue in actorJson['attachment']: + if not propertyValue.get('name'): + continue + if not propertyValue['name'].lower().startswith('Blog'): + continue + if not propertyValue.get('type'): + continue + if not propertyValue.get('value'): + continue + if propertyValue['type'] != 'PropertyValue': + continue + propertyValue['value'] = propertyValue['value'].strip() + if not (propertyValue['value'].startswith('https://') or + propertyValue['value'].startswith('http://') or + propertyValue['value'].startswith('dat://') or + propertyValue['value'].startswith('i2p://')): + continue + if '.' not in propertyValue['value']: + continue + if ' ' in propertyValue['value']: + continue + if ',' in propertyValue['value']: + continue + return propertyValue['value'] + return '' + + +def setBlogAddress(actorJson: {}, blogAddress: str) -> None: + """Sets an blog address for the given actor + """ + if not actorJson.get('attachment'): + actorJson['attachment'] = [] + + # remove any existing value + propertyFound = None + for propertyValue in actorJson['attachment']: + if not propertyValue.get('name'): + continue + if not propertyValue.get('type'): + continue + if not propertyValue['name'].lower().startswith('Blog'): + continue + propertyFound = propertyValue + break + if propertyFound: + actorJson['attachment'].remove(propertyFound) + + if not (blogAddress.startswith('https://') or + blogAddress.startswith('http://') or + blogAddress.startswith('dat://') or + blogAddress.startswith('i2p://')): + return + if '.' not in blogAddress: + return + if ' ' in blogAddress: + return + if ',' in blogAddress: + return + + for propertyValue in actorJson['attachment']: + if not propertyValue.get('name'): + continue + if not propertyValue.get('type'): + continue + if not propertyValue['name'].lower().startswith('blog'): + continue + if propertyValue['type'] != 'PropertyValue': + continue + propertyValue['value'] = blogAddress + return + + newBlogAddress = { + "name": "Blog", + "type": "PropertyValue", + "value": blogAddress + } + actorJson['attachment'].append(newBlogAddress) diff --git a/daemon.py b/daemon.py index b9bb6560d..cf559f250 100644 --- a/daemon.py +++ b/daemon.py @@ -97,6 +97,8 @@ from blog import htmlBlogView from blog import htmlBlogPage from blog import htmlBlogPost from blog import htmlEditBlog +from blog import getBlogAddress +from blog import setBlogAddress from webinterface import htmlCalendarDeleteConfirm from webinterface import htmlDeletePost from webinterface import htmlAbout @@ -1249,6 +1251,7 @@ class PubServer(BaseHTTPRequestHandler): xmppAddress = getXmppAddress(actorJson) matrixAddress = getMatrixAddress(actorJson) ssbAddress = getSSBAddress(actorJson) + blogAddress = getBlogAddress(actorJson) toxAddress = getToxAddress(actorJson) emailAddress = getEmailAddress(actorJson) PGPpubKey = getPGPpubKey(actorJson) @@ -1261,8 +1264,9 @@ class PubServer(BaseHTTPRequestHandler): optionsLink, pageNumber, donateUrl, xmppAddress, matrixAddress, - ssbAddress, toxAddress, - PGPpubKey, emailAddress).encode() + ssbAddress, blogAddress, + toxAddress, PGPpubKey, + emailAddress).encode() self._set_headers('text/html', len(msg), cookie, callingDomain) self._write(msg) @@ -5370,6 +5374,7 @@ class PubServer(BaseHTTPRequestHandler): setTheme(self.server.baseDir, fields['themeDropdown']) # self.server.iconsCache={} + currentEmailAddress = getEmailAddress(actorJson) if fields.get('email'): if fields['email'] != currentEmailAddress: @@ -5379,6 +5384,7 @@ class PubServer(BaseHTTPRequestHandler): if currentEmailAddress: setEmailAddress(actorJson, '') actorChanged = True + currentXmppAddress = getXmppAddress(actorJson) if fields.get('xmppAddress'): if fields['xmppAddress'] != currentXmppAddress: @@ -5389,6 +5395,7 @@ class PubServer(BaseHTTPRequestHandler): if currentXmppAddress: setXmppAddress(actorJson, '') actorChanged = True + currentMatrixAddress = getMatrixAddress(actorJson) if fields.get('matrixAddress'): if fields['matrixAddress'] != currentMatrixAddress: @@ -5399,6 +5406,7 @@ class PubServer(BaseHTTPRequestHandler): if currentMatrixAddress: setMatrixAddress(actorJson, '') actorChanged = True + currentSSBAddress = getSSBAddress(actorJson) if fields.get('ssbAddress'): if fields['ssbAddress'] != currentSSBAddress: @@ -5409,6 +5417,18 @@ class PubServer(BaseHTTPRequestHandler): if currentSSBAddress: setSSBAddress(actorJson, '') actorChanged = True + + currentBlogAddress = getBlogAddress(actorJson) + if fields.get('blogAddress'): + if fields['blogAddress'] != currentBlogAddress: + setBlogAddress(actorJson, + fields['blogAddress']) + actorChanged = True + else: + if currentBlogAddress: + setBlogAddress(actorJson, '') + actorChanged = True + currentToxAddress = getToxAddress(actorJson) if fields.get('toxAddress'): if fields['toxAddress'] != currentToxAddress: @@ -5419,6 +5439,7 @@ class PubServer(BaseHTTPRequestHandler): if currentToxAddress: setToxAddress(actorJson, '') actorChanged = True + currentPGPpubKey = getPGPpubKey(actorJson) if fields.get('pgp'): if fields['pgp'] != currentPGPpubKey: @@ -5429,6 +5450,7 @@ class PubServer(BaseHTTPRequestHandler): if currentPGPpubKey: setPGPpubKey(actorJson, '') actorChanged = True + currentDonateUrl = getDonationUrl(actorJson) if fields.get('donateUrl'): if fields['donateUrl'] != currentDonateUrl: @@ -5439,6 +5461,7 @@ class PubServer(BaseHTTPRequestHandler): if currentDonateUrl: setDonationUrl(actorJson, '') actorChanged = True + if fields.get('instanceTitle'): currInstanceTitle = \ getConfigParam(self.server.baseDir, diff --git a/webinterface.py b/webinterface.py index c00943a89..ea0528acc 100644 --- a/webinterface.py +++ b/webinterface.py @@ -21,6 +21,7 @@ from pgp import getEmailAddress from pgp import getPGPpubKey from xmpp import getXmppAddress from ssb import getSSBAddress +from blog import getBlogAddress from tox import getToxAddress from matrix import getMatrixAddress from donate import getDonationUrl @@ -881,6 +882,7 @@ def htmlEditProfile(translate: {}, baseDir: str, path: str, xmppAddress = '' matrixAddress = '' ssbAddress = '' + blogAddress = '' toxAddress = '' manuallyApprovesFollowers = '' actorJson = loadJson(actorFilename) @@ -889,6 +891,7 @@ def htmlEditProfile(translate: {}, baseDir: str, path: str, xmppAddress = getXmppAddress(actorJson) matrixAddress = getMatrixAddress(actorJson) ssbAddress = getSSBAddress(actorJson) + blogAddress = getBlogAddress(actorJson) toxAddress = getToxAddress(actorJson) emailAddress = getEmailAddress(actorJson) PGPpubKey = getPGPpubKey(actorJson) @@ -1120,10 +1123,17 @@ def htmlEditProfile(translate: {}, baseDir: str, path: str, editProfileForm += \ ' ' + editProfileForm += '
' editProfileForm += \ ' ' + + editProfileForm += '
' + editProfileForm += \ + ' ' + editProfileForm += '
' editProfileForm += \ ' SSB: ' + ssbAddress + '

' + if blogAddress: + optionsStr += \ + '

Blog: ' + \ + blogAddress + '

' if toxAddress: optionsStr += \ '

Tox: ' + toxAddress + '

'