From cc87ff3af6de771ec4caed02954268c6baaa16ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= Date: Fri, 21 Mar 2025 12:29:54 +0200 Subject: [PATCH] Remove old and unused Connector Server endpoints (#5148) --- .../zotero/xpcom/server/server_connector.js | 226 ------------------ test/tests/server_connectorTest.js | 50 ---- 2 files changed, 276 deletions(-) diff --git a/chrome/content/zotero/xpcom/server/server_connector.js b/chrome/content/zotero/xpcom/server/server_connector.js index 4af4bd85d4..fdf8d47fe5 100644 --- a/chrome/content/zotero/xpcom/server/server_connector.js +++ b/chrome/content/zotero/xpcom/server/server_connector.js @@ -218,204 +218,6 @@ Zotero.Server.Connector.Detect.prototype = { }, } -/** - * Performs translation of a given page - * - * Accepts: - * uri - The URI of the page to be saved - * html - document.innerHTML or equivalent - * cookie - document.cookie or equivalent - * translatorID [optional] - a translator ID as returned by /connector/detect - * - * Returns: - * If a single item, sends response code 201 with item in body. - * If multiple items, sends response code 300 with the following content: - * items - list of items in the format typically passed to the selectItems handler - * instanceID - an ID that must be maintained for the subsequent Zotero.Connector.Select call - * uri - the URI of the page for which multiple items are available - */ -Zotero.Server.Connector.SavePage = function() {}; -Zotero.Server.Endpoints["/connector/savePage"] = Zotero.Server.Connector.SavePage; -Zotero.Server.Connector.SavePage.prototype = { - supportedMethods: ["POST"], - supportedDataTypes: ["application/json"], - permitBookmarklet: true, - - /** - * Either loads HTML into a hidden browser and initiates translation, or saves items directly - * to the database - */ - init: function(requestData) { - return new Zotero.Promise(async function(resolve) { - function sendResponseCallback() { - if (arguments.length > 1) { - return resolve(arguments); - } - return resolve(arguments[0]); - } - var data = requestData.data; - var { library, collection, editable } = Zotero.Server.Connector.getSaveTarget(); - var libraryID = library.libraryID; - var targetID = collection ? collection.treeViewID : library.treeViewID; - - if (Zotero.Server.Connector.SessionManager.get(data.sessionID)) { - return sendResponseCallback(409, "application/json", JSON.stringify({ error: "SESSION_EXISTS" })); - } - - // Shouldn't happen as long as My Library exists - if (!library.editable) { - Zotero.logError("Can't add item to read-only library " + library.name); - return sendResponseCallback(500, "application/json", JSON.stringify({ libraryEditable: false })); - } - - var session = Zotero.Server.Connector.SessionManager.create(data.sessionID); - await session.update(targetID); - - this.sendResponse = sendResponseCallback; - this._parsedPostData = data; - - try { - var translators = await Zotero.Server.Connector.Detect.prototype.getTranslators.call(this, requestData); - } catch(e) { - Zotero.logError(e); - session.remove(); - return sendResponseCallback(500); - } - - if(!translators.length) { - Zotero.debug(`No translators available for /connector/savePage ${data.uri}`); - session.remove(); - return this.sendResponse(500); - } - - // set handlers for translation - var me = this; - var translate = this._translate; - translate.setHandler("select", function(obj, item, callback) { return me._selectItems(obj, item, callback) }); - let attachmentTitleData = {}; - translate.setHandler("itemsDone", function(obj, items) { - if(items.length || me.selectedItems === false) { - items = items.map((item) => { - let o = { - id: item.id, - title: item.title, - itemType: item.itemType, - contentType: item.mimeType, - mimeType: item.mimeType, // TODO: Remove - }; - if (item.attachments) { - let id = 0; - for (let attachment of item.attachments) { - attachment.parent = item.id; - attachment.id = id++; - } - o.attachments = item.attachments.map((attachment) => { - // Retaining id and parent info for session progress management - attachmentTitleData[attachment.title] = {id: attachment.id, parent: item.id}; - return { - id: session.id + '_' + attachment.id, // TODO: Remove prefix - title: attachment.title, - contentType: attachment.contentType, - mimeType: attachment.mimeType, // TODO: Remove - }; - }); - }; - session.onProgress(item, 100); - return o; - }); - me.sendResponse(201, "application/json", JSON.stringify({items})); - } else { - me.sendResponse(500); - session.remove(); - } - }); - - translate.setHandler("attachmentProgress", function(obj, attachment, progress, error) { - if (attachmentTitleData[attachment.title]) { - session.onProgress(Object.assign( - {}, - attachment, - attachmentTitleData[attachment.title], - ), progress, error); - } - }); - - translate.setHandler("error", function(obj, err) { - Zotero.logError(err); - sendResponseCallback(500); - session.remove(); - }); - - if (this._parsedPostData.translatorID) { - translate.setTranslator(this._parsedPostData.translatorID); - } else { - translate.setTranslator(translators[0]); - } - let items = await translate.translate({libraryID, collections: collection ? [collection.id] : false}); - items.forEach((item, index) => { - session.addItem(data.items[index].id, item); - }); - }.bind(this)); - }, - - /** - * Callback to be executed when items must be selected - * @param {Zotero.Translate} translate - * @param {Object} itemList ID=>text pairs representing available items - */ - _selectItems: function(translate, itemList, callback) { - var instanceID = Zotero.randomString(); - Zotero.Server.Connector._waitingForSelection[instanceID] = this; - - // Fix for translators that don't create item lists as objects - if(itemList.push && typeof itemList.push === "function") { - var newItemList = {}; - for(var item in itemList) { - newItemList[item] = itemList[item]; - } - itemList = newItemList; - } - - // Send "Multiple Choices" HTTP response - this.sendResponse(300, "application/json", JSON.stringify({selectItems: itemList, instanceID: instanceID, uri: this._parsedPostData.uri})); - this.selectedItemsCallback = callback; - } -} - -/** - * Handle item selection - * - * Accepts: - * selectedItems - a list of items to translate in ID => text format as returned by a selectItems handler - * instanceID - as returned by savePage call - * Returns: - * 201 response code with empty body - */ -Zotero.Server.Connector.SelectItems = function() {}; -Zotero.Server.Endpoints["/connector/selectItems"] = Zotero.Server.Connector.SelectItems; -Zotero.Server.Connector.SelectItems.prototype = { - supportedMethods: ["POST"], - supportedDataTypes: ["application/json"], - permitBookmarklet: true, - - /** - * Finishes up translation when item selection is complete - * @param {String} data POST data or GET query string - * @param {Function} sendResponseCallback function to send HTTP response - */ - init: function(data, sendResponseCallback) { - var saveInstance = Zotero.Server.Connector._waitingForSelection[data.instanceID]; - saveInstance.sendResponse = sendResponseCallback; - - var selectedItems = false; - for(var i in data.selectedItems) { - selectedItems = data.selectedItems; - break; - } - saveInstance.selectedItemsCallback(selectedItems); - } -} - /** * Saves items to DB * @@ -1338,34 +1140,6 @@ Zotero.Server.Connector.Ping.prototype = { } } -/** - * IE messaging hack - * - * Accepts: - * Nothing - * Returns: - * Static Response - */ -Zotero.Server.Connector.IEHack = function() {}; -Zotero.Server.Endpoints["/connector/ieHack"] = Zotero.Server.Connector.IEHack; -Zotero.Server.Connector.IEHack.prototype = { - supportedMethods: ["GET"], - permitBookmarklet: true, - - /** - * Sends a fixed webpage - * @param {String} data POST data or GET query string - * @param {Function} sendResponseCallback function to send HTTP response - */ - init: function(postData, sendResponseCallback) { - sendResponseCallback(200, "text/html", - ''+ - ''+ - ''+ - ''); - } -} - /** * Make an HTTP request from the client. Accepts {@link Zotero.HTTP.request} options and returns a minimal response * object with the same form as the one returned from {@link Zotero.Utilities.Translate#request}. diff --git a/test/tests/server_connectorTest.js b/test/tests/server_connectorTest.js index 9c575fdc38..7ecfe56a3a 100644 --- a/test/tests/server_connectorTest.js +++ b/test/tests/server_connectorTest.js @@ -494,56 +494,6 @@ describe("Connector Server", function () { }); }); - describe("/connector/savePage", function() { - before(async function () { - await selectLibrary(win); - }); - - it("should return 500 if no translator available for page", function* () { - var xmlhttp = yield Zotero.HTTP.request( - 'POST', - connectorServerPath + "/connector/savePage", - { - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - uri: "http://example.com", - html: "TitleBody" - }), - successCodes: false - } - ); - assert.equal(xmlhttp.status, 500); - }); - - it("should translate a page if translators are available", function* () { - var html = Zotero.File.getContentsFromURL(getTestDataUrl('coins.html')); - var promise = waitForItemEvent('add'); - var xmlhttp = yield httpRequest( - 'POST', - connectorServerPath + "/connector/savePage", - { - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - uri: "https://example.com/test", - html - }), - successCodes: false - } - ); - - let ids = yield promise; - var item = Zotero.Items.get(ids[0]); - var title = "Test Page"; - assert.equal(JSON.parse(xmlhttp.responseText).items[0].title, title); - assert.equal(item.getField('title'), title); - assert.equal(xmlhttp.status, 201); - }); - }); - describe("/connector/saveAttachment", function () { const pdfPath = OS.Path.join(getTestDataDirectory().path, 'test.pdf'); let pdfSample, pdfArrayBuffer;