Skip to content

Commit

Permalink
Quick Tags: Prevent breaking non-legacy posts (#2106)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcustyphoon authored Sep 9, 2024
1 parent d003910 commit 090202d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 71 deletions.
87 changes: 31 additions & 56 deletions Extensions/quick_tags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//* TITLE Quick Tags **//
//* VERSION 0.6.9 **//
//* VERSION 0.6.10 **//
//* DESCRIPTION Quickly add tags to posts **//
//* DETAILS Allows you to create tag bundles and add tags to posts without leaving the dashboard. **//
//* DEVELOPER New-XKit **//
Expand Down Expand Up @@ -66,7 +66,9 @@ XKit.extensions.quick_tags = new Object({
}
},

run: function() {
run: async function() {
XKit.interface.form_key() || await XKit.interface.async_form_key();

this.running = true;

XKit.tools.init_css("quick_tags");
Expand Down Expand Up @@ -118,65 +120,38 @@ XKit.extensions.quick_tags = new Object({

// Fetch info about it!
if (!m_post.error) {
XKit.interface.fetch(m_post, function(data) {

// Use Interface to edit the post's tags:
var m_tags = data.data.post.tags;

if (m_tags === "undefined" || typeof m_tags === "undefined" || m_tags == "null") {
m_tags = "";
}

if (XKit.extensions.quick_tags.preferences.append_not_replace.value === true) {
m_tags = m_tags + "," + tags;
} else {
m_tags = tags;
}

if (m_tags.indexOf(",") != -1) {

var split_tags = m_tags.split(",");
var new_data = "";

for (var i = 0; i < split_tags.length; i++) {

if (split_tags[i]) {
if (new_data === "") {
new_data = split_tags[i];
} else {
new_data = new_data + "," + split_tags[i];
}
}

XKit.interface.fetch(m_post, async function(data) {
try {
if (data.status !== 200) throw data;
const { id: post_id, tags: current_tags = '' } = data.data.post;

const current_tags_array = current_tags.split(',').map(tag => tag.trim()).filter(Boolean);
const add_tags_array = tags.split(',').map(tag => tag.replaceAll('#', '').trim()).filter(Boolean);

if (
XKit.extensions.quick_tags.preferences.append_not_replace.value === false &&
current_tags_array.length
) {
await XKit.interface.mass_edit([post_id], { mode: 'remove', tags: current_tags_array });
current_tags_array.splice(0);
}

m_tags = new_data;

}

var m_post_object = XKit.interface.edit_post_object(data.data, { tags: m_tags });

// Now submit it back to the server:
XKit.interface.edit(m_post_object, async function(edit_data) {

XKit.interface.switch_control_button($(m_button), false);

if (edit_data.error === false && edit_data.data.errors === false) {

XKit.interface.switch_control_button($(m_button), false);
await XKit.interface.react.update_view.tags(m_post, m_tags);
await XKit.interface.mass_edit([post_id], { mode: 'add', tags: add_tags_array });
current_tags_array.push(...add_tags_array);

await XKit.interface.react.update_view.tags(m_post, current_tags_array.join(','));
} catch (error) {
if (error.status) {
XKit.window.show("Unable to edit post", `Server responded with status ${error.status}: ${error.message}.<br /><pre style="user-select: text;">${JSON.stringify(error, null, 2)}</pre>`, "error", "<div id=\"xkit-close-message\" class=\"xkit-button default\">OK</div>");
} else if (error.json) {
const response = await error.json();
XKit.window.show("Unable to edit post", `Server responded with status ${response.meta.status}: ${response.meta.msg}.<br /><pre style="user-select: text;">${JSON.stringify(response, null, 2)}</pre>`, "error", "<div id=\"xkit-close-message\" class=\"xkit-button default\">OK</div>");
} else {
// Oops?
if (edit_data.error === true) {
XKit.window.show("Unable to edit post", "Something went wrong, my apologizes.<br/>Please try again later or file a bug report with the error code:<br/>QT01B" + edit_data.status + "<br/>" + edit_data.message, "error", "<div id=\"xkit-close-message\" class=\"xkit-button default\">OK</div>");
} else {
XKit.window.show("Unable to edit post", "Something went wrong, my apologizes.<br/>Please try again later or file a bug report with the error code:<br/>QT01A" + edit_data.status + "<br/>" + edit_data.data.errors, "error", "<div id=\"xkit-close-message\" class=\"xkit-button default\">OK</div>");
}
XKit.window.show("Unable to edit post", `<pre style="user-select: text;">${error}</pre>`, "error", "<div id=\"xkit-close-message\" class=\"xkit-button default\">OK</div>");
}

});

} finally {
XKit.interface.switch_control_button($(m_button), false);
}
}, false);
} else {
XKit.window.show("Unable to edit post", "Something went wrong, my apologies.<br/>Please try again later or file a bug report with the error code:<br/>QT02", "error", "<div id=\"xkit-close-message\" class=\"xkit-button default\">OK</div>");
Expand Down
30 changes: 15 additions & 15 deletions Extensions/xkit_patches.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//* TITLE XKit Patches **//
//* VERSION 7.4.19 **//
//* VERSION 7.4.20 **//
//* DESCRIPTION Patches framework **//
//* DEVELOPER new-xkit **//

Expand Down Expand Up @@ -460,11 +460,12 @@ XKit.extensions.xkit_patches = new Object({

/**
* Edit up to 100 posts at a time via Mass Post Editor
* @param {Object[]} post_ids - array of post IDs to edit
* @param {Object[]} config - settings object
* @param {String} config.mode - "add", "remove", or "delete"
* @param {Object[]} [config.tags] - array of tags to add or remove
* @return {Promise}
*
* @param {string[]} post_ids - array of post IDs to edit
* @param {object} config - settings object
* @param {string} config.mode - "add", "remove", or "delete"
* @param {string[]} [config.tags] - array of tags to add or remove
* @returns {Promise<object>}
*/
XKit.interface.mass_edit = function(post_ids, config) {
const path = {
Expand Down Expand Up @@ -732,12 +733,15 @@ XKit.extensions.xkit_patches = new Object({
};
_.bindAll(XKit.css_map, ['getCssMap', 'keyToClasses', 'keyToCss', 'descendantSelector']);

XKit.tools.Nx_XHR = details => new Promise((resolve, reject) => {
// eslint-disable-next-line no-async-promise-executor
XKit.tools.Nx_XHR = details => new Promise(async (resolve, reject) => {
details.timestamp = new Date().getTime() + Math.random();

const form_key = XKit.interface.form_key() || await XKit.interface.async_form_key();

const standard_headers = {
"X-Requested-With": "XMLHttpRequest",
"X-Tumblr-Form-Key": XKit.interface.form_key(),
"X-Tumblr-Form-Key": form_key,
"X-XKit-Version": XKit.version,
};

Expand Down Expand Up @@ -1088,15 +1092,11 @@ XKit.extensions.xkit_patches = new Object({
var post_tag = XKit.css_map.keyToClasses("tag").join(" ");

for (var i = 0; i < tags_array.length; i++) {
var formatted = encodeURIComponent(tags_array[i]);

if (tags_array[i] === "" || tags_array[i] === " ") { continue; }
const tag = tags_array[i].trim();

if (tags_array[i].substring(0, 1) === " ") {
tags_array[i] = tags_array[i].substring(1);
if (tag) {
m_inner += `<a class="${post_tag}" href="/tagged/${encodeURIComponent(tag)}">#${tag}</a>`;
}

m_inner = m_inner + `<a class="${post_tag}" href=\"/tagged/" ${formatted} \">#${tags_array[i]}</a>`;
}

var tags_class = XKit.css_map.keyToCss("tags");
Expand Down

0 comments on commit 090202d

Please sign in to comment.