hablo/share/js/comments.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

function Comments(modules) {
var comments = modules.cache.make(function(threadId) {
return modules.async.bind(
modules.async.http({method: 'GET', url: url(threadId)}),
function(queryResult) {
if(queryResult.status == 200) {
try {
return modules.async.wrap(render(JSON.parse(queryResult.responseText)));
} catch(e) {
return modules.async.fail('Server returned invalid JSON for ' + url);
}
} else {
return modules.async.fail('Could not load comments at ' + url);
}
}
);
})
return {
get: get
};
function url(threadId) {
return blog.path.commentsAt + '/api/v1/statuses/' + threadId + '/context';
}
function get(articleKey) {
var threadId = blog.articles[articleKey].metadata.comments;
if(blog.path.commentsAt != undefined && threadId != undefined) {
var ul = modules.dom.make('ul');
modules.async.run(
modules.async.bind(
comments.get(threadId),
modules.async.map(function(comments) {
comments.forEach(function(comment) {ul.appendChild(comment);});
})
)
);
return emptySection(ul, threadId);
} else {
return [];
}
}
function emptySection(ul, threadId) {
return [modules.dom.make('div', {class: 'comments'}, [
modules.dom.make('h2', {innerText: 'Comments'}),
ul,
modules.dom.make('a', {
href: blog.path.commentsAt + '/notice/' + threadId,
innerText: 'Comment on the fediverse'
})
])];
}
function render(comments) {
return comments.descendants.map(function(descendant) {
return modules.dom.make('li', {}, [
modules.dom.make('cite', {}, [
modules.dom.make('a', {
href: descendant.account.url,
innerText: descendant.account.username
}),
modules.dom.make('p', {innerText: descendant.created_at}),
modules.dom.make('div', {innerHTML: descendant.content})
])
]);
});
}
}