From 6ec7e5df2808bd52ba3f44204fac4b59c867902b Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 2 Sep 2016 16:03:24 +0100 Subject: [PATCH] Move scalar auth stuff to ScalarAuthClient from RoomSettings --- src/ScalarAuthClient.js | 41 +++++++++++++++++++- src/components/views/messages/TextualBody.js | 8 ++++ src/components/views/rooms/RoomSettings.js | 41 ++++++-------------- 3 files changed, 59 insertions(+), 31 deletions(-) diff --git a/src/ScalarAuthClient.js b/src/ScalarAuthClient.js index d145cebfe0..7721add5cf 100644 --- a/src/ScalarAuthClient.js +++ b/src/ScalarAuthClient.js @@ -18,9 +18,41 @@ var q = require("q"); var request = require('browser-request'); var SdkConfig = require('./SdkConfig'); +var MatrixClientPeg = require('./MatrixClientPeg'); class ScalarAuthClient { - getScalarToken(openid_token_object) { + + constructor() { + this.scalarToken = null; + } + + connect() { + return this.getScalarToken().then((tok) => { + this.scalarToken = tok; + }); + } + + hasCredentials() { + return this.scalarToken != null; // undef or null + } + + // Returns a scalar_token string + getScalarToken() { + var tok = window.localStorage.getItem("mx_scalar_token"); + if (tok) return q(tok); + + // No saved token, so do the dance to get one. First, we + // need an openid bearer token from the HS. + return MatrixClientPeg.get().getOpenIdToken().then((token_object) => { + // Now we can send that to scalar and exchange it for a scalar token + return this.exchangeForScalarToken(token_object); + }).then((token_object) => { + window.localStorage.setItem("mx_scalar_token", token_object); + return token_object; + }); + } + + exchangeForScalarToken(openid_token_object) { var defer = q.defer(); var scalar_rest_url = SdkConfig.get().integrations_rest_url; @@ -43,6 +75,13 @@ class ScalarAuthClient { return defer.promise; } + + getScalarInterfaceUrlForRoom(roomId) { + var url = SdkConfig.get().integrations_ui_url; + url += "?scalar_token=" + encodeURIComponent(this.scalarToken); + url += "&room_id=" + encodeURIComponent(roomId); + return url; + } } module.exports = ScalarAuthClient; diff --git a/src/components/views/messages/TextualBody.js b/src/components/views/messages/TextualBody.js index 19ca9657c2..47e69d9d29 100644 --- a/src/components/views/messages/TextualBody.js +++ b/src/components/views/messages/TextualBody.js @@ -176,15 +176,23 @@ module.exports = React.createClass({ } }, + onStarterLinkClick: function() { + }, + render: function() { const EmojiText = sdk.getComponent('elements.EmojiText'); var mxEvent = this.props.mxEvent; var content = mxEvent.getContent(); + var body = HtmlUtils.bodyToHtml(content, this.props.highlights, {}); if (this.props.highlightLink) { body = { body }; } + else if (content.data && typeof content.data["org.matrix.neb.starter_link"] === "string") { + console.log("Set starter link"); + body = { body }; + } var widgets; if (this.state.links.length && !this.state.widgetHidden && this.props.showUrlPreview) { diff --git a/src/components/views/rooms/RoomSettings.js b/src/components/views/rooms/RoomSettings.js index 145b8559b7..dd3b12b4fc 100644 --- a/src/components/views/rooms/RoomSettings.js +++ b/src/components/views/rooms/RoomSettings.js @@ -65,7 +65,6 @@ module.exports = React.createClass({ // Default to false if it's undefined, otherwise react complains about changing // components from uncontrolled to controlled isRoomPublished: this._originalIsRoomPublished || false, - scalar_token: null, scalar_error: null, }; }, @@ -81,11 +80,14 @@ module.exports = React.createClass({ console.error("Failed to get room visibility: " + err); }); - this.getScalarToken().done((token) => { - this.setState({scalar_token: token}); + this.scalarClient = new ScalarAuthClient(); + this.scalarClient.connect().done(() => { + this.forceUpdate(); }, (err) => { - this.setState({scalar_error: err}); - }); + this.setState({ + scalar_error: err + }); + }) dis.dispatch({ action: 'ui_opacity', @@ -395,34 +397,13 @@ module.exports = React.createClass({ roomState.mayClientSendStateEvent("m.room.guest_access", cli)) }, - getScalarInterfaceUrl: function() { - var url = SdkConfig.get().integrations_ui_url; - url += "?scalar_token=" + encodeURIComponent(this.state.scalar_token); - url += "&room_id=" + encodeURIComponent(this.props.room.roomId); - return url; - }, - - getScalarToken() { - var tok = window.localStorage.getItem("mx_scalar_token"); - if (tok) return q(tok); - - // No saved token, so do the dance to get one. First, we - // need an openid bearer token from the HS. - return MatrixClientPeg.get().getOpenIdToken().then((token_object) => { - // Now we can send that to scalar and exchange it for a scalar token - var scalar_auth_client = new ScalarAuthClient(); - return scalar_auth_client.getScalarToken(token_object); - }).then((token_object) => { - window.localStorage.setItem("mx_scalar_token", token_object); - return token_object; - }); - }, - onManageIntegrations(ev) { ev.preventDefault(); var IntegrationsManager = sdk.getComponent("views.settings.IntegrationsManager"); Modal.createDialog(IntegrationsManager, { - src: this.state.scalar_token ? this.getScalarInterfaceUrl() : null + src: this.scalarClient.hasCredentials() ? + this.scalarClient.getScalarInterfaceUrlForRoom(this.props.room.roomId) : + null }, ""); }, @@ -649,7 +630,7 @@ module.exports = React.createClass({ if (UserSettingsStore.isFeatureEnabled("integration_management")) { let integrations_body; - if (this.state.scalar_token) { + if (this.scalarClient.hasCredentials()) { integrations_body = (
Manage integrations