From cebb2b773fafeca9e87994cf9585885affea870f Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 27 Jun 2019 19:38:12 +0100 Subject: [PATCH 1/3] Add /myavatar command Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/SlashCommands.js | 54 ++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/src/SlashCommands.js b/src/SlashCommands.js index f25bc9af07..37ae51eb1c 100644 --- a/src/SlashCommands.js +++ b/src/SlashCommands.js @@ -34,6 +34,26 @@ import WidgetUtils from "./utils/WidgetUtils"; import {textToHtmlRainbow} from "./utils/colour"; import Promise from "bluebird"; +const requestSingleFileUpload = async () => { + return new Promise((resolve) => { + const fileSelector = document.createElement('input'); + fileSelector.setAttribute('type', 'file'); + fileSelector.onchange = (ev) => { + const file = ev.target.files[0]; + + const UploadConfirmDialog = sdk.getComponent("dialogs.UploadConfirmDialog"); + Modal.createTrackedDialog('Upload Files confirmation', '', UploadConfirmDialog, { + file, + onFinished: (shouldContinue) => { + if (shouldContinue) resolve(MatrixClientPeg.get().uploadContent(file)); + }, + }); + }; + + fileSelector.click(); + }); +}; + class Command { constructor({name, args='', description, runFn, hideCompletionAfterSpace=false}) { this.command = '/' + name; @@ -222,23 +242,7 @@ export const CommandMap = { let promise = Promise.resolve(args); if (!args) { - promise = new Promise((resolve) => { - const fileSelector = document.createElement('input'); - fileSelector.setAttribute('type', 'file'); - fileSelector.onchange = (ev) => { - const file = ev.target.files[0]; - - const UploadConfirmDialog = sdk.getComponent("dialogs.UploadConfirmDialog"); - Modal.createTrackedDialog('Upload Files confirmation', '', UploadConfirmDialog, { - file, - onFinished: (shouldContinue) => { - if (shouldContinue) resolve(cli.uploadContent(file)); - }, - }); - }; - - fileSelector.click(); - }); + promise = requestSingleFileUpload(); } return success(promise.then((url) => { @@ -252,6 +256,22 @@ export const CommandMap = { }, }), + myavatar: new Command({ + name: 'myavatar', + args: '[]', + description: _td('Changes your avatar in all rooms'), + runFn: function(roomId, args) { + let promise = Promise.resolve(args); + if (!args) { + promise = requestSingleFileUpload(); + } + + return success(promise.then((url) => { + return MatrixClientPeg.get().setAvatarUrl(url); + })); + }, + }), + tint: new Command({ name: 'tint', args: ' []', From e8db379fede7fb800c325eb64043bb7b31ff5aba Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 27 Jun 2019 19:41:29 +0100 Subject: [PATCH 2/3] rename helper method Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/SlashCommands.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SlashCommands.js b/src/SlashCommands.js index 37ae51eb1c..b554c4117a 100644 --- a/src/SlashCommands.js +++ b/src/SlashCommands.js @@ -34,7 +34,7 @@ import WidgetUtils from "./utils/WidgetUtils"; import {textToHtmlRainbow} from "./utils/colour"; import Promise from "bluebird"; -const requestSingleFileUpload = async () => { +const singleMxcUpload = async () => { return new Promise((resolve) => { const fileSelector = document.createElement('input'); fileSelector.setAttribute('type', 'file'); @@ -242,7 +242,7 @@ export const CommandMap = { let promise = Promise.resolve(args); if (!args) { - promise = requestSingleFileUpload(); + promise = singleMxcUpload(); } return success(promise.then((url) => { @@ -263,7 +263,7 @@ export const CommandMap = { runFn: function(roomId, args) { let promise = Promise.resolve(args); if (!args) { - promise = requestSingleFileUpload(); + promise = singleMxcUpload(); } return success(promise.then((url) => { From 77dbc793863f1733b9b68cf53a923bbc3250b858 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 29 Jun 2019 07:05:43 +0100 Subject: [PATCH 3/3] clean up promises properly Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/SlashCommands.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/SlashCommands.js b/src/SlashCommands.js index b554c4117a..07ca61487e 100644 --- a/src/SlashCommands.js +++ b/src/SlashCommands.js @@ -45,7 +45,7 @@ const singleMxcUpload = async () => { Modal.createTrackedDialog('Upload Files confirmation', '', UploadConfirmDialog, { file, onFinished: (shouldContinue) => { - if (shouldContinue) resolve(MatrixClientPeg.get().uploadContent(file)); + resolve(shouldContinue ? MatrixClientPeg.get().uploadContent(file) : null); }, }); }; @@ -246,6 +246,7 @@ export const CommandMap = { } return success(promise.then((url) => { + if (!url) return; const ev = room.currentState.getStateEvents('m.room.member', userId); const content = { ...ev ? ev.getContent() : { membership: 'join' }, @@ -267,6 +268,7 @@ export const CommandMap = { } return success(promise.then((url) => { + if (!url) return; return MatrixClientPeg.get().setAvatarUrl(url); })); },