From 45f6b1eda382f431d75a27e6bccf6b0fc9b9d240 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sun, 22 Apr 2018 16:55:06 +0200 Subject: [PATCH] feed: make /feed API call configurable server and client-side On server-side, this can be enabled by providing a base URL to use to build the full URL. Limit also becomes configurable. On client-side, we need to add a switch to know whatever or not the additional link can be displayed. --- docs/docs/configuration/client.rst | 10 +++++++++- docs/docs/configuration/server.rst | 21 +++++++++++++++++++++ isso/js/app/config.js | 3 ++- isso/js/embed.js | 12 +++++++----- isso/tests/test_comments.py | 10 +++++++++- isso/views/comments.py | 8 ++++++-- share/isso.conf | 12 ++++++++++++ 7 files changed, 66 insertions(+), 10 deletions(-) diff --git a/docs/docs/configuration/client.rst b/docs/docs/configuration/client.rst index 9328357..779f47e 100644 --- a/docs/docs/configuration/client.rst +++ b/docs/docs/configuration/client.rst @@ -20,7 +20,8 @@ preferably in the script tag which embeds the JS: data-isso-avatar-bg="#f0f0f0" data-isso-avatar-fg="#9abf88 #5698c4 #e279a3 #9163b6 ..." data-isso-vote="true" - data-vote-levels="" + data-isso-vote-levels="" + data-isso-feed="false" src="/prefix/js/embed.js"> Furthermore you can override the automatic title detection inside @@ -125,3 +126,10 @@ For example, the value `"-5,5"` will cause each `isso-comment` to be given one o - `isso-vote-level-2` for scores of `5` and greater These classes can then be used to customize the appearance of comments (eg. put a star on popular comments) + +data-isso-feed +-------------- + +Enable or disable the addition of a link to the feed for the comment +thread. The link will only be valid if the appropriate setting, in +``[rss]`` section, is also enabled server-side. diff --git a/docs/docs/configuration/server.rst b/docs/docs/configuration/server.rst index 1a443f8..5327c96 100644 --- a/docs/docs/configuration/server.rst +++ b/docs/docs/configuration/server.rst @@ -308,6 +308,27 @@ algorithm Arguments have to be in that order, but can be reduced to `pbkdf2:4096` for example to override the iterations only. +.. _configure-rss: + +RSS +--- + +Isso can provide an Atom feed for each comment thread. Users can use +them to subscribe to comments and be notified of changes. Atom feeds +are enabled as soon as there is a base URL defined in this section. + +.. code-block:: ini + + [rss] + base = + limit = 100 + +base + base URL to use to build complete URI to pages (by appending the URI from Isso) + +limit + number of most recent comments to return for a thread + Appendum -------- diff --git a/isso/js/app/config.js b/isso/js/app/config.js index 952d588..258da6d 100644 --- a/isso/js/app/config.js +++ b/isso/js/app/config.js @@ -15,7 +15,8 @@ define(function() { "avatar-fg": ["#9abf88", "#5698c4", "#e279a3", "#9163b6", "#be5168", "#f19670", "#e4bf80", "#447c69"].join(" "), "vote": true, - "vote-levels": null + "vote-levels": null, + "feed": false }; var js = document.getElementsByTagName("script"); diff --git a/isso/js/embed.js b/isso/js/embed.js index 715413c..a0a53da 100644 --- a/isso/js/embed.js +++ b/isso/js/embed.js @@ -27,11 +27,13 @@ require(["app/lib/ready", "app/config", "app/i18n", "app/api", "app/isso", "app/ return console.log("abort, #isso-thread is missing"); } - var feedLink = $.new('a', i18n.translate('atom-feed')); - var feedLinkWrapper = $.new('span.isso-feedlink'); - feedLink.href = api.feed($("#isso-thread").getAttribute("data-isso-id")); - feedLinkWrapper.append(feedLink); - $("#isso-thread").append(feedLinkWrapper); + if (config["feed"]) { + var feedLink = $.new('a', i18n.translate('atom-feed')); + var feedLinkWrapper = $.new('span.isso-feedlink'); + feedLink.href = api.feed($("#isso-thread").getAttribute("data-isso-id")); + feedLinkWrapper.append(feedLink); + $("#isso-thread").append(feedLinkWrapper); + } $("#isso-thread").append($.new('h4')); $("#isso-thread").append(new isso.Postbox(null)); $("#isso-thread").append('
'); diff --git a/isso/tests/test_comments.py b/isso/tests/test_comments.py index a58ae62..4ba67c8 100644 --- a/isso/tests/test_comments.py +++ b/isso/tests/test_comments.py @@ -31,9 +31,9 @@ class TestComments(unittest.TestCase): fd, self.path = tempfile.mkstemp() conf = config.load(os.path.join(dist.location, "share", "isso.conf")) conf.set("general", "dbpath", self.path) - conf.set("general", "host", "https://example.org") conf.set("guard", "enabled", "off") conf.set("hash", "algorithm", "none") + self.conf = conf class App(Isso, core.Mixin): pass @@ -326,7 +326,13 @@ class TestComments(unittest.TestCase): self.assertListEqual(list(rv.keys()), []) + def testNoFeed(self): + rv = self.get('/feed?uri=%2Fpath%2Fnothing') + self.assertEqual(rv.status_code, 404) + def testFeedEmpty(self): + self.conf.set("rss", "base", "https://example.org") + rv = self.get('/feed?uri=%2Fpath%2Fnothing') self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers['ETag'], '"empty"') @@ -335,6 +341,8 @@ class TestComments(unittest.TestCase): 1970-01-01T01:00:00Ztag:example.org,2018:/isso/thread/path/nothingComments for example.org/path/nothing""") def testFeed(self): + self.conf.set("rss", "base", "https://example.org") + self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'First'})) self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'First', 'parent': 1})) diff --git a/isso/views/comments.py b/isso/views/comments.py index 120fef3..87cf48d 100644 --- a/isso/views/comments.py +++ b/isso/views/comments.py @@ -854,11 +854,15 @@ class API(object): """ @requires(str, 'uri') def feed(self, environ, request, uri): + conf = self.isso.conf.section("rss") + if not conf.get('base'): + raise NotFound + args = { 'uri': uri, 'order_by': 'id', 'asc': 0, - 'limit': 100 + 'limit': conf.getint('limit') } try: args['limit'] = max(int(request.args.get('limit')), args['limit']) @@ -867,7 +871,7 @@ class API(object): except ValueError: return BadRequest("limit should be integer") comments = self.comments.fetch(**args) - base = self.conf.get('host').split()[0] + base = conf.get('base') hostname = urlparse(base).netloc # Let's build an Atom feed. diff --git a/share/isso.conf b/share/isso.conf index 883e2b1..d429215 100644 --- a/share/isso.conf +++ b/share/isso.conf @@ -180,3 +180,15 @@ salt = Eech7co8Ohloopo9Ol6baimi # strengthening. Arguments have to be in that order, but can be reduced to # pbkdf2:4096 for example to override the iterations only. algorithm = pbkdf2 + + +[rss] +# Provide an Atom feed for each comment thread for users to subscribe to. + +# The base URL of pages is needed to build the Atom feed. By appending +# the URI, we should get the complete URL to use to access the page +# with the comments. When empty, Atom feeds are disabled. +base = + +# Limit the number of elements to return for each thread. +limit = 100