__filename__ = "webapp_column_left.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.2.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Web Interface Columns"
import os
from utils import getConfigParam
from utils import getNicknameFromActor
from utils import isEditor
from utils import isArtist
from utils import removeDomainPort
from utils import localActorUrl
from webapp_utils import sharesTimelineJson
from webapp_utils import htmlPostSeparator
from webapp_utils import getLeftImageFile
from webapp_utils import headerButtonsFrontScreen
from webapp_utils import htmlHeaderWithExternalStyle
from webapp_utils import htmlFooter
from webapp_utils import getBannerFile
from webapp_utils import editTextField
from shares import shareCategoryIcon
def _linksExist(base_dir: str) -> bool:
"""Returns true if links have been created
"""
linksFilename = base_dir + '/accounts/links.txt'
return os.path.isfile(linksFilename)
def _getLeftColumnShares(base_dir: str,
http_prefix: str, domain: str, domainFull: str,
nickname: str,
maxSharesInLeftColumn: int,
translate: {},
shared_items_federated_domains: []) -> []:
"""get any shares and turn them into the left column links format
"""
pageNumber = 1
actor = localActorUrl(http_prefix, nickname, domainFull)
# NOTE: this could potentially be slow if the number of federated
# shared items is large
sharesJson, lastPage = \
sharesTimelineJson(actor, pageNumber, maxSharesInLeftColumn,
base_dir, domain, nickname, maxSharesInLeftColumn,
shared_items_federated_domains, 'shares')
if not sharesJson:
return []
linksList = []
ctr = 0
for published, item in sharesJson.items():
sharedesc = item['displayName']
if '<' in sharedesc or '?' in sharedesc:
continue
shareId = item['shareId']
# selecting this link calls htmlShowShare
shareLink = actor + '?showshare=' + shareId
if item.get('category'):
shareLink += '?category=' + item['category']
shareCategory = shareCategoryIcon(item['category'])
linksList.append(shareCategory + sharedesc + ' ' + shareLink)
ctr += 1
if ctr >= maxSharesInLeftColumn:
break
if linksList:
linksList = ['* ' + translate['Shares']] + linksList
return linksList
def _getLeftColumnWanted(base_dir: str,
http_prefix: str, domain: str, domainFull: str,
nickname: str,
maxSharesInLeftColumn: int,
translate: {},
shared_items_federated_domains: []) -> []:
"""get any wanted items and turn them into the left column links format
"""
pageNumber = 1
actor = localActorUrl(http_prefix, nickname, domainFull)
# NOTE: this could potentially be slow if the number of federated
# wanted items is large
sharesJson, lastPage = \
sharesTimelineJson(actor, pageNumber, maxSharesInLeftColumn,
base_dir, domain, nickname, maxSharesInLeftColumn,
shared_items_federated_domains, 'wanted')
if not sharesJson:
return []
linksList = []
ctr = 0
for published, item in sharesJson.items():
sharedesc = item['displayName']
if '<' in sharedesc or ';' in sharedesc:
continue
shareId = item['shareId']
# selecting this link calls htmlShowShare
shareLink = actor + '?showwanted=' + shareId
linksList.append(sharedesc + ' ' + shareLink)
ctr += 1
if ctr >= maxSharesInLeftColumn:
break
if linksList:
linksList = ['* ' + translate['Wanted']] + linksList
return linksList
def getLeftColumnContent(base_dir: str, nickname: str, domainFull: str,
http_prefix: str, translate: {},
editor: bool, artist: bool,
showBackButton: bool, timelinePath: str,
rss_icon_at_top: bool, showHeaderImage: bool,
frontPage: bool, theme: str,
accessKeys: {},
shared_items_federated_domains: []) -> str:
"""Returns html content for the left column
"""
htmlStr = ''
separatorStr = htmlPostSeparator(base_dir, 'left')
domain = removeDomainPort(domainFull)
editImageClass = ''
if showHeaderImage:
leftImageFile, leftColumnImageFilename = \
getLeftImageFile(base_dir, nickname, domain, theme)
# show the image at the top of the column
editImageClass = 'leftColEdit'
if os.path.isfile(leftColumnImageFilename):
editImageClass = 'leftColEditImage'
htmlStr += \
'\n
\n \n' + \
'
\n'
if showBackButton:
htmlStr += \
'
' + \
'\n'
if (editor or rss_icon_at_top) and not showHeaderImage:
htmlStr += '
'
if editImageClass == 'leftColEdit':
htmlStr += '\n
\n'
htmlStr += '
\n'
if editor:
# show the edit icon
htmlStr += \
' ' + \
'\n'
if artist:
# show the theme designer icon
htmlStr += \
' ' + \
'\n'
# RSS icon
if nickname != 'news':
# rss feed for this account
rssUrl = http_prefix + '://' + domainFull + \
'/blog/' + nickname + '/rss.xml'
else:
# rss feed for all accounts on the instance
rssUrl = http_prefix + '://' + domainFull + '/blog/rss.xml'
if not frontPage:
rssTitle = translate['RSS feed for your blog']
else:
rssTitle = translate['RSS feed for this site']
rssIconStr = \
' \n'
if rss_icon_at_top:
htmlStr += rssIconStr
htmlStr += '
\n'
if editImageClass == 'leftColEdit':
htmlStr += '
\n'
if (editor or rss_icon_at_top) and not showHeaderImage:
htmlStr += '
'
# if showHeaderImage:
# htmlStr += ' '
# flag used not to show the first separator
firstSeparatorAdded = False
linksFilename = base_dir + '/accounts/links.txt'
linksFileContainsEntries = False
linksList = None
if os.path.isfile(linksFilename):
with open(linksFilename, 'r') as f:
linksList = f.readlines()
if not frontPage:
# show a number of shares
maxSharesInLeftColumn = 3
sharesList = \
_getLeftColumnShares(base_dir,
http_prefix, domain, domainFull, nickname,
maxSharesInLeftColumn, translate,
shared_items_federated_domains)
if linksList and sharesList:
linksList = sharesList + linksList
wantedList = \
_getLeftColumnWanted(base_dir,
http_prefix, domain, domainFull, nickname,
maxSharesInLeftColumn, translate,
shared_items_federated_domains)
if linksList and wantedList:
linksList = wantedList + linksList
newTabStr = ' target="_blank" rel="nofollow noopener noreferrer"'
if linksList:
htmlStr += '\n'
if firstSeparatorAdded:
htmlStr += separatorStr
htmlStr += \
'