From c4a3118e9f885e92bb9b7d882898e9a51fc3be69 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Tue, 2 Aug 2016 16:37:09 -0400 Subject: PLT-3408 Add SiteURL to config.json (#3692) * PLT-3408 Changed serverside code to get the service's URL from config.json * PLT-3408 Changed most clientside code to use the SiteURL config setting instead of window.location * PLT-3408 Changed default SiteURL to be autodetected --- webapp/actions/websocket_actions.jsx | 22 +++++++-- webapp/components/change_url_modal.jsx | 5 +- webapp/components/channel_header.jsx | 2 +- webapp/components/channel_info_modal.jsx | 3 +- .../components/create_team/components/team_url.jsx | 2 +- webapp/components/file_attachment.jsx | 2 +- .../components/installed_incoming_webhook.jsx | 2 +- webapp/components/login/login_controller.jsx | 2 +- webapp/components/more_channels.jsx | 2 +- webapp/components/more_direct_channels.jsx | 2 +- webapp/components/navbar_dropdown.jsx | 4 +- webapp/components/needs_team.jsx | 2 +- webapp/components/new_channel_flow.jsx | 3 +- webapp/components/popover_list_members.jsx | 3 +- webapp/components/removed_from_channel_modal.jsx | 4 +- webapp/components/search_results_item.jsx | 3 +- webapp/routes/route_team.jsx | 3 +- webapp/stores/team_store.jsx | 18 +++---- webapp/utils/utils.jsx | 55 +--------------------- 19 files changed, 50 insertions(+), 89 deletions(-) (limited to 'webapp') diff --git a/webapp/actions/websocket_actions.jsx b/webapp/actions/websocket_actions.jsx index 61503c68a..e93b2d25b 100644 --- a/webapp/actions/websocket_actions.jsx +++ b/webapp/actions/websocket_actions.jsx @@ -32,12 +32,26 @@ const MAX_WEBSOCKET_FAILS = 7; export function initialize() { if (window.WebSocket) { - let protocol = 'ws://'; - if (window.location.protocol === 'https:') { - protocol = 'wss://'; + let connUrl = window.mm_config.SiteURL; + + // replace the protocol with a websocket one + if (connUrl.startsWith('https:')) { + connUrl = connUrl.replace(/^https:/, 'wss:'); + } else { + connUrl = connUrl.replace(/^http:/, 'ws:'); + } + + // append a port number if one isn't already specified + if (!(/:\d+$/).test(connUrl)) { + if (connUrl.startsWith('wss:')) { + connUrl += ':' + global.window.mm_config.WebsocketSecurePort; + } else { + connUrl += ':' + global.window.mm_config.WebsocketPort; + } } - const connUrl = protocol + location.host + ((/:\d+/).test(location.host) ? '' : Utils.getWebsocketPort(protocol)) + Client.getUsersRoute() + '/websocket'; + // append the websocket api path + connUrl += Client.getUsersRoute() + '/websocket'; WebSocketClient.setEventCallback(handleEvent); WebSocketClient.setReconnectCallback(handleReconnect); diff --git a/webapp/components/change_url_modal.jsx b/webapp/components/change_url_modal.jsx index 9a526a9ee..2219ff317 100644 --- a/webapp/components/change_url_modal.jsx +++ b/webapp/components/change_url_modal.jsx @@ -4,6 +4,7 @@ import ReactDOM from 'react-dom'; import Constants from 'utils/constants.jsx'; import {Modal, Tooltip, OverlayTrigger} from 'react-bootstrap'; +import TeamStore from 'stores/team_store.jsx'; import * as Utils from 'utils/utils.jsx'; import {FormattedMessage} from 'react-intl'; @@ -130,8 +131,8 @@ export default class ChangeUrlModal extends React.Component { serverError =

{this.props.serverError}

; } - const fullTeamUrl = Utils.getTeamURLFromAddressBar(); - const teamURL = Utils.getShortenedTeamURL(); + const fullTeamUrl = TeamStore.getCurrentTeamUrl(); + const teamURL = Utils.getShortenedTeamURL(TeamStore.getCurrentTeamUrl()); const urlTooltip = ( {fullTeamUrl} ); diff --git a/webapp/components/channel_header.jsx b/webapp/components/channel_header.jsx index 573949a5e..f26105c7a 100644 --- a/webapp/components/channel_header.jsx +++ b/webapp/components/channel_header.jsx @@ -123,7 +123,7 @@ export default class ChannelHeader extends React.Component { }); const townsquare = ChannelStore.getByName('town-square'); - browserHistory.push(Utils.getTeamURLNoOriginFromAddressBar() + '/channels/' + townsquare.name); + browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + '/channels/' + townsquare.name); }, (err) => { AsyncClient.dispatchError(err, 'handleLeave'); diff --git a/webapp/components/channel_info_modal.jsx b/webapp/components/channel_info_modal.jsx index b0e2c63fa..7e0ff3873 100644 --- a/webapp/components/channel_info_modal.jsx +++ b/webapp/components/channel_info_modal.jsx @@ -5,6 +5,7 @@ import * as Utils from 'utils/utils.jsx'; import {FormattedMessage} from 'react-intl'; import {Modal} from 'react-bootstrap'; +import TeamStore from 'stores/team_store.jsx'; import * as TextFormatting from 'utils/text_formatting.jsx'; import React from 'react'; @@ -44,7 +45,7 @@ export default class ChannelInfoModal extends React.Component { channelIcon = (); } - const channelURL = Utils.getTeamURLFromAddressBar() + '/channels/' + channel.name; + const channelURL = TeamStore.getCurrentTeamUrl() + '/channels/' + channel.name; let channelPurpose = null; if (channel.purpose) { diff --git a/webapp/components/create_team/components/team_url.jsx b/webapp/components/create_team/components/team_url.jsx index 0fe047ccd..bcbe0a1a1 100644 --- a/webapp/components/create_team/components/team_url.jsx +++ b/webapp/components/create_team/components/team_url.jsx @@ -105,7 +105,7 @@ export default class TeamUrl extends React.Component { nameDivClass += ' has-error'; } - const title = `${Utils.getWindowLocationOrigin()}/`; + const title = `${window.mm_config.SiteURL}/`; const urlTooltip = ( {title} ); diff --git a/webapp/components/file_attachment.jsx b/webapp/components/file_attachment.jsx index f9c361afc..cba9d8288 100644 --- a/webapp/components/file_attachment.jsx +++ b/webapp/components/file_attachment.jsx @@ -102,7 +102,7 @@ class FileAttachment extends React.Component { getFileInfoFromName(name) { var fileInfo = utils.splitFileLocation(name); - fileInfo.path = utils.getWindowLocationOrigin() + Client.getFilesRoute() + '/get' + fileInfo.path; + fileInfo.path = Client.getFilesRoute() + '/get' + fileInfo.path; return fileInfo; } diff --git a/webapp/components/integrations/components/installed_incoming_webhook.jsx b/webapp/components/integrations/components/installed_incoming_webhook.jsx index 2cf3f24b8..008000012 100644 --- a/webapp/components/integrations/components/installed_incoming_webhook.jsx +++ b/webapp/components/integrations/components/installed_incoming_webhook.jsx @@ -97,7 +97,7 @@ export default class InstalledIncomingWebhook extends React.Component { id='installed_integrations.url' defaultMessage='URL: {url}' values={{ - url: Utils.getWindowLocationOrigin() + '/hooks/' + incomingWebhook.id + url: window.mm_config.SiteURL + '/hooks/' + incomingWebhook.id }} /> diff --git a/webapp/components/login/login_controller.jsx b/webapp/components/login/login_controller.jsx index 52f36bb2a..69981cfd6 100644 --- a/webapp/components/login/login_controller.jsx +++ b/webapp/components/login/login_controller.jsx @@ -277,7 +277,7 @@ export default class LoginController extends React.Component { } createLoginOptions() { - const extraParam = Utils.getUrlParameter('extra'); + const extraParam = this.props.location.query.extra; let extraBox = ''; if (extraParam) { if (extraParam === Constants.SIGNIN_CHANGE) { diff --git a/webapp/components/more_channels.jsx b/webapp/components/more_channels.jsx index b7ffff712..724cd2f60 100644 --- a/webapp/components/more_channels.jsx +++ b/webapp/components/more_channels.jsx @@ -74,7 +74,7 @@ export default class MoreChannels extends React.Component { channel, () => { $(ReactDOM.findDOMNode(this.refs.modal)).modal('hide'); - browserHistory.push(Utils.getTeamURLNoOriginFromAddressBar() + '/channels/' + channel.name); + browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + '/channels/' + channel.name); this.setState({joiningChannel: ''}); }, (err) => { diff --git a/webapp/components/more_direct_channels.jsx b/webapp/components/more_direct_channels.jsx index c74df5d1d..24718387e 100644 --- a/webapp/components/more_direct_channels.jsx +++ b/webapp/components/more_direct_channels.jsx @@ -87,7 +87,7 @@ export default class MoreDirectChannels extends React.Component { Utils.openDirectChannelToUser( teammate, (channel) => { - browserHistory.push(Utils.getTeamURLNoOriginFromAddressBar() + '/channels/' + channel.name); + browserHistory.push(TeamStore.getCurrentTeamUrl() + '/channels/' + channel.name); this.setState({loadingDMChannel: -1}); this.handleHide(); }, diff --git a/webapp/components/navbar_dropdown.jsx b/webapp/components/navbar_dropdown.jsx index 81bd31269..f82bd564e 100644 --- a/webapp/components/navbar_dropdown.jsx +++ b/webapp/components/navbar_dropdown.jsx @@ -88,7 +88,7 @@ export default class NavbarDropdown extends React.Component { return (
  • - + - + { - browserHistory.push(Utils.getTeamURLNoOriginFromAddressBar() + '/channels/' + channel.name); + browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + '/channels/' + channel.name); if (channelAlreadyExisted) { this.closePopover(); } diff --git a/webapp/components/removed_from_channel_modal.jsx b/webapp/components/removed_from_channel_modal.jsx index 3bdceadf7..228132803 100644 --- a/webapp/components/removed_from_channel_modal.jsx +++ b/webapp/components/removed_from_channel_modal.jsx @@ -4,10 +4,10 @@ import $ from 'jquery'; import ReactDOM from 'react-dom'; import ChannelStore from 'stores/channel_store.jsx'; +import TeamStore from 'stores/team_store.jsx'; import UserStore from 'stores/user_store.jsx'; import BrowserStore from 'stores/browser_store.jsx'; -import * as Utils from 'utils/utils.jsx'; import {FormattedMessage} from 'react-intl'; import {browserHistory} from 'react-router/es6'; @@ -36,7 +36,7 @@ export default class RemovedFromChannelModal extends React.Component { var townSquare = ChannelStore.getByName('town-square'); setTimeout( () => { - browserHistory.push(Utils.getTeamURLNoOriginFromAddressBar() + '/channels/' + townSquare.name); + browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + '/channels/' + townSquare.name); }, 1); diff --git a/webapp/components/search_results_item.jsx b/webapp/components/search_results_item.jsx index 217cd5568..fb8b23a7f 100644 --- a/webapp/components/search_results_item.jsx +++ b/webapp/components/search_results_item.jsx @@ -4,6 +4,7 @@ import $ from 'jquery'; import UserProfile from './user_profile.jsx'; +import TeamStore from 'stores/team_store.jsx'; import UserStore from 'stores/user_store.jsx'; import * as GlobalActions from 'actions/global_actions.jsx'; @@ -151,7 +152,7 @@ export default class SearchResultsItem extends React.Component { this.hideSidebar(); } this.shrinkSidebar(); - browserHistory.push('/' + window.location.pathname.split('/')[1] + '/pl/' + post.id); + browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + '/pl/' + post.id); } } className='search-item__jump' diff --git a/webapp/routes/route_team.jsx b/webapp/routes/route_team.jsx index 27817710f..15217bfd2 100644 --- a/webapp/routes/route_team.jsx +++ b/webapp/routes/route_team.jsx @@ -12,7 +12,6 @@ import Constants from 'utils/constants.jsx'; const ActionTypes = Constants.ActionTypes; import * as AsyncClient from 'utils/async_client.jsx'; import Client from 'client/web_client.jsx'; -import * as Utils from 'utils/utils.jsx'; import ChannelStore from 'stores/channel_store.jsx'; import emojiRoute from 'routes/route_emoji.jsx'; @@ -57,7 +56,7 @@ function doChannelChange(state, replace, callback) { function preNeedsTeam(nextState, replace, callback) { // First check to make sure you're in the current team // for the current url. - var teamName = Utils.getTeamNameFromUrl(); + const teamName = nextState.params.team; var team = TeamStore.getByName(teamName); const oldTeamId = TeamStore.getCurrentId(); diff --git a/webapp/stores/team_store.jsx b/webapp/stores/team_store.jsx index f4383589a..a482fa3a1 100644 --- a/webapp/stores/team_store.jsx +++ b/webapp/stores/team_store.jsx @@ -11,12 +11,6 @@ const ActionTypes = Constants.ActionTypes; const CHANGE_EVENT = 'change'; var Utils; -function getWindowLocationOrigin() { - if (!Utils) { - Utils = require('utils/utils.jsx'); //eslint-disable-line global-require - } - return Utils.getWindowLocationOrigin(); -} class TeamStoreClass extends EventEmitter { constructor() { @@ -87,23 +81,23 @@ class TeamStoreClass extends EventEmitter { getCurrentTeamUrl() { if (this.getCurrent()) { - return getWindowLocationOrigin() + '/' + this.getCurrent().name; + return window.mm_config.SiteURL + '/' + this.getCurrent().name; } - return null; + return ''; } getCurrentTeamRelativeUrl() { if (this.getCurrent()) { return '/' + this.getCurrent().name; } - return null; + return ''; } getCurrentInviteLink() { const current = this.getCurrent(); if (current) { - return getWindowLocationOrigin() + '/signup_user_complete/?id=' + current.invite_id; + return window.mm_config.SiteURL + '/signup_user_complete/?id=' + current.invite_id; } return ''; @@ -112,10 +106,10 @@ class TeamStoreClass extends EventEmitter { getTeamUrl(id) { const team = this.get(id); if (team) { - return getWindowLocationOrigin() + '/' + team.name; + return window.mm_config.SiteURL + '/' + team.name; } - return null; + return ''; } saveTeam(team) { diff --git a/webapp/utils/utils.jsx b/webapp/utils/utils.jsx index c4cee3235..4b3c8518c 100644 --- a/webapp/utils/utils.jsx +++ b/webapp/utils/utils.jsx @@ -99,20 +99,6 @@ export function isSystemAdmin(roles) { return false; } -export function getDomainWithOutSub() { - var parts = window.location.host.split('.'); - - if (parts.length === 1) { - if (parts[0].indexOf('dockerhost') > -1) { - return 'dockerhost:8065'; - } - - return 'localhost:8065'; - } - - return parts[1] + '.' + parts[2]; -} - export function getCookie(name) { var value = '; ' + document.cookie; var parts = value.split('; ' + name + '='); @@ -171,18 +157,6 @@ export function ding() { } } -export function getUrlParameter(sParam) { - var sPageURL = window.location.search.substring(1); - var sURLVariables = sPageURL.split('&'); - for (var i = 0; i < sURLVariables.length; i++) { - var sParameterName = sURLVariables[i].split('='); - if (sParameterName[0] === sParam) { - return sParameterName[1]; - } - } - return null; -} - export function getDateForUnixTicks(ticks) { return new Date(ticks); } @@ -1015,18 +989,6 @@ export function displayUsernameForUser(user) { return username; } -//IE10 does not set window.location.origin automatically so this must be called instead when using it -export function getWindowLocationOrigin() { - var windowLocationOrigin = window.location.origin; - if (!windowLocationOrigin) { - windowLocationOrigin = window.location.protocol + '//' + window.location.hostname; - if (window.location.port) { - windowLocationOrigin += ':' + window.location.port; - } - } - return windowLocationOrigin; -} - // Converts a file size in bytes into a human-readable string of the form '123MB'. export function fileSizeToString(bytes) { // it's unlikely that we'll have files bigger than this @@ -1045,7 +1007,7 @@ export function fileSizeToString(bytes) { // Converts a filename (like those attached to Post objects) to a url that can be used to retrieve attachments from the server. export function getFileUrl(filename) { - return getWindowLocationOrigin() + Client.getFilesRoute() + '/get' + filename; + return Client.getFilesRoute() + '/get' + filename; } // Gets the name of a file (including extension) from a given url or file path. @@ -1151,20 +1113,7 @@ export function importSlack(file, success, error) { Client.importSlack(formData, success, error); } -export function getTeamURLFromAddressBar() { - return window.location.origin + '/' + window.location.pathname.split('/')[1]; -} - -export function getTeamNameFromUrl() { - return window.location.pathname.split('/')[1]; -} - -export function getTeamURLNoOriginFromAddressBar() { - return '/' + window.location.pathname.split('/')[1]; -} - -export function getShortenedTeamURL() { - const teamURL = getTeamURLFromAddressBar(); +export function getShortenedTeamURL(teamURL = '') { if (teamURL.length > 35) { return teamURL.substring(0, 10) + '...' + teamURL.substring(teamURL.length - 12, teamURL.length) + '/'; } -- cgit v1.2.3-1-g7c22