From c0a3dd459b1c976335487d1bffd4eec33e6516ad Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 18 Jul 2021 19:02:36 +0100 Subject: [PATCH] Setting permitted post languages on edit profile screen --- daemon.py | 14 ++++++++ person.py | 76 ++++++++++++++++++++++++++++++++++++++++++++ translations/ar.json | 3 +- translations/ca.json | 3 +- translations/cy.json | 3 +- translations/de.json | 3 +- translations/en.json | 3 +- translations/es.json | 3 +- translations/fr.json | 3 +- translations/ga.json | 3 +- translations/hi.json | 3 +- translations/it.json | 3 +- translations/ja.json | 3 +- translations/ku.json | 3 +- translations/oc.json | 3 +- translations/pt.json | 3 +- translations/ru.json | 3 +- translations/sw.json | 3 +- translations/zh.json | 3 +- webapp_profile.py | 35 ++++++++++++++++++-- 20 files changed, 157 insertions(+), 19 deletions(-) diff --git a/daemon.py b/daemon.py index 16a229ce5..05296e7fa 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 person import getActorLanguages +from person import setActorLanguages from person import setPersonNotes from person import getDefaultPersonContext from person import savePersonQrcode @@ -4515,6 +4517,18 @@ class PubServer(BaseHTTPRequestHandler): setBlogAddress(actorJson, '') actorChanged = True + # change Languages address + currentShowLanguages = getActorLanguages(actorJson) + if fields.get('showLanguages'): + if fields['showLanguages'] != currentShowLanguages: + setActorLanguages(baseDir, actorJson, + fields['showLanguages']) + actorChanged = True + else: + if currentShowLanguages: + setActorLanguages(baseDir, actorJson, '') + actorChanged = True + # change tox address currentToxAddress = getToxAddress(actorJson) if fields.get('toxAddress'): diff --git a/person.py b/person.py index 1ee5180cb..ea19d40b9 100644 --- a/person.py +++ b/person.py @@ -1353,3 +1353,79 @@ def getPersonAvatarUrl(baseDir: str, personUrl: str, personCache: {}, if personJson['icon'].get('url'): return personJson['icon']['url'] return None + + +def _getActorLanguagesList(actorJson: {}) -> []: + """Returns a list containing languages used by the given actor + """ + for propertyValue in actorJson['attachment']: + if not propertyValue.get('name'): + continue + if not propertyValue['name'].lower().startswith('languages'): + continue + if not propertyValue.get('type'): + continue + if not propertyValue.get('value'): + continue + if not isinstance(propertyValue['value'], list): + continue + if propertyValue['type'] != 'PropertyValue': + continue + return propertyValue['value'] + return [] + + +def getActorLanguages(actorJson: {}) -> str: + """Returns a string containing languages used by the given actor + """ + langList = _getActorLanguagesList(actorJson) + if not langList: + return '' + languagesStr = '' + for lang in languagesStr: + if languagesStr: + languagesStr += ' / ' + lang + else: + languagesStr = lang + return languagesStr + + +def setActorLanguages(baseDir: str, actorJson: {}, languagesStr: str) -> None: + """Sets the languages used by the given actor + """ + separator = ',' + if '/' in languagesStr: + separator = '/' + elif ';' in languagesStr: + separator = ';' + langList = languagesStr.lower().split(separator) + langList2 = [] + for lang in langList: + lang = lang.strip() + languageFilename = baseDir + '/translations/' + lang + '.json' + if os.path.isfile(languageFilename): + langList2.append(lang) + + # 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('languages'): + continue + propertyFound = propertyValue + break + if propertyFound: + actorJson['attachment'].remove(propertyFound) + + if not langList2: + return + + newLanguages = { + "name": "Languages", + "type": "PropertyValue", + "value": langList2 + } + actorJson['attachment'].append(newLanguages) diff --git a/translations/ar.json b/translations/ar.json index 50cca5a13..91c834007 100644 --- a/translations/ar.json +++ b/translations/ar.json @@ -450,5 +450,6 @@ "Export Theme": "موضوع التصدير", "Custom post submit button text": "عرف نشر إرسال نص زر", "Blocked User Agents": "عوامل المستخدم المحظورة", - "Notify me when this account posts": "أعلمني عندما ينشر الحساب هذا" + "Notify me when this account posts": "أعلمني عندما ينشر الحساب هذا", + "Languages": "اللغات" } diff --git a/translations/ca.json b/translations/ca.json index c9a15be88..bec6f3802 100644 --- a/translations/ca.json +++ b/translations/ca.json @@ -450,5 +450,6 @@ "Export Theme": "Tema d'exportació", "Custom post submit button text": "Text de botó d'enviament de publicacions personalitzades", "Blocked User Agents": "Agents d'usuari bloquejats", - "Notify me when this account posts": "Aviseu-me quan publiqui aquest compte" + "Notify me when this account posts": "Aviseu-me quan publiqui aquest compte", + "Languages": "Idiomes" } diff --git a/translations/cy.json b/translations/cy.json index 0cf0e9980..c5d246f25 100644 --- a/translations/cy.json +++ b/translations/cy.json @@ -450,5 +450,6 @@ "Export Theme": "Thema Allforio", "Custom post submit button text": "Testun Post Post Post", "Blocked User Agents": "Asiantau defnyddwyr wedi'u blocio", - "Notify me when this account posts": "Rhoi gwybod i mi pan fydd y cyfrifon cyfrif hwn" + "Notify me when this account posts": "Rhoi gwybod i mi pan fydd y cyfrifon cyfrif hwn", + "Languages": "Ieithoedd" } diff --git a/translations/de.json b/translations/de.json index df10a2e6e..ed3754a9e 100644 --- a/translations/de.json +++ b/translations/de.json @@ -450,5 +450,6 @@ "Export Theme": "Theme exportieren", "Custom post submit button text": "Benutzerdefinierte Post-Senden Schaltfläche Text", "Blocked User Agents": "Blockierte Benutzeragenten", - "Notify me when this account posts": "Benachrichtigen Sie mich, wenn dieses Konto postet" + "Notify me when this account posts": "Benachrichtigen Sie mich, wenn dieses Konto postet", + "Languages": "Sprachen" } diff --git a/translations/en.json b/translations/en.json index 31d1bbf40..d2d8405e7 100644 --- a/translations/en.json +++ b/translations/en.json @@ -450,5 +450,6 @@ "Export Theme": "Export Theme", "Custom post submit button text": "Custom post submit button text", "Blocked User Agents": "Blocked User Agents", - "Notify me when this account posts": "Notify me when this account posts" + "Notify me when this account posts": "Notify me when this account posts", + "Languages": "Languages" } diff --git a/translations/es.json b/translations/es.json index ac096a01d..ef9d0a1e7 100644 --- a/translations/es.json +++ b/translations/es.json @@ -450,5 +450,6 @@ "Export Theme": "Tema de exportación", "Custom post submit button text": "POST POST PERSONALIZADO Botón Texto", "Blocked User Agents": "Agentes de usuario bloqueados", - "Notify me when this account posts": "Notifíqueme cuando se publique esta cuenta" + "Notify me when this account posts": "Notifíqueme cuando se publique esta cuenta", + "Languages": "Idiomas" } diff --git a/translations/fr.json b/translations/fr.json index 6defd9956..cbcba6a5c 100644 --- a/translations/fr.json +++ b/translations/fr.json @@ -450,5 +450,6 @@ "Export Theme": "Thème d'exportation", "Custom post submit button text": "Texte de bouton d'envoi postal personnalisé", "Blocked User Agents": "Agents d'utilisateur bloqués", - "Notify me when this account posts": "Avertissez-moi quand ce compte publie" + "Notify me when this account posts": "Avertissez-moi quand ce compte publie", + "Languages": "Langues" } diff --git a/translations/ga.json b/translations/ga.json index 632b57ab0..f5b4b3799 100644 --- a/translations/ga.json +++ b/translations/ga.json @@ -450,5 +450,6 @@ "Export Theme": "Téama Easpórtála", "Custom post submit button text": "Post saincheaptha Cuir isteach an cnaipe Téacs", "Blocked User Agents": "Gníomhairí úsáideora blocáilte", - "Notify me when this account posts": "Cuir in iúl dom nuair a phostófar an cuntas seo" + "Notify me when this account posts": "Cuir in iúl dom nuair a phostófar an cuntas seo", + "Languages": "Teangacha" } diff --git a/translations/hi.json b/translations/hi.json index 2f3e13f0e..01d615739 100644 --- a/translations/hi.json +++ b/translations/hi.json @@ -450,5 +450,6 @@ "Export Theme": "निर्यात विषय", "Custom post submit button text": "कस्टम पोस्ट सबमिट बटन टेक्स्ट", "Blocked User Agents": "अवरुद्ध उपयोगकर्ता एजेंट", - "Notify me when this account posts": "यह खाता पोस्ट होने पर मुझे सूचित करें" + "Notify me when this account posts": "यह खाता पोस्ट होने पर मुझे सूचित करें", + "Languages": "बोली" } diff --git a/translations/it.json b/translations/it.json index 8406ccbda..5d9081d71 100644 --- a/translations/it.json +++ b/translations/it.json @@ -450,5 +450,6 @@ "Export Theme": "Esportare tema", "Custom post submit button text": "Pulsante di invio del post personalizzato", "Blocked User Agents": "Agenti utente bloccati", - "Notify me when this account posts": "Avvisami quando questo account messaggi" + "Notify me when this account posts": "Avvisami quando questo account messaggi", + "Languages": "Le lingue" } diff --git a/translations/ja.json b/translations/ja.json index 494661a86..157a6bddb 100644 --- a/translations/ja.json +++ b/translations/ja.json @@ -450,5 +450,6 @@ "Export Theme": "テーマをエクスポートします", "Custom post submit button text": "カスタムポスト送信ボタンテキスト", "Blocked User Agents": "ブロックされたユーザーエージェント", - "Notify me when this account posts": "この口座投稿を通知する" + "Notify me when this account posts": "この口座投稿を通知する", + "Languages": "言語" } diff --git a/translations/ku.json b/translations/ku.json index 8d99ddd72..6b87537cd 100644 --- a/translations/ku.json +++ b/translations/ku.json @@ -450,5 +450,6 @@ "Export Theme": "Mijara Export", "Custom post submit button text": "Nivîsa bişkojka paşîn a paşîn", "Blocked User Agents": "Karmendên bikarhêner asteng kirin", - "Notify me when this account posts": "Dema ku ev postên hesabê min agahdar bikin" + "Notify me when this account posts": "Dema ku ev postên hesabê min agahdar bikin", + "Languages": "Ziman" } diff --git a/translations/oc.json b/translations/oc.json index 411216f8a..2ccf6da01 100644 --- a/translations/oc.json +++ b/translations/oc.json @@ -446,5 +446,6 @@ "Export Theme": "Export Theme", "Custom post submit button text": "Custom post submit button text", "Blocked User Agents": "Blocked User Agents", - "Notify me when this account posts": "Notify me when this account posts" + "Notify me when this account posts": "Notify me when this account posts", + "Languages": "Languages" } diff --git a/translations/pt.json b/translations/pt.json index 8d05812de..c1b0efbc1 100644 --- a/translations/pt.json +++ b/translations/pt.json @@ -450,5 +450,6 @@ "Export Theme": "Exportar tema", "Custom post submit button text": "Texto de botão de envio de post personalizado", "Blocked User Agents": "Agentes de usuário bloqueados", - "Notify me when this account posts": "Notifique-me quando esta conta posts" + "Notify me when this account posts": "Notifique-me quando esta conta posts", + "Languages": "Línguas" } diff --git a/translations/ru.json b/translations/ru.json index 4663cef78..d31d4133d 100644 --- a/translations/ru.json +++ b/translations/ru.json @@ -450,5 +450,6 @@ "Export Theme": "Экспортная тема", "Custom post submit button text": "Пользовательский пост Отправить кнопку текста", "Blocked User Agents": "Заблокированные пользовательские агенты", - "Notify me when this account posts": "Сообщите мне, когда эта учетная запись" + "Notify me when this account posts": "Сообщите мне, когда эта учетная запись", + "Languages": "Языки" } diff --git a/translations/sw.json b/translations/sw.json index 4f2fa1bb7..aac84cd23 100644 --- a/translations/sw.json +++ b/translations/sw.json @@ -450,5 +450,6 @@ "Export Theme": "Tuma mandhari", "Custom post submit button text": "Ujumbe wa Desturi Wasilisha Nakala ya kifungo", "Blocked User Agents": "Wakala wa watumiaji waliozuiwa", - "Notify me when this account posts": "Nijulishe wakati akaunti hii ya akaunti." + "Notify me when this account posts": "Nijulishe wakati akaunti hii ya akaunti.", + "Languages": "Lugha" } diff --git a/translations/zh.json b/translations/zh.json index eb143dc04..1ffbe2591 100644 --- a/translations/zh.json +++ b/translations/zh.json @@ -450,5 +450,6 @@ "Export Theme": "出口主题", "Custom post submit button text": "自定义发布提交按钮文本", "Blocked User Agents": "阻止用户代理商", - "Notify me when this account posts": "此帐户帖子时通知我" + "Notify me when this account posts": "此帐户帖子时通知我", + "Languages": "语言" } diff --git a/webapp_profile.py b/webapp_profile.py index 71f697eda..a7b5d12ef 100644 --- a/webapp_profile.py +++ b/webapp_profile.py @@ -25,6 +25,7 @@ from utils import getImageFormats from utils import acctDir from skills import getSkills from theme import getThemesList +from person import getActorLanguages from person import personBoxJson from person import getActorJson from person import getPersonAvatarUrl @@ -1775,7 +1776,26 @@ def _htmlEditProfileOptions(manuallyApprovesFollowers: str, return editProfileForm -def _htmlEditProfileMain(displayNickname: str, bioStr: str, +def _getSupportedLanguages(baseDir: str) -> str: + """Returns a list of supported languages + """ + languagesStr = '' + for subdir, dirs, files in os.walk(baseDir + '/translations'): + for f in files: + if not f.endswith('.json'): + continue + langStr = f.split('.')[0] + if len(langStr) != 2: + continue + if languagesStr: + languagesStr += ' / ' + langStr + else: + languagesStr = langStr + break + return languagesStr + + +def _htmlEditProfileMain(baseDir: str, displayNickname: str, bioStr: str, movedTo: str, donateUrl: str, blogAddress: str, actorJson: {}, translate: {}) -> str: @@ -1846,6 +1866,16 @@ def _htmlEditProfileMain(displayNickname: str, bioStr: str, ' \n' + \ ' \n' + + languagesListStr = _getSupportedLanguages(baseDir) + showLanguages = getActorLanguages(actorJson) + editProfileForm += \ + '
\n' + \ + '
\n' + \ + ' \n' + \ + ' \n' return editProfileForm @@ -2038,7 +2068,8 @@ def htmlEditProfile(cssCache: {}, translate: {}, baseDir: str, path: str, # main info editProfileForm += \ - _htmlEditProfileMain(displayNickname, bioStr, movedTo, donateUrl, + _htmlEditProfileMain(baseDir, displayNickname, bioStr, + movedTo, donateUrl, blogAddress, actorJson, translate) # Option checkboxes