diff --git a/seance.js b/seance.js index 5881c62..6649c4a 100644 --- a/seance.js +++ b/seance.js @@ -325,7 +325,7 @@ class Seance { loglevel: 'info' }) - this.ghostAdmin.posts.add({ + let res = await this.ghostAdmin.posts.add({ title: postMeta.title, custom_excerpt: postMeta.subtitle || null, tags: postMeta.tags, @@ -333,21 +333,33 @@ class Seance { html: markdown.toHTML(await fs.promises.readFile(postOutput, 'utf-8')), feature_image: featuredImagePath }, {source: 'html'}) - .then((res) => { - // Check if user was added - if (res.primary_author.id == 1) { - this.emit('notification', { - message: `WARNING: The admin editor, "${res.primary_author.name}", is set as author for this post. If this is incorrect, there was some problem matching usernames. Please check and set it manually.`, - }) - } - - this.emit('update', { - status: 'progress', - progress: 100, // we don't know the percentage - message: 'Post conveyed successfully', - loglevel: 'info' + + // Check if user was added + if (res.primary_author.id == 1) { + this.emit('notification', { + message: `WARNING: The admin editor, "${res.primary_author.name}", is set as author for this post. If this is incorrect, there was some problem matching usernames. Please check and set it manually.`, }) + } + + this.emit('update', { + status: 'progress', + progress: 100, // we don't know the percentage + message: 'Post conveyed successfully', + loglevel: 'info' }) + + console.log(res) + + return { + slug: res.slug, + id: res.id, + uuid: res.uuid, + preview_url: res.url, + primary_author: res.primary_author, + title: res.title, + subtitle: res.custom_excerpt, + status: res.status, + } }; diff --git a/server.js b/server.js index 2ad7be2..c69a9b5 100644 --- a/server.js +++ b/server.js @@ -193,7 +193,7 @@ app.ws('/ws/push-ghost', (ws, req) => { command = msg.split(' ') if (command.length == 3 && command[0] == 'push') { - const postSlug = command[1] + let postSlug = command[1] // check password if (command[2] != process.env.SEANCE_PW) { @@ -204,6 +204,9 @@ app.ws('/ws/push-ghost', (ws, req) => { // Start the Seance session seance = new Seance() + // catch data + let postId + // set up handlers seance.on('update', (e) => { ws.send(`update: ${e.message}`) @@ -217,13 +220,19 @@ app.ws('/ws/push-ghost', (ws, req) => { ws.send(`error: ${e.message}`) }) - seance.pushToGhost(postSlug) - .then(() => { - console.info(`"${postSlug}" pushed successfully.`) + // run seance and wait till it finishes + let res = await seance.pushToGhost(postSlug) + + console.info(`"${postSlug}" pushed successfully.`) + + // send 'done' message + let ghostSite = await seance.ghostAdmin.site.read() + + let postEditUrl + if (!!res.id) postEditUrl = `${ghostSite.url}ghost/#/editor/post/${res.id}` + if (!!res.slug) postSlug = res.slug + ws.send(`done: ${postSlug} ${postEditUrl}`) - // send 'done' message - ws.send(`done: ${postSlug}`) - }) } else { ws.send('error: Malformed message') return diff --git a/static/app.js b/static/app.js index 3eb40e9..089ad73 100644 --- a/static/app.js +++ b/static/app.js @@ -96,7 +96,7 @@ function mediumFetch(e) { function ghostPush(e) { if(e) { e.preventDefault() } - const postSlug = document.querySelector('[data-post-slug]').dataset.postSlug + let postSlug = document.querySelector('[data-post-slug]').dataset.postSlug var password = document.querySelector('input[name=password]').value console.log('Pushing ' + postSlug) const socketProtocol = (window.location.protocol === 'https:' ? 'wss:' : 'ws:') @@ -147,7 +147,23 @@ function ghostPush(e) { addNotification(commandData, className='is-danger') } else { // show alert - addNotification(`${postSlug} has been pushed to Ghost. Buffer share link: https://www.snipettemag.com/${postSlug}`, 'is-success') + // TODO: don't hard-code destination URL + // (instead, fetch from server's config and display) + + // first, unpack info + let postEditUrl + + commandData = commandData.split(' ') + if (!!commandData[0]) postSlug = commandData[0] + if (!!commandData[1]) postEditUrl = commandData[1] + + let msg = `${postSlug} has been pushed to Ghost. Buffer share link: https://www.snipettemag.com/${postSlug}` + + if (!!postEditUrl) { + msg = msg + `. Edit on Ghost` + } + + addNotification(msg, 'is-success') } // since it's done, clean up