Show recent shared items in left column

main
Bob Mottram 2020-12-06 22:26:39 +00:00
parent f5c1b7bec2
commit 11458f73e4
1 changed files with 87 additions and 42 deletions

View File

@ -11,6 +11,7 @@ from shutil import copyfile
from utils import getConfigParam from utils import getConfigParam
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import isEditor from utils import isEditor
from webapp_utils import sharesTimelineJson
from webapp_utils import htmlPostSeparator from webapp_utils import htmlPostSeparator
from webapp_utils import getLeftImageFile from webapp_utils import getLeftImageFile
from webapp_utils import getImageFile from webapp_utils import getImageFile
@ -28,6 +29,40 @@ def linksExist(baseDir: str) -> bool:
return os.path.isfile(linksFilename) return os.path.isfile(linksFilename)
def getLeftColumnShares(baseDir: str,
httpPrefix: str, domainFull: str,
nickname: str,
maxSharesInLeftColumn: int,
translate: {}) -> []:
"""get any shares and turn them into the left column links format
"""
pageNumber = 1
actor = httpPrefix + '://' + domainFull + '/users/' + nickname
sharesJson, lastPage = \
sharesTimelineJson(actor, pageNumber,
maxSharesInLeftColumn,
baseDir, maxSharesInLeftColumn)
if not sharesJson:
return []
linksList = []
ctr = 0
for published, item in sharesJson.items():
sharedesc = item['displayName']
contactActor = item['actor']
shareLink = actor + \
'?replydm=sharedesc:' + sharedesc + \
'?mention=' + contactActor
linksList.append(sharedesc + ' ' + shareLink)
ctr += 1
if ctr >= maxSharesInLeftColumn:
break
if linksList:
linksList = ['* ' + translate['Shares']] + linksList
return linksList
def getLeftColumnContent(baseDir: str, nickname: str, domainFull: str, def getLeftColumnContent(baseDir: str, nickname: str, domainFull: str,
httpPrefix: str, translate: {}, httpPrefix: str, translate: {},
iconsPath: str, editor: bool, iconsPath: str, editor: bool,
@ -136,53 +171,63 @@ def getLeftColumnContent(baseDir: str, nickname: str, domainFull: str,
linksFilename = baseDir + '/accounts/links.txt' linksFilename = baseDir + '/accounts/links.txt'
linksFileContainsEntries = False linksFileContainsEntries = False
linksList = None
if os.path.isfile(linksFilename): if os.path.isfile(linksFilename):
linksList = None
with open(linksFilename, "r") as f: with open(linksFilename, "r") as f:
linksList = f.readlines() linksList = f.readlines()
if linksList:
for lineStr in linksList: # show a number of shares
if ' ' not in lineStr: maxSharesInLeftColumn = 3
if '#' not in lineStr: sharesList = \
if '*' not in lineStr: getLeftColumnShares(baseDir,
continue httpPrefix, domainFull, nickname,
lineStr = lineStr.strip() maxSharesInLeftColumn, translate)
words = lineStr.split(' ') if linksList and sharesList:
# get the link linksList += sharesList
linkStr = None
for word in words: if linksList:
if word == '#': for lineStr in linksList:
if ' ' not in lineStr:
if '#' not in lineStr:
if '*' not in lineStr:
continue continue
if word == '*': lineStr = lineStr.strip()
continue words = lineStr.split(' ')
if '://' in word: # get the link
linkStr = word linkStr = None
break for word in words:
if linkStr: if word == '#':
lineStr = lineStr.replace(linkStr, '').strip() continue
# avoid any dubious scripts being added if word == '*':
if '<' not in lineStr: continue
# remove trailing comma if present if '://' in word:
if lineStr.endswith(','): linkStr = word
lineStr = lineStr[:len(lineStr)-1] break
# add link to the returned html if linkStr:
htmlStr += \ lineStr = lineStr.replace(linkStr, '').strip()
' <p><a href="' + linkStr + '">' + \ # avoid any dubious scripts being added
lineStr + '</a></p>\n' if '<' not in lineStr:
linksFileContainsEntries = True # remove trailing comma if present
else: if lineStr.endswith(','):
if lineStr.startswith('#') or lineStr.startswith('*'): lineStr = lineStr[:len(lineStr)-1]
lineStr = lineStr[1:].strip() # add link to the returned html
if firstSeparatorAdded: htmlStr += \
htmlStr += separatorStr ' <p><a href="' + linkStr + '">' + \
firstSeparatorAdded = True lineStr + '</a></p>\n'
htmlStr += \
' <h3 class="linksHeader">' + \
lineStr + '</h3>\n'
else:
htmlStr += \
' <p>' + lineStr + '</p>\n'
linksFileContainsEntries = True linksFileContainsEntries = True
else:
if lineStr.startswith('#') or lineStr.startswith('*'):
lineStr = lineStr[1:].strip()
if firstSeparatorAdded:
htmlStr += separatorStr
firstSeparatorAdded = True
htmlStr += \
' <h3 class="linksHeader">' + \
lineStr + '</h3>\n'
else:
htmlStr += \
' <p>' + lineStr + '</p>\n'
linksFileContainsEntries = True
if firstSeparatorAdded: if firstSeparatorAdded:
htmlStr += separatorStr htmlStr += separatorStr