From 878e5593ba7990b22cdc31f83e8a7e12e0cfbfd6 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 31 Jan 2017 11:13:05 +0000 Subject: [PATCH] Implement tracking of referrals (#659) * Implement tracking of referrals This also modifies (or fixes) auto-joining. --- src/RtsClient.js | 28 ++++++++++ src/components/structures/MatrixChat.js | 1 + .../structures/login/Registration.js | 54 ++++++++++++------- 3 files changed, 65 insertions(+), 18 deletions(-) diff --git a/src/RtsClient.js b/src/RtsClient.js index 25ed71b72b..0067d0ae10 100644 --- a/src/RtsClient.js +++ b/src/RtsClient.js @@ -12,4 +12,32 @@ export default class RtsClient { json: true, }); } + + /** + * Track a referral with the Riot Team Server. This should be called once a referred + * user has been successfully registered. + * @param {string} referrer the user ID of one who referred the user to Riot. + * @param {string} user_id the user ID of the user being referred. + * @param {string} user_email the email address linked to `user_id`. + * @returns {Promise} a promise that resolves to [$response, $body], where $response + * is the response object created by the request lib and $body is the object parsed + * from the JSON response body. $body should be { team_token: 'sometoken' } upon + * success. + */ + trackReferral(referrer, user_id, user_email) { + return request({ + url: this._url + '/register', + json: true, + body: {referrer, user_id, user_email}, + method: 'POST', + }); + } + + getTeam(team_token) { + return request({ + url: this._url + '/teamConfiguration', + json: true, + qs: {team_token}, + }); + } } diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index 989ae5aace..6a84fb940f 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -1055,6 +1055,7 @@ module.exports = React.createClass({ sessionId={this.state.register_session_id} idSid={this.state.register_id_sid} email={this.props.startingFragmentQueryParams.email} + referrer={this.props.startingFragmentQueryParams.referrer} username={this.state.upgradeUsername} guestAccessToken={this.state.guestAccessToken} defaultHsUrl={this.getDefaultHsUrl()} diff --git a/src/components/structures/login/Registration.js b/src/components/structures/login/Registration.js index 730f31c8ad..82cdbef9ca 100644 --- a/src/components/structures/login/Registration.js +++ b/src/components/structures/login/Registration.js @@ -48,6 +48,7 @@ module.exports = React.createClass({ defaultIsUrl: React.PropTypes.string, brand: React.PropTypes.string, email: React.PropTypes.string, + referrer: React.PropTypes.string, username: React.PropTypes.string, guestAccessToken: React.PropTypes.string, teamServerConfig: React.PropTypes.shape({ @@ -56,6 +57,7 @@ module.exports = React.createClass({ // URL of the riot-team-server to get team configurations and track referrals teamServerURL: React.PropTypes.string.isRequired, }), + teamSelected: null, defaultDeviceDisplayName: React.PropTypes.string, @@ -209,24 +211,42 @@ module.exports = React.createClass({ accessToken: response.access_token }); - // Auto-join rooms - if (self.state.teamsConfig && self.state.teamsConfig.teams) { - for (let i = 0; i < self.state.teamsConfig.teams.length; i++) { - let team = self.state.teamsConfig.teams[i]; - if (self.state.formVals.email.endsWith(team.domain)) { - console.log("User successfully registered with team " + team.name); + if ( + self._rtsClient && + self.props.referrer && + self.state.teamSelected + ) { + // Track referral, get team_token in order to retrieve team config + self._rtsClient.trackReferral( + self.props.referrer, + response.user_id, + self.state.formVals.email + ).then((args) => { + const teamToken = args[1].team_token; + // Store for use /w welcome pages + window.localStorage.setItem('mx_team_token', teamToken); + + self._rtsClient.getTeam(teamToken).then((args) => { + const team = args[1]; + console.log( + `User successfully registered with team ${team.name}` + ); if (!team.rooms) { - break; + return; } + // Auto-join rooms team.rooms.forEach((room) => { - if (room.autoJoin) { - console.log("Auto-joining " + room.id); - MatrixClientPeg.get().joinRoom(room.id); + if (room.auto_join && room.room_id) { + console.log(`Auto-joining ${room.room_id}`); + MatrixClientPeg.get().joinRoom(room.room_id); } }); - break; - } - } + }, (err) => { + console.error('Error getting team config', err); + }); + }, (err) => { + console.error('Error tracking referral', err); + }); } if (self.props.brand) { @@ -298,11 +318,9 @@ module.exports = React.createClass({ }); }, - onTeamSelected: function(team) { + onTeamSelected: function(teamSelected) { if (!this._unmounted) { - this.setState({ - teamIcon: team ? team.icon : null, - }); + this.setState({ teamSelected }); } }, @@ -407,7 +425,7 @@ module.exports = React.createClass({ return (
- + {this._getRegisterContentJsx()}