Replace duplicate link to YouTube

This commit is contained in:
Omar Roth 2018-08-17 09:25:47 -05:00
parent fa2ba807a3
commit 71aa4d0347
2 changed files with 21 additions and 12 deletions

View File

@ -1685,7 +1685,7 @@ get "/api/v1/comments/:id" do |env|
if format == "json" if format == "json"
next {"comments" => [] of String}.to_json next {"comments" => [] of String}.to_json
else else
next {"content_html" => ""}.to_json next {"contentHtml" => ""}.to_json
end end
end end
ctoken = ctoken["ctoken"] ctoken = ctoken["ctoken"]
@ -1723,7 +1723,7 @@ get "/api/v1/comments/:id" do |env|
if format == "json" if format == "json"
next {"comments" => [] of String}.to_json next {"comments" => [] of String}.to_json
else else
next {"content_html" => ""}.to_json next {"contentHtml" => ""}.to_json
end end
end end
@ -1823,7 +1823,8 @@ get "/api/v1/comments/:id" do |env|
comments = JSON.parse(comments) comments = JSON.parse(comments)
content_html = template_youtube_comments(comments) content_html = template_youtube_comments(comments)
next {"content_html" => content_html}.to_json next {"contentHtml" => content_html,
"commentCount" => comments["commentCount"]}.to_json
end end
elsif source == "reddit" elsif source == "reddit"
client = make_client(REDDIT_URL) client = make_client(REDDIT_URL)
@ -1846,7 +1847,7 @@ get "/api/v1/comments/:id" do |env|
env.response.content_type = "application/json" env.response.content_type = "application/json"
next {"title" => reddit_thread.title, next {"title" => reddit_thread.title,
"permalink" => reddit_thread.permalink, "permalink" => reddit_thread.permalink,
"content_html" => content_html}.to_json "contentHtml" => content_html}.to_json
end end
end end

View File

@ -72,7 +72,7 @@ function load_comments(target) {
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if (xhr.readyState == 4) { if (xhr.readyState == 4) {
if (xhr.status == 200) { if (xhr.status == 200) {
body.innerHTML = xhr.response.content_html; body.innerHTML = xhr.response.contentHtml;
} else { } else {
body.innerHTML = fallback; body.innerHTML = fallback;
} }
@ -106,12 +106,12 @@ function get_reddit_comments() {
<a target="_blank" href="https://reddit.com{permalink}">View more comments on Reddit</a> <a target="_blank" href="https://reddit.com{permalink}">View more comments on Reddit</a>
</b> </b>
</div> </div>
<div>{content_html}</div> <div>{contentHtml}</div>
<hr>`.supplant({ <hr>`.supplant({
title: xhr.response.title, title: xhr.response.title,
permalink: xhr.response.permalink, permalink: xhr.response.permalink,
content_html: xhr.response.content_html contentHtml: xhr.response.contentHtml
}); });
} else { } else {
get_youtube_comments(); get_youtube_comments();
@ -139,12 +139,13 @@ function get_youtube_comments() {
<div> <div>
<h3> <h3>
<a href="javascript:void(0)" onclick="toggle_comments(this)">[ - ]</a> <a href="javascript:void(0)" onclick="toggle_comments(this)">[ - ]</a>
<a target="_blank" href="https://www.youtube.com/watch?v=<%= video.id %>">View more comments on YouTube</a> View {commentCount} comments
</h3> </h3>
</div> </div>
<div>{content_html}</div> <div>{contentHtml}</div>
<hr>`.supplant({ <hr>`.supplant({
content_html: xhr.response.content_html contentHtml: xhr.response.contentHtml,
commentCount: commaSeparateNumber(xhr.response.commentCount)
}); });
} else { } else {
comments = document.getElementById("comments"); comments = document.getElementById("comments");
@ -160,6 +161,13 @@ function get_youtube_comments() {
}; };
} }
function commaSeparateNumber(val){
while (/(\d+)(\d{3})/.test(val.toString())){
val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
return val;
}
String.prototype.supplant = function(o) { String.prototype.supplant = function(o) {
return this.replace(/{([^{}]*)}/g, function(a, b) { return this.replace(/{([^{}]*)}/g, function(a, b) {
var r = o[b]; var r = o[b];