2019-03-02 23:44:09 +01:00
|
|
|
function Metadata(modules) {
|
2019-02-16 08:13:14 +01:00
|
|
|
var comments = modules.cache.make(function(threadId) {
|
|
|
|
return modules.async.bind(
|
2019-04-19 21:31:41 +02:00
|
|
|
modules.async.parallel(
|
|
|
|
getJSON(url(threadId)),
|
|
|
|
getJSON(url(threadId) + '/context'),
|
|
|
|
),
|
|
|
|
modules.async.map(function(t) {
|
|
|
|
return [renderLink(t[0]), renderAnswers(t[1])];
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
get: get,
|
|
|
|
getComments: getComments
|
|
|
|
};
|
|
|
|
|
|
|
|
function url(threadId) {
|
2019-12-21 12:50:38 +01:00
|
|
|
return blog.urls.comments + '/api/v1/statuses/' + threadId;
|
2019-04-19 21:31:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getJSON(url) {
|
|
|
|
return modules.async.bind(
|
|
|
|
modules.async.http({method: 'GET', url: url}),
|
2019-02-16 08:13:14 +01:00
|
|
|
function(queryResult) {
|
|
|
|
if(queryResult.status == 200) {
|
|
|
|
try {
|
2019-04-19 21:31:41 +02:00
|
|
|
return modules.async.wrap(JSON.parse(queryResult.responseText));
|
2019-02-16 08:13:14 +01:00
|
|
|
} catch(e) {
|
|
|
|
return modules.async.fail('Server returned invalid JSON for ' + url);
|
|
|
|
}
|
|
|
|
} else {
|
2019-04-19 21:31:41 +02:00
|
|
|
return modules.async.fail('Could not load page ' + url);
|
2019-02-16 08:13:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-02 23:44:09 +01:00
|
|
|
function getComments(articleKey) {
|
2019-02-16 08:13:14 +01:00
|
|
|
var threadId = blog.articles[articleKey].metadata.comments;
|
2019-12-21 12:50:38 +01:00
|
|
|
if(blog.urls.comments != undefined && threadId != undefined) {
|
2019-02-16 08:13:14 +01:00
|
|
|
var ul = modules.dom.make('ul');
|
2019-04-19 21:31:41 +02:00
|
|
|
var div = emptySection(ul);
|
2019-02-16 08:13:14 +01:00
|
|
|
modules.async.run(
|
|
|
|
modules.async.bind(
|
2019-04-19 21:31:41 +02:00
|
|
|
comments.get(threadId), modules.async.map(populateComments(div, ul))
|
2019-02-16 08:13:14 +01:00
|
|
|
)
|
|
|
|
);
|
2019-04-19 21:31:41 +02:00
|
|
|
return [div];
|
2019-02-16 08:13:14 +01:00
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 21:31:41 +02:00
|
|
|
function populateComments(div, ul) {
|
|
|
|
return function(apiResults) {
|
|
|
|
var post = apiResults[0], comments = apiResults[1];
|
|
|
|
div.appendChild(post);
|
|
|
|
comments.forEach(function(comment) {ul.appendChild(comment);});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function emptySection(ul) {
|
|
|
|
return modules.dom.make('div', {class: 'comments'}, [
|
2019-02-19 21:57:50 +01:00
|
|
|
modules.dom.make('h2', {innerText: blog.wording.commentsSection}),
|
2019-04-19 21:31:41 +02:00
|
|
|
ul
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderLink(post) {
|
|
|
|
return modules.dom.make('a', {
|
|
|
|
href: post.url,
|
|
|
|
innerText: blog.wording.commentsLink
|
|
|
|
});
|
2019-02-16 08:13:14 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 12:16:08 +01:00
|
|
|
function getContent(descendant) {
|
|
|
|
return descendant.content.replace(/:([^: ]+):/g, function(pattern, shortcode) {
|
|
|
|
var emoji = descendant.emojis.find(function(e) {return e.shortcode == shortcode;});
|
|
|
|
if(emoji) {
|
|
|
|
return [
|
|
|
|
'<img title=', shortcode, ' alt=', shortcode, ' src=', emoji.url, ' class="emoji"/>'
|
|
|
|
].join('"');
|
|
|
|
} else {
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-19 21:31:41 +02:00
|
|
|
function renderAnswers(comments) {
|
2019-02-16 08:13:14 +01:00
|
|
|
return comments.descendants.map(function(descendant) {
|
|
|
|
return modules.dom.make('li', {}, [
|
2019-03-03 12:16:08 +01:00
|
|
|
modules.dom.make('a', {href: descendant.account.url}, [
|
|
|
|
modules.dom.make('img', {
|
|
|
|
src: descendant.account.avatar,
|
|
|
|
alt: descendant.account.username + "'s profile picture"
|
|
|
|
})
|
|
|
|
]),
|
2019-03-02 23:44:09 +01:00
|
|
|
modules.dom.make('div', {
|
2019-03-03 12:16:08 +01:00
|
|
|
class: "metadata",
|
2019-03-02 23:44:09 +01:00
|
|
|
innerHTML: modules.template.render('metadata', {
|
|
|
|
author: author(descendant.account.url, descendant.account.username),
|
|
|
|
date: date(descendant.created_at)
|
|
|
|
})
|
|
|
|
}),
|
2019-03-03 12:16:08 +01:00
|
|
|
modules.dom.make('div', {innerHTML: getContent(descendant)})
|
2019-02-16 08:13:14 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
}
|
2019-03-02 23:44:09 +01:00
|
|
|
|
|
|
|
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', {
|
2019-03-03 12:16:08 +01:00
|
|
|
class: "metadata",
|
2019-03-02 23:44:09 +01:00
|
|
|
innerHTML: modules.template.render('metadata', {
|
|
|
|
author: author(key),
|
|
|
|
date: date(key),
|
|
|
|
tags: tags(key)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
2019-02-16 08:13:14 +01:00
|
|
|
}
|