Skip to content

Commit

Permalink
isso: js, py: Fetch public conf once from /config
Browse files Browse the repository at this point in the history
This is an improvement on top of:
02f3ea0
"Have client read out shared settings from server (isso-comments#311)"

So far, the `config` dict was being sent with the server
response when fetching comments from the `fetch()` `/` (GET)
endpoint.

Create new endpoint at `/config`, which will serve a JSON
representation of the server's `public_conf`. Also remove
the `config` object from `fetch()` responses.

Extend the JS client api file to fetch from the `/config`
endpoint, which will only happen once via the
`fetchComments` hook on page load.
  • Loading branch information
ix5 committed Mar 20, 2022
1 parent 16d7766 commit c55c9cc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
13 changes: 13 additions & 0 deletions isso/js/app/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ var fetch = function(tid, limit, nested_limit, parent, lastcreated) {
return deferred.promise;
};

var config = function() {
var deferred = Q.defer();
curl("GET", endpoint + "/config", null, function(rv) {
if (rv.status === 200) {
deferred.resolve(JSON.parse(rv.body));
} else {
deferred.reject(rv.body);
}
});
return deferred.promise;
};

var count = function(urls) {
var deferred = Q.defer();
curl("POST", endpoint + "/count", JSON.stringify(urls), function(rv) {
Expand Down Expand Up @@ -223,4 +235,5 @@ module.exports = {
dislike: dislike,
feed: feed,
preview: preview,
config: config,
};
15 changes: 12 additions & 3 deletions isso/js/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ function fetchComments() {
}

$('#isso-root').textContent = '';
api.fetch(isso_thread.getAttribute("data-isso-id") || location.pathname,
config["max-comments-top"],
config["max-comments-nested"]).then(

// Fetch config from server, will override any local data-isso-* attributes
api.config().then(
function (rv) {
for (let setting in rv.config) {
if (setting in config
Expand All @@ -79,7 +79,16 @@ function fetchComments() {
}
config[setting] = rv.config[setting]
}
},
function(err) {
console.log(err);
}
);

api.fetch(isso_thread.getAttribute("data-isso-id") || location.pathname,
config["max-comments-top"],
config["max-comments-nested"]).then(
function (rv) {
// Finally, create Postbox with configs fetched from server
isso_thread.append(new isso.Postbox(null));

Expand Down
34 changes: 33 additions & 1 deletion isso/views/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class API(object):
('dislike', ('POST', '/id/<int:id>/dislike')),
('demo', ('GET', '/demo')),
('preview', ('POST', '/preview')),
('config', ('GET', '/config')),
('login', ('POST', '/login')),
('admin', ('GET', '/admin'))
]
Expand Down Expand Up @@ -834,7 +835,6 @@ def fetch(self, environ, request, uri):
'total_replies': reply_counts[root_id],
'hidden_replies': reply_counts[root_id] - len(root_list),
'replies': self._process_fetched_list(root_list, plain),
'config': self.public_conf
}
# We are only checking for one level deep comments
if root_id is None:
Expand Down Expand Up @@ -1115,6 +1115,38 @@ def preview(self, environment, request):

return JSON({'text': self.isso.render(data["text"])}, 200)

"""
@api {get} /config fetch client config
@apiGroup Thread
@apiDescription
Fetches configuration of client parameters. The following settings are sent as a config object from the server to the client:
- reply-to-self
- require-author
- require-email
- reply-notifications
- gravatar
@apiSuccess {Object[]} config
The client configuration object.
@apiExample {curl} get the client config:
curl 'https://comments.example.com/config'
@apiSuccessExample Client config:
{
"config": {
"reply-to-self": false,
"require-email": false,
"require-author": false,
"reply-notifications": false,
"gravatar": false
}
}
"""
def config(self, environment, request):
rv = {'config': self.public_conf}
return JSON(rv, 200)

def demo(self, env, req):
return redirect(
get_current_url(env, strip_querystring=True) + '/index.html'
Expand Down

0 comments on commit c55c9cc

Please sign in to comment.