From 11458f73e4cf915cb6d61c7eca0702495903ebfe Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 6 Dec 2020 22:26:39 +0000 Subject: [PATCH] Show recent shared items in left column --- webapp_column_left.py | 129 ++++++++++++++++++++++++++++-------------- 1 file changed, 87 insertions(+), 42 deletions(-) diff --git a/webapp_column_left.py b/webapp_column_left.py index d882400d7..7be6c1a0a 100644 --- a/webapp_column_left.py +++ b/webapp_column_left.py @@ -11,6 +11,7 @@ from shutil import copyfile from utils import getConfigParam from utils import getNicknameFromActor from utils import isEditor +from webapp_utils import sharesTimelineJson from webapp_utils import htmlPostSeparator from webapp_utils import getLeftImageFile from webapp_utils import getImageFile @@ -28,6 +29,40 @@ def linksExist(baseDir: str) -> bool: 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, httpPrefix: str, translate: {}, iconsPath: str, editor: bool, @@ -136,53 +171,63 @@ def getLeftColumnContent(baseDir: str, nickname: str, domainFull: str, linksFilename = baseDir + '/accounts/links.txt' linksFileContainsEntries = False + linksList = None if os.path.isfile(linksFilename): - linksList = None with open(linksFilename, "r") as f: linksList = f.readlines() - if linksList: - for lineStr in linksList: - if ' ' not in lineStr: - if '#' not in lineStr: - if '*' not in lineStr: - continue - lineStr = lineStr.strip() - words = lineStr.split(' ') - # get the link - linkStr = None - for word in words: - if word == '#': + + # show a number of shares + maxSharesInLeftColumn = 3 + sharesList = \ + getLeftColumnShares(baseDir, + httpPrefix, domainFull, nickname, + maxSharesInLeftColumn, translate) + if linksList and sharesList: + linksList += sharesList + + if linksList: + for lineStr in linksList: + if ' ' not in lineStr: + if '#' not in lineStr: + if '*' not in lineStr: continue - if word == '*': - continue - if '://' in word: - linkStr = word - break - if linkStr: - lineStr = lineStr.replace(linkStr, '').strip() - # avoid any dubious scripts being added - if '<' not in lineStr: - # remove trailing comma if present - if lineStr.endswith(','): - lineStr = lineStr[:len(lineStr)-1] - # add link to the returned html - htmlStr += \ - '

' + \ - lineStr + '

\n' - linksFileContainsEntries = True - else: - if lineStr.startswith('#') or lineStr.startswith('*'): - lineStr = lineStr[1:].strip() - if firstSeparatorAdded: - htmlStr += separatorStr - firstSeparatorAdded = True - htmlStr += \ - '

' + \ - lineStr + '

\n' - else: - htmlStr += \ - '

' + lineStr + '

\n' + lineStr = lineStr.strip() + words = lineStr.split(' ') + # get the link + linkStr = None + for word in words: + if word == '#': + continue + if word == '*': + continue + if '://' in word: + linkStr = word + break + if linkStr: + lineStr = lineStr.replace(linkStr, '').strip() + # avoid any dubious scripts being added + if '<' not in lineStr: + # remove trailing comma if present + if lineStr.endswith(','): + lineStr = lineStr[:len(lineStr)-1] + # add link to the returned html + htmlStr += \ + '

' + \ + lineStr + '

\n' linksFileContainsEntries = True + else: + if lineStr.startswith('#') or lineStr.startswith('*'): + lineStr = lineStr[1:].strip() + if firstSeparatorAdded: + htmlStr += separatorStr + firstSeparatorAdded = True + htmlStr += \ + '

' + \ + lineStr + '

\n' + else: + htmlStr += \ + '

' + lineStr + '

\n' + linksFileContainsEntries = True if firstSeparatorAdded: htmlStr += separatorStr