__filename__ = "webapp_timeline.py" __author__ = "Bob Mottram" __license__ = "AGPL3+" __version__ = "1.2.0" __maintainer__ = "Bob Mottram" __email__ = "bob@libreserver.org" __status__ = "Production" __module_group__ = "Timeline" import os import time from shutil import copyfile from utils import is_artist from utils import dangerous_markup from utils import get_config_param from utils import get_full_domain from utils import is_editor from utils import remove_id_ending from utils import acct_dir from utils import is_float from utils import local_actor_url from follow import follower_approval_active from person import is_person_snoozed from markdown import markdown_to_html from webapp_utils import html_keyboard_navigation from webapp_utils import html_hide_from_screen_reader from webapp_utils import html_post_separator from webapp_utils import get_banner_file from webapp_utils import html_header_with_external_style from webapp_utils import html_footer from webapp_utils import shares_timeline_json from webapp_utils import html_highlight_label from webapp_post import prepare_post_from_html_cache from webapp_post import individual_post_as_html from webapp_column_left import get_left_column_content from webapp_column_right import get_right_column_content from webapp_headerbuttons import header_buttons_timeline from posts import is_moderator from announce import is_self_announce def _log_timeline_timing(enableTimingLog: bool, timelineStartTime, boxName: str, debugId: str) -> None: """Create a log of timings for performance tuning """ if not enableTimingLog: return timeDiff = int((time.time() - timelineStartTime) * 1000) if timeDiff > 100: print('TIMELINE TIMING ' + boxName + ' ' + debugId + ' = ' + str(timeDiff)) def _get_help_for_timeline(base_dir: str, boxName: str) -> str: """Shows help text for the given timeline """ # get the filename for help for this timeline helpFilename = base_dir + '/accounts/help_' + boxName + '.md' if not os.path.isfile(helpFilename): language = \ get_config_param(base_dir, 'language') if not language: language = 'en' theme_name = \ get_config_param(base_dir, 'theme') defaultFilename = None if theme_name: defaultFilename = \ base_dir + '/theme/' + theme_name + '/welcome/' + \ 'help_' + boxName + '_' + language + '.md' if not os.path.isfile(defaultFilename): defaultFilename = None if not defaultFilename: defaultFilename = \ base_dir + '/defaultwelcome/' + \ 'help_' + boxName + '_' + language + '.md' if not os.path.isfile(defaultFilename): defaultFilename = \ base_dir + '/defaultwelcome/help_' + boxName + '_en.md' if os.path.isfile(defaultFilename): copyfile(defaultFilename, helpFilename) # show help text if os.path.isfile(helpFilename): instanceTitle = \ get_config_param(base_dir, 'instanceTitle') if not instanceTitle: instanceTitle = 'Epicyon' with open(helpFilename, 'r') as helpFile: helpText = helpFile.read() if dangerous_markup(helpText, False): return '' helpText = helpText.replace('INSTANCE', instanceTitle) return '
' + \ leftColumnStr + ' | \n' # center column containing posts tlStr += '\n'
if not full_width_tl_button_header:
tlStr += \
header_buttons_timeline(defaultTimeline, boxName, pageNumber,
translate, usersPath, mediaButton,
blogsButton, featuresButton,
newsButton, inboxButton,
dmButton, newDM, repliesButton,
newReply, minimal, sentButton,
sharesButtonStr, wantedButtonStr,
bookmarksButtonStr,
eventsButtonStr, moderationButtonStr,
newPostButtonStr, base_dir, nickname,
domain, timelineStartTime,
newCalendarEvent, calendarPath,
calendarImage, followApprovals,
icons_as_buttons, accessKeys)
tlStr += ' \n'
# second row of buttons for moderator actions
tlStr += \
_html_timeline_moderation_buttons(moderator, boxName, nickname,
moderationActionStr, translate)
_log_timeline_timing(enableTimingLog, timelineStartTime, boxName, '6')
if boxName == 'tlshares':
maxSharesPerAccount = itemsPerPage
return (tlStr +
_html_shares_timeline(translate, pageNumber, itemsPerPage,
base_dir, actor, nickname, domain, port,
maxSharesPerAccount, http_prefix,
shared_items_federated_domains,
'shares') +
_html_timeline_end(base_dir, nickname, domain_full,
http_prefix, translate,
moderator, editor,
newswire, positive_voting,
show_publish_as_icon,
rss_icon_at_top, publish_button_at_top,
authorized, theme,
defaultTimeline, accessKeys,
boxName,
enableTimingLog, timelineStartTime) +
html_footer())
elif boxName == 'tlwanted':
maxSharesPerAccount = itemsPerPage
return (tlStr +
_html_shares_timeline(translate, pageNumber, itemsPerPage,
base_dir, actor, nickname, domain, port,
maxSharesPerAccount, http_prefix,
shared_items_federated_domains,
'wanted') +
_html_timeline_end(base_dir, nickname, domain_full,
http_prefix, translate,
moderator, editor,
newswire, positive_voting,
show_publish_as_icon,
rss_icon_at_top, publish_button_at_top,
authorized, theme,
defaultTimeline, accessKeys,
boxName,
enableTimingLog, timelineStartTime) +
html_footer())
_log_timeline_timing(enableTimingLog, timelineStartTime, boxName, '7')
# separator between posts which only appears in shell browsers
# such as Lynx and is not read by screen readers
if boxName != 'tlmedia':
textModeSeparator = \
' ' + _page_number_buttons(usersPath, boxName, pageNumber) tlStr += \ ' ' tlStr += ' \n'
# show each post in the timeline
for item in timelineJson['orderedItems']:
if item['type'] == 'Create' or \
item['type'] == 'Announce':
# is the actor who sent this post snoozed?
if is_person_snoozed(base_dir, nickname, domain,
item['actor']):
continue
if is_self_announce(item):
continue
# is the post in the memory cache of recent ones?
currTlStr = None
if boxName != 'tlmedia' and recent_posts_cache.get('html'):
post_id = remove_id_ending(item['id']).replace('/', '#')
if recent_posts_cache['html'].get(post_id):
currTlStr = recent_posts_cache['html'][post_id]
currTlStr = \
prepare_post_from_html_cache(nickname,
currTlStr,
boxName,
pageNumber)
_log_timeline_timing(enableTimingLog,
timelineStartTime,
boxName, '10')
if not currTlStr:
_log_timeline_timing(enableTimingLog,
timelineStartTime,
boxName, '11')
# read the post from disk
currTlStr = \
individual_post_as_html(signing_priv_key_pem,
False, recent_posts_cache,
max_recent_posts,
translate, pageNumber,
base_dir, session,
cached_webfingers,
person_cache,
nickname, domain, port,
item, None, True,
allow_deletion,
http_prefix, project_version,
boxName,
yt_replace_domain,
twitter_replacement_domain,
show_published_date_only,
peertube_instances,
allow_local_network_access,
theme, system_language,
max_like_count,
boxName != 'dm',
showIndividualPostIcons,
manuallyApproveFollowers,
False, True, useCacheOnly,
cw_lists, lists_enabled)
_log_timeline_timing(enableTimingLog,
timelineStartTime, boxName, '12')
if currTlStr:
if currTlStr not in tlStr:
itemCtr += 1
tlStr += textModeSeparator + currTlStr
if separatorStr:
tlStr += separatorStr
if boxName == 'tlmedia':
tlStr += ' \n'
if itemCtr < 3:
print('Items added to html timeline ' + boxName + ': ' +
str(itemCtr) + ' ' + str(timelineJson['orderedItems']))
# page down arrow
if itemCtr > 0:
tlStr += textModeSeparator
tlStr += \
' \n' + \ ' \n'
profileStr += \
'\n'
profileStr += \
'\n\n'
profileStr += ' \n'
return profileStr
def _html_shares_timeline(translate: {}, pageNumber: int, itemsPerPage: int,
base_dir: str, actor: str,
nickname: str, domain: str, port: int,
maxSharesPerAccount: int, http_prefix: str,
shared_items_federated_domains: [],
sharesFileType: str) -> str:
"""Show shared items timeline as html
"""
sharesJson, lastPage = \
shares_timeline_json(actor, pageNumber, itemsPerPage,
base_dir, domain, nickname, maxSharesPerAccount,
shared_items_federated_domains, sharesFileType)
domain_full = get_full_domain(domain, port)
actor = local_actor_url(http_prefix, nickname, domain_full)
adminNickname = get_config_param(base_dir, 'admin')
adminActor = ''
if adminNickname:
adminActor = \
local_actor_url(http_prefix, adminNickname, domain_full)
timelineStr = ''
if pageNumber > 1:
timelineStr += ' \n'
if sharedItem.get('imageUrl'):
profileStr += '' + sharedItem['summary'] + ' \n'
if sharedItem.get('itemQty'):
if sharedItem['itemQty'] > 1:
profileStr += \
'' + translate['Quantity'] + ': ' + \
str(sharedItem['itemQty']) + ' ' + \ '' + \ '\n' profileStr += \ '\n' if removeButton and domain in shareId: if sharesFileType == 'shares': profileStr += \ ' \n' else: profileStr += \ ' \n' profileStr += ' ' + \ _page_number_buttons(actor, 'tl' + sharesFileType, pageNumber) timelineStr += \ ' |