2019-01-28 08:36:28 +05:30
|
|
|
subscribe_button = document.getElementById("subscribe");
|
|
|
|
if (subscribe_button.getAttribute('onclick')) {
|
|
|
|
subscribe_button["href"] = "javascript:void(0)";
|
|
|
|
}
|
|
|
|
|
2019-01-28 09:42:07 +05:30
|
|
|
function subscribe(timeouts = 0) {
|
2019-03-24 00:35:13 +05:30
|
|
|
subscribe_button = document.getElementById("subscribe");
|
2019-01-28 09:42:07 +05:30
|
|
|
|
|
|
|
if (timeouts > 10) {
|
|
|
|
console.log("Failed to subscribe.");
|
|
|
|
return;
|
|
|
|
}
|
2019-03-24 00:35:13 +05:30
|
|
|
|
2019-04-07 23:29:12 +05:30
|
|
|
var url = "/subscription_ajax?action_create_subscription_to_channel=1&redirect=false&c=<%= ucid %>&referer=<%= env.get("current_page") %>";
|
2019-01-28 08:36:28 +05:30
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.responseType = "json";
|
|
|
|
xhr.timeout = 20000;
|
2019-04-16 09:53:40 +05:30
|
|
|
xhr.open("POST", url, true);
|
|
|
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
2019-04-19 02:53:50 +05:30
|
|
|
xhr.send("csrf_token=<%= URI.escape(env.get?("csrf_token").try &.as(String) || "") %>");
|
2019-01-28 08:36:28 +05:30
|
|
|
|
2019-01-28 09:42:07 +05:30
|
|
|
var fallback = subscribe_button.innerHTML;
|
|
|
|
subscribe_button.onclick = unsubscribe;
|
2019-02-13 02:29:26 +05:30
|
|
|
subscribe_button.innerHTML = '<b><%= translate(locale, "Unsubscribe").gsub("'", "\\'") %> | <%= sub_count_text %></b>'
|
2019-03-24 00:35:13 +05:30
|
|
|
|
2019-01-28 08:36:28 +05:30
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState == 4) {
|
2019-01-28 09:42:07 +05:30
|
|
|
if (xhr.status != 200) {
|
|
|
|
subscribe_button.onclick = subscribe;
|
|
|
|
subscribe_button.innerHTML = fallback;
|
2019-01-28 08:36:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-24 00:35:13 +05:30
|
|
|
|
2019-01-28 09:42:07 +05:30
|
|
|
xhr.ontimeout = function() {
|
|
|
|
console.log("Subscribing timed out.");
|
|
|
|
|
|
|
|
subscribe(timeouts + 1);
|
|
|
|
};
|
2019-01-28 08:36:28 +05:30
|
|
|
}
|
|
|
|
|
2019-01-28 09:42:07 +05:30
|
|
|
function unsubscribe(timeouts = 0) {
|
2019-03-24 00:35:13 +05:30
|
|
|
subscribe_button = document.getElementById("subscribe");
|
|
|
|
|
2019-01-28 09:42:07 +05:30
|
|
|
if (timeouts > 10) {
|
|
|
|
console.log("Failed to subscribe");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 23:29:12 +05:30
|
|
|
var url = "/subscription_ajax?action_remove_subscriptions=1&redirect=false&c=<%= ucid %>&referer=<%= env.get("current_page") %>";
|
2019-01-28 08:36:28 +05:30
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.responseType = "json";
|
|
|
|
xhr.timeout = 20000;
|
2019-04-16 09:53:40 +05:30
|
|
|
xhr.open("POST", url, true);
|
|
|
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
2019-04-19 02:53:50 +05:30
|
|
|
xhr.send("csrf_token=<%= URI.escape(env.get?("csrf_token").try &.as(String) || "") %>");
|
2019-01-28 08:36:28 +05:30
|
|
|
|
2019-01-28 09:42:07 +05:30
|
|
|
var fallback = subscribe_button.innerHTML;
|
|
|
|
subscribe_button.onclick = subscribe;
|
2019-02-13 02:29:26 +05:30
|
|
|
subscribe_button.innerHTML = '<b><%= translate(locale, "Subscribe").gsub("'", "\\'") %> | <%= sub_count_text %></b>'
|
2019-01-28 09:42:07 +05:30
|
|
|
|
2019-01-28 08:36:28 +05:30
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState == 4) {
|
2019-01-28 09:42:07 +05:30
|
|
|
if (xhr.status != 200) {
|
|
|
|
subscribe_button.onclick = unsubscribe;
|
|
|
|
subscribe_button.innerHTML = fallback;
|
2019-01-28 08:36:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-28 09:42:07 +05:30
|
|
|
|
|
|
|
xhr.ontimeout = function() {
|
|
|
|
console.log("Unsubscribing timed out.");
|
|
|
|
|
|
|
|
unsubscribe(timeouts + 1);
|
|
|
|
};
|
2019-01-28 08:36:28 +05:30
|
|
|
}
|