From 7a01f422cf095c096ba88994ba55e2fd63a830a8 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 22 Nov 2020 11:48:53 +0000 Subject: [PATCH] Set a maximum number of posts to appear in the newswire column --- daemon.py | 6 +++++- epicyon.py | 13 ++++++++++++- newsdaemon.py | 3 ++- newswire.py | 10 +++++++++- tests.py | 9 ++++++--- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/daemon.py b/daemon.py index 15c97bc9..7ffaba47 100644 --- a/daemon.py +++ b/daemon.py @@ -12388,7 +12388,8 @@ def loadTokens(baseDir: str, tokensDict: {}, tokensLookup: {}) -> None: tokensLookup[token] = nickname -def runDaemon(allowLocalNetworkAccess: bool, +def runDaemon(maxNewswirePosts: int, + allowLocalNetworkAccess: bool, maxFeedItemSizeKb: int, publishButtonAtTop: bool, rssIconAtTop: bool, @@ -12463,6 +12464,9 @@ def runDaemon(allowLocalNetworkAccess: bool, # newswire storing rss feeds httpd.newswire = {} + # maximum number of posts to appear in the newswire on the right column + httpd.maxNewswirePosts = maxNewswirePosts + # This counter is used to update the list of blocked domains in memory. # It helps to avoid touching the disk and so improves flooding resistance httpd.blocklistUpdateCtr = 0 diff --git a/epicyon.py b/epicyon.py index 605661f1..ecc051bb 100644 --- a/epicyon.py +++ b/epicyon.py @@ -116,6 +116,10 @@ parser.add_argument('--postsPerSource', dest='maxNewswirePostsPerSource', type=int, default=4, help='Maximum newswire posts per feed or account') +parser.add_argument('--maxNewswirePosts', + dest='maxNewswirePosts', type=int, + default=20, + help='Maximum newswire posts in the right column') parser.add_argument('--maxFeedSize', dest='maxNewswireFeedSizeKb', type=int, default=10240, @@ -2001,6 +2005,12 @@ maxNewswirePostsPerSource = \ if maxNewswirePostsPerSource: args.maxNewswirePostsPerSource = int(maxNewswirePostsPerSource) +# set the maximum number of newswire posts appearing in the right column +maxNewswirePosts = \ + getConfigParam(baseDir, 'maxNewswirePosts') +if maxNewswirePosts: + args.maxNewswirePosts = int(maxNewswirePosts) + # set the maximum size of a newswire rss/atom feed in Kilobytes maxNewswireFeedSizeKb = \ getConfigParam(baseDir, 'maxNewswireFeedSizeKb') @@ -2075,7 +2085,8 @@ if setTheme(baseDir, themeName, domain, args.allowLocalNetworkAccess): print('Theme set to ' + themeName) if __name__ == "__main__": - runDaemon(args.allowLocalNetworkAccess, + runDaemon(args.maxNewswirePosts, + args.allowLocalNetworkAccess, args.maxFeedItemSizeKb, args.publishButtonAtTop, args.rssIconAtTop, diff --git a/newsdaemon.py b/newsdaemon.py index 89986617..82b4757a 100644 --- a/newsdaemon.py +++ b/newsdaemon.py @@ -718,7 +718,8 @@ def runNewswireDaemon(baseDir: str, httpd, httpd.maxNewswirePostsPerSource, httpd.maxNewswireFeedSizeKb, httpd.maxTags, - httpd.maxFeedItemSizeKb) + httpd.maxFeedItemSizeKb, + httpd.maxNewswirePosts) except Exception as e: print('WARN: unable to update newswire ' + str(e)) time.sleep(120) diff --git a/newswire.py b/newswire.py index cb497c90..c5241582 100644 --- a/newswire.py +++ b/newswire.py @@ -677,7 +677,8 @@ def addBlogsToNewswire(baseDir: str, domain: str, newswire: {}, def getDictFromNewswire(session, baseDir: str, domain: str, maxPostsPerSource: int, maxFeedSizeKb: int, - maxTags: int, maxFeedItemSizeKb: int) -> {}: + maxTags: int, maxFeedItemSizeKb: int, + maxNewswirePosts: int) -> {}: """Gets rss feeds as a dictionary from newswire file """ subscriptionsFilename = baseDir + '/accounts/newswire.txt' @@ -728,4 +729,11 @@ def getDictFromNewswire(session, baseDir: str, domain: str, # sort into chronological order, latest first sortedResult = OrderedDict(sorted(result.items(), reverse=True)) + + # are there too many posts? If so then remove the oldest ones + noOfPosts = len(sortedResult.items()) + if noOfPosts > maxNewswirePosts: + for n in range(noOfPosts - maxNewswirePosts): + sortedResult.pop() + return sortedResult diff --git a/tests.py b/tests.py index c2de44e8..41c1a055 100644 --- a/tests.py +++ b/tests.py @@ -292,8 +292,9 @@ def createServerAlice(path: str, domain: str, port: int, onionDomain = None i2pDomain = None allowLocalNetworkAccess = True + maxNewswirePosts = 20 print('Server running: Alice') - runDaemon(allowLocalNetworkAccess, + runDaemon(maxNewswirePosts, allowLocalNetworkAccess, 2048, False, True, False, False, True, 10, False, 0, 100, 1024, 5, False, 0, False, 1, False, False, False, @@ -359,8 +360,9 @@ def createServerBob(path: str, domain: str, port: int, onionDomain = None i2pDomain = None allowLocalNetworkAccess = True + maxNewswirePosts = 20 print('Server running: Bob') - runDaemon(allowLocalNetworkAccess, + runDaemon(maxNewswirePosts, allowLocalNetworkAccess, 2048, False, True, False, False, True, 10, False, 0, 100, 1024, 5, False, 0, False, 1, False, False, False, @@ -400,8 +402,9 @@ def createServerEve(path: str, domain: str, port: int, federationList: [], onionDomain = None i2pDomain = None allowLocalNetworkAccess = True + maxNewswirePosts = 20 print('Server running: Eve') - runDaemon(allowLocalNetworkAccess, + runDaemon(maxNewswirePosts, allowLocalNetworkAccess, 2048, False, True, False, False, True, 10, False, 0, 100, 1024, 5, False, 0, False, 1, False, False, False,