From 39ab8ecd5324e3ec48a1315fe6792c90f4d09192 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 12 Aug 2021 21:40:23 +0100 Subject: [PATCH] Website field in profile --- daemon.py | 18 ++++++++- donate.py | 79 ++++++++++++++++++++++++++++++++++++++++ webapp_person_options.py | 1 + webapp_profile.py | 10 ++++- 4 files changed, 105 insertions(+), 3 deletions(-) diff --git a/daemon.py b/daemon.py index cf8a88275..12ca47545 100644 --- a/daemon.py +++ b/daemon.py @@ -50,6 +50,8 @@ from matrix import getMatrixAddress from matrix import setMatrixAddress from donate import getDonationUrl from donate import setDonationUrl +from donate import getWebsite +from donate import setWebsite from person import setPersonNotes from person import getDefaultPersonContext from person import savePersonQrcode @@ -4834,6 +4836,18 @@ class PubServer(BaseHTTPRequestHandler): setDonationUrl(actorJson, '') actorChanged = True + # change website + currentWebsite = getWebsite(actorJson) + if fields.get('websiteUrl'): + if fields['websiteUrl'] != currentWebsite: + setWebsite(actorJson, + fields['websiteUrl']) + actorChanged = True + else: + if currentWebsite: + setWebsite(actorJson, '') + actorChanged = True + # account moved to new address movedTo = '' if actorJson.get('movedTo'): @@ -6146,6 +6160,7 @@ class PubServer(BaseHTTPRequestHandler): if len(optionsList) > 3: optionsLink = optionsList[3] donateUrl = None + websiteUrl = None PGPpubKey = None PGPfingerprint = None xmppAddress = None @@ -6169,6 +6184,7 @@ class PubServer(BaseHTTPRequestHandler): movedTo = actorJson['movedTo'] lockedAccount = getLockedAccount(actorJson) donateUrl = getDonationUrl(actorJson) + websiteUrl = getWebsite(actorJson) xmppAddress = getXmppAddress(actorJson) matrixAddress = getMatrixAddress(actorJson) ssbAddress = getSSBAddress(actorJson) @@ -6207,7 +6223,7 @@ class PubServer(BaseHTTPRequestHandler): optionsActor, optionsProfileUrl, optionsLink, - pageNumber, donateUrl, + pageNumber, donateUrl, websiteUrl, xmppAddress, matrixAddress, ssbAddress, blogAddress, toxAddress, briarAddress, diff --git a/donate.py b/donate.py index f0942e502..5d271be54 100644 --- a/donate.py +++ b/donate.py @@ -14,6 +14,10 @@ def _getDonationTypes() -> str: 'subscribestar') +def _getWebsiteStrings() -> str: + return ('www', 'website', 'web', 'homepage') + + def getDonationUrl(actorJson: {}) -> str: """Returns a link used for donations """ @@ -39,6 +43,27 @@ def getDonationUrl(actorJson: {}) -> str: return '' +def getWebsite(actorJson: {}) -> str: + """Returns a web address link + """ + if not actorJson.get('attachment'): + return '' + matchStrings = _getWebsiteStrings() + for propertyValue in actorJson['attachment']: + if not propertyValue.get('name'): + continue + if propertyValue['name'].lower() not in matchStrings: + continue + if not propertyValue.get('type'): + continue + if not propertyValue.get('value'): + continue + if propertyValue['type'] != 'PropertyValue': + continue + return propertyValue['value'] + return '' + + def setDonationUrl(actorJson: {}, donateUrl: str) -> None: """Sets a link used for donations """ @@ -102,3 +127,57 @@ def setDonationUrl(actorJson: {}, donateUrl: str) -> None: "value": donateValue } actorJson['attachment'].append(newDonate) + + +def setWebsite(actorJson: {}, websiteUrl: str) -> None: + """Sets a web address + """ + notUrl = False + if '.' not in websiteUrl: + notUrl = True + if '://' not in websiteUrl: + notUrl = True + if ' ' in websiteUrl: + notUrl = True + if '<' in websiteUrl: + notUrl = True + + if not actorJson.get('attachment'): + actorJson['attachment'] = [] + + matchStrings = _getWebsiteStrings() + + # 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() not in matchStrings: + continue + propertyFound = propertyValue + break + if propertyFound: + actorJson['attachment'].remove(propertyFound) + if notUrl: + return + + for propertyValue in actorJson['attachment']: + if not propertyValue.get('name'): + continue + if not propertyValue.get('type'): + continue + if not propertyValue['name'].lower() not in matchStrings: + continue + if propertyValue['type'] != 'PropertyValue': + continue + propertyValue['value'] = websiteUrl + return + + newEntry = { + "name": 'Website', + "type": "PropertyValue", + "value": websiteUrl + } + actorJson['attachment'].append(newEntry) diff --git a/webapp_person_options.py b/webapp_person_options.py index bf967f0e2..ae5bdbc56 100644 --- a/webapp_person_options.py +++ b/webapp_person_options.py @@ -40,6 +40,7 @@ def htmlPersonOptions(defaultTimeline: str, optionsLink: str, pageNumber: int, donateUrl: str, + webAddress: str, xmppAddress: str, matrixAddress: str, ssbAddress: str, diff --git a/webapp_profile.py b/webapp_profile.py index c2518159e..32e76bf5e 100644 --- a/webapp_profile.py +++ b/webapp_profile.py @@ -34,6 +34,7 @@ from webfinger import webfingerHandle from posts import parseUserFeed from posts import getPersonBox from donate import getDonationUrl +from donate import getWebsite from xmpp import getXmppAddress from matrix import getMatrixAddress from ssb import getSSBAddress @@ -547,6 +548,7 @@ def htmlProfile(rssIconAtTop: bool, donateSection = '' donateUrl = getDonationUrl(profileJson) + websiteUrl = getWebsite(profileJson) PGPpubKey = getPGPpubKey(profileJson) PGPfingerprint = getPGPfingerprint(profileJson) emailAddress = getEmailAddress(profileJson) @@ -1719,7 +1721,7 @@ def _getSupportedLanguagesSorted(baseDir: str) -> str: def _htmlEditProfileMain(baseDir: str, displayNickname: str, bioStr: str, - movedTo: str, donateUrl: str, + movedTo: str, donateUrl: str, websiteUrl: str, blogAddress: str, actorJson: {}, translate: {}) -> str: """main info on edit profile screen @@ -1771,6 +1773,9 @@ def _htmlEditProfileMain(baseDir: str, displayNickname: str, bioStr: str, editTextField(translate['Donations link'], 'donateUrl', donateUrl, 'https://...') + editProfileForm += \ + editTextField('Website', 'websiteUrl', websiteUrl, 'https://...') + editProfileForm += \ editTextField('Blog', 'blogAddress', blogAddress, 'https://...') @@ -1858,6 +1863,7 @@ def htmlEditProfile(cssCache: {}, translate: {}, baseDir: str, path: str, if actorJson.get('movedTo'): movedTo = actorJson['movedTo'] donateUrl = getDonationUrl(actorJson) + websiteUrl = getWebsite(actorJson) xmppAddress = getXmppAddress(actorJson) matrixAddress = getMatrixAddress(actorJson) ssbAddress = getSSBAddress(actorJson) @@ -1981,7 +1987,7 @@ def htmlEditProfile(cssCache: {}, translate: {}, baseDir: str, path: str, # main info editProfileForm += \ _htmlEditProfileMain(baseDir, displayNickname, bioStr, - movedTo, donateUrl, + movedTo, donateUrl, websiteUrl, blogAddress, actorJson, translate) # Option checkboxes