diff --git a/daemon_post_profile.py b/daemon_post_profile.py index 76cc162f7..1b1e4b19b 100644 --- a/daemon_post_profile.py +++ b/daemon_post_profile.py @@ -81,6 +81,8 @@ from pgp import set_pgp_fingerprint from pgp import get_pgp_fingerprint from pronouns import get_pronouns from pronouns import set_pronouns +from discord import get_discord +from discord import set_discord from youtube import get_youtube from youtube import set_youtube from pixelfed import get_pixelfed @@ -2005,6 +2007,23 @@ def _profile_post_youtube(actor_json: {}, fields: {}, return actor_changed +def _profile_post_discord(actor_json: {}, fields: {}, + actor_changed: bool) -> bool: + """ HTTP POST change discord channel address + """ + current_discord = get_discord(actor_json) + if fields.get('discordChannel'): + if fields['discordChannel'] != current_discord: + set_discord(actor_json, + fields['discordChannel']) + actor_changed = True + else: + if current_discord: + set_discord(actor_json, '') + actor_changed = True + return actor_changed + + def _profile_post_pixelfed(actor_json: {}, fields: {}, actor_changed: bool) -> bool: """ HTTP POST change pixelfed channel address @@ -2910,6 +2929,10 @@ def profile_edit(self, calling_domain: str, cookie: str, _profile_post_pixelfed(actor_json, fields, actor_changed) + actor_changed = \ + _profile_post_discord(actor_json, fields, + actor_changed) + actor_changed = \ _profile_post_youtube(actor_json, fields, actor_changed) diff --git a/daemon_utils.py b/daemon_utils.py index ec6f7889e..d56fc23f4 100644 --- a/daemon_utils.py +++ b/daemon_utils.py @@ -45,6 +45,7 @@ from donate import get_donation_url from donate import get_website from donate import get_gemini_link from pronouns import get_pronouns +from discord import get_discord from youtube import get_youtube from pixelfed import get_pixelfed from peertube import get_peertube @@ -660,6 +661,7 @@ def show_person_options(self, calling_domain: str, path: str, pgp_fingerprint = None pronouns = None pixelfed = None + discord = None youtube = None peertube = None xmpp_address = None @@ -692,6 +694,7 @@ def show_person_options(self, calling_domain: str, path: str, gemini_link = get_gemini_link(actor_json) pronouns = get_pronouns(actor_json) pixelfed = get_pixelfed(actor_json) + discord = get_discord(actor_json) youtube = get_youtube(actor_json) peertube = get_peertube(actor_json) xmpp_address = get_xmpp_address(actor_json) @@ -767,7 +770,8 @@ def show_person_options(self, calling_domain: str, path: str, self.server.blocked_cache, repo_url, self.server.sites_unavailable, - youtube, peertube, pixelfed) + youtube, peertube, pixelfed, + discord) if msg: msg = msg.encode('utf-8') msglen = len(msg) diff --git a/discord.py b/discord.py new file mode 100644 index 000000000..d102dfbc8 --- /dev/null +++ b/discord.py @@ -0,0 +1,101 @@ +__filename__ = "discord.py" +__author__ = "Bob Mottram" +__license__ = "AGPL3+" +__version__ = "1.5.0" +__maintainer__ = "Bob Mottram" +__email__ = "bob@libreserver.org" +__status__ = "Production" +__module_group__ = "Profile Metadata" + + +from utils import get_attachment_property_value +from utils import remove_html +from utils import string_contains + +discord_fieldnames = ['discord'] + + +def get_discord(actor_json: {}) -> str: + """Returns discord for the given actor + """ + if not actor_json.get('attachment'): + return '' + if not isinstance(actor_json['attachment'], list): + return '' + for property_value in actor_json['attachment']: + name_value = None + if property_value.get('name'): + name_value = property_value['name'].lower() + elif property_value.get('schema:name'): + name_value = property_value['schema:name'].lower() + if not name_value: + continue + if not string_contains(name_value, discord_fieldnames): + continue + if not property_value.get('type'): + continue + prop_value_name, _ = \ + get_attachment_property_value(property_value) + if not prop_value_name: + continue + if not property_value['type'].endswith('PropertyValue'): + continue + discord_text = property_value[prop_value_name] + return remove_html(discord_text) + return '' + + +def set_discord(actor_json: {}, discord: str) -> None: + """Sets discord for the given actor + """ + if not actor_json.get('attachment'): + actor_json['attachment'] = [] + + # remove any existing value + property_found = None + for property_value in actor_json['attachment']: + name_value = None + if property_value.get('name'): + name_value = property_value['name'].lower() + elif property_value.get('schema:name'): + name_value = property_value['schema:name'].lower() + if not name_value: + continue + if not property_value.get('type'): + continue + if not string_contains(name_value, discord_fieldnames): + continue + property_found = property_value + break + + if property_found: + actor_json['attachment'].remove(property_found) + + for property_value in actor_json['attachment']: + name_value = None + if property_value.get('name'): + name_value = property_value['name'] + elif property_value.get('schema:name'): + name_value = property_value['schema:name'] + if not name_value: + continue + if not property_value.get('type'): + continue + name_value = name_value.lower() + if not string_contains(name_value, discord_fieldnames): + continue + if not property_value['type'].endswith('PropertyValue'): + continue + prop_value_name, _ = \ + get_attachment_property_value(property_value) + if not prop_value_name: + continue + property_value[prop_value_name] = remove_html(discord) + return + + new_discord = { + "type": "PropertyValue", + "name": "Discord", + "value": remove_html(discord) + } + actor_json['attachment'].append(new_discord) diff --git a/pgp.py b/pgp.py index a6031d535..7b2481616 100644 --- a/pgp.py +++ b/pgp.py @@ -28,6 +28,7 @@ from auth import create_basic_auth_header from session import post_json from pronouns import get_pronouns from pixelfed import get_pixelfed +from discord import get_discord from youtube import get_youtube from peertube import get_peertube from xmpp import get_xmpp_address @@ -746,6 +747,9 @@ def actor_to_vcard(actor: {}, domain: str) -> str: blog_address = get_blog_address(actor) if blog_address: vcard_str += 'SOCIALPROFILE;SERVICE-TYPE=Blog:' + blog_address + '\n' + discord = get_discord(actor) + if discord: + vcard_str += 'SOCIALPROFILE;SERVICE-TYPE=Discord:' + discord + '\n' pixelfed = get_pixelfed(actor) if pixelfed: vcard_str += 'SOCIALPROFILE;SERVICE-TYPE=Pixelfed:' + pixelfed + '\n' @@ -832,6 +836,11 @@ def actor_to_vcard_xml(actor: {}, domain: str) -> str: vcard_str += ' ' + \ 'pixelfed' + \ '' + pixelfed + '\n' + discord = get_discord(actor) + if discord: + vcard_str += ' ' + \ + 'discord' + \ + '' + discord + '\n' youtube = get_youtube(actor) if youtube: vcard_str += ' ' + \ diff --git a/webapp_person_options.py b/webapp_person_options.py index 4351c6432..740953795 100644 --- a/webapp_person_options.py +++ b/webapp_person_options.py @@ -172,7 +172,8 @@ def html_person_options(default_timeline: str, repo_url: str, sites_unavailable: [], youtube: str, peertube: str, - pixelfed: str) -> str: + pixelfed: str, + discord: str) -> str: """Show options for a person: view/follow/block/report """ options_link_str = '' @@ -442,6 +443,11 @@ def html_person_options(default_timeline: str, '

Pixelfed' + \ ': ' + \ pixelfed + '

\n' + if discord: + options_str += \ + '

Discord' + \ + ': ' + \ + discord + '

\n' if youtube: options_str += \ '

YouTube' + \ diff --git a/webapp_profile.py b/webapp_profile.py index 8a82ab136..e60b64da2 100644 --- a/webapp_profile.py +++ b/webapp_profile.py @@ -68,6 +68,7 @@ from donate import get_website from donate import get_gemini_link from pronouns import get_pronouns from pixelfed import get_pixelfed +from discord import get_discord from youtube import get_youtube from peertube import get_peertube from xmpp import get_xmpp_address @@ -329,6 +330,7 @@ def html_profile_after_search(authorized: bool, display_name += '🔒' pronouns = get_pronouns(profile_json) + discord = get_discord(profile_json) youtube = get_youtube(profile_json) peertube = get_peertube(profile_json) pixelfed = get_pixelfed(profile_json) @@ -456,7 +458,8 @@ def html_profile_after_search(authorized: bool, authorized, person_url, no_of_books, birth_date, - youtube, peertube, pixelfed) + youtube, peertube, pixelfed, + discord) domain_full = get_full_domain(domain, port) @@ -826,7 +829,8 @@ def _get_profile_header_after_search(base_dir: str, birth_date: str, youtube: str, peertube: str, - pixelfed: str) -> str: + pixelfed: str, + discord: str) -> str: """The header of a searched for handle, containing background image and avatar """ @@ -948,6 +952,9 @@ def _get_profile_header_after_search(base_dir: str, if pixelfed: html_str += '

Pixelfed: ' + \ pixelfed + '

\n' + if discord: + html_str += '

Discord: ' + \ + discord + '

\n' if repo_url: html_str += '

💻 ' + \ repo_url + '

\n' @@ -1115,6 +1122,7 @@ def html_profile(signing_priv_key_pem: str, email_address = get_email_address(profile_json) pronouns = get_pronouns(profile_json) pixelfed = get_pixelfed(profile_json) + discord = get_discord(profile_json) youtube = get_youtube(profile_json) peertube = get_peertube(profile_json) xmpp_address = get_xmpp_address(profile_json) @@ -1125,8 +1133,8 @@ def html_profile(signing_priv_key_pem: str, cwtch_address = get_cwtch_address(profile_json) verified_site_checkmark = '✔' premium = is_premium_account(base_dir, nickname, domain) - if donate_url or website_url or repo_url or pronouns or youtube or \ - peertube or pixelfed or xmpp_address or matrix_address or \ + if donate_url or website_url or repo_url or pronouns or discord or \ + youtube or peertube or pixelfed or xmpp_address or matrix_address or \ ssb_address or tox_address or briar_address or cwtch_address or \ pgp_pub_key or enigma_pub_key or pgp_fingerprint or email_address: donate_section = '
\n' @@ -1196,6 +1204,10 @@ def html_profile(signing_priv_key_pem: str, donate_section += \ '

Pixelfed: ' + pixelfed + '

\n' + if discord: + donate_section += \ + '

Discord: ' + discord + '

\n' if youtube: donate_section += \ '

YouTube: