epicyon/webapp_headerbuttons.py

395 lines
16 KiB
Python
Raw Normal View History

2020-11-17 20:40:36 +00:00
__filename__ = "webapp_headerbuttons.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2022-02-03 13:58:20 +00:00
__version__ = "1.3.0"
2020-11-17 20:40:36 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-11-17 20:40:36 +00:00
__status__ = "Production"
2021-06-26 11:27:14 +00:00
__module_group__ = "Timeline"
2020-11-17 20:40:36 +00:00
import os
2020-11-17 20:40:36 +00:00
import time
2021-12-26 12:02:29 +00:00
from utils import acct_dir
2020-11-17 20:40:36 +00:00
from datetime import datetime
2021-05-31 11:57:36 +00:00
from datetime import timedelta
2021-12-29 21:55:09 +00:00
from happening import day_events_check
from webapp_utils import html_highlight_label
2020-11-17 20:40:36 +00:00
2021-12-31 23:50:29 +00:00
def header_buttons_timeline(default_timeline: str,
2022-01-03 23:24:44 +00:00
box_name: str,
page_number: int,
2021-12-29 21:55:09 +00:00
translate: {},
2022-01-03 23:24:44 +00:00
users_path: str,
2022-05-30 18:33:51 +00:00
media_button: str,
2022-01-03 23:24:44 +00:00
blogs_button: str,
features_button: str,
news_button: str,
inbox_button: str,
dm_button: str,
new_dm: str,
replies_button: str,
new_reply: str,
2021-12-29 21:55:09 +00:00
minimal: bool,
2022-01-03 23:24:44 +00:00
sent_button: str,
shares_button_str: str,
wanted_button_str: str,
bookmarks_button_str: str,
events_button_str: str,
moderation_button_str: str,
new_post_button_str: str,
2021-12-29 21:55:09 +00:00
base_dir: str,
nickname: str, domain: str,
2022-01-03 23:24:44 +00:00
timeline_start_time,
new_calendar_event: bool,
calendar_path: str,
calendar_image: str,
follow_approvals: str,
2021-12-29 21:55:09 +00:00
icons_as_buttons: bool,
2021-12-31 21:18:12 +00:00
access_keys: {}) -> str:
2020-11-17 20:40:36 +00:00
"""Returns the header at the top of the timeline, containing
buttons for inbox, outbox, search, calendar, etc
"""
# start of the button header with inbox, outbox, etc
2022-05-25 09:43:14 +00:00
tl_str = '<div id="containerHeader" class="containerHeader"><nav>\n'
2020-11-17 20:40:36 +00:00
# first button
2021-12-31 23:50:29 +00:00
if default_timeline == 'tlmedia':
2022-01-03 23:24:44 +00:00
tl_str += \
2022-05-25 10:36:15 +00:00
'<a href="' + users_path + '/tlmedia" tabindex="2" ' + \
'accesskey="' + access_keys['menuMedia'] + '"'
if box_name == 'tlmedia':
tl_str += ' aria-current="location"'
tl_str += \
2021-04-23 12:24:34 +00:00
'><button class="' + \
2022-05-30 18:33:51 +00:00
media_button + '"><span>' + translate['Media'] + \
2020-11-17 20:40:36 +00:00
'</span></button></a>'
2021-12-31 23:50:29 +00:00
elif default_timeline == 'tlblogs':
2022-01-03 23:24:44 +00:00
tl_str += \
'<a href="' + users_path + \
2022-05-25 10:36:15 +00:00
'/tlblogs" tabindex="2"'
if box_name == 'tlblogs':
tl_str += ' aria-current="location"'
tl_str += \
'><button class="' + \
2022-05-24 22:07:29 +00:00
blogs_button + '" accesskey="' + access_keys['menuBlogs'] + \
'"><span>' + translate['Blogs'] + '</span></button></a>'
2021-12-31 23:50:29 +00:00
elif default_timeline == 'tlfeatures':
2022-01-03 23:24:44 +00:00
tl_str += \
'<a href="' + users_path + \
2022-05-25 10:36:15 +00:00
'/tlfeatures" tabindex="2"'
if box_name == 'tlfeatures':
tl_str += ' aria-current="location"'
tl_str += \
'><button class="' + \
2022-01-03 23:24:44 +00:00
features_button + '"><span>' + translate['Features'] + \
2020-11-17 20:40:36 +00:00
'</span></button></a>'
else:
2022-01-03 23:24:44 +00:00
tl_str += \
'<a href="' + users_path + \
2022-05-25 10:36:15 +00:00
'/inbox" tabindex="2"><button class="' + \
inbox_button + '"'
if box_name == 'inbox':
tl_str += ' aria-current="location"'
tl_str += \
2022-05-24 21:54:27 +00:00
' accesskey="' + access_keys['menuInbox'] + '">' + \
'<span>' + translate['Inbox'] + '</span></button></a>'
2020-11-17 20:40:36 +00:00
# if this is a news instance and we are viewing the news timeline
2022-01-03 23:24:44 +00:00
features_header = False
if default_timeline == 'tlfeatures' and box_name == 'tlfeatures':
features_header = True
2020-11-17 20:40:36 +00:00
2022-01-03 23:24:44 +00:00
if not features_header:
tl_str += \
2022-05-25 10:54:35 +00:00
'<a href="' + users_path + '/dm" tabindex="2"'
if box_name == 'dm':
tl_str += ' aria-current="location"'
tl_str += \
'><button class="' + dm_button + \
2022-05-24 21:54:27 +00:00
'" accesskey="' + access_keys['menuDM'] + '">' + \
'<span>' + html_highlight_label(translate['DM'], new_dm) + \
2020-11-17 20:40:36 +00:00
'</span></button></a>'
2022-01-03 23:24:44 +00:00
replies_index_filename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/tlreplies.index'
2022-01-03 23:24:44 +00:00
if os.path.isfile(replies_index_filename):
tl_str += \
2022-05-25 10:54:35 +00:00
'<a href="' + users_path + '/tlreplies" tabindex="2"'
if box_name == 'tlreplies':
tl_str += ' aria-current="location"'
tl_str += \
2022-05-24 21:54:27 +00:00
'><button class="' + replies_button + '" ' + \
'accesskey="' + access_keys['menuReplies'] + '"><span>' + \
2022-01-03 23:24:44 +00:00
html_highlight_label(translate['Replies'], new_reply) + \
'</span></button></a>'
2020-11-17 20:40:36 +00:00
# typically the media button
2021-12-31 23:50:29 +00:00
if default_timeline != 'tlmedia':
2022-01-03 23:24:44 +00:00
if not minimal and not features_header:
tl_str += \
2022-05-25 10:54:35 +00:00
'<a href="' + users_path + '/tlmedia" tabindex="2" ' + \
'accesskey="' + access_keys['menuMedia'] + '"'
if box_name == 'tlmedia':
tl_str += ' aria-current="location"'
tl_str += \
'><button class="' + \
2022-05-30 18:33:51 +00:00
media_button + '"><span>' + translate['Media'] + \
2020-11-17 20:40:36 +00:00
'</span></button></a>'
else:
if not minimal:
2022-01-03 23:24:44 +00:00
tl_str += \
'<a href="' + users_path + \
2022-05-25 10:54:35 +00:00
'/inbox" tabindex="2"'
if box_name == 'inbox':
tl_str += ' aria-current="location"'
tl_str += \
'><button class="' + \
2022-01-03 23:24:44 +00:00
inbox_button + '"><span>' + translate['Inbox'] + \
2020-11-17 20:40:36 +00:00
'</span></button></a>'
2022-01-03 23:24:44 +00:00
if not features_header:
2020-11-17 20:40:36 +00:00
# typically the blogs button
# but may change if this is a blogging oriented instance
2021-12-31 23:50:29 +00:00
if default_timeline != 'tlblogs':
2020-12-03 12:19:57 +00:00
if not minimal:
2022-01-03 23:24:44 +00:00
title_str = translate['Blogs']
2021-12-31 23:50:29 +00:00
if default_timeline == 'tlfeatures':
2022-01-03 23:24:44 +00:00
title_str = translate['Article']
tl_str += \
'<a href="' + users_path + \
2022-05-25 10:54:35 +00:00
'/tlblogs" tabindex="2"'
if box_name == 'tlblogs':
tl_str += ' aria-current="location"'
tl_str += \
2022-05-24 22:07:29 +00:00
'><button class="' + blogs_button + '" accesskey="' + \
access_keys['menuBlogs'] + '"><span>' + title_str + \
2020-11-17 20:40:36 +00:00
'</span></button></a>'
else:
if not minimal:
2022-01-03 23:24:44 +00:00
tl_str += \
'<a href="' + users_path + \
2022-05-25 10:54:35 +00:00
'/inbox" tabindex="2"'
if box_name == 'inbox':
tl_str += ' aria-current="location"'
tl_str += \
'><button class="' + \
2022-01-03 23:24:44 +00:00
inbox_button + '"><span>' + translate['Inbox'] + \
2020-11-17 20:40:36 +00:00
'</span></button></a>'
# typically the news button
# but may change if this is a news oriented instance
2021-12-31 23:50:29 +00:00
if default_timeline == 'tlfeatures':
2022-01-03 23:24:44 +00:00
if not features_header:
tl_str += \
'<a href="' + users_path + \
2022-05-25 10:54:35 +00:00
'/inbox" tabindex="2"'
if box_name == 'inbox':
tl_str += ' aria-current="location"'
tl_str += \
'><button class="' + \
2022-05-24 21:54:27 +00:00
inbox_button + '" accesskey="' + \
access_keys['menuInbox'] + '"><span>' + translate['Inbox'] + \
2020-11-17 20:40:36 +00:00
'</span></button></a>'
# show todays events buttons on the first inbox page
2022-01-03 23:24:44 +00:00
happening_str = ''
if box_name == 'inbox' and page_number == 1:
2021-05-31 11:57:36 +00:00
now = datetime.now()
tomorrow = datetime.now() + timedelta(1)
twodays = datetime.now() + timedelta(2)
2021-12-29 21:55:09 +00:00
if day_events_check(base_dir, nickname, domain, now):
2020-11-17 20:40:36 +00:00
# happening today button
2021-12-25 19:19:14 +00:00
if not icons_as_buttons:
2022-01-03 23:24:44 +00:00
happening_str += \
'<a href="' + users_path + '/calendar?year=' + \
2020-11-17 20:40:36 +00:00
str(now.year) + '?month=' + str(now.month) + \
2022-05-25 10:54:35 +00:00
'?day=' + str(now.day) + '" tabindex="2">' + \
2020-11-17 20:40:36 +00:00
'<button class="buttonevent">' + \
translate['Happening Today'] + '</button></a>'
else:
2022-01-03 23:24:44 +00:00
happening_str += \
'<a href="' + users_path + '/calendar?year=' + \
2020-11-17 20:40:36 +00:00
str(now.year) + '?month=' + str(now.month) + \
2022-05-25 10:54:35 +00:00
'?day=' + str(now.day) + '" tabindex="2">' + \
2020-11-17 20:40:36 +00:00
'<button class="button">' + \
translate['Happening Today'] + '</button></a>'
2021-12-29 21:55:09 +00:00
elif day_events_check(base_dir, nickname, domain, tomorrow):
2021-05-31 11:57:36 +00:00
# happening tomorrow button
2021-12-25 19:19:14 +00:00
if not icons_as_buttons:
2022-01-03 23:24:44 +00:00
happening_str += \
'<a href="' + users_path + '/calendar?year=' + \
2021-05-31 11:57:36 +00:00
str(tomorrow.year) + '?month=' + str(tomorrow.month) + \
2022-05-25 10:54:35 +00:00
'?day=' + str(tomorrow.day) + '" tabindex="2">' + \
2021-05-31 11:57:36 +00:00
'<button class="buttonevent">' + \
translate['Happening Tomorrow'] + '</button></a>'
else:
2022-01-03 23:24:44 +00:00
happening_str += \
'<a href="' + users_path + '/calendar?year=' + \
2021-05-31 11:57:36 +00:00
str(tomorrow.year) + '?month=' + str(tomorrow.month) + \
2022-05-25 10:54:35 +00:00
'?day=' + str(tomorrow.day) + '" tabindex="2">' + \
2021-05-31 11:57:36 +00:00
'<button class="button">' + \
translate['Happening Tomorrow'] + '</button></a>'
2021-12-29 21:55:09 +00:00
elif day_events_check(base_dir, nickname, domain, twodays):
2021-12-25 19:19:14 +00:00
if not icons_as_buttons:
2022-01-03 23:24:44 +00:00
happening_str += \
'<a href="' + users_path + \
2022-05-25 10:54:35 +00:00
'/calendar" tabindex="2">' + \
2021-05-31 11:57:36 +00:00
'<button class="buttonevent">' + \
translate['Happening This Week'] + '</button></a>'
else:
2022-01-03 23:24:44 +00:00
happening_str += \
'<a href="' + users_path + \
2022-05-25 10:54:35 +00:00
'/calendar" tabindex="2">' + \
2021-05-31 11:57:36 +00:00
'<button class="button">' + \
translate['Happening This Week'] + '</button></a>'
2020-11-17 20:40:36 +00:00
2022-01-03 23:24:44 +00:00
if not features_header:
2020-11-17 20:40:36 +00:00
# button for the outbox
2022-01-03 23:24:44 +00:00
tl_str += \
'<a href="' + users_path + '/outbox"'
if box_name == 'outbox':
tl_str += ' aria-current="location"'
tl_str += \
'><button class="' + \
2022-05-25 10:54:35 +00:00
sent_button + '" tabindex="2" accesskey="' + \
2022-05-24 21:54:27 +00:00
access_keys['menuOutbox'] + '">' + \
'<span>' + translate['Sent'] + '</span></button></a>'
2020-11-17 20:40:36 +00:00
# add other buttons
2022-01-03 23:24:44 +00:00
tl_str += \
shares_button_str + wanted_button_str + bookmarks_button_str + \
events_button_str + \
moderation_button_str + happening_str + new_post_button_str
2020-11-17 20:40:36 +00:00
2022-01-03 23:24:44 +00:00
if not features_header:
2021-12-25 19:19:14 +00:00
if not icons_as_buttons:
2020-11-17 20:40:36 +00:00
# the search icon
2022-01-03 23:24:44 +00:00
tl_str += \
'<a class="imageAnchor" href="' + users_path + \
2022-05-24 21:58:36 +00:00
'/search" accesskey="' + access_keys['menuSearch'] + \
2022-05-25 10:58:43 +00:00
'" tabindex="3">' + \
2022-05-25 10:36:15 +00:00
'<img loading="lazy" decoding="async" src="/' + \
2020-12-09 13:08:26 +00:00
'icons/search.png" title="' + \
2020-11-17 20:40:36 +00:00
translate['Search and follow'] + '" alt="| ' + \
translate['Search and follow'] + \
'" class="timelineicon"/></a>'
else:
# the search button
2022-01-03 23:24:44 +00:00
tl_str += \
'<a href="' + users_path + \
2022-05-25 10:58:43 +00:00
'/search" tabindex="3">' + \
2022-05-24 21:58:36 +00:00
'<button class="button" ' + \
'accesskey="' + access_keys['menuSearch'] + '>' + \
2020-11-17 20:40:36 +00:00
'<span>' + translate['Search'] + \
'</span></button></a>'
# benchmark 5
2022-01-03 23:24:44 +00:00
time_diff = int((time.time() - timeline_start_time) * 1000)
2021-12-31 21:18:12 +00:00
if time_diff > 100:
2022-01-03 23:24:44 +00:00
print('TIMELINE TIMING ' + box_name + ' 5 = ' + str(time_diff))
2020-11-17 20:40:36 +00:00
# the calendar button
2022-01-03 23:24:44 +00:00
if not features_header:
calendar_alt_text = translate['Calendar']
if new_calendar_event:
2020-11-17 20:40:36 +00:00
# indicate that the calendar icon is highlighted
2022-01-03 23:24:44 +00:00
calendar_alt_text = '*' + calendar_alt_text + '*'
2021-12-25 19:19:14 +00:00
if not icons_as_buttons:
2022-01-03 23:24:44 +00:00
tl_str += \
2020-11-17 20:40:36 +00:00
' <a class="imageAnchor" href="' + \
2022-01-03 23:24:44 +00:00
users_path + calendar_path + \
2022-05-24 22:02:00 +00:00
'" accesskey="' + access_keys['menuCalendar'] + \
2022-05-25 10:58:43 +00:00
'" tabindex="3">' + \
2022-05-25 10:36:15 +00:00
'<img loading="lazy" decoding="async" src="/icons/' + \
2022-01-03 23:24:44 +00:00
calendar_image + '" title="' + translate['Calendar'] + \
'" alt="| ' + calendar_alt_text + \
2020-11-17 20:40:36 +00:00
'" class="timelineicon"/></a>\n'
else:
2022-01-03 23:24:44 +00:00
tl_str += \
'<a href="' + users_path + calendar_path + \
2022-05-25 10:58:43 +00:00
'" tabindex="3"><button class="button" accesskey="' + \
2022-05-24 22:02:00 +00:00
access_keys['menuCalendar'] + '">' + \
2020-11-17 20:40:36 +00:00
'<span>' + translate['Calendar'] + \
'</span></button></a>'
2022-01-03 23:24:44 +00:00
if not features_header:
2020-11-17 20:40:36 +00:00
# the show/hide button, for a simpler header appearance
2021-12-25 19:19:14 +00:00
if not icons_as_buttons:
2022-01-03 23:24:44 +00:00
tl_str += \
2020-11-17 20:40:36 +00:00
' <a class="imageAnchor" href="' + \
2022-05-25 10:58:43 +00:00
users_path + '/minimal" tabindex="3">' + \
2022-05-25 10:36:15 +00:00
'<img loading="lazy" decoding="async" src="/icons' + \
2020-11-17 20:40:36 +00:00
'/showhide.png" title="' + translate['Show/Hide Buttons'] + \
'" alt="| ' + translate['Show/Hide Buttons'] + \
'" class="timelineicon"/></a>\n'
else:
2022-01-03 23:24:44 +00:00
tl_str += \
'<a href="' + users_path + '/minimal' + \
2022-05-25 10:58:43 +00:00
'" tabindex="3"><button class="button">' + \
2020-11-17 20:40:36 +00:00
'<span>' + translate['Show/Hide Buttons'] + \
'</span></button></a>'
2022-01-03 23:24:44 +00:00
if features_header:
tl_str += \
2022-05-25 10:54:35 +00:00
'<a href="' + users_path + '/inbox" tabindex="2"'
if box_name == 'inbox':
tl_str += ' aria-current="location"'
tl_str += \
'><button class="button">' + \
2020-11-17 20:40:36 +00:00
'<span>' + translate['User'] + '</span></button></a>'
# the newswire button to show right column links
2021-12-25 19:19:14 +00:00
if not icons_as_buttons:
2022-01-03 23:24:44 +00:00
tl_str += \
2020-11-17 20:40:36 +00:00
'<a class="imageAnchorMobile" href="' + \
2022-01-03 23:24:44 +00:00
users_path + '/newswiremobile">' + \
2022-03-28 08:47:53 +00:00
'<img loading="lazy" decoding="async" src="/icons' + \
2020-11-17 20:40:36 +00:00
'/newswire.png" title="' + translate['News'] + \
'" alt="| ' + translate['News'] + \
'" class="timelineicon"/></a>'
else:
# NOTE: deliberately no \n at end of line
2022-01-03 23:24:44 +00:00
tl_str += \
2020-11-17 20:40:36 +00:00
'<a href="' + \
2022-01-03 23:24:44 +00:00
users_path + '/newswiremobile' + \
2022-05-25 10:54:35 +00:00
'" tabindex="2"><button class="buttonMobile">' + \
2020-11-17 20:40:36 +00:00
'<span>' + translate['Newswire'] + \
'</span></button></a>'
# the links button to show left column links
2021-12-25 19:19:14 +00:00
if not icons_as_buttons:
2022-01-03 23:24:44 +00:00
tl_str += \
2020-11-17 20:40:36 +00:00
'<a class="imageAnchorMobile" href="' + \
2022-01-03 23:24:44 +00:00
users_path + '/linksmobile">' + \
2022-03-28 08:47:53 +00:00
'<img loading="lazy" decoding="async" src="/icons' + \
2020-11-17 20:40:36 +00:00
'/links.png" title="' + translate['Edit Links'] + \
'" alt="| ' + translate['Edit Links'] + \
'" class="timelineicon"/></a>'
else:
# NOTE: deliberately no \n at end of line
2022-01-03 23:24:44 +00:00
tl_str += \
2020-11-17 20:40:36 +00:00
'<a href="' + \
2022-01-03 23:24:44 +00:00
users_path + '/linksmobile' + \
2022-05-25 10:54:35 +00:00
'" tabindex="2"><button class="buttonMobile">' + \
2020-11-17 20:40:36 +00:00
'<span>' + translate['Links'] + \
'</span></button></a>'
2022-01-03 23:24:44 +00:00
if features_header:
tl_str += \
2022-05-25 10:54:35 +00:00
'<a href="' + users_path + '/editprofile" tabindex="2">' + \
2020-11-17 20:40:36 +00:00
'<button class="buttonDesktop">' + \
'<span>' + translate['Settings'] + '</span></button></a>'
2022-01-03 23:24:44 +00:00
if not features_header:
tl_str += follow_approvals
2020-11-17 20:40:36 +00:00
2021-12-25 19:19:14 +00:00
if not icons_as_buttons:
2020-11-17 20:40:36 +00:00
# end of headericons div
2022-01-03 23:24:44 +00:00
tl_str += '</div>'
2020-11-17 20:40:36 +00:00
# end of the button header with inbox, outbox, etc
2022-01-03 23:24:44 +00:00
tl_str += ' </nav></div>\n'
return tl_str