epicyon/webapp_search.py

1482 lines
61 KiB
Python
Raw Normal View History

2020-11-09 19:41:01 +00:00
__filename__ = "webapp_search.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2023-01-21 23:03:30 +00:00
__version__ = "1.4.0"
2020-11-09 19:41:01 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-11-09 19:41:01 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "Web Interface"
2020-11-09 19:41:01 +00:00
import os
from shutil import copyfile
import urllib.parse
2023-12-09 14:18:24 +00:00
from utils import get_url_from_post
2023-11-20 22:27:58 +00:00
from utils import date_from_string_format
from utils import get_attributed_to
2023-08-03 17:31:47 +00:00
from utils import get_actor_from_post_id
from utils import remove_html
2023-01-07 11:45:19 +00:00
from utils import harmless_markup
2023-01-05 15:56:49 +00:00
from utils import remove_id_ending
from utils import has_object_dict
2022-12-18 15:29:54 +00:00
from utils import acct_handle_dir
2021-12-26 11:29:40 +00:00
from utils import get_base_content_from_post
2021-12-26 18:46:43 +00:00
from utils import is_account_dir
2021-12-26 14:08:58 +00:00
from utils import get_config_param
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
2021-12-26 13:27:57 +00:00
from utils import is_editor
2021-12-26 15:13:34 +00:00
from utils import load_json
2021-12-27 22:19:18 +00:00
from utils import get_nickname_from_actor
2021-12-26 20:36:08 +00:00
from utils import locate_post
2021-12-28 14:41:10 +00:00
from utils import is_public_post
2021-12-27 15:52:08 +00:00
from utils import first_paragraph_from_string
2021-12-28 13:07:02 +00:00
from utils import search_box_posts
2021-12-26 18:32:02 +00:00
from utils import get_alt_path
2021-12-26 12:02:29 +00:00
from utils import acct_dir
2021-12-26 10:19:59 +00:00
from utils import local_actor_url
2023-01-02 10:24:35 +00:00
from utils import escape_text
2021-12-28 20:32:11 +00:00
from skills import no_of_actor_skills
from skills import get_skills_from_list
2021-12-29 21:55:09 +00:00
from categories import get_hashtag_category
from feeds import rss2tag_header
from feeds import rss2tag_footer
2022-08-23 18:32:17 +00:00
from webapp_utils import get_banner_file
from webapp_utils import html_common_emoji
2021-12-29 21:55:09 +00:00
from webapp_utils import set_custom_background
from webapp_utils import html_keyboard_navigation
from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer
from webapp_utils import get_search_banner_file
from webapp_utils import html_post_separator
from webapp_utils import html_search_result_share
from webapp_post import individual_post_as_html
from webapp_hashtagswarm import html_hash_tag_swarm
2022-08-22 15:39:24 +00:00
from maps import html_hashtag_maps
2023-08-13 09:58:02 +00:00
from session import get_json_valid
from session import get_json
2021-12-29 21:55:09 +00:00
2022-09-03 12:22:50 +00:00
def html_search_emoji(translate: {}, base_dir: str, search_str: str,
nickname: str, domain: str, theme: str,
access_keys: {}) -> str:
2020-11-09 19:41:01 +00:00
"""Search results for emoji
"""
# emoji.json is generated so that it can be customized and the changes
# will be retained even if default_emoji.json is subsequently updated
2021-12-25 16:17:53 +00:00
if not os.path.isfile(base_dir + '/emoji/emoji.json'):
copyfile(base_dir + '/emoji/default_emoji.json',
base_dir + '/emoji/emoji.json')
2020-11-09 19:41:01 +00:00
2022-01-04 13:19:00 +00:00
search_str = search_str.lower().replace(':', '').strip('\n').strip('\r')
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon-profile.css'
2021-12-25 16:17:53 +00:00
if os.path.isfile(base_dir + '/epicyon.css'):
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon.css'
2020-11-09 19:41:01 +00:00
2022-01-04 13:19:00 +00:00
emoji_lookup_filename = base_dir + '/emoji/emoji.json'
2022-01-01 20:36:56 +00:00
custom_emoji_lookup_filename = base_dir + '/emojicustom/emoji.json'
# create header
2022-01-04 13:19:00 +00:00
instance_title = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
2022-01-04 13:19:00 +00:00
emoji_form = \
html_header_with_external_style(css_filename, instance_title, None)
2022-09-03 12:22:50 +00:00
# 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'
2022-01-04 13:19:00 +00:00
emoji_form += '<center><h1>' + \
translate['Emoji Search'] + \
'</h1></center>'
2020-11-09 19:41:01 +00:00
# does the lookup file exist?
2022-01-04 13:19:00 +00:00
if not os.path.isfile(emoji_lookup_filename):
emoji_form += '<center><h5>' + \
2021-11-01 23:42:37 +00:00
translate['No results'] + '</h5></center>'
2022-01-04 13:19:00 +00:00
emoji_form += html_footer()
return emoji_form
2022-01-04 13:19:00 +00:00
emoji_json = load_json(emoji_lookup_filename)
if emoji_json:
2022-01-01 20:36:56 +00:00
if os.path.isfile(custom_emoji_lookup_filename):
custom_emoji_json = load_json(custom_emoji_lookup_filename)
if custom_emoji_json:
2022-01-04 13:19:00 +00:00
emoji_json = dict(emoji_json, **custom_emoji_json)
2021-11-01 23:46:31 +00:00
results = {}
2022-01-04 13:19:00 +00:00
for emoji_name, filename in emoji_json.items():
if search_str in emoji_name:
results[emoji_name] = filename + '.png'
for emoji_name, filename in emoji_json.items():
if emoji_name in search_str:
results[emoji_name] = filename + '.png'
2021-11-01 23:46:31 +00:00
if not results:
2022-01-04 13:19:00 +00:00
emoji_form += '<center><h5>' + \
2021-11-01 23:46:31 +00:00
translate['No results'] + '</h5></center>'
2022-01-04 13:19:00 +00:00
heading_shown = False
emoji_form += '<center>'
msg_str1 = translate['Copy the text then paste it into your post']
2022-03-28 08:47:53 +00:00
msg_str2 = ':<img loading="lazy" decoding="async" ' + \
'class="searchEmoji" src="/emoji/'
2022-01-04 13:19:00 +00:00
for emoji_name, filename in results.items():
2021-12-25 16:17:53 +00:00
if not os.path.isfile(base_dir + '/emoji/' + filename):
if not os.path.isfile(base_dir + '/emojicustom/' + filename):
2021-11-01 23:17:14 +00:00
continue
2022-01-04 13:19:00 +00:00
if not heading_shown:
emoji_form += \
'<center><h5>' + msg_str1 + '</h5></center>'
heading_shown = True
emoji_form += \
'<h3>:' + emoji_name + msg_str2 + filename + '"/></h3>'
emoji_form += '</center>'
2022-01-04 13:19:00 +00:00
emoji_form += html_footer()
return emoji_form
2020-11-09 19:41:01 +00:00
2022-01-04 13:19:00 +00:00
def _match_shared_item(search_str_lower_list: [],
shared_item: {}) -> bool:
"""Returns true if the shared item matches search criteria
"""
2022-01-04 13:19:00 +00:00
for search_substr in search_str_lower_list:
search_substr = search_substr.strip()
if shared_item.get('location'):
if search_substr in shared_item['location'].lower():
2021-07-27 19:23:55 +00:00
return True
2022-01-04 13:19:00 +00:00
if search_substr in shared_item['summary'].lower():
return True
2022-01-04 13:19:00 +00:00
if search_substr in shared_item['displayName'].lower():
return True
2022-01-04 13:19:00 +00:00
if search_substr in shared_item['category'].lower():
return True
return False
2021-12-29 21:55:09 +00:00
def _html_search_result_share_page(actor: str, domain_full: str,
2022-01-04 13:19:00 +00:00
calling_domain: str, page_number: int,
search_str_lower: str, translate: {},
2021-12-29 21:55:09 +00:00
previous: bool) -> str:
2021-07-27 09:57:52 +00:00
"""Returns the html for the previous button on shared items search results
"""
2022-01-04 13:19:00 +00:00
post_actor = get_alt_path(actor, domain_full, calling_domain)
2021-07-27 09:57:52 +00:00
# previous page link, needs to be a POST
2021-07-27 10:05:57 +00:00
if previous:
2022-01-04 13:19:00 +00:00
page_number -= 1
title_str = translate['Page up']
image_url = 'pageup.png'
2021-07-27 10:05:57 +00:00
else:
2022-01-04 13:19:00 +00:00
page_number += 1
title_str = translate['Page down']
image_url = 'pagedown.png'
shared_items_form = \
'<form method="POST" action="' + post_actor + '/searchhandle?page=' + \
str(page_number) + '">\n'
shared_items_form += \
2021-07-27 09:57:52 +00:00
' <input type="hidden" ' + 'name="actor" value="' + actor + '">\n'
2022-01-04 13:19:00 +00:00
shared_items_form += \
2021-07-27 09:57:52 +00:00
' <input type="hidden" ' + 'name="searchtext" value="' + \
2022-01-04 13:19:00 +00:00
search_str_lower + '"><br>\n'
shared_items_form += \
2021-07-27 09:57:52 +00:00
' <center>\n' + ' <a href="' + actor + \
'" type="submit" name="submitSearch">\n'
2022-01-04 13:19:00 +00:00
shared_items_form += \
2022-03-28 08:47:53 +00:00
' <img loading="lazy" decoding="async" ' + \
'class="pageicon" src="/icons' + \
2022-01-04 13:19:00 +00:00
'/' + image_url + '" title="' + title_str + \
'" alt="' + title_str + '"/></a>\n'
shared_items_form += ' </center>\n'
shared_items_form += '</form>\n'
return shared_items_form
2021-07-27 09:57:52 +00:00
2022-01-04 13:19:00 +00:00
def _html_shares_result(base_dir: str, shares_json: {}, page_number: int,
results_per_page: int,
search_str_lower_list: [], curr_page: int, ctr: int,
2021-12-29 21:55:09 +00:00
calling_domain: str, http_prefix: str,
2022-01-04 13:19:00 +00:00
domain_full: str, contact_nickname: str, actor: str,
results_exist: bool, search_str_lower: str,
translate: {},
shares_file_type: str) -> (bool, int, int, str):
"""Result for shared items search
"""
2022-01-04 13:19:00 +00:00
shared_items_form = ''
if curr_page > page_number:
return results_exist, curr_page, ctr, shared_items_form
2022-01-04 13:19:00 +00:00
for name, shared_item in shares_json.items():
if _match_shared_item(search_str_lower_list, shared_item):
if curr_page == page_number:
# show individual search result
2022-01-04 13:19:00 +00:00
shared_items_form += \
html_search_result_share(base_dir, shared_item, translate,
2021-12-29 21:55:09 +00:00
http_prefix, domain_full,
2022-01-04 13:19:00 +00:00
contact_nickname,
name, actor, shares_file_type,
shared_item['category'], False)
2022-01-04 13:19:00 +00:00
if not results_exist and curr_page > 1:
# show the previous page button
2022-01-04 13:19:00 +00:00
shared_items_form += \
2021-12-29 21:55:09 +00:00
_html_search_result_share_page(actor, domain_full,
calling_domain,
2022-01-04 13:19:00 +00:00
page_number,
search_str_lower,
2021-12-29 21:55:09 +00:00
translate, True)
2022-01-04 13:19:00 +00:00
results_exist = True
ctr += 1
2022-01-04 13:19:00 +00:00
if ctr >= results_per_page:
curr_page += 1
if curr_page > page_number:
# show the next page button
2022-01-04 13:19:00 +00:00
shared_items_form += \
2021-12-29 21:55:09 +00:00
_html_search_result_share_page(actor, domain_full,
calling_domain,
2022-01-04 13:19:00 +00:00
page_number,
search_str_lower,
2021-12-29 21:55:09 +00:00
translate, False)
2022-01-04 13:19:00 +00:00
return results_exist, curr_page, ctr, shared_items_form
ctr = 0
2022-01-04 13:19:00 +00:00
return results_exist, curr_page, ctr, shared_items_form
2022-06-01 17:45:59 +00:00
def html_search_shared_items(translate: {},
2022-01-04 13:19:00 +00:00
base_dir: str, search_str: str,
page_number: int,
results_per_page: int,
2021-12-29 21:55:09 +00:00
http_prefix: str,
domain_full: str, actor: str,
calling_domain: str,
shared_items_federated_domains: [],
2022-09-03 13:43:52 +00:00
shares_file_type: str,
nickname: str, domain: str, theme_name: str,
access_keys: {}) -> str:
2020-11-09 19:41:01 +00:00
"""Search results for shared items
"""
2022-01-04 13:19:00 +00:00
curr_page = 1
2020-11-09 19:41:01 +00:00
ctr = 0
2022-01-04 13:19:00 +00:00
shared_items_form = ''
search_str_lower = urllib.parse.unquote(search_str)
search_str_lower = search_str_lower.lower().strip('\n').strip('\r')
search_str_lower_list = search_str_lower.split('+')
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon-profile.css'
2021-12-25 16:17:53 +00:00
if os.path.isfile(base_dir + '/epicyon.css'):
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon.css'
2020-11-09 19:41:01 +00:00
2022-01-04 13:19:00 +00:00
instance_title = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
2022-01-04 13:19:00 +00:00
shared_items_form = \
html_header_with_external_style(css_filename, instance_title, None)
if shares_file_type == 'shares':
title_str = translate['Shared Items Search']
else:
2022-01-04 13:19:00 +00:00
title_str = translate['Wanted Items Search']
2022-09-03 13:43:52 +00:00
# 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'
2022-01-04 13:19:00 +00:00
shared_items_form += \
2020-12-07 12:06:30 +00:00
'<center><h1>' + \
2022-01-04 13:19:00 +00:00
'<a href="' + actor + '/search">' + title_str + '</a></h1></center>'
results_exist = False
for _, dirs, files in os.walk(base_dir + '/accounts'):
for handle in dirs:
2021-12-26 18:46:43 +00:00
if not is_account_dir(handle):
continue
2022-01-04 13:19:00 +00:00
contact_nickname = handle.split('@')[0]
2022-12-18 15:29:54 +00:00
shares_filename = acct_handle_dir(base_dir, handle) + \
2022-01-04 13:19:00 +00:00
'/' + shares_file_type + '.json'
if not os.path.isfile(shares_filename):
continue
2020-11-09 19:41:01 +00:00
2022-01-04 13:19:00 +00:00
shares_json = load_json(shares_filename)
if not shares_json:
continue
2020-11-09 19:41:01 +00:00
2022-01-04 13:19:00 +00:00
(results_exist, curr_page, ctr,
result_str) = _html_shares_result(base_dir, shares_json,
page_number,
results_per_page,
search_str_lower_list,
curr_page, ctr,
calling_domain, http_prefix,
domain_full,
contact_nickname,
actor, results_exist,
search_str_lower, translate,
shares_file_type)
shared_items_form += result_str
if curr_page > page_number:
break
2020-12-13 22:13:45 +00:00
break
# search federated shared items
2022-01-04 13:19:00 +00:00
if shares_file_type == 'shares':
catalogs_dir = base_dir + '/cache/catalogs'
else:
2022-01-04 13:19:00 +00:00
catalogs_dir = base_dir + '/cache/wantedItems'
if curr_page <= page_number and os.path.isdir(catalogs_dir):
for _, dirs, files in os.walk(catalogs_dir):
for fname in files:
if '#' in fname:
continue
2022-01-04 13:19:00 +00:00
if not fname.endswith('.' + shares_file_type + '.json'):
continue
2022-01-04 13:19:00 +00:00
federated_domain = fname.split('.')[0]
if federated_domain not in shared_items_federated_domains:
continue
2022-01-04 13:19:00 +00:00
shares_filename = catalogs_dir + '/' + fname
shares_json = load_json(shares_filename)
if not shares_json:
continue
2022-01-04 13:19:00 +00:00
(results_exist, curr_page, ctr,
result_str) = _html_shares_result(base_dir, shares_json,
page_number,
results_per_page,
search_str_lower_list,
curr_page, ctr,
calling_domain, http_prefix,
domain_full,
contact_nickname,
actor, results_exist,
search_str_lower, translate,
shares_file_type)
shared_items_form += result_str
if curr_page > page_number:
break
break
2022-01-04 13:19:00 +00:00
if not results_exist:
shared_items_form += \
'<center><h5>' + translate['No results'] + '</h5></center>\n'
2022-01-04 13:19:00 +00:00
shared_items_form += html_footer()
return shared_items_form
2020-11-09 19:41:01 +00:00
2022-06-01 17:45:59 +00:00
def html_search_emoji_text_entry(translate: {},
2021-12-29 21:55:09 +00:00
base_dir: str, path: str) -> str:
2020-11-09 19:41:01 +00:00
"""Search for an emoji by name
"""
# emoji.json is generated so that it can be customized and the changes
# will be retained even if default_emoji.json is subsequently updated
2021-12-25 16:17:53 +00:00
if not os.path.isfile(base_dir + '/emoji/emoji.json'):
copyfile(base_dir + '/emoji/default_emoji.json',
base_dir + '/emoji/emoji.json')
2020-11-09 19:41:01 +00:00
actor = path.replace('/search', '')
2021-12-29 21:55:09 +00:00
set_custom_background(base_dir, 'search-background', 'follow-background')
2020-11-09 19:41:01 +00:00
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon-follow.css'
2021-12-25 16:17:53 +00:00
if os.path.isfile(base_dir + '/follow.css'):
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/follow.css'
2020-11-09 19:41:01 +00:00
2022-01-04 13:19:00 +00:00
instance_title = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
2022-01-04 13:19:00 +00:00
emoji_str = \
html_header_with_external_style(css_filename, instance_title, None)
emoji_str += '<div class="follow">\n'
emoji_str += ' <div class="followAvatar">\n'
emoji_str += ' <center>\n'
emoji_str += \
2020-11-09 19:41:01 +00:00
' <p class="followText">' + \
translate['Enter an emoji name to search for'] + '</p>\n'
2022-01-04 13:19:00 +00:00
emoji_str += ' <form role="search" method="POST" action="' + \
2020-11-09 19:41:01 +00:00
actor + '/searchhandleemoji">\n'
2022-01-04 13:19:00 +00:00
emoji_str += ' <input type="hidden" name="actor" value="' + \
2020-11-09 19:41:01 +00:00
actor + '">\n'
2022-01-04 13:19:00 +00:00
emoji_str += ' <input type="text" name="searchtext" autofocus><br>\n'
emoji_str += \
2020-11-09 19:41:01 +00:00
' <button type="submit" class="button" name="submitSearch">' + \
2022-06-03 12:58:06 +00:00
translate['Search'] + '</button>\n'
2022-01-04 13:19:00 +00:00
emoji_str += ' </form>\n'
emoji_str += ' </center>\n'
emoji_str += ' </div>\n'
emoji_str += ' <center>\n'
emoji_str += ' <div class="container"><p>\n'
emoji_str += html_common_emoji(base_dir, 16) + '\n'
emoji_str += ' </p></div>\n'
emoji_str += ' </center>\n'
2022-04-18 22:49:05 +00:00
emoji_str += '</div>\n'
2022-01-04 13:19:00 +00:00
emoji_str += html_footer()
return emoji_str
2020-11-09 19:41:01 +00:00
2022-06-01 17:45:59 +00:00
def html_search(translate: {}, base_dir: str, path: str, domain: str,
2021-12-31 23:50:29 +00:00
default_timeline: str, theme: str,
2021-12-31 21:18:12 +00:00
text_mode_banner: str, access_keys: {}) -> str:
2020-11-09 19:41:01 +00:00
"""Search called from the timeline icon
"""
actor = path.replace('/search', '')
2022-01-04 13:19:00 +00:00
search_nickname = get_nickname_from_actor(actor)
if not search_nickname:
return ''
2020-11-09 19:41:01 +00:00
2021-12-29 21:55:09 +00:00
set_custom_background(base_dir, 'search-background', 'follow-background')
2020-11-09 19:41:01 +00:00
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon-search.css'
2021-12-25 16:17:53 +00:00
if os.path.isfile(base_dir + '/search.css'):
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/search.css'
2020-11-09 19:41:01 +00:00
2022-01-04 13:19:00 +00:00
instance_title = get_config_param(base_dir, 'instanceTitle')
follow_str = \
html_header_with_external_style(css_filename, instance_title, None)
2020-11-09 19:41:01 +00:00
2022-08-23 19:50:55 +00:00
# set a search banner
search_banner_filename = \
acct_dir(base_dir, search_nickname, domain) + \
'/search_banner.png'
if not os.path.isfile(search_banner_filename):
if os.path.isfile(base_dir +
'/theme/' + theme + '/search_banner.png'):
copyfile(base_dir +
'/theme/' + theme + '/search_banner.png',
search_banner_filename)
2020-11-09 19:41:01 +00:00
# show a banner above the search box
2022-01-04 13:19:00 +00:00
search_banner_file, search_banner_filename = \
get_search_banner_file(base_dir, search_nickname, domain, theme)
text_mode_banner_str = html_keyboard_navigation(text_mode_banner, {}, {})
if text_mode_banner_str is None:
text_mode_banner_str = ''
if os.path.isfile(search_banner_filename):
timeline_key = access_keys['menuTimeline']
users_path = '/users/' + search_nickname
follow_str += \
'<header>\n' + text_mode_banner_str + \
'<a href="' + users_path + '/' + default_timeline + '" title="' + \
2020-11-09 19:41:01 +00:00
translate['Switch to timeline view'] + '" alt="' + \
2021-04-23 09:15:53 +00:00
translate['Switch to timeline view'] + '" ' + \
2022-01-04 13:19:00 +00:00
'accesskey="' + timeline_key + '">\n'
2022-03-28 08:47:53 +00:00
follow_str += '<img loading="lazy" decoding="async" ' + \
'class="timeline-banner" src="' + \
2022-01-04 13:19:00 +00:00
users_path + '/' + search_banner_file + '" alt="" /></a>\n' + \
2020-12-27 16:57:15 +00:00
'</header>\n'
2020-11-09 19:41:01 +00:00
# show the search box
2022-01-04 13:19:00 +00:00
follow_str += '<div class="follow">\n'
follow_str += ' <div class="followAvatar">\n'
follow_str += ' <center>\n'
follow_str += \
2021-08-10 09:10:25 +00:00
' <p class="followText">' + translate['Search screen text'] + '</p>\n'
2022-01-04 13:19:00 +00:00
follow_str += ' <form role="search" method="POST" ' + \
2020-11-09 19:41:01 +00:00
'accept-charset="UTF-8" action="' + actor + '/searchhandle">\n'
2022-01-04 13:19:00 +00:00
follow_str += \
2020-11-09 19:41:01 +00:00
' <input type="hidden" name="actor" value="' + actor + '">\n'
2022-01-04 13:19:00 +00:00
follow_str += ' <input type="text" name="searchtext" autofocus><br>\n'
submit_key = access_keys['submitButton']
follow_str += ' <button type="submit" class="button" ' + \
'name="submitSearch" accesskey="' + submit_key + '">' + \
2022-06-03 12:58:06 +00:00
translate['Search'] + '</button>\n'
2022-01-04 13:19:00 +00:00
follow_str += ' </form>\n'
2021-10-20 13:33:34 +00:00
2022-01-04 13:19:00 +00:00
cached_hashtag_swarm_filename = \
acct_dir(base_dir, search_nickname, domain) + '/.hashtagSwarm'
swarm_str = ''
if os.path.isfile(cached_hashtag_swarm_filename):
2021-10-20 13:33:34 +00:00
try:
2022-06-09 14:46:30 +00:00
with open(cached_hashtag_swarm_filename, 'r',
encoding='utf-8') as fp_swarm:
2022-01-04 13:19:00 +00:00
swarm_str = fp_swarm.read()
2021-11-25 22:22:54 +00:00
except OSError:
2021-12-29 21:55:09 +00:00
print('EX: html_search unable to read cached hashtag swarm ' +
2022-01-04 13:19:00 +00:00
cached_hashtag_swarm_filename)
if not swarm_str:
swarm_str = html_hash_tag_swarm(base_dir, actor, translate)
if swarm_str:
2021-10-20 14:20:27 +00:00
try:
2022-06-09 14:46:30 +00:00
with open(cached_hashtag_swarm_filename, 'w+',
encoding='utf-8') as fp_hash:
2022-01-04 13:19:00 +00:00
fp_hash.write(swarm_str)
2021-11-25 22:22:54 +00:00
except OSError:
2021-12-29 21:55:09 +00:00
print('EX: html_search unable to save cached hashtag swarm ' +
2022-01-04 13:19:00 +00:00
cached_hashtag_swarm_filename)
2021-10-20 13:33:34 +00:00
2023-02-12 20:30:07 +00:00
follow_str += ' <p class="hashtagswarm">' + swarm_str + '</p><br>\n'
2022-01-04 13:19:00 +00:00
follow_str += ' </center>\n'
follow_str += ' </div>\n'
follow_str += '</div>\n'
follow_str += html_footer()
return follow_str
2020-11-09 19:41:01 +00:00
2022-06-01 17:45:59 +00:00
def html_skills_search(actor: str, translate: {}, base_dir: str,
2022-01-04 13:19:00 +00:00
skillsearch: str, instance_only: bool,
2022-09-03 13:21:06 +00:00
posts_per_page: int,
nickname: str, domain: str, theme_name: str,
access_keys: {}) -> str:
2020-11-19 14:02:16 +00:00
"""Show a page containing search results for a skill
2020-11-09 19:41:01 +00:00
"""
2020-11-19 14:02:16 +00:00
if skillsearch.startswith('*'):
skillsearch = skillsearch[1:].strip()
2020-11-09 19:41:01 +00:00
2020-11-19 14:02:16 +00:00
skillsearch = skillsearch.lower().strip('\n').strip('\r')
results = []
# search instance accounts
2022-01-04 13:19:00 +00:00
for subdir, _, files in os.walk(base_dir + '/accounts/'):
for fname in files:
if not fname.endswith('.json'):
2020-11-09 19:41:01 +00:00
continue
2022-01-04 13:19:00 +00:00
if not is_account_dir(fname):
2021-04-21 16:09:56 +00:00
continue
2022-01-04 13:19:00 +00:00
actor_filename = os.path.join(subdir, fname)
actor_json = load_json(actor_filename)
2022-05-30 18:33:51 +00:00
if not actor_json:
continue
if actor_json.get('id') and \
no_of_actor_skills(actor_json) > 0 and \
actor_json.get('name') and \
actor_json.get('icon'):
actor = actor_json['id']
actor_skills_list = actor_json['hasOccupation']['skills']
skills = get_skills_from_list(actor_skills_list)
for skill_name, skill_level in skills.items():
skill_name = skill_name.lower()
if not (skill_name in skillsearch or
skillsearch in skill_name):
continue
skill_level_str = str(skill_level)
if skill_level < 100:
skill_level_str = '0' + skill_level_str
if skill_level < 10:
skill_level_str = '0' + skill_level_str
2023-12-09 14:18:24 +00:00
url_str = get_url_from_post(actor_json['icon']['url'])
icon_url = remove_html(url_str)
2022-05-30 18:33:51 +00:00
index_str = \
skill_level_str + ';' + actor + ';' + \
actor_json['name'] + \
';' + icon_url
2022-05-30 18:33:51 +00:00
if index_str not in results:
results.append(index_str)
2020-12-13 22:13:45 +00:00
break
2022-01-04 13:19:00 +00:00
if not instance_only:
2020-11-19 14:02:16 +00:00
# search actor cache
2022-01-04 13:19:00 +00:00
for subdir, _, files in os.walk(base_dir + '/cache/actors/'):
for fname in files:
if not fname.endswith('.json'):
2020-11-19 14:02:16 +00:00
continue
2022-01-04 13:19:00 +00:00
if not is_account_dir(fname):
2021-04-21 16:09:56 +00:00
continue
2022-01-04 13:19:00 +00:00
actor_filename = os.path.join(subdir, fname)
cached_actor_json = load_json(actor_filename)
2022-05-30 18:33:51 +00:00
if not cached_actor_json:
continue
if cached_actor_json.get('actor'):
actor_json = cached_actor_json['actor']
if actor_json.get('id') and \
no_of_actor_skills(actor_json) > 0 and \
actor_json.get('name') and \
actor_json.get('icon'):
actor = actor_json['id']
actor_skills_list = \
actor_json['hasOccupation']['skills']
skills = get_skills_from_list(actor_skills_list)
for skill_name, skill_level in skills.items():
skill_name = skill_name.lower()
if not (skill_name in skillsearch or
skillsearch in skill_name):
continue
skill_level_str = str(skill_level)
if skill_level < 100:
skill_level_str = '0' + skill_level_str
if skill_level < 10:
skill_level_str = '0' + skill_level_str
2023-12-09 14:18:24 +00:00
url_str = \
get_url_from_post(actor_json['icon']['url'])
icon_url = remove_html(url_str)
2022-05-30 18:33:51 +00:00
index_str = \
skill_level_str + ';' + actor + ';' + \
actor_json['name'] + \
';' + icon_url
2022-05-30 18:33:51 +00:00
if index_str not in results:
results.append(index_str)
2020-12-13 22:13:45 +00:00
break
2020-11-19 14:02:16 +00:00
results.sort(reverse=True)
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon-profile.css'
2021-12-25 16:17:53 +00:00
if os.path.isfile(base_dir + '/epicyon.css'):
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon.css'
2020-11-19 14:02:16 +00:00
2022-01-04 13:19:00 +00:00
instance_title = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
2022-01-04 13:19:00 +00:00
skill_search_form = \
html_header_with_external_style(css_filename, instance_title, None)
2022-09-03 13:21:06 +00:00
# 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'
2022-01-04 13:19:00 +00:00
skill_search_form += \
2020-12-07 12:28:13 +00:00
'<center><h1><a href = "' + actor + '/search">' + \
2020-12-07 12:25:32 +00:00
translate['Skills search'] + ': ' + \
skillsearch + \
'</a></h1></center>'
2020-11-19 14:02:16 +00:00
if len(results) == 0:
2022-01-04 13:19:00 +00:00
skill_search_form += \
2020-11-19 14:02:16 +00:00
'<center><h5>' + translate['No results'] + \
'</h5></center>'
else:
2022-01-04 13:19:00 +00:00
skill_search_form += '<center>'
2020-11-19 14:02:16 +00:00
ctr = 0
2022-01-04 13:19:00 +00:00
for skill_match in results:
skill_match_fields = skill_match.split(';')
if len(skill_match_fields) != 4:
2020-11-09 19:41:01 +00:00
continue
2022-01-04 13:19:00 +00:00
actor = skill_match_fields[1]
actor_name = skill_match_fields[2]
avatar_url = skill_match_fields[3]
skill_search_form += \
2020-11-19 14:02:16 +00:00
'<div class="search-result""><a href="' + \
actor + '/skills">'
2022-01-04 13:19:00 +00:00
skill_search_form += \
2022-03-28 08:47:53 +00:00
'<img loading="lazy" decoding="async" src="' + avatar_url + \
2022-01-04 13:19:00 +00:00
'" alt="" /><span class="search-result-text">' + actor_name + \
2020-11-19 14:02:16 +00:00
'</span></a></div>'
ctr += 1
2022-01-04 13:19:00 +00:00
if ctr >= posts_per_page:
2020-11-19 14:02:16 +00:00
break
2022-01-04 13:19:00 +00:00
skill_search_form += '</center>'
skill_search_form += html_footer()
return skill_search_form
2020-11-09 19:41:01 +00:00
2020-11-19 14:02:16 +00:00
2022-06-01 17:45:59 +00:00
def html_history_search(translate: {}, base_dir: str,
2021-12-29 21:55:09 +00:00
http_prefix: str,
nickname: str, domain: str,
historysearch: str,
2022-01-04 13:19:00 +00:00
posts_per_page: int, page_number: int,
2021-12-29 21:55:09 +00:00
project_version: str,
recent_posts_cache: {},
max_recent_posts: int,
session,
cached_webfingers,
person_cache: {},
port: int,
yt_replace_domain: str,
twitter_replacement_domain: str,
show_published_date_only: bool,
peertube_instances: [],
allow_local_network_access: bool,
2022-05-30 18:33:51 +00:00
theme_name: str, box_name: str,
2021-12-29 21:55:09 +00:00
system_language: str,
max_like_count: int,
signing_priv_key_pem: str,
cw_lists: {},
2022-02-25 19:12:40 +00:00
lists_enabled: str,
2022-07-05 14:40:26 +00:00
timezone: str, bold_reading: bool,
dogwhistles: {}, access_keys: {},
2023-01-13 15:04:48 +00:00
min_images_for_accounts: [],
buy_sites: {}) -> str:
2020-11-19 14:02:16 +00:00
"""Show a page containing search results for your post history
"""
if historysearch.startswith("'"):
2020-11-19 14:02:16 +00:00
historysearch = historysearch[1:].strip()
historysearch = historysearch.lower().strip('\n').strip('\r')
2022-01-04 13:19:00 +00:00
box_filenames = \
2021-12-28 13:07:02 +00:00
search_box_posts(base_dir, nickname, domain,
2022-05-30 18:33:51 +00:00
historysearch, posts_per_page, box_name)
2020-11-19 14:02:16 +00:00
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon-profile.css'
2021-12-25 16:17:53 +00:00
if os.path.isfile(base_dir + '/epicyon.css'):
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon.css'
2020-11-19 14:02:16 +00:00
2022-01-04 13:19:00 +00:00
instance_title = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
2022-01-04 13:19:00 +00:00
history_search_form = \
html_header_with_external_style(css_filename, instance_title, None)
2020-11-19 14:02:16 +00:00
# add the page title
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2021-12-26 10:19:59 +00:00
actor = local_actor_url(http_prefix, nickname, domain_full)
2022-01-04 13:19:00 +00:00
history_search_title = '🔍 ' + translate['Your Posts']
2022-05-30 18:33:51 +00:00
if box_name == 'bookmarks':
2022-01-04 13:19:00 +00:00
history_search_title = '🔍 ' + translate['Bookmarks']
2021-05-03 22:31:06 +00:00
2022-09-03 12:50:08 +00:00
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'
2022-01-04 13:19:00 +00:00
history_search_form += \
2020-12-07 12:16:30 +00:00
'<center><h1><a href="' + actor + '/search">' + \
2022-01-04 13:19:00 +00:00
history_search_title + '</a></h1></center>'
2020-11-19 14:02:16 +00:00
2022-01-04 13:19:00 +00:00
if len(box_filenames) == 0:
history_search_form += \
2020-11-19 14:02:16 +00:00
'<center><h5>' + translate['No results'] + \
'</h5></center>'
2022-12-23 21:51:57 +00:00
history_search_form += html_footer()
2022-01-04 13:19:00 +00:00
return history_search_form
2020-11-19 14:02:16 +00:00
2022-01-04 13:19:00 +00:00
separator_str = html_post_separator(base_dir, None)
2020-11-19 14:02:16 +00:00
# ensure that the page number is in bounds
2022-01-04 13:19:00 +00:00
if not page_number:
page_number = 1
elif page_number < 1:
page_number = 1
2020-11-19 14:02:16 +00:00
# get the start end end within the index file
2022-01-04 13:19:00 +00:00
start_index = int((page_number - 1) * posts_per_page)
end_index = start_index + posts_per_page
no_of_box_filenames = len(box_filenames)
if end_index >= no_of_box_filenames and no_of_box_filenames > 0:
end_index = no_of_box_filenames - 1
index = start_index
minimize_all_images = False
if nickname in min_images_for_accounts:
minimize_all_images = True
2022-01-04 13:19:00 +00:00
while index <= end_index:
post_filename = box_filenames[index]
2021-12-26 23:41:34 +00:00
if not post_filename:
2020-11-19 14:02:16 +00:00
index += 1
continue
2021-12-26 23:41:34 +00:00
post_json_object = load_json(post_filename)
2021-12-25 22:09:19 +00:00
if not post_json_object:
2020-11-19 14:02:16 +00:00
index += 1
continue
2022-01-04 13:19:00 +00:00
show_individual_post_icons = True
2021-12-25 21:29:53 +00:00
allow_deletion = False
2022-01-04 13:19:00 +00:00
post_str = \
2021-12-29 21:55:09 +00:00
individual_post_as_html(signing_priv_key_pem,
True, recent_posts_cache,
max_recent_posts,
translate, None,
base_dir, session, cached_webfingers,
person_cache,
nickname, domain, port,
post_json_object,
None, True, allow_deletion,
http_prefix, project_version,
'search',
yt_replace_domain,
twitter_replacement_domain,
show_published_date_only,
peertube_instances,
allow_local_network_access,
theme_name, system_language,
max_like_count,
2022-01-04 13:19:00 +00:00
show_individual_post_icons,
show_individual_post_icons,
2021-12-29 21:55:09 +00:00
False, False, False, False,
2022-02-25 19:12:40 +00:00
cw_lists, lists_enabled,
2022-07-05 14:40:26 +00:00
timezone, False, bold_reading,
dogwhistles,
2023-01-13 15:04:48 +00:00
minimize_all_images, None,
buy_sites)
2022-01-04 13:19:00 +00:00
if post_str:
history_search_form += separator_str + post_str
2020-11-19 14:02:16 +00:00
index += 1
2022-01-04 13:19:00 +00:00
history_search_form += html_footer()
return history_search_form
2020-11-09 19:41:01 +00:00
2022-06-01 17:45:59 +00:00
def html_hashtag_search(nickname: str, domain: str, port: int,
2021-12-29 21:55:09 +00:00
recent_posts_cache: {}, max_recent_posts: int,
translate: {},
2022-01-04 13:19:00 +00:00
base_dir: str, hashtag: str, page_number: int,
posts_per_page: int,
2021-12-29 21:55:09 +00:00
session, cached_webfingers: {}, person_cache: {},
http_prefix: str, project_version: str,
yt_replace_domain: str,
twitter_replacement_domain: str,
show_published_date_only: bool,
peertube_instances: [],
allow_local_network_access: bool,
theme_name: str, system_language: str,
max_like_count: int,
signing_priv_key_pem: str,
2022-02-25 19:12:40 +00:00
cw_lists: {}, lists_enabled: str,
2022-07-05 14:40:26 +00:00
timezone: str, bold_reading: bool,
2022-08-23 18:32:17 +00:00
dogwhistles: {}, map_format: str,
access_keys: {}, box_name: str,
2023-01-13 15:04:48 +00:00
min_images_for_accounts: [],
buy_sites: {}) -> str:
2020-11-09 19:41:01 +00:00
"""Show a page containing search results for a hashtag
2021-07-21 13:43:04 +00:00
or after selecting a hashtag from the swarm
2020-11-09 19:41:01 +00:00
"""
if hashtag.startswith('#'):
hashtag = hashtag[1:]
hashtag = urllib.parse.unquote(hashtag)
2022-01-04 13:19:00 +00:00
hashtag_index_file = base_dir + '/tags/' + hashtag + '.txt'
if not os.path.isfile(hashtag_index_file):
2020-11-09 19:41:01 +00:00
if hashtag != hashtag.lower():
hashtag = hashtag.lower()
2022-01-04 13:19:00 +00:00
hashtag_index_file = base_dir + '/tags/' + hashtag + '.txt'
if not os.path.isfile(hashtag_index_file):
print('WARN: hashtag file not found ' + hashtag_index_file)
2020-11-09 19:41:01 +00:00
return None
2022-01-04 13:19:00 +00:00
separator_str = html_post_separator(base_dir, None)
2020-11-09 19:41:01 +00:00
# check that the directory for the nickname exists
if nickname:
2022-01-04 13:19:00 +00:00
account_dir = acct_dir(base_dir, nickname, domain)
if not os.path.isdir(account_dir):
2020-11-09 19:41:01 +00:00
nickname = None
# read the index
2022-06-09 14:46:30 +00:00
with open(hashtag_index_file, 'r', encoding='utf-8') as fp_hash:
2022-01-04 13:19:00 +00:00
lines = fp_hash.readlines()
2020-11-09 19:41:01 +00:00
# read the css
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon-profile.css'
2021-12-25 16:17:53 +00:00
if os.path.isfile(base_dir + '/epicyon.css'):
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon.css'
2020-11-09 19:41:01 +00:00
# ensure that the page number is in bounds
2022-01-04 13:19:00 +00:00
if not page_number:
page_number = 1
elif page_number < 1:
page_number = 1
2020-11-09 19:41:01 +00:00
# get the start end end within the index file
2022-01-04 13:19:00 +00:00
start_index = int((page_number - 1) * posts_per_page)
end_index = start_index + posts_per_page
no_of_lines = len(lines)
if end_index >= no_of_lines and no_of_lines > 0:
end_index = no_of_lines - 1
2020-11-09 19:41:01 +00:00
2022-01-04 13:19:00 +00:00
instance_title = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
2022-01-04 13:19:00 +00:00
hashtag_search_form = \
html_header_with_external_style(css_filename, instance_title, None)
2022-08-23 18:32:17 +00:00
2020-11-09 19:41:01 +00:00
if nickname:
2022-08-23 18:32:17 +00:00
# banner at top
banner_file, _ = \
get_banner_file(base_dir, nickname, domain, theme_name)
hashtag_search_form += \
'<header>\n' + \
'<a href="/users/' + nickname + '/' + box_name + '" title="' + \
2022-08-23 18:51:34 +00:00
translate['Search and follow'] + '" alt="' + \
translate['Search and follow'] + '" ' + \
2022-08-23 18:32:17 +00:00
'aria-flowto="containerHeader" tabindex="1" accesskey="' + \
2022-08-23 18:55:10 +00:00
access_keys['menuSearch'] + '">\n'
2022-08-23 18:32:17 +00:00
hashtag_search_form += '<img loading="lazy" decoding="async" ' + \
'class="timeline-banner" alt="" ' + \
'src="/users/' + nickname + '/' + banner_file + '" /></a>\n' + \
'</header>\n'
# add the page title
2022-01-04 13:19:00 +00:00
hashtag_search_form += '<center>\n' + \
2020-11-09 19:41:01 +00:00
'<h1><a href="/users/' + nickname + '/search">#' + \
2022-08-23 09:13:36 +00:00
hashtag + '</a>'
2020-11-09 19:41:01 +00:00
else:
2022-08-23 18:32:17 +00:00
# add the page title
2022-01-04 13:19:00 +00:00
hashtag_search_form += '<center>\n' + \
2022-08-23 09:10:42 +00:00
'<h1>#' + hashtag
2020-12-01 22:21:33 +00:00
# RSS link for hashtag feed
2022-08-23 09:08:50 +00:00
hashtag_search_form += ' <a href="/tags/rss2/' + hashtag + '">'
2022-01-04 13:19:00 +00:00
hashtag_search_form += \
2020-12-01 22:21:33 +00:00
'<img style="width:3%;min-width:50px" ' + \
2022-03-28 08:47:53 +00:00
'loading="lazy" decoding="async" ' + \
'alt="RSS 2.0" title="RSS 2.0" src="/' + \
2022-08-23 09:10:42 +00:00
'icons/logorss.png" /></a></h1>\n'
2020-11-09 19:41:01 +00:00
2022-08-23 09:08:50 +00:00
# maps for geolocations with this hashtag
maps_str = html_hashtag_maps(base_dir, hashtag, translate, map_format,
nickname, domain)
2022-08-23 09:08:50 +00:00
if maps_str:
maps_str = '<center>' + maps_str + '</center>\n'
hashtag_search_form += maps_str
2020-12-01 21:44:27 +00:00
# edit the category for this hashtag
2021-12-26 13:27:57 +00:00
if is_editor(base_dir, nickname):
2021-12-29 21:55:09 +00:00
category = get_hashtag_category(base_dir, hashtag)
2022-01-04 13:19:00 +00:00
hashtag_search_form += '<div class="hashtagCategoryContainer">\n'
hashtag_search_form += ' <form enctype="multipart/form-data" ' + \
2020-12-02 11:55:40 +00:00
'method="POST" accept-charset="UTF-8" action="' + \
2020-12-02 11:12:56 +00:00
'/users/' + nickname + '/tags/' + hashtag + \
'/sethashtagcategory">\n'
2022-01-04 13:19:00 +00:00
hashtag_search_form += ' <center>\n'
hashtag_search_form += translate['Category']
hashtag_search_form += \
2020-12-01 22:56:26 +00:00
' <input type="text" style="width: 20ch" ' + \
2020-12-01 22:54:09 +00:00
'name="hashtagCategory" value="' + category + '">\n'
2022-01-04 13:19:00 +00:00
hashtag_search_form += \
2020-12-01 21:44:27 +00:00
' <button type="submit" class="button" name="submitYes">' + \
2022-05-23 18:48:45 +00:00
translate['Publish'] + '</button>\n'
2022-01-04 13:19:00 +00:00
hashtag_search_form += ' </center>\n'
hashtag_search_form += ' </form>\n'
hashtag_search_form += '</div>\n'
2022-01-04 13:19:00 +00:00
if start_index > 0:
2020-11-09 19:41:01 +00:00
# previous page link
2022-01-04 13:19:00 +00:00
hashtag_search_form += \
2020-11-09 19:41:01 +00:00
' <center>\n' + \
2021-10-20 16:08:20 +00:00
' <a href="/users/' + nickname + \
'/tags/' + hashtag + '?page=' + \
2022-01-04 13:19:00 +00:00
str(page_number - 1) + \
2022-03-28 08:47:53 +00:00
'"><img loading="lazy" decoding="async" ' + \
'class="pageicon" src="/' + \
2020-12-09 13:08:26 +00:00
'icons/pageup.png" title="' + \
2020-11-09 19:41:01 +00:00
translate['Page up'] + \
'" alt="' + translate['Page up'] + \
'"></a>\n </center>\n'
2022-01-04 13:19:00 +00:00
index = start_index
2023-01-06 11:04:27 +00:00
text_mode_separator = '<div class="transparent"><hr></div>'
2022-01-04 13:19:00 +00:00
while index <= end_index:
2021-12-26 19:47:06 +00:00
post_id = lines[index].strip('\n').strip('\r')
if ' ' not in post_id:
2021-12-27 22:19:18 +00:00
nickname = get_nickname_from_actor(post_id)
2020-11-09 19:41:01 +00:00
if not nickname:
index += 1
continue
else:
2022-01-04 13:19:00 +00:00
post_fields = post_id.split(' ')
if len(post_fields) != 3:
2020-11-09 19:41:01 +00:00
index += 1
continue
2022-01-04 13:19:00 +00:00
nickname = post_fields[1]
post_id = post_fields[2]
2021-12-26 23:41:34 +00:00
post_filename = locate_post(base_dir, nickname, domain, post_id)
if not post_filename:
2020-11-09 19:41:01 +00:00
index += 1
continue
2021-12-26 23:41:34 +00:00
post_json_object = load_json(post_filename)
2021-12-25 22:09:19 +00:00
if not post_json_object:
2020-11-30 10:44:37 +00:00
index += 1
continue
2021-12-28 14:41:10 +00:00
if not is_public_post(post_json_object):
2020-11-30 10:44:37 +00:00
index += 1
continue
2022-01-04 13:19:00 +00:00
show_individual_post_icons = False
2020-11-30 10:44:37 +00:00
if nickname:
2022-01-04 13:19:00 +00:00
show_individual_post_icons = True
2021-12-25 21:29:53 +00:00
allow_deletion = False
2022-01-04 13:19:00 +00:00
show_repeats = show_individual_post_icons
show_icons = show_individual_post_icons
manually_approves_followers = False
show_public_only = False
store_to_sache = False
allow_downloads = True
avatar_url = None
show_avatar_options = True
minimize_all_images = False
if nickname in min_images_for_accounts:
minimize_all_images = True
2022-01-04 13:19:00 +00:00
post_str = \
2021-12-29 21:55:09 +00:00
individual_post_as_html(signing_priv_key_pem,
2022-01-04 13:19:00 +00:00
allow_downloads, recent_posts_cache,
2021-12-29 21:55:09 +00:00
max_recent_posts,
translate, None,
base_dir, session, cached_webfingers,
person_cache,
nickname, domain, port,
post_json_object,
2022-01-04 13:19:00 +00:00
avatar_url, show_avatar_options,
2021-12-29 21:55:09 +00:00
allow_deletion,
http_prefix, project_version,
'search',
yt_replace_domain,
twitter_replacement_domain,
show_published_date_only,
peertube_instances,
allow_local_network_access,
theme_name, system_language,
max_like_count,
2022-01-04 13:19:00 +00:00
show_repeats, show_icons,
manually_approves_followers,
show_public_only,
store_to_sache, False, cw_lists,
2022-03-24 13:14:41 +00:00
lists_enabled, timezone, False,
bold_reading, dogwhistles,
2023-01-13 15:04:48 +00:00
minimize_all_images, None,
buy_sites)
2022-01-04 13:19:00 +00:00
if post_str:
2023-01-06 11:04:27 +00:00
hashtag_search_form += \
text_mode_separator + separator_str + post_str
2020-11-09 19:41:01 +00:00
index += 1
2023-01-06 11:04:27 +00:00
hashtag_search_form += text_mode_separator
2022-01-04 13:19:00 +00:00
if end_index < no_of_lines - 1:
2020-11-09 19:41:01 +00:00
# next page link
2022-01-04 13:19:00 +00:00
hashtag_search_form += \
2020-11-09 19:41:01 +00:00
' <center>\n' + \
2021-10-20 16:08:20 +00:00
' <a href="/users/' + nickname + '/tags/' + hashtag + \
2022-01-04 13:19:00 +00:00
'?page=' + str(page_number + 1) + \
2022-03-28 08:47:53 +00:00
'"><img loading="lazy" decoding="async" ' + \
'class="pageicon" src="/icons' + \
2020-11-09 19:41:01 +00:00
'/pagedown.png" title="' + translate['Page down'] + \
'" alt="' + translate['Page down'] + '"></a>' + \
' </center>'
2022-01-04 13:19:00 +00:00
hashtag_search_form += html_footer()
return hashtag_search_form
2020-11-09 19:41:01 +00:00
def html_hashtag_search_remote(nickname: str, domain: str, port: int,
recent_posts_cache: {}, max_recent_posts: int,
translate: {},
base_dir: str, hashtag_url: str,
page_number: int, posts_per_page: int,
session, cached_webfingers: {},
person_cache: {},
http_prefix: str, project_version: str,
yt_replace_domain: str,
twitter_replacement_domain: str,
show_published_date_only: bool,
peertube_instances: [],
allow_local_network_access: bool,
theme_name: str, system_language: str,
max_like_count: int,
signing_priv_key_pem: str,
cw_lists: {}, lists_enabled: str,
timezone: str, bold_reading: bool,
dogwhistles: {},
min_images_for_accounts: [],
2023-01-13 15:04:48 +00:00
debug: bool, buy_sites: {}) -> str:
"""Show a page containing search results for a remote hashtag
"""
2023-01-18 10:20:36 +00:00
hashtag = urllib.parse.unquote(hashtag_url.split('/')[-1])
profile_str = 'https://www.w3.org/ns/activitystreams'
as_header = {
2023-01-05 23:08:18 +00:00
'Accept': 'application/activity+json; profile="' + profile_str + '"'
}
hashtag_url_with_page = hashtag_url
if '?page=' not in hashtag_url_with_page:
hashtag_url_with_page += '?page=' + str(page_number)
hashtag_json = \
2023-01-05 23:29:12 +00:00
get_json(signing_priv_key_pem,
session, hashtag_url_with_page, as_header, None, debug,
__version__, http_prefix, domain)
lines = []
2023-08-13 09:58:02 +00:00
if get_json_valid(hashtag_json):
if 'orderedItems' in hashtag_json:
lines = hashtag_json['orderedItems']
2023-01-05 23:34:30 +00:00
else:
print('No orderedItems in hashtag collection ' + str(hashtag_json))
2023-01-05 23:03:16 +00:00
else:
print('WARN: no hashtags returned for url ' + hashtag_url)
if not lines:
return ''
separator_str = html_post_separator(base_dir, None)
# check that the directory for the nickname exists
if nickname:
account_dir = acct_dir(base_dir, nickname, domain)
if not os.path.isdir(account_dir):
return None
# read the css
css_filename = base_dir + '/epicyon-profile.css'
if os.path.isfile(base_dir + '/epicyon.css'):
css_filename = base_dir + '/epicyon.css'
# ensure that the page number is in bounds
if not page_number:
page_number = 1
elif page_number < 1:
page_number = 1
instance_title = \
get_config_param(base_dir, 'instanceTitle')
hashtag_search_form = \
html_header_with_external_style(css_filename, instance_title, None)
# add the page title
hashtag_search_form += '<center>\n' + \
'<h1>#' + hashtag
# RSS link for hashtag feed
hashtag_rss = hashtag_url
if '.html' in hashtag_rss:
hashtag_rss = hashtag_rss.replace('.html', '')
hashtag_search_form += ' <a href="' + hashtag_rss + '.rss">'
hashtag_search_form += \
'<img style="width:3%;min-width:50px" ' + \
'loading="lazy" decoding="async" ' + \
'alt="RSS 2.0" title="RSS 2.0" src="/' + \
'icons/logorss.png" /></a></h1>\n'
tag_link = '/users/' + nickname + '?remotetag=' + \
hashtag_url.replace('/', '--')
if page_number > 1 and hashtag_json.get('prev'):
# previous page link
hashtag_search_form += \
' <center>\n' + \
' <a href="' + tag_link + ';page=' + \
str(page_number - 1) + \
'"><img loading="lazy" decoding="async" ' + \
'class="pageicon" src="/' + \
'icons/pageup.png" title="' + \
translate['Page up'] + \
'" alt="' + translate['Page up'] + \
'"></a>\n </center>\n'
2023-01-06 11:04:27 +00:00
text_mode_separator = '<div class="transparent"><hr></div>'
post_ctr = 0
2023-01-05 22:54:29 +00:00
for post_id in lines:
2023-01-05 23:37:52 +00:00
print('Hashtag post_id ' + post_id)
post_json_object = \
get_json(signing_priv_key_pem,
session, post_id, as_header, None, debug,
__version__, http_prefix, domain)
2023-08-13 09:58:02 +00:00
if not get_json_valid(post_json_object):
2023-01-05 23:03:16 +00:00
print('No hashtag post for ' + post_id)
continue
if not isinstance(post_json_object, dict):
2023-01-05 23:37:52 +00:00
print('Hashtag post is not a dict ' + str(post_json_object))
continue
if not has_object_dict(post_json_object):
if post_json_object.get('id') and \
'to' in post_json_object and \
2023-01-05 23:48:00 +00:00
'cc' in post_json_object:
new_url = \
2023-01-05 23:48:00 +00:00
remove_id_ending(post_json_object['id'])
2023-08-03 17:31:47 +00:00
actor = get_actor_from_post_id(new_url)
new_post_json_object = {
"type": "Create",
2023-01-05 23:48:00 +00:00
"id": new_url + '/activity',
"to": post_json_object['to'],
"cc": post_json_object['cc'],
2023-01-05 23:48:00 +00:00
"actor": actor,
"object": post_json_object
}
post_json_object = new_post_json_object
else:
2023-01-05 23:37:52 +00:00
print('Hashtag post does not contain necessary fields ' +
str(post_json_object))
continue
if not is_public_post(post_json_object):
2023-01-05 23:03:16 +00:00
print('Hashtag post is not public ' + post_id)
continue
2023-01-07 11:45:19 +00:00
# render harmless any dangerous markup
harmless_markup(post_json_object)
show_individual_post_icons = False
allow_deletion = False
show_repeats = show_individual_post_icons
show_icons = show_individual_post_icons
manually_approves_followers = False
show_public_only = False
store_to_sache = False
allow_downloads = True
avatar_url = None
show_avatar_options = True
minimize_all_images = False
if nickname in min_images_for_accounts:
minimize_all_images = True
post_str = \
individual_post_as_html(signing_priv_key_pem,
allow_downloads, recent_posts_cache,
max_recent_posts,
translate, None,
base_dir, session, cached_webfingers,
person_cache,
nickname, domain, port,
post_json_object,
avatar_url, show_avatar_options,
allow_deletion,
http_prefix, project_version,
'search',
yt_replace_domain,
twitter_replacement_domain,
show_published_date_only,
peertube_instances,
allow_local_network_access,
theme_name, system_language,
max_like_count,
show_repeats, show_icons,
manually_approves_followers,
show_public_only,
store_to_sache, False, cw_lists,
lists_enabled, timezone, False,
bold_reading, dogwhistles,
2023-01-13 15:04:48 +00:00
minimize_all_images, None,
buy_sites)
if post_str:
2023-01-06 11:04:27 +00:00
hashtag_search_form += \
text_mode_separator + separator_str + post_str
post_ctr += 1
if post_ctr >= posts_per_page:
break
2023-01-06 11:04:27 +00:00
hashtag_search_form += text_mode_separator
if post_ctr >= 5 and hashtag_json.get('next'):
# next page link
hashtag_search_form += \
' <center>\n' + \
' <a href="' + tag_link + \
';page=' + str(page_number + 1) + \
'"><img loading="lazy" decoding="async" ' + \
'class="pageicon" src="/icons' + \
'/pagedown.png" title="' + translate['Page down'] + \
'" alt="' + translate['Page down'] + '"></a>' + \
' </center>'
hashtag_search_form += html_footer()
return hashtag_search_form
2021-12-29 21:55:09 +00:00
def rss_hashtag_search(nickname: str, domain: str, port: int,
recent_posts_cache: {}, max_recent_posts: int,
translate: {},
base_dir: str, hashtag: str,
2022-01-04 13:19:00 +00:00
posts_per_page: int,
2021-12-29 21:55:09 +00:00
session, cached_webfingers: {}, person_cache: {},
http_prefix: str, project_version: str,
yt_replace_domain: str,
twitter_replacement_domain: str,
system_language: str) -> str:
2020-11-09 19:41:01 +00:00
"""Show an rss feed for a hashtag
"""
if hashtag.startswith('#'):
hashtag = hashtag[1:]
hashtag = urllib.parse.unquote(hashtag)
2022-01-04 13:19:00 +00:00
hashtag_index_file = base_dir + '/tags/' + hashtag + '.txt'
if not os.path.isfile(hashtag_index_file):
2020-11-09 19:41:01 +00:00
if hashtag != hashtag.lower():
hashtag = hashtag.lower()
2022-01-04 13:19:00 +00:00
hashtag_index_file = base_dir + '/tags/' + hashtag + '.txt'
if not os.path.isfile(hashtag_index_file):
print('WARN: hashtag file not found ' + hashtag_index_file)
2020-11-09 19:41:01 +00:00
return None
# check that the directory for the nickname exists
if nickname:
2022-01-04 13:19:00 +00:00
account_dir = acct_dir(base_dir, nickname, domain)
if not os.path.isdir(account_dir):
2020-11-09 19:41:01 +00:00
nickname = None
# read the index
lines = []
2022-06-09 14:46:30 +00:00
with open(hashtag_index_file, 'r', encoding='utf-8') as fp_hash:
2022-01-04 13:19:00 +00:00
lines = fp_hash.readlines()
2020-11-09 19:41:01 +00:00
if not lines:
return None
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2020-11-09 19:41:01 +00:00
2022-01-04 13:19:00 +00:00
max_feed_length = 10
2022-01-08 10:58:54 +00:00
hashtag_feed = rss2tag_header(hashtag, http_prefix, domain_full)
for index, _ in enumerate(lines):
2021-12-26 19:47:06 +00:00
post_id = lines[index].strip('\n').strip('\r')
if ' ' not in post_id:
2021-12-27 22:19:18 +00:00
nickname = get_nickname_from_actor(post_id)
2020-11-09 19:41:01 +00:00
if not nickname:
index += 1
2022-01-04 13:19:00 +00:00
if index >= max_feed_length:
2020-11-09 19:41:01 +00:00
break
continue
else:
2022-01-04 13:19:00 +00:00
post_fields = post_id.split(' ')
if len(post_fields) != 3:
2020-11-09 19:41:01 +00:00
index += 1
2022-01-04 13:19:00 +00:00
if index >= max_feed_length:
2020-11-09 19:41:01 +00:00
break
continue
2022-01-04 13:19:00 +00:00
nickname = post_fields[1]
post_id = post_fields[2]
2021-12-26 23:41:34 +00:00
post_filename = locate_post(base_dir, nickname, domain, post_id)
if not post_filename:
2020-11-09 19:41:01 +00:00
index += 1
2022-01-04 13:19:00 +00:00
if index >= max_feed_length:
2020-11-09 19:41:01 +00:00
break
continue
2021-12-26 23:41:34 +00:00
post_json_object = load_json(post_filename)
2021-12-25 22:09:19 +00:00
if post_json_object:
2021-12-28 14:41:10 +00:00
if not is_public_post(post_json_object):
2020-11-09 19:41:01 +00:00
index += 1
2022-01-04 13:19:00 +00:00
if index >= max_feed_length:
2020-11-09 19:41:01 +00:00
break
continue
# add to feed
2023-01-08 22:23:02 +00:00
if 'content' in post_json_object['object'] and \
2021-12-25 22:09:19 +00:00
post_json_object['object'].get('attributedTo') and \
post_json_object['object'].get('published'):
published = post_json_object['object']['published']
2023-11-20 22:27:58 +00:00
pub_date = date_from_string_format(published,
["%Y-%m-%dT%H:%M:%S%z"])
2022-01-04 13:19:00 +00:00
rss_date_str = pub_date.strftime("%a, %d %b %Y %H:%M:%S UT")
hashtag_feed += ' <item>'
attrib_field = post_json_object['object']['attributedTo']
attrib = get_attributed_to(attrib_field)
if attrib:
hashtag_feed += \
' <author>' + attrib + '</author>'
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('summary'):
2022-01-04 13:19:00 +00:00
hashtag_feed += \
2020-11-09 19:41:01 +00:00
' <title>' + \
2023-01-02 10:24:35 +00:00
escape_text(post_json_object['object']['summary']) + \
2020-11-09 19:41:01 +00:00
'</title>'
description = \
2021-12-26 11:29:40 +00:00
get_base_content_from_post(post_json_object,
system_language)
2021-12-27 15:52:08 +00:00
description = first_paragraph_from_string(description)
2023-01-02 10:24:35 +00:00
description = escape_text(description)
2022-01-04 13:19:00 +00:00
hashtag_feed += \
2020-11-09 19:41:01 +00:00
' <description>' + description + '</description>'
2022-01-04 13:19:00 +00:00
hashtag_feed += \
' <pubDate>' + rss_date_str + '</pubDate>'
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('attachment'):
for attach in post_json_object['object']['attachment']:
2020-11-09 19:41:01 +00:00
if not attach.get('url'):
continue
2023-12-09 14:18:24 +00:00
url_str = get_url_from_post(attach['url'])
attach_url = remove_html(url_str)
2022-01-04 13:19:00 +00:00
hashtag_feed += \
' <link>' + attach_url + '</link>'
2022-01-04 13:19:00 +00:00
hashtag_feed += ' </item>'
2020-11-09 19:41:01 +00:00
index += 1
2022-01-04 13:19:00 +00:00
if index >= max_feed_length:
2020-11-09 19:41:01 +00:00
break
2022-01-04 13:19:00 +00:00
return hashtag_feed + rss2tag_footer()
2023-01-05 15:56:49 +00:00
def hashtag_search_json(nickname: str, domain: str, port: int,
base_dir: str, hashtag: str,
page_number: int, posts_per_page: int,
http_prefix: str) -> {}:
"""Show a json collection for a hashtag
"""
if hashtag.startswith('#'):
hashtag = hashtag[1:]
hashtag = urllib.parse.unquote(hashtag)
hashtag_index_file = base_dir + '/tags/' + hashtag + '.txt'
if not os.path.isfile(hashtag_index_file):
if hashtag != hashtag.lower():
hashtag = hashtag.lower()
hashtag_index_file = base_dir + '/tags/' + hashtag + '.txt'
if not os.path.isfile(hashtag_index_file):
print('WARN: hashtag file not found ' + hashtag_index_file)
return None
# check that the directory for the nickname exists
if nickname:
account_dir = acct_dir(base_dir, nickname, domain)
if not os.path.isdir(account_dir):
nickname = None
# read the index
lines = []
with open(hashtag_index_file, 'r', encoding='utf-8') as fp_hash:
lines = fp_hash.readlines()
if not lines:
return None
domain_full = get_full_domain(domain, port)
url = http_prefix + '://' + domain_full + '/tags/' + \
hashtag + '?page=' + str(page_number)
hashtag_json = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': url,
'orderedItems': [],
'totalItems': 0,
'type': 'OrderedCollection'
}
2023-01-06 15:29:34 +00:00
hashtag_json['first'] = \
http_prefix + '://' + domain_full + '/tags/' + \
hashtag + '?page=1'
if page_number > 1:
hashtag_json['prev'] = \
http_prefix + '://' + domain_full + '/tags/' + \
hashtag + '?page=' + str(page_number - 1)
2023-01-05 15:56:49 +00:00
page_items = 0
for index, _ in enumerate(lines):
post_id = lines[index].strip('\n').strip('\r')
if ' ' not in post_id:
nickname = get_nickname_from_actor(post_id)
if not nickname:
continue
else:
post_fields = post_id.split(' ')
if len(post_fields) != 3:
continue
nickname = post_fields[1]
post_id = post_fields[2]
post_filename = locate_post(base_dir, nickname, domain, post_id)
if not post_filename:
continue
post_json_object = load_json(post_filename)
2023-01-05 17:12:55 +00:00
if not post_json_object:
continue
if not has_object_dict(post_json_object):
continue
if not is_public_post(post_json_object):
continue
if not post_json_object['object'].get('id'):
continue
# add to feed
page_items += 1
if page_items < posts_per_page * (page_number - 1):
continue
id_str = remove_id_ending(post_json_object['object']['id'])
hashtag_json['orderedItems'].append(id_str)
hashtag_json['totalItems'] += 1
2023-01-05 15:56:49 +00:00
if hashtag_json['totalItems'] >= posts_per_page:
2023-01-06 15:29:34 +00:00
hashtag_json['next'] = \
http_prefix + '://' + domain_full + '/tags/' + \
hashtag + '?page=' + str(page_number + 1)
2023-01-05 15:56:49 +00:00
break
return hashtag_json