__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, timelinePath: 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') 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 += \
'