2015-06-23 15:41:25 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-07-02 12:18:03 +00:00
|
|
|
var MatrixClientPeg = require("../../MatrixClientPeg");
|
2015-07-08 17:18:03 +00:00
|
|
|
var RoomListSorter = require("../../RoomListSorter");
|
2015-09-18 17:39:16 +00:00
|
|
|
var Presence = require("../../Presence");
|
2015-06-19 11:53:48 +00:00
|
|
|
var dis = require("../../dispatcher");
|
2015-09-18 17:39:16 +00:00
|
|
|
var q = require("q");
|
2015-06-09 16:40:42 +00:00
|
|
|
|
2015-09-15 12:34:36 +00:00
|
|
|
var sdk = require('../../index');
|
2015-09-18 17:39:16 +00:00
|
|
|
var MatrixTools = require('../../MatrixTools');
|
2015-07-03 10:12:54 +00:00
|
|
|
|
2015-06-19 11:53:48 +00:00
|
|
|
module.exports = {
|
2015-09-18 17:39:16 +00:00
|
|
|
PageTypes: {
|
|
|
|
RoomView: "room_view",
|
|
|
|
UserSettings: "user_settings",
|
|
|
|
CreateRoom: "create_room",
|
|
|
|
RoomDirectory: "room_directory",
|
|
|
|
},
|
|
|
|
|
|
|
|
AuxPanel: {
|
|
|
|
RoomSettings: "room_settings",
|
|
|
|
},
|
|
|
|
|
2015-06-11 17:25:29 +00:00
|
|
|
getInitialState: function() {
|
2015-09-18 17:39:16 +00:00
|
|
|
var s = {
|
2015-07-03 10:12:54 +00:00
|
|
|
logged_in: !!(MatrixClientPeg.get() && MatrixClientPeg.get().credentials),
|
2015-09-18 17:39:16 +00:00
|
|
|
ready: false,
|
2015-06-11 17:25:29 +00:00
|
|
|
};
|
2015-09-18 17:39:16 +00:00
|
|
|
if (s.logged_in) {
|
|
|
|
if (MatrixClientPeg.get().getRooms().length) {
|
|
|
|
s.page_type = this.PageTypes.RoomView;
|
|
|
|
} else {
|
|
|
|
s.page_type = this.PageTypes.RoomDirectory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
2015-06-11 17:25:29 +00:00
|
|
|
},
|
|
|
|
|
2015-06-12 13:59:33 +00:00
|
|
|
componentDidMount: function() {
|
2015-06-12 16:34:17 +00:00
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
2015-06-12 13:59:33 +00:00
|
|
|
if (this.state.logged_in) {
|
|
|
|
this.startMatrixClient();
|
|
|
|
}
|
2015-06-18 14:03:57 +00:00
|
|
|
this.focusComposer = false;
|
2015-06-25 16:41:55 +00:00
|
|
|
document.addEventListener("keydown", this.onKeyDown);
|
2015-07-07 12:30:38 +00:00
|
|
|
window.addEventListener("focus", this.onFocus);
|
2015-07-15 18:25:36 +00:00
|
|
|
if (this.state.logged_in) {
|
|
|
|
this.notifyNewScreen('');
|
|
|
|
} else {
|
|
|
|
this.notifyNewScreen('login');
|
|
|
|
}
|
2015-06-12 13:59:33 +00:00
|
|
|
},
|
|
|
|
|
2015-06-12 16:34:17 +00:00
|
|
|
componentWillUnmount: function() {
|
|
|
|
dis.unregister(this.dispatcherRef);
|
2015-06-25 16:41:55 +00:00
|
|
|
document.removeEventListener("keydown", this.onKeyDown);
|
2015-09-18 17:39:16 +00:00
|
|
|
window.removeEventListener("focus", this.onFocus);
|
2015-06-12 16:34:17 +00:00
|
|
|
},
|
|
|
|
|
2015-06-18 14:03:57 +00:00
|
|
|
componentDidUpdate: function() {
|
|
|
|
if (this.focusComposer) {
|
|
|
|
dis.dispatch({action: 'focus_composer'});
|
|
|
|
this.focusComposer = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-12 16:34:17 +00:00
|
|
|
onAction: function(payload) {
|
2015-06-25 16:41:55 +00:00
|
|
|
var roomIndexDelta = 1;
|
2015-09-15 12:34:36 +00:00
|
|
|
var Notifier = sdk.getComponent('organisms.Notifier');
|
2015-06-25 16:41:55 +00:00
|
|
|
|
2015-10-05 14:31:08 +00:00
|
|
|
var self = this;
|
2015-06-12 16:34:17 +00:00
|
|
|
switch (payload.action) {
|
|
|
|
case 'logout':
|
2015-07-16 15:12:54 +00:00
|
|
|
if (window.localStorage) {
|
|
|
|
window.localStorage.clear();
|
|
|
|
}
|
2015-07-03 10:12:54 +00:00
|
|
|
Notifier.stop();
|
2015-09-18 17:39:16 +00:00
|
|
|
Presence.stop();
|
|
|
|
MatrixClientPeg.get().stopClient();
|
2015-07-02 12:18:03 +00:00
|
|
|
MatrixClientPeg.get().removeAllListeners();
|
2015-09-18 17:39:16 +00:00
|
|
|
MatrixClientPeg.unset();
|
2015-10-05 14:31:08 +00:00
|
|
|
this.notifyNewScreen('login');
|
2015-10-09 12:48:17 +00:00
|
|
|
this.replaceState({
|
|
|
|
logged_in: false,
|
|
|
|
ready: false
|
|
|
|
});
|
2015-06-12 16:34:17 +00:00
|
|
|
break;
|
2015-07-13 18:14:02 +00:00
|
|
|
case 'start_registration':
|
|
|
|
if (this.state.logged_in) return;
|
2015-07-15 19:33:12 +00:00
|
|
|
var newState = payload.params || {};
|
|
|
|
newState.screen = 'register';
|
|
|
|
if (
|
|
|
|
payload.params &&
|
|
|
|
payload.params.client_secret &&
|
|
|
|
payload.params.session_id &&
|
|
|
|
payload.params.hs_url &&
|
|
|
|
payload.params.is_url &&
|
|
|
|
payload.params.sid
|
|
|
|
) {
|
|
|
|
newState.register_client_secret = payload.params.client_secret;
|
|
|
|
newState.register_session_id = payload.params.session_id;
|
|
|
|
newState.register_hs_url = payload.params.hs_url;
|
|
|
|
newState.register_is_url = payload.params.is_url;
|
|
|
|
newState.register_id_sid = payload.params.sid;
|
|
|
|
}
|
|
|
|
this.replaceState(newState);
|
2015-07-15 18:25:36 +00:00
|
|
|
this.notifyNewScreen('register');
|
2015-07-13 18:14:02 +00:00
|
|
|
break;
|
|
|
|
case 'start_login':
|
|
|
|
if (this.state.logged_in) return;
|
2015-07-15 18:25:36 +00:00
|
|
|
this.replaceState({
|
2015-07-13 18:14:02 +00:00
|
|
|
screen: 'login'
|
|
|
|
});
|
2015-07-15 18:25:36 +00:00
|
|
|
this.notifyNewScreen('login');
|
2015-07-13 18:14:02 +00:00
|
|
|
break;
|
2015-06-12 17:01:38 +00:00
|
|
|
case 'view_room':
|
2015-07-03 10:12:54 +00:00
|
|
|
this.focusComposer = true;
|
2015-10-05 14:31:08 +00:00
|
|
|
var newState = {
|
2015-09-18 17:39:16 +00:00
|
|
|
currentRoom: payload.room_id,
|
|
|
|
page_type: this.PageTypes.RoomView,
|
2015-10-05 14:31:08 +00:00
|
|
|
};
|
2015-09-18 17:39:16 +00:00
|
|
|
if (this.sdkReady) {
|
|
|
|
// if the SDK is not ready yet, remember what room
|
|
|
|
// we're supposed to be on but don't notify about
|
|
|
|
// the new screen yet (we won't be showing it yet)
|
|
|
|
// The normal case where this happens is navigating
|
|
|
|
// to the room in the URL bar on page load.
|
|
|
|
var presentedId = payload.room_id;
|
|
|
|
var room = MatrixClientPeg.get().getRoom(payload.room_id);
|
|
|
|
if (room) {
|
|
|
|
var theAlias = MatrixTools.getCanonicalAliasForRoom(room);
|
|
|
|
if (theAlias) presentedId = theAlias;
|
|
|
|
}
|
|
|
|
this.notifyNewScreen('room/'+presentedId);
|
2015-10-09 11:05:40 +00:00
|
|
|
newState.ready = true;
|
2015-09-18 17:39:16 +00:00
|
|
|
}
|
2015-10-05 14:31:08 +00:00
|
|
|
this.setState(newState);
|
2015-06-12 17:01:38 +00:00
|
|
|
break;
|
2015-06-25 16:41:55 +00:00
|
|
|
case 'view_prev_room':
|
|
|
|
roomIndexDelta = -1;
|
|
|
|
case 'view_next_room':
|
2015-07-08 18:47:58 +00:00
|
|
|
var allRooms = RoomListSorter.mostRecentActivityFirst(
|
|
|
|
MatrixClientPeg.get().getRooms()
|
|
|
|
);
|
2015-06-25 16:41:55 +00:00
|
|
|
var roomIndex = -1;
|
|
|
|
for (var i = 0; i < allRooms.length; ++i) {
|
|
|
|
if (allRooms[i].roomId == this.state.currentRoom) {
|
|
|
|
roomIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
roomIndex = (roomIndex + roomIndexDelta) % allRooms.length;
|
2015-09-18 17:39:16 +00:00
|
|
|
if (roomIndex < 0) roomIndex = allRooms.length - 1;
|
|
|
|
this.focusComposer = true;
|
2015-06-25 16:41:55 +00:00
|
|
|
this.setState({
|
|
|
|
currentRoom: allRooms[roomIndex].roomId
|
|
|
|
});
|
2015-09-18 17:39:16 +00:00
|
|
|
this.notifyNewScreen('room/'+allRooms[roomIndex].roomId);
|
|
|
|
break;
|
|
|
|
case 'view_indexed_room':
|
|
|
|
var allRooms = RoomListSorter.mostRecentActivityFirst(
|
|
|
|
MatrixClientPeg.get().getRooms()
|
|
|
|
);
|
|
|
|
var roomIndex = payload.roomIndex;
|
|
|
|
if (allRooms[roomIndex]) {
|
|
|
|
this.focusComposer = true;
|
|
|
|
this.setState({
|
|
|
|
currentRoom: allRooms[roomIndex].roomId
|
|
|
|
});
|
|
|
|
this.notifyNewScreen('room/'+allRooms[roomIndex].roomId);
|
|
|
|
}
|
|
|
|
break;
|
2015-10-05 14:31:08 +00:00
|
|
|
case 'view_room_alias':
|
|
|
|
MatrixClientPeg.get().getRoomIdForAlias(payload.room_alias).done(function(result) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: result.room_id
|
|
|
|
});
|
|
|
|
});
|
|
|
|
break;
|
2015-09-18 17:39:16 +00:00
|
|
|
case 'view_user_settings':
|
|
|
|
this.setState({
|
|
|
|
page_type: this.PageTypes.UserSettings,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'view_create_room':
|
|
|
|
this.setState({
|
|
|
|
page_type: this.PageTypes.CreateRoom,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'view_room_directory':
|
|
|
|
this.setState({
|
|
|
|
page_type: this.PageTypes.RoomDirectory,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'notifier_enabled':
|
|
|
|
this.forceUpdate();
|
2015-06-25 16:41:55 +00:00
|
|
|
break;
|
2015-06-12 16:34:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-12 12:45:22 +00:00
|
|
|
onLoggedIn: function() {
|
2015-07-13 18:14:02 +00:00
|
|
|
this.setState({
|
2015-07-15 18:25:36 +00:00
|
|
|
screen: undefined,
|
2015-07-13 18:14:02 +00:00
|
|
|
logged_in: true
|
|
|
|
});
|
2015-06-12 13:59:33 +00:00
|
|
|
this.startMatrixClient();
|
2015-07-15 18:25:36 +00:00
|
|
|
this.notifyNewScreen('');
|
2015-06-12 13:59:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
startMatrixClient: function() {
|
2015-09-15 12:34:36 +00:00
|
|
|
var Notifier = sdk.getComponent('organisms.Notifier');
|
2015-07-02 12:18:03 +00:00
|
|
|
var cli = MatrixClientPeg.get();
|
2015-09-15 12:34:36 +00:00
|
|
|
var self = this;
|
2015-06-12 13:59:33 +00:00
|
|
|
cli.on('syncComplete', function() {
|
2015-09-18 17:39:16 +00:00
|
|
|
self.sdkReady = true;
|
2015-09-25 16:22:42 +00:00
|
|
|
|
|
|
|
if (self.starting_room_alias) {
|
2015-10-05 14:31:08 +00:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room_alias',
|
|
|
|
room_alias: self.starting_room_alias
|
2015-09-25 16:22:42 +00:00
|
|
|
});
|
2015-10-05 14:31:08 +00:00
|
|
|
delete self.starting_room_alias;
|
2015-09-18 17:39:16 +00:00
|
|
|
} else {
|
2015-09-25 16:22:42 +00:00
|
|
|
if (!self.state.currentRoom) {
|
|
|
|
var firstRoom = null;
|
|
|
|
if (cli.getRooms() && cli.getRooms().length) {
|
|
|
|
firstRoom = RoomListSorter.mostRecentActivityFirst(
|
|
|
|
cli.getRooms()
|
|
|
|
)[0].roomId;
|
|
|
|
self.setState({ready: true, currentRoom: firstRoom, page_type: self.PageTypes.RoomView});
|
|
|
|
} else {
|
|
|
|
self.setState({ready: true, page_type: self.PageTypes.RoomDirectory});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.setState({ready: true, page_type: self.PageTypes.RoomView});
|
|
|
|
}
|
|
|
|
|
|
|
|
// we notifyNewScreen now because now the room will actually be displayed,
|
|
|
|
// and (mostly) now we can get the correct alias.
|
|
|
|
var presentedId = self.state.currentRoom;
|
|
|
|
var room = MatrixClientPeg.get().getRoom(self.state.currentRoom);
|
|
|
|
if (room) {
|
|
|
|
var theAlias = MatrixTools.getCanonicalAliasForRoom(room);
|
|
|
|
if (theAlias) presentedId = theAlias;
|
|
|
|
}
|
|
|
|
self.notifyNewScreen('room/'+presentedId);
|
|
|
|
dis.dispatch({action: 'focus_composer'});
|
2015-10-05 14:31:08 +00:00
|
|
|
}
|
2015-06-12 13:59:33 +00:00
|
|
|
});
|
2015-09-18 17:39:16 +00:00
|
|
|
cli.on('Call.incoming', function(call) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'incoming_call',
|
|
|
|
call: call
|
|
|
|
});
|
|
|
|
});
|
2015-07-03 10:12:54 +00:00
|
|
|
Notifier.start();
|
2015-09-18 17:39:16 +00:00
|
|
|
Presence.start();
|
2015-06-12 13:59:33 +00:00
|
|
|
cli.startClient();
|
2015-06-11 17:25:29 +00:00
|
|
|
},
|
2015-06-25 16:41:55 +00:00
|
|
|
|
|
|
|
onKeyDown: function(ev) {
|
|
|
|
if (ev.altKey) {
|
2015-09-18 17:39:16 +00:00
|
|
|
if (ev.ctrlKey && ev.keyCode > 48 && ev.keyCode < 58) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_indexed_room',
|
|
|
|
roomIndex: ev.keyCode - 49,
|
|
|
|
});
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
2015-06-25 16:41:55 +00:00
|
|
|
switch (ev.keyCode) {
|
|
|
|
case 38:
|
|
|
|
dis.dispatch({action: 'view_prev_room'});
|
|
|
|
ev.stopPropagation();
|
2015-09-18 17:39:16 +00:00
|
|
|
ev.preventDefault();
|
2015-06-25 16:41:55 +00:00
|
|
|
break;
|
|
|
|
case 40:
|
|
|
|
dis.dispatch({action: 'view_next_room'});
|
|
|
|
ev.stopPropagation();
|
2015-09-18 17:39:16 +00:00
|
|
|
ev.preventDefault();
|
2015-06-25 16:41:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 12:30:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onFocus: function(ev) {
|
|
|
|
dis.dispatch({action: 'focus_composer'});
|
2015-07-15 18:25:36 +00:00
|
|
|
},
|
|
|
|
|
2015-07-16 11:44:04 +00:00
|
|
|
showScreen: function(screen, params) {
|
2015-07-15 19:33:12 +00:00
|
|
|
if (screen == 'register') {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'start_registration',
|
|
|
|
params: params
|
|
|
|
});
|
|
|
|
} else if (screen == 'login') {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'start_login',
|
|
|
|
params: params
|
|
|
|
});
|
2015-09-18 17:39:16 +00:00
|
|
|
} else if (screen.indexOf('room/') == 0) {
|
|
|
|
var roomString = screen.split('/')[1];
|
|
|
|
if (roomString[0] == '#') {
|
2015-10-05 14:31:08 +00:00
|
|
|
if (this.state.logged_in) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room_alias',
|
|
|
|
room_alias: roomString
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Okay, we'll take you here soon...
|
|
|
|
this.starting_room_alias = roomString;
|
|
|
|
// ...but you're still going to have to log in.
|
|
|
|
this.notifyNewScreen('login');
|
|
|
|
}
|
2015-09-18 17:39:16 +00:00
|
|
|
} else {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
2015-09-25 16:22:42 +00:00
|
|
|
room_id: roomString
|
2015-09-18 17:39:16 +00:00
|
|
|
});
|
2015-09-25 16:22:42 +00:00
|
|
|
}
|
2015-07-15 19:33:12 +00:00
|
|
|
}
|
2015-07-15 18:25:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
notifyNewScreen: function(screen) {
|
|
|
|
if (this.props.onNewScreen) {
|
|
|
|
this.props.onNewScreen(screen);
|
|
|
|
}
|
2015-06-25 16:41:55 +00:00
|
|
|
}
|
2015-06-19 11:53:48 +00:00
|
|
|
};
|