Skip to content

Commit

Permalink
PLT-3408 Add SiteURL to config.json (mattermost#3692)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hmhealey committed Aug 2, 2016
1 parent 9a18f2a commit 0955fa3
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 89 deletions.
22 changes: 18 additions & 4 deletions actions/websocket_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,26 @@ const MAX_WEBSOCKET_FAILS = 7;

export function initialize() {
if (window.WebSocket) {
let protocol = 'ws:https://';
if (window.location.protocol === 'https:') {
protocol = 'wss:https://';
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);
Expand Down
5 changes: 3 additions & 2 deletions components/change_url_modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -130,8 +131,8 @@ export default class ChangeUrlModal extends React.Component {
serverError = <div className='form-group has-error'><p className='input__help error'>{this.props.serverError}</p></div>;
}

const fullTeamUrl = Utils.getTeamURLFromAddressBar();
const teamURL = Utils.getShortenedTeamURL();
const fullTeamUrl = TeamStore.getCurrentTeamUrl();
const teamURL = Utils.getShortenedTeamURL(TeamStore.getCurrentTeamUrl());
const urlTooltip = (
<Tooltip id='urlTooltip'>{fullTeamUrl}</Tooltip>
);
Expand Down
2 changes: 1 addition & 1 deletion components/channel_header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion components/channel_info_modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -44,7 +45,7 @@ export default class ChannelInfoModal extends React.Component {
channelIcon = (<span className='fa fa-lock'/>);
}

const channelURL = Utils.getTeamURLFromAddressBar() + '/channels/' + channel.name;
const channelURL = TeamStore.getCurrentTeamUrl() + '/channels/' + channel.name;

let channelPurpose = null;
if (channel.purpose) {
Expand Down
2 changes: 1 addition & 1 deletion components/create_team/components/team_url.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
<Tooltip id='urlTooltip'>{title}</Tooltip>
);
Expand Down
2 changes: 1 addition & 1 deletion components/file_attachment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}}
/>
</span>
Expand Down
2 changes: 1 addition & 1 deletion components/login/login_controller.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion components/more_channels.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion components/more_direct_channels.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
Expand Down
4 changes: 2 additions & 2 deletions components/navbar_dropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default class NavbarDropdown extends React.Component {

return (
<li>
<Link to={'/' + Utils.getTeamNameFromUrl() + '/emoji'}>
<Link to={'/' + this.props.teamName + '/emoji'}>
<FormattedMessage
id='navbar_dropdown.emoji'
defaultMessage='Custom Emoji'
Expand Down Expand Up @@ -207,7 +207,7 @@ export default class NavbarDropdown extends React.Component {
if (integrationsEnabled && (isAdmin || window.mm_config.EnableOnlyAdminIntegrations !== 'true')) {
integrationsLink = (
<li>
<Link to={'/' + Utils.getTeamNameFromUrl() + '/integrations'}>
<Link to={'/' + this.props.teamName + '/integrations'}>
<FormattedMessage
id='navbar_dropdown.integrations'
defaultMessage='Integrations'
Expand Down
2 changes: 1 addition & 1 deletion components/needs_team.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class NeedsTeam extends React.Component {
// Go to tutorial if we are first arriving
const tutorialStep = PreferenceStore.getInt(Preferences.TUTORIAL_STEP, UserStore.getCurrentId(), 999);
if (tutorialStep <= TutorialSteps.INTRO_SCREENS) {
browserHistory.push(Utils.getTeamURLNoOriginFromAddressBar() + '/tutorial');
browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + '/tutorial');
}
}

Expand Down
3 changes: 2 additions & 1 deletion components/new_channel_flow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import * as Utils from 'utils/utils.jsx';
import Client from 'client/web_client.jsx';
import TeamStore from 'stores/team_store.jsx';
import UserStore from 'stores/user_store.jsx';

import NewChannelModal from './new_channel_modal.jsx';
Expand Down Expand Up @@ -117,7 +118,7 @@ class NewChannelFlow extends React.Component {
});

this.props.onModalDismissed();
browserHistory.push(Utils.getTeamURLNoOriginFromAddressBar() + '/channels/' + data2.channel.name);
browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + '/channels/' + data2.channel.name);
}
);
},
Expand Down
3 changes: 2 additions & 1 deletion components/popover_list_members.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import $ from 'jquery';

import TeamStore from 'stores/team_store.jsx';
import UserStore from 'stores/user_store.jsx';
import {Popover, Overlay} from 'react-bootstrap';
import * as Utils from 'utils/utils.jsx';
Expand Down Expand Up @@ -36,7 +37,7 @@ export default class PopoverListMembers extends React.Component {
Utils.openDirectChannelToUser(
teammate,
(channel, channelAlreadyExisted) => {
browserHistory.push(Utils.getTeamURLNoOriginFromAddressBar() + '/channels/' + channel.name);
browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + '/channels/' + channel.name);
if (channelAlreadyExisted) {
this.closePopover();
}
Expand Down
4 changes: 2 additions & 2 deletions components/removed_from_channel_modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion components/search_results_item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'
Expand Down
3 changes: 1 addition & 2 deletions routes/route_team.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();

Expand Down
18 changes: 6 additions & 12 deletions stores/team_store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 '';
Expand All @@ -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) {
Expand Down
55 changes: 2 additions & 53 deletions utils/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '=');
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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) + '/';
}
Expand Down

0 comments on commit 0955fa3

Please sign in to comment.