diff --git a/src/js/classes/storage.js b/src/js/classes/storage.js index 73a05555..1cde4957 100644 --- a/src/js/classes/storage.js +++ b/src/js/classes/storage.js @@ -202,11 +202,13 @@ export const StorageJs = (type = 'gist') => { this.lastStorageHost = newHost; }, token: undefined, + isTokenInvalid: true, gistId: undefined, setCredentials: function(token, gistId){ //console.log("using gist credentials", {token, gistId}) this.token = token this.gistId = gistId + this.isTokenInvalid = !token || token.length < 5 }, filesInGist: {}, getFilesInGist: function(fileKey) { @@ -215,6 +217,9 @@ export const StorageJs = (type = 'gist') => { console.error(`${fileKey} not found in gist`, this.filesInGist); }, getGist: function(gistId, onFail = () => {}) { + if (!gistId) { + throw new Error('No gist id specified'); + } const fetchAddress = `https://api.github.com/gists/${gistId}`; return fetch(fetchAddress, { method: 'GET', @@ -231,15 +236,24 @@ export const StorageJs = (type = 'gist') => { 401: "Is your Gist Token valid?", 404: `Is your Gist ID online?\n\naddress:\n${fetchAddress}` } - alert(`Failed to get:\n${fetchAddress}...\n\nSTATUS: ${data.status}\n${data.status in GistStatusHints ? GistStatusHints[data.status] : ""}`) + console.error('GOT -- ', { data, fetchAddress, data }) + onFail(`Failed to get:\n${fetchAddress}...\n\nSTATUS: ${data.status}\n${data.status in GistStatusHints ? GistStatusHints[data.status] : ""}`) if (data.status in GistStatusHints) onFail(data.status); if (data.status === 404) window.open(fetchAddress, '_blank').focus(); + + // try to fetch without authorisation + return fetch(fetchAddress).then(data=> { + console.warn("Got data without authorisation") + this.isTokenInvalid = true; + return data.json() + }) } + this.isTokenInvalid = false; return data.json(); }) .then(content => { console.log('NEW from get::', { content }); - this.filesInGist = content.files; + this.filesInGist = content.files || {}; const inputOptions = {}; Object.keys(this.filesInGist).forEach(key => { inputOptions[key] = key; @@ -256,6 +270,9 @@ export const StorageJs = (type = 'gist') => { hasGistSettings: function() { return this.gistId && this.gistId.length > 0; }, + getIsTokenInvalid: function() { + return !this.token || this.token.length < 5 || this.isTokenInvalid + }, getGistFile: function(onFail = () => {}) { return this.getGist(this.gistId, onFail); }, diff --git a/src/public/plugins/index.js b/src/public/plugins/index.js index 74f10b16..a8232e47 100644 --- a/src/public/plugins/index.js +++ b/src/public/plugins/index.js @@ -246,9 +246,12 @@ export var Plugins = function (app) { }); }; + const isGistTokenInvalid = () => { + return app.data.storage.getIsTokenInvalid(); + } const getGistPluginFiles = () => { - return new Promise((resolve, reject) => { - if (!app.settings.gistPluginsFile()) reject(); + return new Promise((resolve) => { + // if (!app.settings.gistPluginsFile()) reject("No"); app.data.storage .getGist(app.settings.gistPluginsFile()) .then(({ filesInGist }) => { @@ -291,7 +294,8 @@ export var Plugins = function (app) { setVloatilePlugin, setVloatilePlugins, getGistPluginFiles, - saveGistPlugin + saveGistPlugin, + isGistTokenInvalid }; // built in plugin initiation diff --git a/src/public/plugins/plugin-editor.js b/src/public/plugins/plugin-editor.js index c28ff06b..65168258 100644 --- a/src/public/plugins/plugin-editor.js +++ b/src/public/plugins/plugin-editor.js @@ -90,7 +90,8 @@ export var PluginEditor = function ({ setVloatilePlugin, setVloatilePlugins, getGistPluginFiles, - saveGistPlugin + saveGistPlugin, + isGistTokenInvalid }) { const self = this; this.name = 'PluginEditor'; @@ -123,7 +124,7 @@ export var PluginEditor = function ({ document.getElementById('js-editor').style.display = mode === 'edit' ? 'block' : 'none'; document.getElementById('diff-editor').style.display = - mode === 'commit' && app.settings.gistPluginsFile() ? 'block' : 'none'; + mode === 'commit' ? 'block' : 'none'; document.getElementById('plugin-differ-commit').style.display = mode === 'commit' ? 'block' : 'none'; document.getElementById('plugin-output-previewer').style.display = @@ -151,22 +152,33 @@ export var PluginEditor = function ({ beautify.beautify(this.editor.session); if (this.mode === 'commit') { + fileContents = this.editor.getValue(); + this.differ + .getEditors() + .left.getSession() + .setValue(fileContents); getGistPluginFiles().then(gistPluginFiles => { const gistPluginFile = gistPluginFiles.find( item => item.filename == fileName ); - console.log({ gistPluginFile }, this.differ.getEditors()); - fileContents = this.editor.getValue(); + + const isTokenInvalid = isGistTokenInvalid() + const gistAccesError = isTokenInvalid? `//Access to gist writing failed\n\n//Do you have a valid token.\n// It needs to have permission to edit the gist file.`: `//${fileName}\n\n//Gist with this filename is missing.\n// Have you deleted/renamed it?` this.differ .getEditors() - .left.getSession() - .setValue(fileContents); - + .right.getSession() + .setValue(gistPluginFile && !isTokenInvalid ? gistPluginFile.content : gistAccesError); + + this.differ.getEditors().right.setReadOnly(isTokenInvalid); + document.getElementById('plugin-differ-commit').className = isTokenInvalid ? "disabled" : "" + }) + .catch(error=>{ + this.differ.getEditors().right.setReadOnly(true); + document.getElementById('plugin-differ-commit').className = "disabled"; this.differ .getEditors() .right.getSession() - .setValue(gistPluginFile ? gistPluginFile.content : `//${fileName}\n\n//Gist with this filename is missing.\n// Have you deleted/renamed it?`); - // this.differ.getEditors().right.setReadOnly(this.mode === 'commit'); + .setValue(error.toString()) }); } if (this.mode === 'test') {