2017-05-24 15:56:13 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations 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.
|
|
|
|
*/
|
|
|
|
import dis from '../dispatcher';
|
|
|
|
import {Store} from 'flux/utils';
|
|
|
|
import MatrixClientPeg from '../MatrixClientPeg';
|
2017-06-02 10:53:10 +00:00
|
|
|
import sdk from '../index';
|
|
|
|
import Modal from '../Modal';
|
|
|
|
import { _t } from '../languageHandler';
|
2017-05-24 15:56:13 +00:00
|
|
|
|
|
|
|
const INITIAL_STATE = {
|
|
|
|
// Whether we're joining the currently viewed room
|
|
|
|
joining: false,
|
2017-06-08 15:00:12 +00:00
|
|
|
// Any error that has occurred during joining
|
2017-05-24 15:56:13 +00:00
|
|
|
joinError: null,
|
2017-06-08 15:00:12 +00:00
|
|
|
// The room ID of the room currently being viewed
|
2017-05-24 15:56:13 +00:00
|
|
|
roomId: null,
|
2017-06-08 14:47:41 +00:00
|
|
|
|
2017-06-08 16:27:04 +00:00
|
|
|
// The event to scroll to when the room is first viewed
|
2017-06-08 14:47:41 +00:00
|
|
|
initialEventId: null,
|
|
|
|
// The offset to display the initial event at (see scrollStateMap)
|
|
|
|
initialEventPixelOffset: null,
|
|
|
|
// Whether to highlight the initial event
|
|
|
|
isInitialEventHighlighted: false,
|
|
|
|
|
2017-05-24 15:56:13 +00:00
|
|
|
// The room alias of the room (or null if not originally specified in view_room)
|
|
|
|
roomAlias: null,
|
|
|
|
// Whether the current room is loading
|
|
|
|
roomLoading: false,
|
|
|
|
// Any error that has occurred during loading
|
|
|
|
roomLoadError: null,
|
2017-06-08 13:17:49 +00:00
|
|
|
// A map from room id to scroll state.
|
|
|
|
//
|
|
|
|
// If there is no special scroll state (ie, we are following the live
|
|
|
|
// timeline), the scroll state is null. Otherwise, it is an object with
|
|
|
|
// the following properties:
|
|
|
|
//
|
|
|
|
// focussedEvent: the ID of the 'focussed' event. Typically this is
|
|
|
|
// the last event fully visible in the viewport, though if we
|
|
|
|
// have done an explicit scroll to an explicit event, it will be
|
|
|
|
// that event.
|
|
|
|
//
|
|
|
|
// pixelOffset: the number of pixels the window is scrolled down
|
|
|
|
// from the focussedEvent.
|
|
|
|
scrollStateMap: {},
|
2017-06-16 15:12:52 +00:00
|
|
|
|
|
|
|
forwardingEvent: null,
|
2017-05-24 15:56:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class for storing application state for RoomView. This is the RoomView's interface
|
|
|
|
* with a subset of the js-sdk.
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
class RoomViewStore extends Store {
|
|
|
|
constructor() {
|
|
|
|
super(dis);
|
|
|
|
|
|
|
|
// Initialise state
|
|
|
|
this._state = INITIAL_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setState(newState) {
|
|
|
|
this._state = Object.assign(this._state, newState);
|
|
|
|
this.__emitChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
__onDispatch(payload) {
|
|
|
|
switch (payload.action) {
|
|
|
|
// view_room:
|
2017-06-08 13:17:49 +00:00
|
|
|
// - room_alias: '#somealias:matrix.org'
|
|
|
|
// - room_id: '!roomid123:matrix.org'
|
|
|
|
// - event_id: '$213456782:matrix.org'
|
|
|
|
// - event_offset: 100
|
|
|
|
// - highlighted: true
|
2017-05-24 15:56:13 +00:00
|
|
|
case 'view_room':
|
|
|
|
this._viewRoom(payload);
|
|
|
|
break;
|
2017-06-01 17:01:30 +00:00
|
|
|
case 'view_room_error':
|
|
|
|
this._viewRoomError(payload);
|
|
|
|
break;
|
2017-05-25 16:04:42 +00:00
|
|
|
case 'will_join':
|
|
|
|
this._setState({
|
|
|
|
joining: true,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'cancel_join':
|
|
|
|
this._setState({
|
|
|
|
joining: false,
|
|
|
|
});
|
|
|
|
break;
|
2017-05-24 15:56:13 +00:00
|
|
|
// join_room:
|
|
|
|
// - opts: options for joinRoom
|
|
|
|
case 'join_room':
|
|
|
|
this._joinRoom(payload);
|
|
|
|
break;
|
2017-06-02 10:53:10 +00:00
|
|
|
case 'joined_room':
|
|
|
|
this._joinedRoom(payload);
|
|
|
|
break;
|
|
|
|
case 'join_room_error':
|
|
|
|
this._joinRoomError(payload);
|
|
|
|
break;
|
2017-05-25 16:16:16 +00:00
|
|
|
case 'on_logged_out':
|
|
|
|
this.reset();
|
|
|
|
break;
|
2017-06-08 13:17:49 +00:00
|
|
|
case 'update_scroll_state':
|
|
|
|
this._updateScrollState(payload);
|
|
|
|
break;
|
2017-06-16 15:12:52 +00:00
|
|
|
case 'forward_event':
|
|
|
|
this._setState({
|
|
|
|
forwardingEvent: payload.event,
|
|
|
|
});
|
|
|
|
break;
|
2017-05-24 15:56:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_viewRoom(payload) {
|
2017-06-01 17:01:30 +00:00
|
|
|
if (payload.room_id) {
|
2017-06-08 14:54:23 +00:00
|
|
|
const newState = {
|
2017-06-01 17:01:30 +00:00
|
|
|
roomId: payload.room_id,
|
2017-06-14 11:05:25 +00:00
|
|
|
roomAlias: payload.room_alias,
|
2017-06-08 14:47:41 +00:00
|
|
|
initialEventId: payload.event_id,
|
2017-06-08 16:28:21 +00:00
|
|
|
initialEventPixelOffset: undefined,
|
2017-06-08 14:47:41 +00:00
|
|
|
isInitialEventHighlighted: payload.highlighted,
|
2017-06-16 15:12:52 +00:00
|
|
|
forwardingEvent: null,
|
2017-06-02 08:22:48 +00:00
|
|
|
roomLoading: false,
|
|
|
|
roomLoadError: null,
|
2017-06-16 17:24:07 +00:00
|
|
|
// should peek by default
|
|
|
|
shouldPeek: payload.should_peek === undefined ? true : payload.should_peek,
|
2017-06-08 14:54:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// If an event ID wasn't specified, default to the one saved for this room
|
|
|
|
// via update_scroll_state. Assume initialEventPixelOffset should be set.
|
|
|
|
if (!newState.initialEventId) {
|
|
|
|
const roomScrollState = this._state.scrollStateMap[payload.room_id];
|
|
|
|
if (roomScrollState) {
|
|
|
|
newState.initialEventId = roomScrollState.focussedEvent;
|
|
|
|
newState.initialEventPixelOffset = roomScrollState.pixelOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 15:12:52 +00:00
|
|
|
if (this._state.forwardingEvent) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'send_event',
|
|
|
|
room_id: newState.roomId,
|
|
|
|
event: this._state.forwardingEvent,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-08 14:54:23 +00:00
|
|
|
this._setState(newState);
|
2017-06-02 08:22:48 +00:00
|
|
|
} else if (payload.room_alias) {
|
2017-06-08 13:17:49 +00:00
|
|
|
// Resolve the alias and then do a second dispatch with the room ID acquired
|
2017-05-24 15:56:13 +00:00
|
|
|
this._setState({
|
2017-06-01 17:01:30 +00:00
|
|
|
roomId: null,
|
2017-06-08 14:47:41 +00:00
|
|
|
initialEventId: null,
|
|
|
|
initialEventPixelOffset: null,
|
|
|
|
isInitialEventHighlighted: null,
|
2017-06-01 17:01:30 +00:00
|
|
|
roomAlias: payload.room_alias,
|
2017-05-24 15:56:13 +00:00
|
|
|
roomLoading: true,
|
2017-06-01 17:01:30 +00:00
|
|
|
roomLoadError: null,
|
2017-05-24 15:56:13 +00:00
|
|
|
});
|
2017-06-01 17:01:30 +00:00
|
|
|
MatrixClientPeg.get().getRoomIdForAlias(payload.room_alias).done(
|
2017-05-24 15:56:13 +00:00
|
|
|
(result) => {
|
2017-06-01 17:01:30 +00:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: result.room_id,
|
2017-06-08 13:17:49 +00:00
|
|
|
event_id: payload.event_id,
|
|
|
|
highlighted: payload.highlighted,
|
2017-06-01 17:01:30 +00:00
|
|
|
room_alias: payload.room_alias,
|
2017-05-24 15:56:13 +00:00
|
|
|
});
|
|
|
|
}, (err) => {
|
2017-06-01 17:01:30 +00:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room_error',
|
|
|
|
room_id: null,
|
|
|
|
room_alias: payload.room_alias,
|
|
|
|
err: err,
|
2017-05-24 15:56:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 17:01:30 +00:00
|
|
|
_viewRoomError(payload) {
|
|
|
|
this._setState({
|
|
|
|
roomId: payload.room_id,
|
|
|
|
roomAlias: payload.room_alias,
|
|
|
|
roomLoading: false,
|
|
|
|
roomLoadError: payload.err,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-24 15:56:13 +00:00
|
|
|
_joinRoom(payload) {
|
|
|
|
this._setState({
|
|
|
|
joining: true,
|
|
|
|
});
|
2017-06-08 16:40:53 +00:00
|
|
|
MatrixClientPeg.get().joinRoom(
|
|
|
|
this._state.roomAlias || this._state.roomId, payload.opts,
|
|
|
|
).done(() => {
|
2017-06-02 10:53:10 +00:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'joined_room',
|
|
|
|
});
|
|
|
|
}, (err) => {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'join_room_error',
|
|
|
|
err: err,
|
|
|
|
});
|
|
|
|
const msg = err.message ? err.message : JSON.stringify(err);
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
|
|
|
title: _t("Failed to join room"),
|
|
|
|
description: msg,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_joinedRoom(payload) {
|
2017-06-02 12:41:41 +00:00
|
|
|
this._setState({
|
|
|
|
joining: false,
|
|
|
|
});
|
2017-06-02 10:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_joinRoomError(payload) {
|
2017-06-02 12:41:41 +00:00
|
|
|
this._setState({
|
|
|
|
joining: false,
|
|
|
|
joinError: payload.err,
|
|
|
|
});
|
2017-05-24 15:56:13 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 13:17:49 +00:00
|
|
|
_updateScrollState(payload) {
|
|
|
|
// Clobber existing scroll state for the given room ID
|
|
|
|
const newScrollStateMap = this._state.scrollStateMap;
|
|
|
|
newScrollStateMap[payload.room_id] = payload.scroll_state;
|
|
|
|
this._setState({
|
|
|
|
scrollStateMap: newScrollStateMap,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-24 15:56:13 +00:00
|
|
|
reset() {
|
|
|
|
this._state = Object.assign({}, INITIAL_STATE);
|
|
|
|
}
|
|
|
|
|
2017-06-08 15:00:12 +00:00
|
|
|
// The room ID of the room currently being viewed
|
2017-05-24 15:56:13 +00:00
|
|
|
getRoomId() {
|
|
|
|
return this._state.roomId;
|
|
|
|
}
|
|
|
|
|
2017-06-08 16:28:56 +00:00
|
|
|
// The event to scroll to when the room is first viewed
|
2017-06-08 14:47:41 +00:00
|
|
|
getInitialEventId() {
|
|
|
|
return this._state.initialEventId;
|
2017-06-08 13:17:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 15:00:12 +00:00
|
|
|
// The offset to display the initial event at (see scrollStateMap)
|
2017-06-08 14:47:41 +00:00
|
|
|
getInitialEventPixelOffset() {
|
|
|
|
return this._state.initialEventPixelOffset;
|
2017-06-08 13:17:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 15:00:12 +00:00
|
|
|
// Whether to highlight the initial event
|
2017-06-08 14:47:41 +00:00
|
|
|
isInitialEventHighlighted() {
|
|
|
|
return this._state.isInitialEventHighlighted;
|
2017-06-08 13:17:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 15:00:12 +00:00
|
|
|
// The room alias of the room (or null if not originally specified in view_room)
|
2017-05-24 15:56:13 +00:00
|
|
|
getRoomAlias() {
|
|
|
|
return this._state.roomAlias;
|
|
|
|
}
|
|
|
|
|
2017-06-08 15:00:12 +00:00
|
|
|
// Whether the current room is loading (true whilst resolving an alias)
|
2017-05-24 15:56:13 +00:00
|
|
|
isRoomLoading() {
|
|
|
|
return this._state.roomLoading;
|
|
|
|
}
|
|
|
|
|
2017-06-08 15:00:12 +00:00
|
|
|
// Any error that has occurred during loading
|
2017-06-01 17:01:30 +00:00
|
|
|
getRoomLoadError() {
|
|
|
|
return this._state.roomLoadError;
|
|
|
|
}
|
|
|
|
|
2017-06-08 15:00:12 +00:00
|
|
|
// Whether we're joining the currently viewed room
|
2017-05-24 15:56:13 +00:00
|
|
|
isJoining() {
|
|
|
|
return this._state.joining;
|
|
|
|
}
|
|
|
|
|
2017-06-08 15:00:12 +00:00
|
|
|
// Any error that has occurred during joining
|
2017-05-24 15:56:13 +00:00
|
|
|
getJoinError() {
|
|
|
|
return this._state.joinError;
|
|
|
|
}
|
2017-06-16 15:12:52 +00:00
|
|
|
|
|
|
|
// The mxEvent if one is about to be forwarded
|
|
|
|
getForwardingEvent() {
|
|
|
|
return this._state.forwardingEvent;
|
|
|
|
}
|
2017-06-19 00:53:35 +00:00
|
|
|
|
2017-06-16 17:24:07 +00:00
|
|
|
shouldPeek() {
|
|
|
|
return this._state.shouldPeek;
|
|
|
|
}
|
2017-05-24 15:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let singletonRoomViewStore = null;
|
|
|
|
if (!singletonRoomViewStore) {
|
|
|
|
singletonRoomViewStore = new RoomViewStore();
|
|
|
|
}
|
|
|
|
module.exports = singletonRoomViewStore;
|