114 lines
2.9 KiB
JavaScript
114 lines
2.9 KiB
JavaScript
function Metadata(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,
|
|
getComments: getComments
|
|
};
|
|
|
|
function url(threadId) {
|
|
return blog.path.commentsAt + '/api/v1/statuses/' + threadId + '/context';
|
|
}
|
|
|
|
function getComments(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: blog.wording.commentsSection}),
|
|
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('div', {
|
|
innerHTML: modules.template.render('metadata', {
|
|
author: author(descendant.account.url, descendant.account.username),
|
|
date: date(descendant.created_at)
|
|
})
|
|
}),
|
|
modules.dom.make('div', {innerHTML: descendant.content})
|
|
]);
|
|
});
|
|
}
|
|
|
|
function author(key, name) {
|
|
var authorUrl = key;
|
|
if(blog.articles[key] != undefined) {
|
|
authorUrl = blog.articles[key].metadata.author;
|
|
}
|
|
if(authorUrl) {
|
|
var author = name || authorUrl.replace(/.*\//, '');
|
|
return '<a href="' + authorUrl + '">' + author + '</a>';
|
|
}
|
|
}
|
|
|
|
function date(key) {
|
|
if(blog.articles[key] != undefined) {
|
|
var date = new Date(blog.articles[key].metadata.date * 1000);
|
|
} else {
|
|
var date = new Date(key);
|
|
}
|
|
var format = blog.wording.dateFormat;
|
|
if(format[0] != '[') {
|
|
if(format[0] != '"') {
|
|
format = '"' + format + '"';
|
|
}
|
|
format = '[' + format + ']';
|
|
}
|
|
return Date.prototype.toLocaleDateString.apply(date, JSON.parse(format));
|
|
}
|
|
|
|
function tags(key) {
|
|
var tags = blog.articles[key].tagged;
|
|
return tags.length < 1 ? null : tags.map(function(tag) {
|
|
return '<a class="tag" href="/' + tag + '">' + tag + '</a>';
|
|
}).join(', ');
|
|
}
|
|
|
|
function get(key) {
|
|
return modules.dom.make('div', {
|
|
innerHTML: modules.template.render('metadata', {
|
|
author: author(key),
|
|
date: date(key),
|
|
tags: tags(key)
|
|
})
|
|
});
|
|
}
|
|
}
|