element-web/src/stores/RoomViewStore.tsx

445 lines
16 KiB
TypeScript
Raw Normal View History

/*
Copyright 2017 Vector Creations Ltd
Copyright 2017, 2018 New Vector Ltd
Copyright 2019 The Matrix.org Foundation C.I.C.
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.
*/
2021-12-14 15:34:54 +00:00
import React, { ReactNode } from "react";
2021-05-24 13:34:06 +00:00
import { Store } from 'flux/utils';
import { MatrixError } from "matrix-js-sdk/src/http-api";
import { logger } from "matrix-js-sdk/src/logger";
import dis from '../dispatcher/dispatcher';
2021-05-24 13:34:06 +00:00
import { MatrixClientPeg } from '../MatrixClientPeg';
import * as sdk from '../index';
import Modal from '../Modal';
import { _t } from '../languageHandler';
import { getCachedRoomIDForAlias, storeRoomAliasInCache } from '../RoomAliasCache';
2021-05-24 13:34:06 +00:00
import { ActionPayload } from "../dispatcher/payloads";
import { Action } from "../dispatcher/actions";
import { retry } from "../utils/promise";
2020-10-29 15:53:14 +00:00
import CountlyAnalytics from "../CountlyAnalytics";
import { TimelineRenderingType } from "../contexts/RoomContext";
const NUM_JOIN_RETRY = 5;
const INITIAL_STATE = {
// Whether we're joining the currently viewed room (see isJoining())
joining: false,
2017-06-08 15:00:12 +00:00
// Any error that has occurred during joining
joinError: null,
2017-06-08 15:00:12 +00:00
// The room ID of the room currently being viewed
roomId: null,
2017-06-08 16:27:04 +00:00
// The event to scroll to when the room is first viewed
initialEventId: null,
initialEventPixelOffset: null,
// Whether to highlight the initial event
isInitialEventHighlighted: false,
// 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,
quotingEvent: null,
replyingToEvent: null,
shouldPeek: false,
viaServers: [],
wasContextSwitch: false,
};
/**
* 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<ActionPayload> {
private state = INITIAL_STATE; // initialize state
constructor() {
super(dis);
}
setState(newState: Partial<typeof INITIAL_STATE>) {
// If values haven't changed, there's nothing to do.
// This only tries a shallow comparison, so unchanged objects will slip
// through, but that's probably okay for now.
let stateChanged = false;
for (const key of Object.keys(newState)) {
if (this.state[key] !== newState[key]) {
stateChanged = true;
break;
}
}
if (!stateChanged) {
return;
}
this.state = Object.assign(this.state, newState);
this.__emitChange();
}
__onDispatch(payload) { // eslint-disable-line @typescript-eslint/naming-convention
switch (payload.action) {
// view_room:
// - room_alias: '#somealias:matrix.org'
// - room_id: '!roomid123:matrix.org'
// - event_id: '$213456782:matrix.org'
// - event_offset: 100
// - highlighted: true
case Action.ViewRoom:
this.viewRoom(payload);
break;
// for these events blank out the roomId as we are no longer in the RoomView
case 'view_create_group':
case 'view_welcome_page':
case 'view_home_page':
case 'view_my_groups':
case 'view_group':
this.setState({
roomId: null,
roomAlias: null,
viaServers: [],
wasContextSwitch: false,
});
break;
2017-06-01 17:01:30 +00:00
case 'view_room_error':
this.viewRoomError(payload);
2017-06-01 17:01:30 +00:00
break;
case 'will_join':
this.setState({
joining: true,
});
break;
case 'cancel_join':
this.setState({
joining: false,
});
break;
// join_room:
// - opts: options for joinRoom
2021-05-24 13:34:06 +00:00
case Action.JoinRoom:
this.joinRoom(payload);
break;
2021-05-24 13:34:06 +00:00
case Action.JoinRoomError:
this.joinRoomError(payload);
break;
2021-05-24 13:34:06 +00:00
case Action.JoinRoomReady:
this.setState({ shouldPeek: false });
break;
case 'on_client_not_viable':
case 'on_logged_out':
this.reset();
break;
case 'reply_to_event':
// If currently viewed room does not match the room in which we wish to reply then change rooms
// this can happen when performing a search across all rooms
if (payload.context === TimelineRenderingType.Room) {
if (payload.event
&& payload.event.getRoomId() !== this.state.roomId) {
dis.dispatch({
action: Action.ViewRoom,
room_id: payload.event.getRoomId(),
replyingToEvent: payload.event,
});
} else {
this.setState({
replyingToEvent: payload.event,
});
}
}
break;
case 'open_room_settings': {
// FIXME: Using an import will result in test failures
const RoomSettingsDialog = sdk.getComponent("dialogs.RoomSettingsDialog");
Modal.createTrackedDialog('Room settings', '', RoomSettingsDialog, {
roomId: payload.room_id || this.state.roomId,
initialTabId: payload.initial_tab_id,
2019-04-08 18:12:04 +00:00
}, /*className=*/null, /*isPriority=*/false, /*isStatic=*/true);
break;
}
}
}
private async viewRoom(payload: ActionPayload) {
2017-06-01 17:01:30 +00:00
if (payload.room_id) {
const newState = {
2017-06-01 17:01:30 +00:00
roomId: payload.room_id,
roomAlias: payload.room_alias,
initialEventId: payload.event_id,
isInitialEventHighlighted: payload.highlighted,
roomLoading: false,
roomLoadError: null,
// should peek by default
shouldPeek: payload.should_peek === undefined ? true : payload.should_peek,
// have we sent a join request for this room and are waiting for a response?
joining: payload.joining || false,
// Reset replyingToEvent because we don't want cross-room because bad UX
replyingToEvent: null,
// pull the user out of Room Settings
isEditingSettings: false,
viaServers: payload.via_servers,
wasContextSwitch: payload.context_switch,
};
// Allow being given an event to be replied to when switching rooms but sanity check its for this room
if (payload.replyingToEvent && payload.replyingToEvent.getRoomId() === payload.room_id) {
newState.replyingToEvent = payload.replyingToEvent;
}
this.setState(newState);
if (payload.auto_join) {
2021-05-24 13:34:06 +00:00
dis.dispatch({
...payload,
action: Action.JoinRoom,
roomId: payload.room_id,
});
}
} else if (payload.room_alias) {
// Try the room alias to room ID navigation cache first to avoid
// blocking room navigation on the homeserver.
2019-11-12 13:29:01 +00:00
let roomId = getCachedRoomIDForAlias(payload.room_alias);
if (!roomId) {
// Room alias cache miss, so let's ask the homeserver. Resolve the alias
// and then do a second dispatch with the room ID acquired.
this.setState({
2019-11-12 13:29:01 +00:00
roomId: null,
initialEventId: null,
initialEventPixelOffset: null,
isInitialEventHighlighted: null,
roomAlias: payload.room_alias,
roomLoading: true,
roomLoadError: null,
viaServers: payload.via_servers,
wasContextSwitch: payload.context_switch,
});
2019-11-12 13:29:01 +00:00
try {
const result = await MatrixClientPeg.get().getRoomIdForAlias(payload.room_alias);
storeRoomAliasInCache(payload.room_alias, result.room_id);
roomId = result.room_id;
} catch (err) {
logger.error("RVS failed to get room id for alias: ", err);
2019-11-12 13:29:01 +00:00
dis.dispatch({
action: 'view_room_error',
room_id: null,
room_alias: payload.room_alias,
err,
});
return;
}
}
2019-11-12 13:29:01 +00:00
dis.dispatch({
action: Action.ViewRoom,
2019-11-12 13:29:01 +00:00
room_id: roomId,
event_id: payload.event_id,
highlighted: payload.highlighted,
room_alias: payload.room_alias,
auto_join: payload.auto_join,
oob_data: payload.oob_data,
viaServers: payload.via_servers,
wasContextSwitch: payload.context_switch,
});
}
}
private viewRoomError(payload: ActionPayload) {
this.setState({
2017-06-01 17:01:30 +00:00
roomId: payload.room_id,
roomAlias: payload.room_alias,
roomLoading: false,
roomLoadError: payload.err,
});
}
private async joinRoom(payload: ActionPayload) {
2020-10-29 15:53:14 +00:00
const startTime = CountlyAnalytics.getTimestamp();
this.setState({
joining: true,
});
const cli = MatrixClientPeg.get();
const address = this.state.roomAlias || this.state.roomId;
2021-04-28 08:07:02 +00:00
const viaServers = this.state.viaServers || [];
try {
2021-06-18 15:21:46 +00:00
await retry<any, MatrixError>(() => cli.joinRoom(address, {
viaServers,
...payload.opts,
}), NUM_JOIN_RETRY, (err) => {
// if we received a Gateway timeout then retry
return err.httpStatus === 504;
});
2020-10-29 15:53:14 +00:00
CountlyAnalytics.instance.trackRoomJoin(startTime, this.state.roomId, payload._type);
// We do *not* clear the 'joining' flag because the Room object and/or our 'joined' member event may not
// have come down the sync stream yet, and that's the point at which we'd consider the user joined to the
// room.
2021-05-24 13:34:06 +00:00
dis.dispatch({
action: Action.JoinRoomReady,
roomId: this.state.roomId,
});
} catch (err) {
dis.dispatch({
2021-05-24 13:34:06 +00:00
action: Action.JoinRoomError,
roomId: this.state.roomId,
err: err,
});
}
}
private static getInvitingUserId(roomId: string): string {
const cli = MatrixClientPeg.get();
const room = cli.getRoom(roomId);
if (room && room.getMyMembership() === "invite") {
const myMember = room.getMember(cli.getUserId());
const inviteEvent = myMember ? myMember.events.member : null;
return inviteEvent && inviteEvent.getSender();
}
}
2021-12-14 15:34:54 +00:00
public showJoinRoomError(err: MatrixError, roomId: string) {
let msg: ReactNode = err.message ? err.message : JSON.stringify(err);
logger.log("Failed to join room:", msg);
2021-05-24 13:34:06 +00:00
if (err.name === "ConnectionError") {
msg = _t("There was an error joining the room");
} else if (err.errcode === 'M_INCOMPATIBLE_ROOM_VERSION') {
msg = <div>
{ _t("Sorry, your homeserver is too old to participate in this room.") }<br />
{ _t("Please contact your homeserver administrator.") }
2021-05-24 13:34:06 +00:00
</div>;
} else if (err.httpStatus === 404) {
const invitingUserId = RoomViewStore.getInvitingUserId(roomId);
2021-05-24 13:34:06 +00:00
// only provide a better error message for invites
if (invitingUserId) {
// if the inviting user is on the same HS, there can only be one cause: they left.
if (invitingUserId.endsWith(`:${MatrixClientPeg.get().getDomain()}`)) {
msg = _t("The person who invited you already left the room.");
} else {
msg = _t("The person who invited you already left the room, or their server is offline.");
}
}
}
// FIXME: Using an import will result in test failures
2021-05-24 13:34:06 +00:00
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
Modal.createTrackedDialog('Failed to join room', '', ErrorDialog, {
title: _t("Failed to join room"),
description: msg,
});
}
private joinRoomError(payload: ActionPayload) {
this.setState({
joining: false,
joinError: payload.err,
});
this.showJoinRoomError(payload.err, this.state.roomId);
}
public reset() {
this.state = Object.assign({}, INITIAL_STATE);
}
2017-06-08 15:00:12 +00:00
// The room ID of the room currently being viewed
public getRoomId() {
return this.state.roomId;
}
2017-06-08 16:28:56 +00:00
// The event to scroll to when the room is first viewed
public getInitialEventId() {
return this.state.initialEventId;
}
2017-06-08 15:00:12 +00:00
// Whether to highlight the initial event
public isInitialEventHighlighted() {
return this.state.isInitialEventHighlighted;
}
2017-06-08 15:00:12 +00:00
// The room alias of the room (or null if not originally specified in view_room)
public getRoomAlias() {
return this.state.roomAlias;
}
2017-06-08 15:00:12 +00:00
// Whether the current room is loading (true whilst resolving an alias)
public isRoomLoading() {
return this.state.roomLoading;
}
2017-06-08 15:00:12 +00:00
// Any error that has occurred during loading
public getRoomLoadError() {
return this.state.roomLoadError;
2017-06-01 17:01:30 +00:00
}
// True if we're expecting the user to be joined to the room currently being
// viewed. Note that this is left true after the join request has finished,
// since we should still consider a join to be in progress until the room
// & member events come down the sync.
//
// This flag remains true after the room has been sucessfully joined,
// (this store doesn't listen for the appropriate member events)
2017-09-19 09:21:20 +00:00
// so you should always observe the joined state from the member event
// if a room object is present.
// ie. The correct logic is:
2017-09-19 09:21:20 +00:00
// if (room) {
// if (myMember.membership == 'joined') {
// // user is joined to the room
// } else {
// // Not joined
// }
// } else {
// if (RoomViewStore.isJoining()) {
// // show spinner
// } else {
// // show join prompt
// }
// }
public isJoining() {
return this.state.joining;
}
2017-06-08 15:00:12 +00:00
// Any error that has occurred during joining
public getJoinError() {
return this.state.joinError;
}
// The mxEvent if one is currently being replied to/quoted
public getQuotingEvent() {
return this.state.replyingToEvent;
}
public shouldPeek() {
return this.state.shouldPeek;
}
public getWasContextSwitch() {
return this.state.wasContextSwitch;
}
}
let singletonRoomViewStore: RoomViewStore = null;
if (!singletonRoomViewStore) {
singletonRoomViewStore = new RoomViewStore();
}
export default singletonRoomViewStore;