__filename__ = "webapp_column_left.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.3.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Web Interface Columns"
import os
from utils import get_config_param
from utils import get_nickname_from_actor
from utils import is_editor
from utils import is_artist
from utils import remove_domain_port
from utils import local_actor_url
from webapp_utils import shares_timeline_json
from webapp_utils import html_post_separator
from webapp_utils import get_left_image_file
from webapp_utils import header_buttons_front_screen
from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer
from webapp_utils import get_banner_file
from webapp_utils import edit_text_field
from shares import share_category_icon
def _links_exist(base_dir: str) -> bool:
"""Returns true if links have been created
"""
links_filename = base_dir + '/accounts/links.txt'
return os.path.isfile(links_filename)
def _get_left_column_shares(base_dir: str,
http_prefix: str, domain: str, domain_full: str,
nickname: str,
max_shares_in_left_column: int,
translate: {},
shared_items_federated_domains: []) -> []:
"""get any shares and turn them into the left column links format
"""
page_number = 1
actor = local_actor_url(http_prefix, nickname, domain_full)
# NOTE: this could potentially be slow if the number of federated
# shared items is large
shares_json, _ = \
shares_timeline_json(actor, page_number, max_shares_in_left_column,
base_dir, domain, nickname,
max_shares_in_left_column,
shared_items_federated_domains, 'shares')
if not shares_json:
return []
links_list = []
ctr = 0
for _, item in shares_json.items():
sharedesc = item['displayName']
if '<' in sharedesc or '?' in sharedesc:
continue
share_id = item['shareId']
# selecting this link calls html_show_share
share_link = actor + '?showshare=' + share_id
if item.get('category'):
share_link += '?category=' + item['category']
share_category = share_category_icon(item['category'])
links_list.append(share_category + sharedesc + ' ' + share_link)
ctr += 1
if ctr >= max_shares_in_left_column:
break
if links_list:
links_list = ['* ' + translate['Shares']] + links_list
return links_list
def _get_left_column_wanted(base_dir: str,
http_prefix: str, domain: str, domain_full: str,
nickname: str,
max_shares_in_left_column: int,
translate: {},
shared_items_federated_domains: []) -> []:
"""get any wanted items and turn them into the left column links format
"""
page_number = 1
actor = local_actor_url(http_prefix, nickname, domain_full)
# NOTE: this could potentially be slow if the number of federated
# wanted items is large
shares_json, _ = \
shares_timeline_json(actor, page_number, max_shares_in_left_column,
base_dir, domain, nickname,
max_shares_in_left_column,
shared_items_federated_domains, 'wanted')
if not shares_json:
return []
links_list = []
ctr = 0
for _, item in shares_json.items():
sharedesc = item['displayName']
if '<' in sharedesc or ';' in sharedesc:
continue
share_id = item['shareId']
# selecting this link calls html_show_share
share_link = actor + '?showwanted=' + share_id
links_list.append(sharedesc + ' ' + share_link)
ctr += 1
if ctr >= max_shares_in_left_column:
break
if links_list:
links_list = ['* ' + translate['Wanted']] + links_list
return links_list
def get_left_column_content(base_dir: str, nickname: str, domain_full: str,
http_prefix: str, translate: {},
editor: bool, artist: bool,
show_back_button: bool, timeline_path: str,
rss_icon_at_top: bool, show_header_image: bool,
front_page: bool, theme: str,
access_keys: {},
shared_items_federated_domains: []) -> str:
"""Returns html content for the left column
"""
html_str = ''
separator_str = html_post_separator(base_dir, 'left')
domain = remove_domain_port(domain_full)
edit_image_class = ''
if show_header_image:
left_image_file, left_column_image_filename = \
get_left_image_file(base_dir, nickname, domain, theme)
# show the image at the top of the column
edit_image_class = 'leftColEdit'
if os.path.isfile(left_column_image_filename):
edit_image_class = 'leftColEditImage'
html_str += \
'\n
\n \n' + \
' \n'
if show_back_button:
html_str += \
' ' + \
'\n'
if (editor or rss_icon_at_top) and not show_header_image:
html_str += '
'
if edit_image_class == 'leftColEdit':
html_str += '\n
\n'
html_str += ' \n'
if editor:
# show the edit icon
html_str += \
'
' + \
'\n'
if artist:
# show the theme designer icon
html_str += \
'
' + \
'\n'
# RSS icon
if nickname != 'news':
# rss feed for this account
rss_url = http_prefix + '://' + domain_full + \
'/blog/' + nickname + '/rss.xml'
else:
# rss feed for all accounts on the instance
rss_url = http_prefix + '://' + domain_full + '/blog/rss.xml'
if not front_page:
rss_title = translate['RSS feed for your blog']
else:
rss_title = translate['RSS feed for this site']
rss_icon_str = \
'
' + \
'\n'
if rss_icon_at_top:
html_str += rss_icon_str
html_str += '
\n'
if edit_image_class == 'leftColEdit':
html_str += ' \n'
if (editor or rss_icon_at_top) and not show_header_image:
html_str += '
'
# if show_header_image:
# html_str += '
'
# flag used not to show the first separator
first_separator_added = False
links_filename = base_dir + '/accounts/links.txt'
links_file_contains_entries = False
links_list = None
if os.path.isfile(links_filename):
with open(links_filename, 'r', encoding='utf-8') as fp_links:
links_list = fp_links.readlines()
if not front_page:
# show a number of shares
max_shares_in_left_column = 3
shares_list = \
_get_left_column_shares(base_dir,
http_prefix, domain, domain_full, nickname,
max_shares_in_left_column, translate,
shared_items_federated_domains)
if links_list and shares_list:
links_list = shares_list + links_list
wanted_list = \
_get_left_column_wanted(base_dir,
http_prefix, domain, domain_full, nickname,
max_shares_in_left_column, translate,
shared_items_federated_domains)
if links_list and wanted_list:
links_list = wanted_list + links_list
new_tab_str = ' target="_blank" rel="nofollow noopener noreferrer"'
if links_list:
html_str += '
\n'
if first_separator_added:
html_str += separator_str
html_str += \
'
' + translate['Shares Catalog'] + '
'
html_str += \
'
' + \
translate['Key Shortcuts'] + '
'
html_str += \
'
' + \
translate['About this Instance'] + '
'
html_str += \
'
' + \
translate['Terms of Service'] + '
'
if links_file_contains_entries and not rss_icon_at_top:
html_str += '
' + rss_icon_str + '
'
return html_str
def html_links_mobile(css_cache: {}, base_dir: str,
nickname: str, domain_full: str,
http_prefix: str, translate,
timeline_path: str, authorized: bool,
rss_icon_at_top: bool,
icons_as_buttons: bool,
default_timeline: str,
theme: str, access_keys: {},
shared_items_federated_domains: []) -> str:
"""Show the left column links within mobile view
"""
html_str = ''
# the css filename
css_filename = base_dir + '/epicyon-profile.css'
if os.path.isfile(base_dir + '/epicyon.css'):
css_filename = base_dir + '/epicyon.css'
# is the user a site editor?
if nickname == 'news':
editor = False
artist = False
else:
editor = is_editor(base_dir, nickname)
artist = is_artist(base_dir, nickname)
domain = remove_domain_port(domain_full)
instance_title = \
get_config_param(base_dir, 'instanceTitle')
html_str = \
html_header_with_external_style(css_filename, instance_title, None)
banner_file, _ = \
get_banner_file(base_dir, nickname, domain, theme)
html_str += \
'
' + \
'\n'
html_str += '
\n'
html_str += '
' + \
header_buttons_front_screen(translate, nickname,
'links', authorized,
icons_as_buttons) + ''
html_str += \
get_left_column_content(base_dir, nickname, domain_full,
http_prefix, translate,
editor, artist,
False, timeline_path,
rss_icon_at_top, False, False,
theme, access_keys,
shared_items_federated_domains)
if editor and not _links_exist(base_dir):
html_str += '
\n\n '
html_str += translate['Select the edit icon to add web links']
html_str += '\n\n'
# end of col-left-mobile
html_str += '\n'
html_str += '
\n' + html_footer()
return html_str
def html_edit_links(css_cache: {}, translate: {}, base_dir: str, path: str,
domain: str, port: int, http_prefix: str,
default_timeline: str, theme: str,
access_keys: {}) -> str:
"""Shows the edit links screen
"""
if '/users/' not in path:
return ''
path = path.replace('/inbox', '').replace('/outbox', '')
path = path.replace('/shares', '').replace('/wanted', '')
nickname = get_nickname_from_actor(path)
if not nickname:
return ''
# is the user a moderator?
if not is_editor(base_dir, nickname):
return ''
css_filename = base_dir + '/epicyon-links.css'
if os.path.isfile(base_dir + '/links.css'):
css_filename = base_dir + '/links.css'
# filename of the banner shown at the top
banner_file, _ = \
get_banner_file(base_dir, nickname, domain, theme)
instance_title = \
get_config_param(base_dir, 'instanceTitle')
edit_links_form = \
html_header_with_external_style(css_filename, instance_title, None)
# top banner
edit_links_form += \
'\n'
edit_links_form += \
'