From 45160a2e73c6f1b1ad2c6304362ce014f46b150c Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 20 Jun 2020 02:30:09 +0100 Subject: [PATCH] Desktop: API: Add support for external editing from API --- .../lib/services/ExternalEditWatcher.js | 15 ++++++++++++ ReactNativeClient/lib/services/rest/Api.js | 23 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/ReactNativeClient/lib/services/ExternalEditWatcher.js b/ReactNativeClient/lib/services/ExternalEditWatcher.js index 8e77fca7a1..d3075b02df 100644 --- a/ReactNativeClient/lib/services/ExternalEditWatcher.js +++ b/ReactNativeClient/lib/services/ExternalEditWatcher.js @@ -26,6 +26,21 @@ class ExternalEditWatcher { return this.instance_; } + externalApi() { + return { + openAndWatch: async ({ noteId }) => { + const note = await Note.load(noteId); + return this.openAndWatch(note); + }, + stopWatching: async ({ noteId }) => { + return this.stopWatching(noteId); + }, + noteIsWatched: async ({ noteId }) => { + return this.noteIsWatched(noteId); + }, + }; + } + tempDir() { return Setting.value('profileDir'); } diff --git a/ReactNativeClient/lib/services/rest/Api.js b/ReactNativeClient/lib/services/rest/Api.js index 806fb3e953..6e5ed0af56 100644 --- a/ReactNativeClient/lib/services/rest/Api.js +++ b/ReactNativeClient/lib/services/rest/Api.js @@ -25,6 +25,8 @@ 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); @@ -377,6 +379,27 @@ class Api { return options; } + async execServiceActionFromRequest_(externalApi, request) { + const action = externalApi[request.action]; + if (!action) throw new ErrorNotFound(`Invalid action: ${request.action}`); + const args = Object.assign({}, request); + delete args.action; + return action(args); + } + + async action_services(request, serviceName) { + this.checkToken_(request); + + if (request.method !== 'POST') throw new ErrorMethodNotAllowed(); + + if (serviceName === 'externalEditWatcher') { + const externalApi = ExternalEditWatcher.instance().externalApi(); + return this.execServiceActionFromRequest_(externalApi, JSON.parse(request.body)); + } else { + throw new ErrorNotFound(`No such service: ${serviceName}`); + } + } + async action_notes(request, id = null, link = null) { this.checkToken_(request);