fix broken changes from merge

This commit is contained in:
Corban-Lee Jones 2024-04-01 01:34:49 +01:00
parent 8e5498c191
commit e1fd10e25f
2 changed files with 34 additions and 328 deletions

View File

@ -1,3 +1,17 @@
function createSubCategory(serverId) {
var template = $($("#subCategoryTemplate").html());
template.find(".cat-id").text(serverId);
var server = getServer(serverId).then(resp => {
template.find(".cat-icon").attr("src", resp.icon_url);
template.find(".cat-name").text(resp.name);
});
return template
}
function createSubscriptionItem(data) {
var template = $($("#subItemTemplate").html());
@ -123,12 +137,25 @@ function loadSubscriptions() {
$("#subscriptionContainer").empty();
getSubscriptions().then(resp => {
updateSubscriptionCount(resp.results.length, true);
for (i = 0; i < resp.results.length; i++) {
var sub = resp.results[i];
console.log(JSON.stringify(sub));
var subElem = createSubscriptionItem(sub);
$("#subscriptionContainer").append(subElem);
}
var categorisedSubs = {};
$.each(resp.results, function(index, sub) {
categorisedSubs[sub.server] = categorisedSubs[sub.server] || [];
categorisedSubs[sub.server].push(sub);
});
console.log(JSON.stringify(categorisedSubs, null, 4))
$.each(categorisedSubs, function(server, subs) {
var categoryElem = createSubCategory(server);
$("#subscriptionContainer").append(categoryElem);
$.each(subs, function(index, sub) {
var subElem = createSubscriptionItem(sub);
categoryElem.find(".sub-container").append(subElem);
});
})
});
}

View File

@ -167,7 +167,7 @@
</script>
<script id="subItemTemplate" type="text/template">
<div class="col-md-6 col-lg-4 col-xxl-3">
<div class="col-md-6 col-lg-4 col-xxl-3 mb-4">
<div class="sub-item layers bd bg-body h-100 rounded-3" data-uuid="">
<div class="layer w-100">
<div class="peers px-4 py-3 flex-nowrap">
@ -222,325 +222,4 @@
</script>
<script src="{% static 'js/api.js' %}"></script>
<script src="{% static 'js/subscriptions.js' %}"></script>
<script type="text/javascript">
function createSubCategory(serverId) {
var template = $($("#subCategoryTemplate").html());
template.find(".cat-id").text(serverId);
var server = getServer(serverId).then(resp => {
template.find(".cat-icon").attr("src", resp.icon_url);
template.find(".cat-name").text(resp.name);
});
return template
}
function createSubscriptionItem(data) {
var template = $($("#subItemTemplate").html());
// Store the uuid for later reference
template.find(".sub-item").attr("data-uuid", data.uuid);
// Display data
template.find(".sub-name").text(data.name);
template.find(".sub-uuid").text(data.uuid);
template.find(".sub-rss").text(data.rss_url).attr("href", data.rss_url);
template.find(".sub-img").attr("src", data.image);
template.find(".sub-channel-count").text(data.targets.split(";").length);
// Display Sub Description
if (!data.extra_notes) {
template.find(".sub-desc").hide();
} else {
template.find(".sub-desc").text(data.extra_notes);
}
// Display formatted datetime
var displayDate = new Date(data.creation_datetime).toISOString().slice(0, 10);
template.find(".sub-datetime").text(displayDate);
// Provide button functionality
template.find(".sub-edit").attr("onclick", `subEditModal("${data.uuid}");`);
template.find(".sub-delete").attr("onclick", `confirmUnsubscribe("${data.uuid}");`);
// Enable tooltips
template.find('[data-bs-toggle="tooltip"]').tooltip();
// Make the switch toggle the active flag
template.find(".sub-active").prop("checked", data.active).change(function() {
var checkbox = $(this);
checkbox.prop("disabled", true);
var isChecked = checkbox.prop("checked");
var activeText = isChecked === true ? "active" : "inactive";
var formData = new FormData();
formData.append("active", isChecked);
patchSubscription(data.uuid, formData).then(function(resp) {
showToast("success", "Subscription Modified", `<b>${data.name}</b> is now <b>${activeText}</b>.`);
checkbox.prop("disabled", false);
})
});
return template
}
$(document).ready(function() {
loadGuilds();
loadSubscriptions();
});
function loadGuilds() {
$.ajax({
url: "/guilds",
type: "GET",
success: function(response) {
// Add each guild as a selectable option
for (i = 1; i < response.length; i++) {
var guild = response[i];
var option = $("<option>", {text: guild.name, value: guild.id})
$("#editSubServer").append(option);
}
// Bind the select to update channels on change
$("#editSubServer").change(function() {
var selectedGuildId = $(this).find("option:selected").attr("value");
loadChannels(selectedGuildId);
});
},
error: function(response) {
console.error(JSON.stringify(response, null, 4));
showToast("danger", `Error Loading Guilds`, "Couldn't load user guilds. Try refreshing the page.", 15000);
}
});
}
function processChannelsResponseError(response) {
switch (response.code) {
// 50001:
// Forbidden response
case 50001:
showToast(
"danger",
`Discord API Error: ${response.code}`,
`PYRSS Bot is lacking forbidden from fetching channels for this server.
Ensure that at least one condition is true:
<ul>
<li>The server is a community server.</li>
<li>PYRSS Bot is a member of the server.</li>
</ul>
`,
10000
);
break;
default:
showToast("danger", `Discord API Error: ${response.code}`, response.message, 10000);
break;
}
}
function loadChannels(guildID) {
$("#editSubChannels").empty();
$("#editSubChannels").val(null);
$.ajax({
url: `/channels?guild=${guildID}`,
type: "GET",
success: function(response) {
// Validate the response, if property "code" exists, there is an error.
// A valid response only returns a list.
if (response.hasOwnProperty("code")) {
processChannelsResponseError(response);
return;
}
if (response.hasOwnProperty("code") && response.code === 50001) {
showToast(
"danger", "Unable to fetch channels",
"PYRSS Bot is lacking permissions to fetch the channels from this server, ensure one of two conditions is met: <br><ul><li>The server is a community server</li><li>PYRSS Bot is a member of the server</li></ul>",
10000
);
return;
}
for (i = 1; i < response.length; i++) {
var channel = response[i];
if (channel.type !== 0) {
continue
}
var selectedChannelIDs;
try {
selectedChannelIDs = $("#editSubChannels").attr("data-current").split(";");
}
catch {
selectedChannelIDs = [];
}
$("#editSubChannels").append($("<option>", {
value: channel.id,
text: "#" + channel.name,
selected: selectedChannelIDs.includes(channel.id.toString())
}));
}
},
error: function(response) {
alert(JSON.stringify(response, null, 4));
// showToast("danger", `Error Loading Guilds`, "Couldn't load user guilds. Try refreshing the page.", 15000);
if (response.code == "50001") {
alert("PYRSS Bot is Missing Access to this Server");
}
else {
alert("unknown error fetching channels " + response.code)
}
}
});
}
function updateSubscriptionCount(difference, overwrite) {
const beforeChange = overwrite ? 0 : Number($(".subs-count").text());
$(".subs-count").text(beforeChange + difference);
}
function loadSubscriptions() {
$("#subscriptionContainer").empty();
getSubscriptions(ordering="server").then(resp => {
updateSubscriptionCount(resp.results.length, true);
var categorisedSubs = {};
$.each(resp.results, function(index, sub) {
categorisedSubs[sub.server] = categorisedSubs[sub.server] || [];
categorisedSubs[sub.server].push(sub);
});
console.log(JSON.stringify(categorisedSubs, null, 4))
$.each(categorisedSubs, function(server, subs) {
var categoryElem = createSubCategory(server);
$("#subscriptionContainer").append(categoryElem);
$.each(subs, function(index, sub) {
var subElem = createSubscriptionItem(sub);
categoryElem.find(".sub-container").append(subElem);
});
})
// for (i = 0; i < resp.results.length; i++) {
// var sub = resp.results[i];
// console.log(JSON.stringify(sub));
// var subElem = createSubscriptionItem(sub);
// }
});
}
function confirmUnsubscribe(uuid) {
var title = $(`.sub-item[data-uuid='${uuid}'] .sub-name`).text();
$(".del-sub-name").text(title);
$(".del-sub-uuid").text(uuid);
$(".del-sub-confirm").attr("onclick", `unsubscribe("${uuid}")`)
$("#subDeleteModal").modal("show");
}
function unsubscribe(uuid) {
var subElem = $(`#subscriptionContainer .sub-item[data-uuid="${uuid}"]`);
subElem.find("button").prop("disabled", true);
var subName = subElem.find(".sub-name").text();
deleteSubscription(uuid).then(resp => {
subElem.parent().remove();
updateSubscriptionCount(-1);
$("#subDeleteModal").modal("hide");
showToast(
"success",
"Deleted Subscription",
`Successfully deleted <b>${subName}</b>.`
);
});
}
function subEditModal(uuid) {
var modal = $("#subEditModal");
modal.find("input").val(null);
modal.find("textarea").val(null);
modal.find("select").val("");
$("#editSubChannels").empty();
$("#subEditForm").removeClass("was-validated");
$("#navDetailsTab").click();
if (uuid === -1) {
modal.find(".modal-title").text("New Subscription");
}
else {
modal.find(".modal-title").text("Edit Subscription");
getSubscription(uuid).then(resp => {
// alert(JSON.stringify(resp, null, 4));
$("#editSubName").val(resp.name);
$("#editSubURL").val(resp.rss_url);
$("#editSubServer").val(String(resp.server)).trigger("change");
$("#editSubChannels").attr("data-current", resp.targets);
$("#editSubNotes").val(resp.extra_notes);
});
}
$("#subEditModal").attr("data-uuid", uuid);
$("#subEditModal").modal("show");
}
function submitSubEditModal() {
// Validation
var form = $("#subEditForm");
if (!form[0].checkValidity()) {
form.addClass("was-validated");
return;
}
const uuid = $("#subEditModal").attr("data-uuid");
const subName = $("#editSubName").val();
var formData = new FormData();
formData.append("uuid", uuid);
formData.append("name", subName);
formData.append("rss_url", $("#editSubURL").val());
formData.append("server", $("#editSubServer").val());
formData.append("extra_notes", $("#editSubNotes").val());
formData.append("active", true);
var selectedTargets = $("#editSubChannels option:selected").toArray().map(item => item.value).join(';');
formData.append("targets", selectedTargets);
var imageFile = $("#editSubImage")[0].files[0];
if (imageFile) {
formData.append("image", imageFile);
}
if (uuid === "-1") {
newSubscription(formData).then(resp => {
loadSubscriptions();
$("#subEditModal").modal("hide");
showToast("success", "Subscription Created", `<b>${subName}</b> successfully created.`);
})
}
else {
editSubscription(uuid, formData).then(resp => {
loadSubscriptions();
$("#subEditModal").modal("hide");
showToast("success", "Subscription Modified", `<b>${subName}</b> successfully modified.`);
});
}
}
</script>
{% endblock javascripts %}