Pull out welcome user chat code to a separate function
also expand on comment
This commit is contained in:
parent
929e9dc653
commit
30726d6cf9
1 changed files with 59 additions and 52 deletions
|
@ -1133,68 +1133,75 @@ export default React.createClass({
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts a chat with the welcome user, if the user doesn't already have one
|
||||||
|
* @returns {string} The room ID of the new room, or null if no room was created
|
||||||
|
*/
|
||||||
|
async _startWelcomeUserChat() {
|
||||||
|
// We can end up with multiple tabs post-registration where the user
|
||||||
|
// might then end up with a session and we don't want them all making
|
||||||
|
// a chat with the welcome user: try to de-dupe.
|
||||||
|
// We need to wait for the first sync to complete for this to
|
||||||
|
// work though.
|
||||||
|
let waitFor;
|
||||||
|
if (!this.firstSyncComplete) {
|
||||||
|
waitFor = this.firstSyncPromise.promise;
|
||||||
|
} else {
|
||||||
|
waitFor = Promise.resolve();
|
||||||
|
}
|
||||||
|
await waitFor;
|
||||||
|
|
||||||
|
const welcomeUserRooms = DMRoomMap.shared().getDMRoomsForUserId(
|
||||||
|
this.props.config.welcomeUserId,
|
||||||
|
);
|
||||||
|
if (welcomeUserRooms.length === 0) {
|
||||||
|
const roomId = await createRoom({
|
||||||
|
dmUserId: this.props.config.welcomeUserId,
|
||||||
|
// Only view the welcome user if we're NOT looking at a room
|
||||||
|
andView: !this.state.currentRoomId,
|
||||||
|
});
|
||||||
|
// This is a bit of a hack, but since the deduplication relies
|
||||||
|
// on m.direct being up to date, we need to force a sync
|
||||||
|
// of the database, otherwise if the user goes to the other
|
||||||
|
// tab before the next save happens (a few minutes), the
|
||||||
|
// saved sync will be restored from the db and this code will
|
||||||
|
// run without the update to m.direct, making another welcome
|
||||||
|
// user room (it doesn't wait for new data from the server, just
|
||||||
|
// the saved sync to be loaded).
|
||||||
|
const saveWelcomeUser = (ev) => {
|
||||||
|
if (
|
||||||
|
ev.getType() == 'm.direct' &&
|
||||||
|
ev.getContent() &&
|
||||||
|
ev.getContent()[this.props.config.welcomeUserId]
|
||||||
|
) {
|
||||||
|
MatrixClientPeg.get().store.save(true);
|
||||||
|
MatrixClientPeg.get().removeListener(
|
||||||
|
"accountData", saveWelcomeUser,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
MatrixClientPeg.get().on("accountData", saveWelcomeUser);
|
||||||
|
|
||||||
|
return roomId;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when a new logged in session has started
|
* Called when a new logged in session has started
|
||||||
*/
|
*/
|
||||||
_onLoggedIn: function() {
|
_onLoggedIn: async function() {
|
||||||
this.setStateForNewView({ view: VIEWS.LOGGED_IN });
|
this.setStateForNewView({ view: VIEWS.LOGGED_IN });
|
||||||
if (MatrixClientPeg.currentUserIsJustRegistered()) {
|
if (true || MatrixClientPeg.currentUserIsJustRegistered()) {
|
||||||
MatrixClientPeg.setJustRegisteredUserId(null);
|
MatrixClientPeg.setJustRegisteredUserId(null);
|
||||||
|
|
||||||
if (this.props.config.welcomeUserId && getCurrentLanguage().startsWith("en")) {
|
if (this.props.config.welcomeUserId && getCurrentLanguage().startsWith("en")) {
|
||||||
// We can end up with multiple tabs post-registration where the user
|
const welcomeUserRoom = await this._startWelcomeUserChat();
|
||||||
// might then end up with a session and we don't want them all making
|
if (welcomeUserRoom === null) {
|
||||||
// a chat with the welcome user: try to de-dupe.
|
|
||||||
// We need to wait for the first sync to complete for this to
|
|
||||||
// work though.
|
|
||||||
let waitFor;
|
|
||||||
if (!this.firstSyncComplete) {
|
|
||||||
waitFor = this.firstSyncPromise.promise;
|
|
||||||
} else {
|
|
||||||
waitFor = Promise.resolve();
|
|
||||||
}
|
|
||||||
waitFor.then(async () => {
|
|
||||||
const welcomeUserRooms = DMRoomMap.shared().getDMRoomsForUserId(
|
|
||||||
this.props.config.welcomeUserId,
|
|
||||||
);
|
|
||||||
if (welcomeUserRooms.length === 0) {
|
|
||||||
const roomId = await createRoom({
|
|
||||||
dmUserId: this.props.config.welcomeUserId,
|
|
||||||
// Only view the welcome user if we're NOT looking at a room
|
|
||||||
andView: !this.state.currentRoomId,
|
|
||||||
});
|
|
||||||
// This is a bit of a hack, but since the deduplication relies
|
|
||||||
// on m.direct being up to date, we need to force a sync
|
|
||||||
// of the database, otherwise if the user goes to the other
|
|
||||||
// tab before the next save happens (a few minutes), the
|
|
||||||
// saved sync will be restored from the db and this code will
|
|
||||||
// run without the update to m.direct, making another welcome
|
|
||||||
// user room.
|
|
||||||
const saveWelcomeUser = (ev) => {
|
|
||||||
if (
|
|
||||||
ev.getType() == 'm.direct' &&
|
|
||||||
ev.getContent() &&
|
|
||||||
ev.getContent()[this.props.config.welcomeUserId]
|
|
||||||
) {
|
|
||||||
MatrixClientPeg.get().store.save(true);
|
|
||||||
MatrixClientPeg.get().removeListener(
|
|
||||||
"accountData", saveWelcomeUser,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
MatrixClientPeg.get().on("accountData", saveWelcomeUser);
|
|
||||||
|
|
||||||
// if successful, return because we're already
|
|
||||||
// viewing the welcomeUserId room
|
|
||||||
// else, if failed, fall through to view_home_page
|
|
||||||
if (roomId) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// We didn't rediret to the welcome user room, so show
|
// We didn't rediret to the welcome user room, so show
|
||||||
// the homepage.
|
// the homepage.
|
||||||
dis.dispatch({action: 'view_home_page'});
|
dis.dispatch({action: 'view_home_page'});
|
||||||
});
|
}
|
||||||
} else {
|
} else {
|
||||||
// The user has just logged in after registering,
|
// The user has just logged in after registering,
|
||||||
// so show the homepage.
|
// so show the homepage.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue