epicyon/webapp_search.py

1014 lines
41 KiB
Python
Raw Normal View History

2020-11-09 19:41:01 +00:00
__filename__ = "webapp_search.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.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
from datetime import datetime
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 19:05:25 +00:00
from utils import get_domain_from_actor
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
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
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
def html_search_emoji(css_cache: {}, translate: {},
base_dir: str, http_prefix: str,
searchStr: str) -> 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
searchStr = searchStr.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
2021-12-25 16:17:53 +00:00
emojiLookupFilename = base_dir + '/emoji/emoji.json'
customEmojiLookupFilename = base_dir + '/emojicustom/emoji.json'
# create header
2021-01-11 19:46:21 +00:00
instanceTitle = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
2021-12-29 21:55:09 +00:00
emojiForm = \
2021-12-31 21:18:12 +00:00
html_header_with_external_style(css_filename, instanceTitle, None)
emojiForm += '<center><h1>' + \
translate['Emoji Search'] + \
'</h1></center>'
2020-11-09 19:41:01 +00:00
# does the lookup file exist?
if not os.path.isfile(emojiLookupFilename):
2021-11-01 23:42:37 +00:00
emojiForm += '<center><h5>' + \
translate['No results'] + '</h5></center>'
2021-12-29 21:55:09 +00:00
emojiForm += html_footer()
2021-11-01 23:42:37 +00:00
return emojiForm
2021-12-26 15:13:34 +00:00
emojiJson = load_json(emojiLookupFilename)
if emojiJson:
2021-11-01 23:11:41 +00:00
if os.path.isfile(customEmojiLookupFilename):
2021-12-26 15:13:34 +00:00
customEmojiJson = load_json(customEmojiLookupFilename)
2021-11-01 23:42:37 +00:00
if customEmojiJson:
emojiJson = dict(emojiJson, **customEmojiJson)
2021-11-01 23:46:31 +00:00
results = {}
for emojiName, filename in emojiJson.items():
if searchStr in emojiName:
results[emojiName] = filename + '.png'
for emojiName, filename in emojiJson.items():
if emojiName in searchStr:
results[emojiName] = filename + '.png'
2021-11-01 23:46:31 +00:00
if not results:
emojiForm += '<center><h5>' + \
translate['No results'] + '</h5></center>'
headingShown = False
emojiForm += '<center>'
msgStr1 = translate['Copy the text then paste it into your post']
msgStr2 = ':<img loading="lazy" class="searchEmoji" src="/emoji/'
for emojiName, 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
if not headingShown:
emojiForm += \
2021-11-01 23:17:14 +00:00
'<center><h5>' + msgStr1 + '</h5></center>'
headingShown = True
emojiForm += \
'<h3>:' + emojiName + msgStr2 + filename + '"/></h3>'
emojiForm += '</center>'
2021-12-29 21:55:09 +00:00
emojiForm += html_footer()
2020-11-09 19:41:01 +00:00
return emojiForm
2021-12-29 21:55:09 +00:00
def _match_shared_item(searchStrLowerList: [],
sharedItem: {}) -> bool:
"""Returns true if the shared item matches search criteria
"""
for searchSubstr in searchStrLowerList:
searchSubstr = searchSubstr.strip()
2021-07-27 19:23:55 +00:00
if sharedItem.get('location'):
if searchSubstr in sharedItem['location'].lower():
return True
2021-09-19 14:18:32 +00:00
if searchSubstr in sharedItem['summary'].lower():
return True
elif searchSubstr in sharedItem['displayName'].lower():
return True
elif searchSubstr in sharedItem['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,
calling_domain: str, pageNumber: int,
searchStrLower: str, translate: {},
previous: bool) -> str:
2021-07-27 09:57:52 +00:00
"""Returns the html for the previous button on shared items search results
"""
2021-12-26 18:32:02 +00:00
postActor = 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:
pageNumber -= 1
titleStr = translate['Page up']
imageUrl = 'pageup.png'
else:
pageNumber += 1
titleStr = translate['Page down']
imageUrl = 'pagedown.png'
2021-07-27 09:57:52 +00:00
sharedItemsForm = \
'<form method="POST" action="' + postActor + '/searchhandle?page=' + \
2021-07-27 10:05:57 +00:00
str(pageNumber) + '">\n'
2021-07-27 09:57:52 +00:00
sharedItemsForm += \
' <input type="hidden" ' + 'name="actor" value="' + actor + '">\n'
sharedItemsForm += \
' <input type="hidden" ' + 'name="searchtext" value="' + \
searchStrLower + '"><br>\n'
sharedItemsForm += \
' <center>\n' + ' <a href="' + actor + \
'" type="submit" name="submitSearch">\n'
sharedItemsForm += \
' <img loading="lazy" ' + 'class="pageicon" src="/icons' + \
2021-07-27 10:05:57 +00:00
'/' + imageUrl + '" title="' + titleStr + \
'" alt="' + titleStr + '"/></a>\n'
2021-07-27 09:57:52 +00:00
sharedItemsForm += ' </center>\n'
sharedItemsForm += '</form>\n'
return sharedItemsForm
2021-12-29 21:55:09 +00:00
def _html_shares_result(base_dir: str,
sharesJson: {}, pageNumber: int, resultsPerPage: int,
searchStrLowerList: [], currPage: int, ctr: int,
calling_domain: str, http_prefix: str,
domain_full: str, contactNickname: str, actor: str,
resultsExist: bool, searchStrLower: str, translate: {},
sharesFileType: str) -> (bool, int, int, str):
"""Result for shared items search
"""
sharedItemsForm = ''
if currPage > pageNumber:
return resultsExist, currPage, ctr, sharedItemsForm
for name, sharedItem in sharesJson.items():
2021-12-29 21:55:09 +00:00
if _match_shared_item(searchStrLowerList, sharedItem):
if currPage == pageNumber:
# show individual search result
sharedItemsForm += \
2021-12-29 21:55:09 +00:00
html_search_result_share(base_dir, sharedItem, translate,
http_prefix, domain_full,
contactNickname,
name, actor, sharesFileType,
sharedItem['category'])
if not resultsExist and currPage > 1:
# show the previous page button
sharedItemsForm += \
2021-12-29 21:55:09 +00:00
_html_search_result_share_page(actor, domain_full,
calling_domain,
pageNumber,
searchStrLower,
translate, True)
resultsExist = True
ctr += 1
if ctr >= resultsPerPage:
currPage += 1
if currPage > pageNumber:
# show the next page button
sharedItemsForm += \
2021-12-29 21:55:09 +00:00
_html_search_result_share_page(actor, domain_full,
calling_domain,
pageNumber,
searchStrLower,
translate, False)
return resultsExist, currPage, ctr, sharedItemsForm
ctr = 0
return resultsExist, currPage, ctr, sharedItemsForm
2021-12-29 21:55:09 +00:00
def html_search_shared_items(css_cache: {}, translate: {},
base_dir: str, searchStr: str,
pageNumber: int,
resultsPerPage: int,
http_prefix: str,
domain_full: str, actor: str,
calling_domain: str,
shared_items_federated_domains: [],
sharesFileType: str) -> str:
2020-11-09 19:41:01 +00:00
"""Search results for shared items
"""
currPage = 1
ctr = 0
sharedItemsForm = ''
searchStrLower = urllib.parse.unquote(searchStr)
searchStrLower = searchStrLower.lower().strip('\n').strip('\r')
searchStrLowerList = searchStrLower.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
2021-01-11 19:46:21 +00:00
instanceTitle = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
sharedItemsForm = \
2021-12-31 21:18:12 +00:00
html_header_with_external_style(css_filename, instanceTitle, None)
if sharesFileType == 'shares':
titleStr = translate['Shared Items Search']
else:
titleStr = translate['Wanted Items Search']
sharedItemsForm += \
2020-12-07 12:06:30 +00:00
'<center><h1>' + \
'<a href="' + actor + '/search">' + titleStr + '</a></h1></center>'
resultsExist = False
2021-12-25 16:17:53 +00:00
for subdir, 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
contactNickname = handle.split('@')[0]
2021-12-25 16:17:53 +00:00
sharesFilename = base_dir + '/accounts/' + handle + \
'/' + sharesFileType + '.json'
if not os.path.isfile(sharesFilename):
continue
2020-11-09 19:41:01 +00:00
2021-12-26 15:13:34 +00:00
sharesJson = load_json(sharesFilename)
if not sharesJson:
continue
2020-11-09 19:41:01 +00:00
(resultsExist, currPage, ctr,
2021-12-29 21:55:09 +00:00
resultStr) = _html_shares_result(base_dir, sharesJson, pageNumber,
resultsPerPage,
searchStrLowerList,
currPage, ctr,
calling_domain, http_prefix,
domain_full,
contactNickname,
actor, resultsExist,
searchStrLower, translate,
sharesFileType)
sharedItemsForm += resultStr
if currPage > pageNumber:
break
2020-12-13 22:13:45 +00:00
break
# search federated shared items
if sharesFileType == 'shares':
2021-12-25 16:17:53 +00:00
catalogsDir = base_dir + '/cache/catalogs'
else:
2021-12-25 16:17:53 +00:00
catalogsDir = base_dir + '/cache/wantedItems'
if currPage <= pageNumber and os.path.isdir(catalogsDir):
for subdir, dirs, files in os.walk(catalogsDir):
for f in files:
if '#' in f:
continue
if not f.endswith('.' + sharesFileType + '.json'):
continue
federatedDomain = f.split('.')[0]
2021-12-25 18:05:01 +00:00
if federatedDomain not in shared_items_federated_domains:
continue
sharesFilename = catalogsDir + '/' + f
2021-12-26 15:13:34 +00:00
sharesJson = load_json(sharesFilename)
if not sharesJson:
continue
(resultsExist, currPage, ctr,
2021-12-29 21:55:09 +00:00
resultStr) = _html_shares_result(base_dir, sharesJson,
pageNumber,
resultsPerPage,
searchStrLowerList,
currPage, ctr,
calling_domain, http_prefix,
domain_full,
contactNickname,
actor, resultsExist,
searchStrLower, translate,
sharesFileType)
sharedItemsForm += resultStr
if currPage > pageNumber:
break
break
if not resultsExist:
sharedItemsForm += \
'<center><h5>' + translate['No results'] + '</h5></center>\n'
2021-12-29 21:55:09 +00:00
sharedItemsForm += html_footer()
2020-11-09 19:41:01 +00:00
return sharedItemsForm
2021-12-29 21:55:09 +00:00
def html_search_emoji_text_entry(css_cache: {}, translate: {},
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-27 19:05:25 +00:00
domain, port = get_domain_from_actor(actor)
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-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
2021-01-11 19:46:21 +00:00
instanceTitle = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
2021-12-29 21:55:09 +00:00
emojiStr = \
2021-12-31 21:18:12 +00:00
html_header_with_external_style(css_filename, instanceTitle, None)
2020-11-09 19:41:01 +00:00
emojiStr += '<div class="follow">\n'
emojiStr += ' <div class="followAvatar">\n'
emojiStr += ' <center>\n'
emojiStr += \
' <p class="followText">' + \
translate['Enter an emoji name to search for'] + '</p>\n'
2020-12-27 17:17:48 +00:00
emojiStr += ' <form role="search" method="POST" action="' + \
2020-11-09 19:41:01 +00:00
actor + '/searchhandleemoji">\n'
emojiStr += ' <input type="hidden" name="actor" value="' + \
actor + '">\n'
emojiStr += ' <input type="text" name="searchtext" autofocus><br>\n'
emojiStr += \
' <button type="submit" class="button" name="submitSearch">' + \
translate['Submit'] + '</button>\n'
emojiStr += ' </form>\n'
emojiStr += ' </center>\n'
emojiStr += ' </div>\n'
emojiStr += '</div>\n'
2021-12-29 21:55:09 +00:00
emojiStr += html_footer()
2020-11-09 19:41:01 +00:00
return emojiStr
2021-12-29 21:55:09 +00:00
def html_search(css_cache: {}, 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', '')
2021-12-27 22:19:18 +00:00
searchNickname = get_nickname_from_actor(actor)
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
2021-12-26 14:08:58 +00:00
instanceTitle = get_config_param(base_dir, 'instanceTitle')
2021-12-29 21:55:09 +00:00
followStr = \
2021-12-31 21:18:12 +00:00
html_header_with_external_style(css_filename, instanceTitle, None)
2020-11-09 19:41:01 +00:00
# show a banner above the search box
searchBannerFile, searchBannerFilename = \
2021-12-29 21:55:09 +00:00
get_search_banner_file(base_dir, searchNickname, domain, theme)
2020-11-09 19:41:01 +00:00
2021-12-29 21:55:09 +00:00
text_mode_bannerStr = html_keyboard_navigation(text_mode_banner, {}, {})
2021-12-25 23:09:49 +00:00
if text_mode_bannerStr is None:
text_mode_bannerStr = ''
2021-02-07 13:33:30 +00:00
2020-11-09 19:41:01 +00:00
if os.path.isfile(searchBannerFilename):
2021-12-31 21:18:12 +00:00
timelineKey = access_keys['menuTimeline']
2020-11-09 19:41:01 +00:00
usersPath = '/users/' + searchNickname
followStr += \
2021-12-25 23:09:49 +00:00
'<header>\n' + text_mode_bannerStr + \
2021-12-31 23:50:29 +00:00
'<a href="' + usersPath + '/' + 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'] + '" ' + \
'accesskey="' + timelineKey + '">\n'
2020-11-09 19:41:01 +00:00
followStr += '<img loading="lazy" class="timeline-banner" src="' + \
2021-02-01 20:20:13 +00:00
usersPath + '/' + searchBannerFile + '" 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
followStr += '<div class="follow">\n'
followStr += ' <div class="followAvatar">\n'
followStr += ' <center>\n'
followStr += \
2021-08-10 09:10:25 +00:00
' <p class="followText">' + translate['Search screen text'] + '</p>\n'
2020-12-27 16:59:03 +00:00
followStr += ' <form role="search" method="POST" ' + \
2020-11-09 19:41:01 +00:00
'accept-charset="UTF-8" action="' + actor + '/searchhandle">\n'
followStr += \
' <input type="hidden" name="actor" value="' + actor + '">\n'
followStr += ' <input type="text" name="searchtext" autofocus><br>\n'
2021-12-31 21:18:12 +00:00
submitKey = access_keys['submitButton']
2020-11-09 19:41:01 +00:00
followStr += ' <button type="submit" class="button" ' + \
2021-04-23 09:15:53 +00:00
'name="submitSearch" accesskey="' + submitKey + '">' + \
translate['Submit'] + '</button>\n'
2020-11-09 19:41:01 +00:00
followStr += ' </form>\n'
2021-10-20 13:33:34 +00:00
cachedHashtagSwarmFilename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, searchNickname, domain) + '/.hashtagSwarm'
2021-10-20 13:33:34 +00:00
swarmStr = ''
if os.path.isfile(cachedHashtagSwarmFilename):
try:
with open(cachedHashtagSwarmFilename, 'r') as fp:
swarmStr = fp.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 ' +
2021-10-29 18:48:15 +00:00
cachedHashtagSwarmFilename)
2021-10-20 13:33:34 +00:00
if not swarmStr:
2021-12-29 21:55:09 +00:00
swarmStr = html_hash_tag_swarm(base_dir, actor, translate)
2021-10-20 14:20:27 +00:00
if swarmStr:
try:
with open(cachedHashtagSwarmFilename, 'w+') as fp:
fp.write(swarmStr)
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 ' +
2021-10-29 18:48:15 +00:00
cachedHashtagSwarmFilename)
2021-10-20 13:33:34 +00:00
followStr += ' <p class="hashtagswarm">' + swarmStr + '</p>\n'
2020-11-09 19:41:01 +00:00
followStr += ' </center>\n'
followStr += ' </div>\n'
followStr += '</div>\n'
2021-12-29 21:55:09 +00:00
followStr += html_footer()
2020-11-09 19:41:01 +00:00
return followStr
2021-12-29 21:55:09 +00:00
def html_skills_search(actor: str,
css_cache: {}, translate: {}, base_dir: str,
http_prefix: str,
skillsearch: str, instanceOnly: bool,
postsPerPage: int) -> 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
2021-12-25 16:17:53 +00:00
for subdir, dirs, files in os.walk(base_dir + '/accounts/'):
2020-11-09 19:41:01 +00:00
for f in files:
2020-11-19 14:02:16 +00:00
if not f.endswith('.json'):
2020-11-09 19:41:01 +00:00
continue
2021-12-26 18:46:43 +00:00
if not is_account_dir(f):
2021-04-21 16:09:56 +00:00
continue
2020-11-19 14:02:16 +00:00
actorFilename = os.path.join(subdir, f)
2021-12-26 15:13:34 +00:00
actor_json = load_json(actorFilename)
2021-12-26 10:29:52 +00:00
if actor_json:
if actor_json.get('id') and \
2021-12-28 20:32:11 +00:00
no_of_actor_skills(actor_json) > 0 and \
2021-12-26 10:29:52 +00:00
actor_json.get('name') and \
actor_json.get('icon'):
actor = actor_json['id']
actorSkillsList = actor_json['hasOccupation']['skills']
2021-12-28 20:32:11 +00:00
skills = get_skills_from_list(actorSkillsList)
for skillName, skillLevel in skills.items():
2020-11-19 14:02:16 +00:00
skillName = skillName.lower()
if not (skillName in skillsearch or
skillsearch in skillName):
continue
skillLevelStr = str(skillLevel)
if skillLevel < 100:
skillLevelStr = '0' + skillLevelStr
if skillLevel < 10:
skillLevelStr = '0' + skillLevelStr
indexStr = \
skillLevelStr + ';' + actor + ';' + \
2021-12-26 10:29:52 +00:00
actor_json['name'] + \
';' + actor_json['icon']['url']
2020-11-19 14:02:16 +00:00
if indexStr not in results:
results.append(indexStr)
2020-12-13 22:13:45 +00:00
break
2020-11-19 14:02:16 +00:00
if not instanceOnly:
# search actor cache
2021-12-25 16:17:53 +00:00
for subdir, dirs, files in os.walk(base_dir + '/cache/actors/'):
2020-11-19 14:02:16 +00:00
for f in files:
if not f.endswith('.json'):
continue
2021-12-26 18:46:43 +00:00
if not is_account_dir(f):
2021-04-21 16:09:56 +00:00
continue
2020-11-19 14:02:16 +00:00
actorFilename = os.path.join(subdir, f)
2021-12-26 15:13:34 +00:00
cachedActorJson = load_json(actorFilename)
2020-11-19 14:02:16 +00:00
if cachedActorJson:
if cachedActorJson.get('actor'):
2021-12-26 10:29:52 +00:00
actor_json = cachedActorJson['actor']
if actor_json.get('id') and \
2021-12-28 20:32:11 +00:00
no_of_actor_skills(actor_json) > 0 and \
2021-12-26 10:29:52 +00:00
actor_json.get('name') and \
actor_json.get('icon'):
actor = actor_json['id']
2021-05-14 17:41:05 +00:00
actorSkillsList = \
2021-12-26 10:29:52 +00:00
actor_json['hasOccupation']['skills']
2021-12-28 20:32:11 +00:00
skills = get_skills_from_list(actorSkillsList)
for skillName, skillLevel in skills.items():
2020-11-19 14:02:16 +00:00
skillName = skillName.lower()
if not (skillName in skillsearch or
skillsearch in skillName):
continue
skillLevelStr = str(skillLevel)
if skillLevel < 100:
skillLevelStr = '0' + skillLevelStr
if skillLevel < 10:
skillLevelStr = '0' + skillLevelStr
indexStr = \
skillLevelStr + ';' + actor + ';' + \
2021-12-26 10:29:52 +00:00
actor_json['name'] + \
';' + actor_json['icon']['url']
2020-11-19 14:02:16 +00:00
if indexStr not in results:
results.append(indexStr)
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
2021-01-11 19:46:21 +00:00
instanceTitle = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
skillSearchForm = \
2021-12-31 21:18:12 +00:00
html_header_with_external_style(css_filename, instanceTitle, None)
2020-11-19 14:02:16 +00:00
skillSearchForm += \
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:
skillSearchForm += \
'<center><h5>' + translate['No results'] + \
'</h5></center>'
else:
skillSearchForm += '<center>'
ctr = 0
for skillMatch in results:
skillMatchFields = skillMatch.split(';')
if len(skillMatchFields) != 4:
2020-11-09 19:41:01 +00:00
continue
2020-11-19 14:02:16 +00:00
actor = skillMatchFields[1]
actorName = skillMatchFields[2]
avatarUrl = skillMatchFields[3]
skillSearchForm += \
'<div class="search-result""><a href="' + \
actor + '/skills">'
skillSearchForm += \
'<img loading="lazy" src="' + avatarUrl + \
2021-02-01 20:20:13 +00:00
'" alt="" /><span class="search-result-text">' + actorName + \
2020-11-19 14:02:16 +00:00
'</span></a></div>'
ctr += 1
if ctr >= postsPerPage:
break
skillSearchForm += '</center>'
2021-12-29 21:55:09 +00:00
skillSearchForm += html_footer()
2020-11-19 14:02:16 +00:00
return skillSearchForm
2020-11-09 19:41:01 +00:00
2020-11-19 14:02:16 +00:00
2021-12-29 21:55:09 +00:00
def html_history_search(css_cache: {}, translate: {}, base_dir: str,
http_prefix: str,
nickname: str, domain: str,
historysearch: str,
postsPerPage: int, pageNumber: int,
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,
theme_name: str, boxName: str,
system_language: str,
max_like_count: int,
signing_priv_key_pem: str,
cw_lists: {},
lists_enabled: str) -> 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')
boxFilenames = \
2021-12-28 13:07:02 +00:00
search_box_posts(base_dir, nickname, domain,
historysearch, postsPerPage, boxName)
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
2021-01-11 19:46:21 +00:00
instanceTitle = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
2020-11-19 14:02:16 +00:00
historySearchForm = \
2021-12-31 21:18:12 +00:00
html_header_with_external_style(css_filename, instanceTitle, 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)
2021-05-03 22:31:06 +00:00
historySearchTitle = '🔍 ' + translate['Your Posts']
2021-05-03 22:33:12 +00:00
if boxName == 'bookmarks':
2021-05-03 22:31:06 +00:00
historySearchTitle = '🔍 ' + translate['Bookmarks']
2020-11-19 14:02:16 +00:00
historySearchForm += \
2020-12-07 12:16:30 +00:00
'<center><h1><a href="' + actor + '/search">' + \
2021-05-03 22:31:06 +00:00
historySearchTitle + '</a></h1></center>'
2020-11-19 14:02:16 +00:00
if len(boxFilenames) == 0:
historySearchForm += \
'<center><h5>' + translate['No results'] + \
'</h5></center>'
return historySearchForm
2021-12-29 21:55:09 +00:00
separatorStr = html_post_separator(base_dir, None)
2020-11-19 14:02:16 +00:00
# ensure that the page number is in bounds
if not pageNumber:
pageNumber = 1
elif pageNumber < 1:
pageNumber = 1
# get the start end end within the index file
startIndex = int((pageNumber - 1) * postsPerPage)
endIndex = startIndex + postsPerPage
noOfBoxFilenames = len(boxFilenames)
if endIndex >= noOfBoxFilenames and noOfBoxFilenames > 0:
endIndex = noOfBoxFilenames - 1
index = startIndex
while index <= endIndex:
2021-12-26 23:41:34 +00:00
post_filename = boxFilenames[index]
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
showIndividualPostIcons = True
2021-12-25 21:29:53 +00:00
allow_deletion = False
2020-11-19 14:02:16 +00:00
postStr = \
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,
showIndividualPostIcons,
showIndividualPostIcons,
False, False, False, False,
cw_lists, lists_enabled)
2020-11-19 14:02:16 +00:00
if postStr:
historySearchForm += separatorStr + postStr
index += 1
2021-12-29 21:55:09 +00:00
historySearchForm += html_footer()
2020-11-19 14:02:16 +00:00
return historySearchForm
2020-11-09 19:41:01 +00:00
2021-12-29 21:55:09 +00:00
def html_hashtag_search(css_cache: {},
nickname: str, domain: str, port: int,
recent_posts_cache: {}, max_recent_posts: int,
translate: {},
base_dir: str, hashtag: str, pageNumber: int,
postsPerPage: 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) -> 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)
2021-12-25 16:17:53 +00:00
hashtagIndexFile = base_dir + '/tags/' + hashtag + '.txt'
2020-11-09 19:41:01 +00:00
if not os.path.isfile(hashtagIndexFile):
if hashtag != hashtag.lower():
hashtag = hashtag.lower()
2021-12-25 16:17:53 +00:00
hashtagIndexFile = base_dir + '/tags/' + hashtag + '.txt'
2020-11-09 19:41:01 +00:00
if not os.path.isfile(hashtagIndexFile):
print('WARN: hashtag file not found ' + hashtagIndexFile)
return None
2021-12-29 21:55:09 +00:00
separatorStr = html_post_separator(base_dir, None)
2020-11-09 19:41:01 +00:00
# check that the directory for the nickname exists
if nickname:
2021-12-26 12:02:29 +00:00
accountDir = acct_dir(base_dir, nickname, domain)
2021-07-13 21:59:53 +00:00
if not os.path.isdir(accountDir):
2020-11-09 19:41:01 +00:00
nickname = None
# read the index
2021-07-13 14:40:49 +00:00
with open(hashtagIndexFile, 'r') as f:
2020-11-09 19:41:01 +00:00
lines = f.readlines()
# 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
if not pageNumber:
pageNumber = 1
elif pageNumber < 1:
pageNumber = 1
# get the start end end within the index file
startIndex = int((pageNumber - 1) * postsPerPage)
endIndex = startIndex + postsPerPage
noOfLines = len(lines)
if endIndex >= noOfLines and noOfLines > 0:
endIndex = noOfLines - 1
# add the page title
2021-01-11 19:46:21 +00:00
instanceTitle = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
hashtagSearchForm = \
2021-12-31 21:18:12 +00:00
html_header_with_external_style(css_filename, instanceTitle, None)
2020-11-09 19:41:01 +00:00
if nickname:
hashtagSearchForm += '<center>\n' + \
'<h1><a href="/users/' + nickname + '/search">#' + \
2020-12-01 22:21:33 +00:00
hashtag + '</a></h1>\n'
2020-11-09 19:41:01 +00:00
else:
hashtagSearchForm += '<center>\n' + \
2020-12-01 22:21:33 +00:00
'<h1>#' + hashtag + '</h1>\n'
# RSS link for hashtag feed
hashtagSearchForm += '<a href="/tags/rss2/' + hashtag + '">'
hashtagSearchForm += \
'<img style="width:3%;min-width:50px" ' + \
'loading="lazy" alt="RSS 2.0" title="RSS 2.0" src="/' + \
2020-12-09 13:08:26 +00:00
'icons/logorss.png" /></a></center>\n'
2020-11-09 19:41:01 +00:00
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)
hashtagSearchForm += '<div class="hashtagCategoryContainer">\n'
2020-12-02 11:55:40 +00:00
hashtagSearchForm += ' <form enctype="multipart/form-data" ' + \
'method="POST" accept-charset="UTF-8" action="' + \
2020-12-02 11:12:56 +00:00
'/users/' + nickname + '/tags/' + hashtag + \
'/sethashtagcategory">\n'
2020-12-01 21:44:27 +00:00
hashtagSearchForm += ' <center>\n'
2020-12-01 21:48:12 +00:00
hashtagSearchForm += translate['Category']
2020-12-01 21:44:27 +00:00
hashtagSearchForm += \
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'
2020-12-01 21:44:27 +00:00
hashtagSearchForm += \
' <button type="submit" class="button" name="submitYes">' + \
translate['Submit'] + '</button>\n'
hashtagSearchForm += ' </center>\n'
hashtagSearchForm += ' </form>\n'
hashtagSearchForm += '</div>\n'
2020-11-09 19:41:01 +00:00
if startIndex > 0:
# previous page link
hashtagSearchForm += \
' <center>\n' + \
2021-10-20 16:08:20 +00:00
' <a href="/users/' + nickname + \
'/tags/' + hashtag + '?page=' + \
2020-11-09 19:41:01 +00:00
str(pageNumber - 1) + \
'"><img loading="lazy" 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'
index = startIndex
while index <= endIndex:
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:
2021-12-26 19:47:06 +00:00
postFields = post_id.split(' ')
2020-11-09 19:41:01 +00:00
if len(postFields) != 3:
index += 1
continue
nickname = postFields[1]
2021-12-26 19:47:06 +00:00
post_id = postFields[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
showIndividualPostIcons = False
if nickname:
showIndividualPostIcons = True
2021-12-25 21:29:53 +00:00
allow_deletion = False
2020-11-30 10:44:37 +00:00
showRepeats = showIndividualPostIcons
showIcons = showIndividualPostIcons
manuallyApprovesFollowers = False
showPublicOnly = False
storeToCache = False
allowDownloads = True
avatarUrl = None
showAvatarOptions = True
postStr = \
2021-12-29 21:55:09 +00:00
individual_post_as_html(signing_priv_key_pem,
allowDownloads, recent_posts_cache,
max_recent_posts,
translate, None,
base_dir, session, cached_webfingers,
person_cache,
nickname, domain, port,
post_json_object,
avatarUrl, showAvatarOptions,
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,
showRepeats, showIcons,
manuallyApprovesFollowers,
showPublicOnly,
storeToCache, False, cw_lists,
lists_enabled)
2020-11-30 10:44:37 +00:00
if postStr:
hashtagSearchForm += separatorStr + postStr
2020-11-09 19:41:01 +00:00
index += 1
if endIndex < noOfLines - 1:
# next page link
hashtagSearchForm += \
' <center>\n' + \
2021-10-20 16:08:20 +00:00
' <a href="/users/' + nickname + '/tags/' + hashtag + \
2020-11-09 19:41:01 +00:00
'?page=' + str(pageNumber + 1) + \
2020-12-09 13:08:26 +00:00
'"><img loading="lazy" class="pageicon" src="/icons' + \
2020-11-09 19:41:01 +00:00
'/pagedown.png" title="' + translate['Page down'] + \
'" alt="' + translate['Page down'] + '"></a>' + \
' </center>'
2021-12-29 21:55:09 +00:00
hashtagSearchForm += html_footer()
2020-11-09 19:41:01 +00:00
return hashtagSearchForm
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,
postsPerPage: int,
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)
2021-12-25 16:17:53 +00:00
hashtagIndexFile = base_dir + '/tags/' + hashtag + '.txt'
2020-11-09 19:41:01 +00:00
if not os.path.isfile(hashtagIndexFile):
if hashtag != hashtag.lower():
hashtag = hashtag.lower()
2021-12-25 16:17:53 +00:00
hashtagIndexFile = base_dir + '/tags/' + hashtag + '.txt'
2020-11-09 19:41:01 +00:00
if not os.path.isfile(hashtagIndexFile):
print('WARN: hashtag file not found ' + hashtagIndexFile)
return None
# check that the directory for the nickname exists
if nickname:
2021-12-26 12:02:29 +00:00
accountDir = acct_dir(base_dir, nickname, domain)
2021-07-13 21:59:53 +00:00
if not os.path.isdir(accountDir):
2020-11-09 19:41:01 +00:00
nickname = None
# read the index
lines = []
2021-07-13 14:40:49 +00:00
with open(hashtagIndexFile, 'r') as f:
2020-11-09 19:41:01 +00:00
lines = f.readlines()
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
maxFeedLength = 10
hashtagFeed = \
2021-12-29 21:55:09 +00:00
rss2tag_header(hashtag, http_prefix, domain_full)
2020-11-09 19:41:01 +00:00
for index in range(len(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
if index >= maxFeedLength:
break
continue
else:
2021-12-26 19:47:06 +00:00
postFields = post_id.split(' ')
2020-11-09 19:41:01 +00:00
if len(postFields) != 3:
index += 1
if index >= maxFeedLength:
break
continue
nickname = postFields[1]
2021-12-26 19:47:06 +00:00
post_id = postFields[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
if index >= maxFeedLength:
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
if index >= maxFeedLength:
break
continue
# add to feed
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('content') and \
post_json_object['object'].get('attributedTo') and \
post_json_object['object'].get('published'):
published = post_json_object['object']['published']
2020-11-09 19:41:01 +00:00
pubDate = datetime.strptime(published, "%Y-%m-%dT%H:%M:%SZ")
rssDateStr = pubDate.strftime("%a, %d %b %Y %H:%M:%S UT")
hashtagFeed += ' <item>'
hashtagFeed += \
' <author>' + \
2021-12-25 22:09:19 +00:00
post_json_object['object']['attributedTo'] + \
2020-11-09 19:41:01 +00:00
'</author>'
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('summary'):
2020-11-09 19:41:01 +00:00
hashtagFeed += \
' <title>' + \
2021-12-25 22:09:19 +00:00
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)
2020-11-09 19:41:01 +00:00
hashtagFeed += \
' <description>' + description + '</description>'
hashtagFeed += \
' <pubDate>' + rssDateStr + '</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
hashtagFeed += \
' <link>' + attach['url'] + '</link>'
hashtagFeed += ' </item>'
index += 1
if index >= maxFeedLength:
break
2021-12-29 21:55:09 +00:00
return hashtagFeed + rss2tag_footer()