epicyon/webapp_headerbuttons.py

337 lines
14 KiB
Python
Raw Normal View History

2020-11-17 20:40:36 +00:00
__filename__ = "webapp_headerbuttons.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.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,
2021-12-29 21:55:09 +00:00
boxName: str,
pageNumber: int,
translate: {},
usersPath: str,
mediaButton: str,
blogsButton: str,
featuresButton: str,
newsButton: str,
inboxButton: str,
dmButton: str,
newDM: str,
repliesButton: str,
newReply: str,
minimal: bool,
sentButton: str,
sharesButtonStr: str,
wantedButtonStr: str,
bookmarksButtonStr: str,
eventsButtonStr: str,
moderationButtonStr: str,
newPostButtonStr: str,
base_dir: str,
nickname: str, domain: str,
timelineStartTime,
newCalendarEvent: bool,
calendarPath: str,
calendarImage: str,
followApprovals: str,
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
2020-12-27 16:42:03 +00:00
tlStr = '<div 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':
2020-11-17 20:40:36 +00:00
tlStr += \
2021-07-06 09:44:45 +00:00
'<a href="' + usersPath + '/tlmedia" tabindex="-1" ' + \
2021-12-31 21:18:12 +00:00
'accesskey="' + access_keys['menuMedia'] + '"' + \
2021-04-23 12:24:34 +00:00
'><button class="' + \
2020-11-17 20:40:36 +00:00
mediaButton + '"><span>' + translate['Media'] + \
'</span></button></a>'
2021-12-31 23:50:29 +00:00
elif default_timeline == 'tlblogs':
2020-11-17 20:40:36 +00:00
tlStr += \
'<a href="' + usersPath + \
2021-02-05 12:49:53 +00:00
'/tlblogs" tabindex="-1"><button class="' + \
2020-11-17 20:40:36 +00:00
blogsButton + '"><span>' + translate['Blogs'] + \
'</span></button></a>'
2021-12-31 23:50:29 +00:00
elif default_timeline == 'tlfeatures':
2020-11-27 11:28:56 +00:00
tlStr += \
'<a href="' + usersPath + \
2021-02-05 12:49:53 +00:00
'/tlfeatures" tabindex="-1"><button class="' + \
2020-11-27 12:42:51 +00:00
featuresButton + '"><span>' + translate['Features'] + \
2020-11-17 20:40:36 +00:00
'</span></button></a>'
else:
tlStr += \
'<a href="' + usersPath + \
2021-02-05 12:49:53 +00:00
'/inbox" tabindex="-1"><button class="' + \
2020-11-17 20:40:36 +00:00
inboxButton + '"><span>' + \
translate['Inbox'] + '</span></button></a>'
# if this is a news instance and we are viewing the news timeline
2020-11-29 09:38:35 +00:00
featuresHeader = False
2021-12-31 23:50:29 +00:00
if default_timeline == 'tlfeatures' and boxName == 'tlfeatures':
2020-11-29 09:38:35 +00:00
featuresHeader = True
2020-11-17 20:40:36 +00:00
2020-11-29 09:38:35 +00:00
if not featuresHeader:
2020-11-17 20:40:36 +00:00
tlStr += \
'<a href="' + usersPath + \
2021-02-05 12:49:53 +00:00
'/dm" tabindex="-1"><button class="' + dmButton + \
2021-12-29 21:55:09 +00:00
'"><span>' + html_highlight_label(translate['DM'], newDM) + \
2020-11-17 20:40:36 +00:00
'</span></button></a>'
repliesIndexFilename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/tlreplies.index'
if os.path.isfile(repliesIndexFilename):
tlStr += \
2021-02-05 12:49:53 +00:00
'<a href="' + usersPath + '/tlreplies" tabindex="-1">' + \
'<button class="' + repliesButton + '"><span>' + \
2021-12-29 21:55:09 +00:00
html_highlight_label(translate['Replies'], newReply) + \
'</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':
2020-11-29 09:38:35 +00:00
if not minimal and not featuresHeader:
2020-11-17 20:40:36 +00:00
tlStr += \
2021-07-06 09:44:45 +00:00
'<a href="' + usersPath + '/tlmedia" tabindex="-1" ' + \
2021-12-31 21:18:12 +00:00
'accesskey="' + access_keys['menuMedia'] + '">' + \
2021-04-23 12:31:20 +00:00
'<button class="' + \
2020-11-17 20:40:36 +00:00
mediaButton + '"><span>' + translate['Media'] + \
'</span></button></a>'
else:
if not minimal:
tlStr += \
'<a href="' + usersPath + \
2021-02-05 12:49:53 +00:00
'/inbox" tabindex="-1"><button class="' + \
2021-06-22 12:42:52 +00:00
inboxButton + '"><span>' + translate['Inbox'] + \
2020-11-17 20:40:36 +00:00
'</span></button></a>'
2020-12-03 12:19:57 +00:00
if not featuresHeader:
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:
2020-11-17 20:40:36 +00:00
titleStr = translate['Blogs']
2021-12-31 23:50:29 +00:00
if default_timeline == 'tlfeatures':
2020-11-17 20:40:36 +00:00
titleStr = translate['Article']
tlStr += \
'<a href="' + usersPath + \
2021-02-05 12:49:53 +00:00
'/tlblogs" tabindex="-1"><button class="' + \
2020-11-17 20:40:36 +00:00
blogsButton + '"><span>' + titleStr + \
'</span></button></a>'
else:
if not minimal:
tlStr += \
'<a href="' + usersPath + \
2021-02-05 12:49:53 +00:00
'/inbox" tabindex="-1"><button class="' + \
2020-11-17 20:40:36 +00:00
inboxButton + '"><span>' + translate['Inbox'] + \
'</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':
2020-11-29 09:38:35 +00:00
if not featuresHeader:
2020-11-17 20:40:36 +00:00
tlStr += \
'<a href="' + usersPath + \
2021-02-05 12:49:53 +00:00
'/inbox" tabindex="-1"><button class="' + \
2020-11-17 20:40:36 +00:00
inboxButton + '"><span>' + translate['Inbox'] + \
'</span></button></a>'
# show todays events buttons on the first inbox page
happeningStr = ''
if boxName == 'inbox' and pageNumber == 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:
2020-11-17 20:40:36 +00:00
happeningStr += \
'<a href="' + usersPath + '/calendar?year=' + \
str(now.year) + '?month=' + str(now.month) + \
2021-02-05 12:49:53 +00:00
'?day=' + str(now.day) + '" tabindex="-1">' + \
2020-11-17 20:40:36 +00:00
'<button class="buttonevent">' + \
translate['Happening Today'] + '</button></a>'
else:
happeningStr += \
'<a href="' + usersPath + '/calendar?year=' + \
str(now.year) + '?month=' + str(now.month) + \
2021-02-05 12:49:53 +00:00
'?day=' + str(now.day) + '" tabindex="-1">' + \
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:
2021-05-31 11:57:36 +00:00
happeningStr += \
'<a href="' + usersPath + '/calendar?year=' + \
str(tomorrow.year) + '?month=' + str(tomorrow.month) + \
'?day=' + str(tomorrow.day) + '" tabindex="-1">' + \
'<button class="buttonevent">' + \
translate['Happening Tomorrow'] + '</button></a>'
else:
happeningStr += \
'<a href="' + usersPath + '/calendar?year=' + \
str(tomorrow.year) + '?month=' + str(tomorrow.month) + \
'?day=' + str(tomorrow.day) + '" tabindex="-1">' + \
'<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:
2021-05-31 11:57:36 +00:00
happeningStr += \
'<a href="' + usersPath + \
'/calendar" tabindex="-1">' + \
'<button class="buttonevent">' + \
translate['Happening This Week'] + '</button></a>'
else:
happeningStr += \
'<a href="' + usersPath + \
'/calendar" tabindex="-1">' + \
'<button class="button">' + \
translate['Happening This Week'] + '</button></a>'
2020-11-17 20:40:36 +00:00
2020-11-29 09:38:35 +00:00
if not featuresHeader:
2020-11-17 20:40:36 +00:00
# button for the outbox
tlStr += \
2021-07-06 09:44:45 +00:00
'<a href="' + usersPath + '/outbox"><button class="' + \
sentButton + '" tabindex="-1"><span>' + translate['Sent'] + \
2020-11-17 20:40:36 +00:00
'</span></button></a>'
# add other buttons
tlStr += \
2021-08-09 18:41:05 +00:00
sharesButtonStr + wantedButtonStr + bookmarksButtonStr + \
eventsButtonStr + \
2020-11-17 20:40:36 +00:00
moderationButtonStr + happeningStr + newPostButtonStr
2020-11-29 09:38:35 +00:00
if not featuresHeader:
2021-12-25 19:19:14 +00:00
if not icons_as_buttons:
2020-11-17 20:40:36 +00:00
# the search icon
tlStr += \
'<a class="imageAnchor" href="' + usersPath + \
'/search"><img loading="lazy" 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
tlStr += \
'<a href="' + usersPath + \
2021-02-05 12:49:53 +00:00
'/search" tabindex="-1"><button class="button">' + \
2020-11-17 20:40:36 +00:00
'<span>' + translate['Search'] + \
'</span></button></a>'
# benchmark 5
2021-12-31 21:18:12 +00:00
time_diff = int((time.time() - timelineStartTime) * 1000)
if time_diff > 100:
print('TIMELINE TIMING ' + boxName + ' 5 = ' + str(time_diff))
2020-11-17 20:40:36 +00:00
# the calendar button
2020-12-03 12:19:57 +00:00
if not featuresHeader:
2020-11-17 20:40:36 +00:00
calendarAltText = translate['Calendar']
if newCalendarEvent:
# indicate that the calendar icon is highlighted
calendarAltText = '*' + calendarAltText + '*'
2021-12-25 19:19:14 +00:00
if not icons_as_buttons:
2020-11-17 20:40:36 +00:00
tlStr += \
' <a class="imageAnchor" href="' + \
usersPath + calendarPath + \
2020-12-09 13:08:26 +00:00
'"><img loading="lazy" src="/icons/' + \
2020-11-17 20:40:36 +00:00
calendarImage + '" title="' + translate['Calendar'] + \
'" alt="| ' + calendarAltText + \
'" class="timelineicon"/></a>\n'
else:
tlStr += \
'<a href="' + usersPath + calendarPath + \
2021-02-05 12:49:53 +00:00
'" tabindex="-1"><button class="button">' + \
2020-11-17 20:40:36 +00:00
'<span>' + translate['Calendar'] + \
'</span></button></a>'
2020-11-29 09:38:35 +00:00
if not featuresHeader:
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:
2020-11-17 20:40:36 +00:00
tlStr += \
' <a class="imageAnchor" href="' + \
usersPath + '/minimal' + \
2020-12-09 13:08:26 +00:00
'"><img loading="lazy" 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:
tlStr += \
'<a href="' + usersPath + '/minimal' + \
2021-02-05 12:49:53 +00:00
'" tabindex="-1"><button class="button">' + \
2020-11-17 20:40:36 +00:00
'<span>' + translate['Show/Hide Buttons'] + \
'</span></button></a>'
2020-11-29 09:38:35 +00:00
if featuresHeader:
2020-11-17 20:40:36 +00:00
tlStr += \
2021-02-05 12:49:53 +00:00
'<a href="' + usersPath + '/inbox" tabindex="-1">' + \
2020-11-17 20:40:36 +00:00
'<button class="button">' + \
'<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:
2020-11-17 20:40:36 +00:00
tlStr += \
'<a class="imageAnchorMobile" href="' + \
usersPath + '/newswiremobile">' + \
2020-12-09 13:08:26 +00:00
'<img loading="lazy" 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
tlStr += \
'<a href="' + \
usersPath + '/newswiremobile' + \
2021-02-05 12:49:53 +00:00
'" tabindex="-1"><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:
2020-11-17 20:40:36 +00:00
tlStr += \
'<a class="imageAnchorMobile" href="' + \
usersPath + '/linksmobile">' + \
2020-12-09 13:08:26 +00:00
'<img loading="lazy" 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
tlStr += \
'<a href="' + \
usersPath + '/linksmobile' + \
2021-02-05 12:49:53 +00:00
'" tabindex="-1"><button class="buttonMobile">' + \
2020-11-17 20:40:36 +00:00
'<span>' + translate['Links'] + \
'</span></button></a>'
2020-11-29 09:38:35 +00:00
if featuresHeader:
2020-11-17 20:40:36 +00:00
tlStr += \
2021-02-05 12:49:53 +00:00
'<a href="' + usersPath + '/editprofile" tabindex="-1">' + \
2020-11-17 20:40:36 +00:00
'<button class="buttonDesktop">' + \
'<span>' + translate['Settings'] + '</span></button></a>'
2020-11-29 09:38:35 +00:00
if not featuresHeader:
2020-11-17 20:40:36 +00:00
tlStr += followApprovals
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
tlStr += '</div>'
# end of the button header with inbox, outbox, etc
2020-12-27 16:42:03 +00:00
tlStr += ' </nav></div>\n'
2020-11-17 20:40:36 +00:00
return tlStr