Merge branch 'main' of gitlab.com:bashrc2/epicyon

merge-requests/30/head
Bob Mottram 2022-09-03 18:24:15 +01:00
commit 6ad7eaa446
4 changed files with 147 additions and 34 deletions

View File

@ -15,6 +15,7 @@ import email.parser
import urllib.parse import urllib.parse
from shutil import copyfile from shutil import copyfile
from dateutil.parser import parse from dateutil.parser import parse
from utils import get_user_paths
from utils import convert_published_to_local_timezone from utils import convert_published_to_local_timezone
from utils import has_object_dict from utils import has_object_dict
from utils import valid_hash_tag from utils import valid_hash_tag
@ -742,8 +743,28 @@ def post_tag_exists(tagType: str, tagName: str, tags: {}) -> bool:
return False return False
def _add_mention(word_str: str, http_prefix: str, following: str, def _mention_to_url(base_dir: str, http_prefix: str,
petnames: str, replace_mentions: {}, domain: str, nickname: str) -> str:
"""Convert https://somedomain/@somenick to
https://somedomain/users/somenick
This uses the hack of trying the cache directory to see if
there is a matching actor
"""
possible_paths = get_user_paths()
cache_dir = base_dir + '/cache/actors'
cache_path_start = cache_dir + '/' + http_prefix + ':##' + domain
for users_path in possible_paths:
users_path = users_path.replace('/', '#')
possible_cache_entry = \
cache_path_start + users_path + nickname + '.json'
if os.path.isfile(possible_cache_entry):
return http_prefix + '://' + \
domain + users_path.replace('#', '/') + nickname
return http_prefix + '://' + domain + '/users/' + nickname
def _add_mention(base_dir: str, word_str: str, http_prefix: str,
following: [], petnames: [], replace_mentions: {},
recipients: [], tags: {}) -> bool: recipients: [], tags: {}) -> bool:
"""Detects mentions and adds them to the replacements dict and """Detects mentions and adds them to the replacements dict and
recipients list recipients list
@ -761,8 +782,9 @@ def _add_mention(word_str: str, http_prefix: str, following: str,
if possible_nickname == follow_nick: if possible_nickname == follow_nick:
follow_str = remove_eol(follow) follow_str = remove_eol(follow)
replace_domain = follow_str.split('@')[1] replace_domain = follow_str.split('@')[1]
recipient_actor = http_prefix + "://" + \ recipient_actor = \
replace_domain + "/@" + possible_nickname _mention_to_url(base_dir, http_prefix,
replace_domain, possible_nickname)
if recipient_actor not in recipients: if recipient_actor not in recipients:
recipients.append(recipient_actor) recipients.append(recipient_actor)
tags[word_str] = { tags[word_str] = {
@ -771,8 +793,7 @@ def _add_mention(word_str: str, http_prefix: str, following: str,
'type': 'Mention' 'type': 'Mention'
} }
replace_mentions[word_str] = \ replace_mentions[word_str] = \
"<span class=\"h-card\"><a href=\"" + http_prefix + \ "<span class=\"h-card\"><a href=\"" + recipient_actor + \
"://" + replace_domain + "/@" + possible_nickname + \
"\" tabindex=\"10\" class=\"u-url mention\">@<span>" + \ "\" tabindex=\"10\" class=\"u-url mention\">@<span>" + \
possible_nickname + "</span></a></span>" possible_nickname + "</span></a></span>"
return True return True
@ -788,8 +809,9 @@ def _add_mention(word_str: str, http_prefix: str, following: str,
follow_str = remove_eol(follow) follow_str = remove_eol(follow)
replace_nickname = follow_str.split('@')[0] replace_nickname = follow_str.split('@')[0]
replace_domain = follow_str.split('@')[1] replace_domain = follow_str.split('@')[1]
recipient_actor = http_prefix + "://" + \ recipient_actor = \
replace_domain + "/@" + replace_nickname _mention_to_url(base_dir, http_prefix,
replace_domain, replace_nickname)
if recipient_actor not in recipients: if recipient_actor not in recipients:
recipients.append(recipient_actor) recipients.append(recipient_actor)
tags[word_str] = { tags[word_str] = {
@ -798,9 +820,8 @@ def _add_mention(word_str: str, http_prefix: str, following: str,
'type': 'Mention' 'type': 'Mention'
} }
replace_mentions[word_str] = \ replace_mentions[word_str] = \
"<span class=\"h-card\"><a href=\"" + http_prefix + \ "<span class=\"h-card\"><a href=\"" + \
"://" + replace_domain + "/@" + replace_nickname + \ recipient_actor + "\" tabindex=\"10\" " + \
"\" tabindex=\"10\" " + \
"class=\"u-url mention\">@<span>" + \ "class=\"u-url mention\">@<span>" + \
replace_nickname + "</span></a></span>" replace_nickname + "</span></a></span>"
return True return True
@ -821,8 +842,9 @@ def _add_mention(word_str: str, http_prefix: str, following: str,
for follow in following: for follow in following:
if remove_eol(follow) != possible_handle: if remove_eol(follow) != possible_handle:
continue continue
recipient_actor = http_prefix + "://" + \ recipient_actor = \
possible_domain + "/@" + possible_nickname _mention_to_url(base_dir, http_prefix,
possible_domain, possible_nickname)
if recipient_actor not in recipients: if recipient_actor not in recipients:
recipients.append(recipient_actor) recipients.append(recipient_actor)
tags[word_str] = { tags[word_str] = {
@ -831,16 +853,16 @@ def _add_mention(word_str: str, http_prefix: str, following: str,
'type': 'Mention' 'type': 'Mention'
} }
replace_mentions[word_str] = \ replace_mentions[word_str] = \
"<span class=\"h-card\"><a href=\"" + http_prefix + \ "<span class=\"h-card\"><a href=\"" + recipient_actor + \
"://" + possible_domain + "/@" + possible_nickname + \
"\" tabindex=\"10\" class=\"u-url mention\">@<span>" + \ "\" tabindex=\"10\" class=\"u-url mention\">@<span>" + \
possible_nickname + "</span></a></span>" possible_nickname + "</span></a></span>"
return True return True
# @nick@domain # @nick@domain
if not (possible_domain == 'localhost' or '.' in possible_domain): if not (possible_domain == 'localhost' or '.' in possible_domain):
return False return False
recipient_actor = http_prefix + "://" + \ recipient_actor = \
possible_domain + "/@" + possible_nickname _mention_to_url(base_dir, http_prefix,
possible_domain, possible_nickname)
if recipient_actor not in recipients: if recipient_actor not in recipients:
recipients.append(recipient_actor) recipients.append(recipient_actor)
tags[word_str] = { tags[word_str] = {
@ -849,8 +871,7 @@ def _add_mention(word_str: str, http_prefix: str, following: str,
'type': 'Mention' 'type': 'Mention'
} }
replace_mentions[word_str] = \ replace_mentions[word_str] = \
"<span class=\"h-card\"><a href=\"" + http_prefix + \ "<span class=\"h-card\"><a href=\"" + recipient_actor + \
"://" + possible_domain + "/@" + possible_nickname + \
"\" tabindex=\"10\" class=\"u-url mention\">@<span>" + \ "\" tabindex=\"10\" class=\"u-url mention\">@<span>" + \
possible_nickname + "</span></a></span>" possible_nickname + "</span></a></span>"
return True return True
@ -1225,8 +1246,9 @@ def add_html_tags(base_dir: str, http_prefix: str,
long_words_list.append(word_str) long_words_list.append(word_str)
first_char = word_str[0] first_char = word_str[0]
if first_char == '@': if first_char == '@':
if _add_mention(word_str, http_prefix, following, petnames, if _add_mention(base_dir, word_str, http_prefix, following,
replace_mentions, recipients, hashtags): petnames, replace_mentions, recipients,
hashtags):
prev_word_str = '' prev_word_str = ''
continue continue
elif first_char == '#': elif first_char == '#':

View File

@ -3995,13 +3995,16 @@ class PubServer(BaseHTTPRequestHandler):
break break
# skill search # skill search
search_str = search_str.replace('*', '').strip() search_str = search_str.replace('*', '').strip()
nickname = get_nickname_from_actor(actor_str)
skill_str = \ skill_str = \
html_skills_search(actor_str, html_skills_search(actor_str,
self.server.translate, self.server.translate,
base_dir, base_dir,
search_str, search_str,
self.server.instance_only_skills_search, self.server.instance_only_skills_search,
64) 64, nickname, domain,
self.server.theme_name,
self.server.access_keys)
if skill_str: if skill_str:
msg = skill_str.encode('utf-8') msg = skill_str.encode('utf-8')
msglen = len(msg) msglen = len(msg)
@ -4081,7 +4084,8 @@ class PubServer(BaseHTTPRequestHandler):
self.server.cw_lists, self.server.cw_lists,
self.server.lists_enabled, self.server.lists_enabled,
timezone, bold_reading, timezone, bold_reading,
self.server.dogwhistles) self.server.dogwhistles,
self.server.access_keys)
if history_str: if history_str:
msg = history_str.encode('utf-8') msg = history_str.encode('utf-8')
msglen = len(msg) msglen = len(msg)
@ -4162,7 +4166,8 @@ class PubServer(BaseHTTPRequestHandler):
self.server.cw_lists, self.server.cw_lists,
self.server.lists_enabled, self.server.lists_enabled,
timezone, bold_reading, timezone, bold_reading,
self.server.dogwhistles) self.server.dogwhistles,
self.server.access_keys)
if bookmarks_str: if bookmarks_str:
msg = bookmarks_str.encode('utf-8') msg = bookmarks_str.encode('utf-8')
msglen = len(msg) msglen = len(msg)
@ -4355,10 +4360,13 @@ class PubServer(BaseHTTPRequestHandler):
search_str = \ search_str = \
search_str.replace(' emoji', '') search_str.replace(' emoji', '')
# emoji search # emoji search
nickname = get_nickname_from_actor(actor_str)
emoji_str = \ emoji_str = \
html_search_emoji(self.server.translate, html_search_emoji(self.server.translate,
base_dir, base_dir, search_str,
search_str) nickname, domain,
self.server.theme_name,
self.server.access_keys)
if emoji_str: if emoji_str:
msg = emoji_str.encode('utf-8') msg = emoji_str.encode('utf-8')
msglen = len(msg) msglen = len(msg)
@ -4371,6 +4379,7 @@ class PubServer(BaseHTTPRequestHandler):
# wanted items search # wanted items search
shared_items_federated_domains = \ shared_items_federated_domains = \
self.server.shared_items_federated_domains self.server.shared_items_federated_domains
nickname = get_nickname_from_actor(actor_str)
wanted_items_str = \ wanted_items_str = \
html_search_shared_items(self.server.translate, html_search_shared_items(self.server.translate,
base_dir, base_dir,
@ -4380,7 +4389,9 @@ class PubServer(BaseHTTPRequestHandler):
domain_full, domain_full,
actor_str, calling_domain, actor_str, calling_domain,
shared_items_federated_domains, shared_items_federated_domains,
'wanted') 'wanted', nickname, domain,
self.server.theme_name,
self.server.access_keys)
if wanted_items_str: if wanted_items_str:
msg = wanted_items_str.encode('utf-8') msg = wanted_items_str.encode('utf-8')
msglen = len(msg) msglen = len(msg)
@ -4393,6 +4404,7 @@ class PubServer(BaseHTTPRequestHandler):
# shared items search # shared items search
shared_items_federated_domains = \ shared_items_federated_domains = \
self.server.shared_items_federated_domains self.server.shared_items_federated_domains
nickname = get_nickname_from_actor(actor_str)
shared_items_str = \ shared_items_str = \
html_search_shared_items(self.server.translate, html_search_shared_items(self.server.translate,
base_dir, base_dir,
@ -4402,7 +4414,9 @@ class PubServer(BaseHTTPRequestHandler):
domain_full, domain_full,
actor_str, calling_domain, actor_str, calling_domain,
shared_items_federated_domains, shared_items_federated_domains,
'shares') 'shares', nickname, domain,
self.server.theme_name,
self.server.access_keys)
if shared_items_str: if shared_items_str:
msg = shared_items_str.encode('utf-8') msg = shared_items_str.encode('utf-8')
msglen = len(msg) msglen = len(msg)

View File

@ -4697,7 +4697,7 @@ def _test_reply_to_public_post(base_dir: str) -> None:
# print(str(reply)) # print(str(reply))
expected_str = \ expected_str = \
'<p><span class=\"h-card\">' + \ '<p><span class=\"h-card\">' + \
'<a href=\"https://rat.site/@ninjarodent\" tabindex="10" ' + \ '<a href=\"https://rat.site/users/ninjarodent\" tabindex="10" ' + \
'class=\"u-url mention\">@<span>ninjarodent</span>' + \ 'class=\"u-url mention\">@<span>ninjarodent</span>' + \
'</a></span> This is a test.</p>' '</a></span> This is a test.</p>'
if reply['object']['content'] != expected_str: if reply['object']['content'] != expected_str:
@ -4707,7 +4707,8 @@ def _test_reply_to_public_post(base_dir: str) -> None:
reply['object']['contentMap'][system_language] = reply['object']['content'] reply['object']['contentMap'][system_language] = reply['object']['content']
assert reply['object']['tag'][0]['type'] == 'Mention' assert reply['object']['tag'][0]['type'] == 'Mention'
assert reply['object']['tag'][0]['name'] == '@ninjarodent@rat.site' assert reply['object']['tag'][0]['name'] == '@ninjarodent@rat.site'
assert reply['object']['tag'][0]['href'] == 'https://rat.site/@ninjarodent' assert reply['object']['tag'][0]['href'] == \
'https://rat.site/users/ninjarodent'
assert len(reply['object']['to']) == 1 assert len(reply['object']['to']) == 1
assert reply['object']['to'][0].endswith('#Public') assert reply['object']['to'][0].endswith('#Public')
assert len(reply['object']['cc']) >= 1 assert len(reply['object']['cc']) >= 1

View File

@ -44,7 +44,9 @@ from webapp_hashtagswarm import html_hash_tag_swarm
from maps import html_hashtag_maps from maps import html_hashtag_maps
def html_search_emoji(translate: {}, base_dir: str, search_str: str) -> str: def html_search_emoji(translate: {}, base_dir: str, search_str: str,
nickname: str, domain: str, theme: str,
access_keys: {}) -> str:
"""Search results for emoji """Search results for emoji
""" """
# emoji.json is generated so that it can be customized and the changes # emoji.json is generated so that it can be customized and the changes
@ -66,6 +68,24 @@ def html_search_emoji(translate: {}, base_dir: str, search_str: str) -> str:
get_config_param(base_dir, 'instanceTitle') get_config_param(base_dir, 'instanceTitle')
emoji_form = \ emoji_form = \
html_header_with_external_style(css_filename, instance_title, None) html_header_with_external_style(css_filename, instance_title, None)
# show top banner
if nickname and domain and theme:
banner_file, _ = \
get_banner_file(base_dir, nickname, domain, theme)
emoji_form += \
'<header>\n' + \
'<a href="/users/' + nickname + '/search" title="' + \
translate['Search and follow'] + '" alt="' + \
translate['Search and follow'] + '" ' + \
'aria-flowto="containerHeader" tabindex="1" accesskey="' + \
access_keys['menuSearch'] + '">\n'
emoji_form += \
'<img loading="lazy" decoding="async" ' + \
'class="timeline-banner" alt="" ' + \
'src="/users/' + nickname + '/' + banner_file + '" /></a>\n' + \
'</header>\n'
emoji_form += '<center><h1>' + \ emoji_form += '<center><h1>' + \
translate['Emoji Search'] + \ translate['Emoji Search'] + \
'</h1></center>' '</h1></center>'
@ -229,7 +249,9 @@ def html_search_shared_items(translate: {},
domain_full: str, actor: str, domain_full: str, actor: str,
calling_domain: str, calling_domain: str,
shared_items_federated_domains: [], shared_items_federated_domains: [],
shares_file_type: str) -> str: shares_file_type: str,
nickname: str, domain: str, theme_name: str,
access_keys: {}) -> str:
"""Search results for shared items """Search results for shared items
""" """
curr_page = 1 curr_page = 1
@ -250,6 +272,24 @@ def html_search_shared_items(translate: {},
title_str = translate['Shared Items Search'] title_str = translate['Shared Items Search']
else: else:
title_str = translate['Wanted Items Search'] title_str = translate['Wanted Items Search']
# show top banner
if nickname and domain and theme_name:
banner_file, _ = \
get_banner_file(base_dir, nickname, domain, theme_name)
shared_items_form += \
'<header>\n' + \
'<a href="/users/' + nickname + '/search" title="' + \
translate['Search and follow'] + '" alt="' + \
translate['Search and follow'] + '" ' + \
'aria-flowto="containerHeader" tabindex="1" accesskey="' + \
access_keys['menuSearch'] + '">\n'
shared_items_form += \
'<img loading="lazy" decoding="async" ' + \
'class="timeline-banner" alt="" ' + \
'src="/users/' + nickname + '/' + banner_file + '" /></a>\n' + \
'</header>\n'
shared_items_form += \ shared_items_form += \
'<center><h1>' + \ '<center><h1>' + \
'<a href="' + actor + '/search">' + title_str + '</a></h1></center>' '<a href="' + actor + '/search">' + title_str + '</a></h1></center>'
@ -482,7 +522,9 @@ def html_search(translate: {}, base_dir: str, path: str, domain: str,
def html_skills_search(actor: str, translate: {}, base_dir: str, def html_skills_search(actor: str, translate: {}, base_dir: str,
skillsearch: str, instance_only: bool, skillsearch: str, instance_only: bool,
posts_per_page: int) -> str: posts_per_page: int,
nickname: str, domain: str, theme_name: str,
access_keys: {}) -> str:
"""Show a page containing search results for a skill """Show a page containing search results for a skill
""" """
if skillsearch.startswith('*'): if skillsearch.startswith('*'):
@ -576,6 +618,24 @@ def html_skills_search(actor: str, translate: {}, base_dir: str,
get_config_param(base_dir, 'instanceTitle') get_config_param(base_dir, 'instanceTitle')
skill_search_form = \ skill_search_form = \
html_header_with_external_style(css_filename, instance_title, None) html_header_with_external_style(css_filename, instance_title, None)
# show top banner
if nickname and domain and theme_name:
banner_file, _ = \
get_banner_file(base_dir, nickname, domain, theme_name)
skill_search_form += \
'<header>\n' + \
'<a href="/users/' + nickname + '/search" title="' + \
translate['Search and follow'] + '" alt="' + \
translate['Search and follow'] + '" ' + \
'aria-flowto="containerHeader" tabindex="1" accesskey="' + \
access_keys['menuSearch'] + '">\n'
skill_search_form += \
'<img loading="lazy" decoding="async" ' + \
'class="timeline-banner" alt="" ' + \
'src="/users/' + nickname + '/' + banner_file + '" /></a>\n' + \
'</header>\n'
skill_search_form += \ skill_search_form += \
'<center><h1><a href = "' + actor + '/search">' + \ '<center><h1><a href = "' + actor + '/search">' + \
translate['Skills search'] + ': ' + \ translate['Skills search'] + ': ' + \
@ -635,7 +695,7 @@ def html_history_search(translate: {}, base_dir: str,
cw_lists: {}, cw_lists: {},
lists_enabled: str, lists_enabled: str,
timezone: str, bold_reading: bool, timezone: str, bold_reading: bool,
dogwhistles: {}) -> str: dogwhistles: {}, access_keys: {}) -> str:
"""Show a page containing search results for your post history """Show a page containing search results for your post history
""" """
if historysearch.startswith("'"): if historysearch.startswith("'"):
@ -663,6 +723,22 @@ def html_history_search(translate: {}, base_dir: str,
if box_name == 'bookmarks': if box_name == 'bookmarks':
history_search_title = '🔍 ' + translate['Bookmarks'] history_search_title = '🔍 ' + translate['Bookmarks']
if nickname and domain and theme_name:
banner_file, _ = \
get_banner_file(base_dir, nickname, domain, theme_name)
history_search_form += \
'<header>\n' + \
'<a href="/users/' + nickname + '/search" title="' + \
translate['Search and follow'] + '" alt="' + \
translate['Search and follow'] + '" ' + \
'aria-flowto="containerHeader" tabindex="1" accesskey="' + \
access_keys['menuSearch'] + '">\n'
history_search_form += \
'<img loading="lazy" decoding="async" ' + \
'class="timeline-banner" alt="" ' + \
'src="/users/' + nickname + '/' + banner_file + '" /></a>\n' + \
'</header>\n'
history_search_form += \ history_search_form += \
'<center><h1><a href="' + actor + '/search">' + \ '<center><h1><a href="' + actor + '/search">' + \
history_search_title + '</a></h1></center>' history_search_title + '</a></h1></center>'