From 89ca8e08d659a221f46f445a6b3fedbd14aa4e55 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 20 Jun 2020 12:03:22 +0100 Subject: [PATCH] Desktop: API: Fixed externalEditWatcher/noteIsWatched call, fixed tests --- .eslintignore | 1 + .gitignore | 1 + ReactNativeClient/lib/ClipperServer.js | 7 +++++-- .../lib/services/ExternalEditWatcher.js | 3 ++- ReactNativeClient/lib/services/rest/Api.js | 15 ++++++--------- .../lib/services/rest/actionApi.desktop.ts | 7 +++++++ 6 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 ReactNativeClient/lib/services/rest/actionApi.desktop.ts diff --git a/.eslintignore b/.eslintignore index 9cb5dc8227..3df656a84a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -114,6 +114,7 @@ ReactNativeClient/lib/services/keychain/KeychainServiceDriver.mobile.js ReactNativeClient/lib/services/keychain/KeychainServiceDriver.node.js ReactNativeClient/lib/services/keychain/KeychainServiceDriverBase.js ReactNativeClient/lib/services/ResourceEditWatcher.js +ReactNativeClient/lib/services/rest/actionApi.desktop.js ReactNativeClient/lib/services/SettingUtils.js ReactNativeClient/lib/services/UndoRedoService.js ReactNativeClient/lib/ShareExtension.js diff --git a/.gitignore b/.gitignore index 31032b44ea..6b2b19d1dd 100644 --- a/.gitignore +++ b/.gitignore @@ -104,6 +104,7 @@ ReactNativeClient/lib/services/keychain/KeychainServiceDriver.mobile.js ReactNativeClient/lib/services/keychain/KeychainServiceDriver.node.js ReactNativeClient/lib/services/keychain/KeychainServiceDriverBase.js ReactNativeClient/lib/services/ResourceEditWatcher.js +ReactNativeClient/lib/services/rest/actionApi.desktop.js ReactNativeClient/lib/services/SettingUtils.js ReactNativeClient/lib/services/UndoRedoService.js ReactNativeClient/lib/ShareExtension.js diff --git a/ReactNativeClient/lib/ClipperServer.js b/ReactNativeClient/lib/ClipperServer.js index 14a597e5d6..f63eb0f2e0 100644 --- a/ReactNativeClient/lib/ClipperServer.js +++ b/ReactNativeClient/lib/ClipperServer.js @@ -4,6 +4,7 @@ const { Logger } = require('lib/logger.js'); const { randomClipperPort, startPort } = require('lib/randomClipperPort'); const enableServerDestroy = require('server-destroy'); const Api = require('lib/services/rest/Api'); +const actionApi = require('lib/services/rest/ActionApi.desktop').default; const ApiResponse = require('lib/services/rest/ApiResponse'); const multiparty = require('multiparty'); @@ -15,7 +16,7 @@ class ClipperServer { this.port_ = null; this.api_ = new Api(() => { return Setting.value('api.token'); - }); + }, actionApi); } static instance() { @@ -143,6 +144,8 @@ class ClipperServer { writeResponseInstance(code, response); } else if (typeof response === 'string') { writeResponseText(code, response); + } else if (response === null || response === undefined) { + writeResponseText(code, ''); } else { writeResponseJson(code, response); } @@ -155,7 +158,7 @@ class ClipperServer { const execRequest = async (request, body = '', files = []) => { try { const response = await this.api_.route(request.method, url.pathname, url.query, body, files); - writeResponse(200, response ? response : ''); + writeResponse(200, response); } catch (error) { this.logger().error(error); const httpCode = error.httpCode ? error.httpCode : 500; diff --git a/ReactNativeClient/lib/services/ExternalEditWatcher.js b/ReactNativeClient/lib/services/ExternalEditWatcher.js index d3075b02df..ccea96d8bd 100644 --- a/ReactNativeClient/lib/services/ExternalEditWatcher.js +++ b/ReactNativeClient/lib/services/ExternalEditWatcher.js @@ -36,7 +36,8 @@ class ExternalEditWatcher { return this.stopWatching(noteId); }, noteIsWatched: async ({ noteId }) => { - return this.noteIsWatched(noteId); + const note = await Note.load(noteId); + return this.noteIsWatched(note); }, }; } diff --git a/ReactNativeClient/lib/services/rest/Api.js b/ReactNativeClient/lib/services/rest/Api.js index 6e5ed0af56..4b93b90dd7 100644 --- a/ReactNativeClient/lib/services/rest/Api.js +++ b/ReactNativeClient/lib/services/rest/Api.js @@ -25,8 +25,6 @@ const uri2path = require('file-uri-to-path'); const { MarkupToHtml } = require('lib/joplin-renderer'); const { uuid } = require('lib/uuid'); -const ExternalEditWatcher = require('lib/services/ExternalEditWatcher'); - class ApiError extends Error { constructor(message, httpCode = 400) { super(message); @@ -60,10 +58,11 @@ class ErrorBadRequest extends ApiError { } class Api { - constructor(token = null) { + constructor(token = null, actionApi = null) { this.token_ = token; this.knownNounces_ = {}; this.logger_ = new Logger(); + this.actionApi_ = actionApi; } get token() { @@ -391,13 +390,11 @@ class Api { this.checkToken_(request); if (request.method !== 'POST') throw new ErrorMethodNotAllowed(); + if (!this.actionApi_) throw new ErrorNotFound('No action API has been setup!'); + if (!this.actionApi_[serviceName]) throw new ErrorNotFound(`No such service: ${serviceName}`); - if (serviceName === 'externalEditWatcher') { - const externalApi = ExternalEditWatcher.instance().externalApi(); - return this.execServiceActionFromRequest_(externalApi, JSON.parse(request.body)); - } else { - throw new ErrorNotFound(`No such service: ${serviceName}`); - } + const externalApi = this.actionApi_[serviceName](); + return this.execServiceActionFromRequest_(externalApi, JSON.parse(request.body)); } async action_notes(request, id = null, link = null) { diff --git a/ReactNativeClient/lib/services/rest/actionApi.desktop.ts b/ReactNativeClient/lib/services/rest/actionApi.desktop.ts new file mode 100644 index 0000000000..df2222d288 --- /dev/null +++ b/ReactNativeClient/lib/services/rest/actionApi.desktop.ts @@ -0,0 +1,7 @@ +const ExternalEditWatcher = require('lib/services/ExternalEditWatcher'); + +export default { + + externalEditWatcher: () => ExternalEditWatcher.instance().externalApi(), + +};