2017-05-24 18:56:13 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-07-03 13:16:44 +03:00
|
|
|
Copyright 2017, 2018 New Vector Ltd
|
2019-12-20 03:45:24 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2017-05-24 18:56:13 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2020-07-21 12:14:12 +03:00
|
|
|
|
2021-12-14 18:34:54 +03:00
|
|
|
import React, { ReactNode } from "react";
|
2021-05-24 16:34:06 +03:00
|
|
|
import { Store } from 'flux/utils';
|
|
|
|
import { MatrixError } from "matrix-js-sdk/src/http-api";
|
2021-10-23 01:23:32 +03:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-02-10 17:29:55 +03:00
|
|
|
import { ViewRoom as ViewRoomEvent } from "matrix-analytics-events/types/typescript/ViewRoom";
|
2022-02-17 21:03:27 +03:00
|
|
|
import { JoinedRoom as JoinedRoomEvent } from "matrix-analytics-events/types/typescript/JoinedRoom";
|
2022-02-14 22:31:13 +03:00
|
|
|
import { JoinRule } from "matrix-js-sdk/src/@types/partials";
|
2022-02-17 21:03:27 +03:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2022-02-22 15:18:08 +03:00
|
|
|
import { ClientEvent } from "matrix-js-sdk/src/client";
|
2020-07-21 12:14:12 +03:00
|
|
|
|
|
|
|
import dis from '../dispatcher/dispatcher';
|
2021-05-24 16:34:06 +03:00
|
|
|
import { MatrixClientPeg } from '../MatrixClientPeg';
|
2017-06-02 13:53:10 +03:00
|
|
|
import Modal from '../Modal';
|
|
|
|
import { _t } from '../languageHandler';
|
2019-11-12 14:43:18 +03:00
|
|
|
import { getCachedRoomIDForAlias, storeRoomAliasInCache } from '../RoomAliasCache';
|
2021-05-24 16:34:06 +03:00
|
|
|
import { ActionPayload } from "../dispatcher/payloads";
|
|
|
|
import { Action } from "../dispatcher/actions";
|
|
|
|
import { retry } from "../utils/promise";
|
2021-10-19 18:05:34 +03:00
|
|
|
import { TimelineRenderingType } from "../contexts/RoomContext";
|
2022-02-10 17:29:55 +03:00
|
|
|
import { PosthogAnalytics } from "../PosthogAnalytics";
|
|
|
|
import { ViewRoomPayload } from "../dispatcher/payloads/ViewRoomPayload";
|
2022-02-14 22:31:13 +03:00
|
|
|
import DMRoomMap from "../utils/DMRoomMap";
|
|
|
|
import SpaceStore from "./spaces/SpaceStore";
|
|
|
|
import { isMetaSpace, MetaSpace } from "./spaces";
|
2022-02-17 21:03:27 +03:00
|
|
|
import { JoinRoomPayload } from "../dispatcher/payloads/JoinRoomPayload";
|
|
|
|
import { JoinRoomReadyPayload } from "../dispatcher/payloads/JoinRoomReadyPayload";
|
2022-02-22 13:04:27 +03:00
|
|
|
import { JoinRoomErrorPayload } from "../dispatcher/payloads/JoinRoomErrorPayload";
|
|
|
|
import { ViewRoomErrorPayload } from "../dispatcher/payloads/ViewRoomErrorPayload";
|
2022-03-03 02:33:40 +03:00
|
|
|
import RoomSettingsDialog from "../components/views/dialogs/RoomSettingsDialog";
|
|
|
|
import ErrorDialog from "../components/views/dialogs/ErrorDialog";
|
2021-09-21 18:48:09 +03:00
|
|
|
|
2020-09-14 17:16:29 +03:00
|
|
|
const NUM_JOIN_RETRY = 5;
|
2017-05-24 18:56:13 +03:00
|
|
|
|
|
|
|
const INITIAL_STATE = {
|
2017-09-15 17:07:09 +03:00
|
|
|
// Whether we're joining the currently viewed room (see isJoining())
|
2017-05-24 18:56:13 +03:00
|
|
|
joining: false,
|
2017-06-08 18:00:12 +03:00
|
|
|
// Any error that has occurred during joining
|
2017-05-24 18:56:13 +03:00
|
|
|
joinError: null,
|
2017-06-08 18:00:12 +03:00
|
|
|
// The room ID of the room currently being viewed
|
2017-05-24 18:56:13 +03:00
|
|
|
roomId: null,
|
2017-06-08 17:47:41 +03:00
|
|
|
|
2017-06-08 19:27:04 +03:00
|
|
|
// The event to scroll to when the room is first viewed
|
2017-06-08 17:47:41 +03:00
|
|
|
initialEventId: null,
|
2020-07-21 12:14:12 +03:00
|
|
|
initialEventPixelOffset: null,
|
2017-06-08 17:47:41 +03:00
|
|
|
// Whether to highlight the initial event
|
|
|
|
isInitialEventHighlighted: false,
|
|
|
|
|
2017-05-24 18:56:13 +03: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-16 18:12:52 +03:00
|
|
|
|
2020-07-21 12:14:12 +03:00
|
|
|
replyingToEvent: null,
|
|
|
|
|
|
|
|
shouldPeek: false,
|
2021-04-28 11:04:02 +03:00
|
|
|
|
|
|
|
viaServers: [],
|
2021-04-29 11:37:21 +03:00
|
|
|
|
|
|
|
wasContextSwitch: false,
|
2017-05-24 18:56:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class for storing application state for RoomView. This is the RoomView's interface
|
|
|
|
* with a subset of the js-sdk.
|
|
|
|
* ```
|
|
|
|
*/
|
2020-07-21 12:14:12 +03:00
|
|
|
class RoomViewStore extends Store<ActionPayload> {
|
|
|
|
private state = INITIAL_STATE; // initialize state
|
|
|
|
|
2019-01-17 12:29:37 +03:00
|
|
|
constructor() {
|
|
|
|
super(dis);
|
2017-05-24 18:56:13 +03:00
|
|
|
}
|
|
|
|
|
2020-07-21 12:14:12 +03:00
|
|
|
setState(newState: Partial<typeof INITIAL_STATE>) {
|
2020-02-06 20:57:17 +03:00
|
|
|
// 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)) {
|
2020-07-21 12:14:12 +03:00
|
|
|
if (this.state[key] !== newState[key]) {
|
2020-02-06 20:57:17 +03:00
|
|
|
stateChanged = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!stateChanged) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-21 12:14:12 +03:00
|
|
|
this.state = Object.assign(this.state, newState);
|
2017-05-24 18:56:13 +03:00
|
|
|
this.__emitChange();
|
|
|
|
}
|
|
|
|
|
2021-07-20 00:43:11 +03:00
|
|
|
__onDispatch(payload) { // eslint-disable-line @typescript-eslint/naming-convention
|
2017-05-24 18:56:13 +03:00
|
|
|
switch (payload.action) {
|
|
|
|
// view_room:
|
2017-06-08 16:17:49 +03:00
|
|
|
// - room_alias: '#somealias:matrix.org'
|
|
|
|
// - room_id: '!roomid123:matrix.org'
|
|
|
|
// - event_id: '$213456782:matrix.org'
|
|
|
|
// - event_offset: 100
|
|
|
|
// - highlighted: true
|
2021-11-25 23:49:43 +03:00
|
|
|
case Action.ViewRoom:
|
2020-07-21 12:14:12 +03:00
|
|
|
this.viewRoom(payload);
|
2017-05-24 18:56:13 +03:00
|
|
|
break;
|
2020-07-21 12:22:03 +03:00
|
|
|
// for these events blank out the roomId as we are no longer in the RoomView
|
2020-07-21 12:20:30 +03:00
|
|
|
case 'view_welcome_page':
|
2022-02-22 13:04:27 +03:00
|
|
|
case Action.ViewHomePage:
|
2020-07-21 12:14:12 +03:00
|
|
|
this.setState({
|
2017-10-24 18:32:52 +03:00
|
|
|
roomId: null,
|
|
|
|
roomAlias: null,
|
2021-04-28 11:04:02 +03:00
|
|
|
viaServers: [],
|
2021-04-29 11:37:21 +03:00
|
|
|
wasContextSwitch: false,
|
2017-10-24 18:32:52 +03:00
|
|
|
});
|
|
|
|
break;
|
2022-02-22 13:04:27 +03:00
|
|
|
case Action.ViewRoomError:
|
2020-07-21 12:14:12 +03:00
|
|
|
this.viewRoomError(payload);
|
2017-06-01 20:01:30 +03:00
|
|
|
break;
|
2017-05-25 19:04:42 +03:00
|
|
|
case 'will_join':
|
2020-07-21 12:14:12 +03:00
|
|
|
this.setState({
|
2017-05-25 19:04:42 +03:00
|
|
|
joining: true,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'cancel_join':
|
2020-07-21 12:14:12 +03:00
|
|
|
this.setState({
|
2017-05-25 19:04:42 +03:00
|
|
|
joining: false,
|
|
|
|
});
|
|
|
|
break;
|
2017-05-24 18:56:13 +03:00
|
|
|
// join_room:
|
|
|
|
// - opts: options for joinRoom
|
2021-05-24 16:34:06 +03:00
|
|
|
case Action.JoinRoom:
|
2020-07-21 12:14:12 +03:00
|
|
|
this.joinRoom(payload);
|
2017-05-24 18:56:13 +03:00
|
|
|
break;
|
2021-05-24 16:34:06 +03:00
|
|
|
case Action.JoinRoomError:
|
2020-07-21 12:14:12 +03:00
|
|
|
this.joinRoomError(payload);
|
2017-06-02 13:53:10 +03:00
|
|
|
break;
|
2022-02-17 21:03:27 +03:00
|
|
|
case Action.JoinRoomReady: {
|
2022-01-07 12:23:54 +03:00
|
|
|
if (this.state.roomId === payload.roomId) {
|
|
|
|
this.setState({ shouldPeek: false });
|
|
|
|
}
|
2022-02-17 21:03:27 +03:00
|
|
|
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
|
|
|
|
const updateMetrics = () => {
|
|
|
|
const room = cli.getRoom(payload.roomId);
|
|
|
|
const numMembers = room.getJoinedMemberCount();
|
|
|
|
const roomSize = numMembers > 1000 ? "MoreThanAThousand"
|
|
|
|
: numMembers > 100 ? "OneHundredAndOneToAThousand"
|
|
|
|
: numMembers > 10 ? "ElevenToOneHundred"
|
|
|
|
: numMembers > 2 ? "ThreeToTen"
|
|
|
|
: numMembers > 1 ? "Two"
|
|
|
|
: "One";
|
|
|
|
|
|
|
|
PosthogAnalytics.instance.trackEvent<JoinedRoomEvent>({
|
|
|
|
eventName: "JoinedRoom",
|
|
|
|
trigger: payload.metricsTrigger,
|
|
|
|
roomSize,
|
|
|
|
isDM: !!DMRoomMap.shared().getUserIdForRoomId(room.roomId),
|
|
|
|
isSpace: room.isSpaceRoom(),
|
|
|
|
});
|
|
|
|
|
2022-02-22 15:18:08 +03:00
|
|
|
cli.off(ClientEvent.Room, updateMetrics);
|
2022-02-17 21:03:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if (cli.getRoom(payload.roomId)) {
|
|
|
|
updateMetrics();
|
|
|
|
} else {
|
2022-02-22 15:18:08 +03:00
|
|
|
cli.on(ClientEvent.Room, updateMetrics);
|
2022-02-17 21:03:27 +03:00
|
|
|
}
|
|
|
|
|
2020-03-31 12:37:56 +03:00
|
|
|
break;
|
2022-02-17 21:03:27 +03:00
|
|
|
}
|
2019-07-04 01:46:37 +03:00
|
|
|
case 'on_client_not_viable':
|
2017-05-25 19:16:16 +03:00
|
|
|
case 'on_logged_out':
|
|
|
|
this.reset();
|
|
|
|
break;
|
2018-02-20 02:41:07 +03:00
|
|
|
case 'reply_to_event':
|
2019-09-09 11:34:08 +03:00
|
|
|
// If currently viewed room does not match the room in which we wish to reply then change rooms
|
2022-03-11 18:40:49 +03:00
|
|
|
// this can happen when performing a search across all rooms. Persist the data from this event for
|
|
|
|
// both room and search timeline rendering types, search will get auto-closed by RoomView at this time.
|
|
|
|
if ([TimelineRenderingType.Room, TimelineRenderingType.Search].includes(payload.context)) {
|
2022-03-11 20:21:28 +03:00
|
|
|
if (payload.event && payload.event.getRoomId() !== this.state.roomId) {
|
2022-02-10 17:29:55 +03:00
|
|
|
dis.dispatch<ViewRoomPayload>({
|
2021-11-25 23:49:43 +03:00
|
|
|
action: Action.ViewRoom,
|
2021-10-19 18:05:34 +03:00
|
|
|
room_id: payload.event.getRoomId(),
|
|
|
|
replyingToEvent: payload.event,
|
2022-02-17 21:03:27 +03:00
|
|
|
metricsTrigger: undefined, // room doesn't change
|
2021-10-19 18:05:34 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
replyingToEvent: payload.event,
|
|
|
|
});
|
|
|
|
}
|
2019-09-09 11:34:08 +03:00
|
|
|
}
|
2018-02-20 02:41:07 +03:00
|
|
|
break;
|
2019-02-04 23:25:26 +03:00
|
|
|
case 'open_room_settings': {
|
|
|
|
Modal.createTrackedDialog('Room settings', '', RoomSettingsDialog, {
|
2020-07-21 12:14:12 +03:00
|
|
|
roomId: payload.room_id || this.state.roomId,
|
2021-06-10 13:14:43 +03:00
|
|
|
initialTabId: payload.initial_tab_id,
|
2019-04-08 21:12:04 +03:00
|
|
|
}, /*className=*/null, /*isPriority=*/false, /*isStatic=*/true);
|
2018-05-29 15:16:39 +03:00
|
|
|
break;
|
2019-02-04 23:25:26 +03:00
|
|
|
}
|
2017-05-24 18:56:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:29:55 +03:00
|
|
|
private async viewRoom(payload: ViewRoomPayload): Promise<void> {
|
2017-06-01 20:01:30 +03:00
|
|
|
if (payload.room_id) {
|
2022-02-17 21:03:27 +03:00
|
|
|
if (payload.metricsTrigger !== null && payload.room_id !== this.state.roomId) {
|
2022-02-14 22:31:13 +03:00
|
|
|
let activeSpace: ViewRoomEvent["activeSpace"];
|
|
|
|
if (SpaceStore.instance.activeSpace === MetaSpace.Home) {
|
|
|
|
activeSpace = "Home";
|
|
|
|
} else if (isMetaSpace(SpaceStore.instance.activeSpace)) {
|
|
|
|
activeSpace = "Meta";
|
|
|
|
} else {
|
|
|
|
activeSpace = SpaceStore.instance.activeSpaceRoom.getJoinRule() === JoinRule.Public
|
|
|
|
? "Public"
|
|
|
|
: "Private";
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:29:55 +03:00
|
|
|
PosthogAnalytics.instance.trackEvent<ViewRoomEvent>({
|
|
|
|
eventName: "ViewRoom",
|
2022-02-17 21:03:27 +03:00
|
|
|
trigger: payload.metricsTrigger,
|
|
|
|
viaKeyboard: payload.metricsViaKeyboard,
|
2022-02-14 22:31:13 +03:00
|
|
|
isDM: !!DMRoomMap.shared().getUserIdForRoomId(payload.room_id),
|
|
|
|
isSpace: MatrixClientPeg.get().getRoom(payload.room_id)?.isSpaceRoom(),
|
|
|
|
activeSpace,
|
2022-02-10 17:29:55 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-08 17:54:23 +03:00
|
|
|
const newState = {
|
2017-06-01 20:01:30 +03:00
|
|
|
roomId: payload.room_id,
|
2017-06-14 14:05:25 +03:00
|
|
|
roomAlias: payload.room_alias,
|
2017-06-08 17:47:41 +03:00
|
|
|
initialEventId: payload.event_id,
|
|
|
|
isInitialEventHighlighted: payload.highlighted,
|
2017-06-02 11:22:48 +03:00
|
|
|
roomLoading: false,
|
|
|
|
roomLoadError: null,
|
2017-06-16 20:24:07 +03:00
|
|
|
// should peek by default
|
|
|
|
shouldPeek: payload.should_peek === undefined ? true : payload.should_peek,
|
2017-09-15 00:22:21 +03:00
|
|
|
// have we sent a join request for this room and are waiting for a response?
|
|
|
|
joining: payload.joining || false,
|
2018-02-20 02:41:07 +03:00
|
|
|
// Reset replyingToEvent because we don't want cross-room because bad UX
|
|
|
|
replyingToEvent: null,
|
2021-04-28 11:04:02 +03:00
|
|
|
viaServers: payload.via_servers,
|
2021-04-29 11:37:21 +03:00
|
|
|
wasContextSwitch: payload.context_switch,
|
2017-06-08 17:54:23 +03:00
|
|
|
};
|
2019-01-17 12:29:37 +03:00
|
|
|
|
2019-09-09 11:34:08 +03:00
|
|
|
// Allow being given an event to be replied to when switching rooms but sanity check its for this room
|
2022-02-10 17:29:55 +03:00
|
|
|
if (payload.replyingToEvent?.getRoomId() === payload.room_id) {
|
2019-09-09 11:34:08 +03:00
|
|
|
newState.replyingToEvent = payload.replyingToEvent;
|
2022-03-23 16:38:21 +03:00
|
|
|
} else if (this.state.roomId === payload.room_id) {
|
|
|
|
// if the room isn't being changed, e.g visiting a permalink then maintain replyingToEvent
|
|
|
|
newState.replyingToEvent = this.state.replyingToEvent;
|
2019-09-09 11:34:08 +03:00
|
|
|
}
|
|
|
|
|
2020-07-21 12:14:12 +03:00
|
|
|
this.setState(newState);
|
2019-01-17 12:29:37 +03:00
|
|
|
|
2017-09-15 01:06:00 +03:00
|
|
|
if (payload.auto_join) {
|
2022-02-17 21:03:27 +03:00
|
|
|
dis.dispatch<JoinRoomPayload>({
|
2021-05-24 16:34:06 +03:00
|
|
|
...payload,
|
|
|
|
action: Action.JoinRoom,
|
|
|
|
roomId: payload.room_id,
|
2022-02-17 21:03:27 +03:00
|
|
|
metricsTrigger: payload.metricsTrigger as JoinRoomPayload["metricsTrigger"],
|
2021-05-24 16:34:06 +03:00
|
|
|
});
|
2017-09-15 01:06:00 +03:00
|
|
|
}
|
2017-06-02 11:22:48 +03:00
|
|
|
} else if (payload.room_alias) {
|
2019-11-12 14:43:18 +03:00
|
|
|
// Try the room alias to room ID navigation cache first to avoid
|
|
|
|
// blocking room navigation on the homeserver.
|
2019-11-12 16:29:01 +03: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.
|
2020-07-21 12:14:12 +03:00
|
|
|
this.setState({
|
2019-11-12 16:29:01 +03:00
|
|
|
roomId: null,
|
|
|
|
initialEventId: null,
|
|
|
|
initialEventPixelOffset: null,
|
|
|
|
isInitialEventHighlighted: null,
|
|
|
|
roomAlias: payload.room_alias,
|
|
|
|
roomLoading: true,
|
|
|
|
roomLoadError: null,
|
2021-04-28 11:04:02 +03:00
|
|
|
viaServers: payload.via_servers,
|
2021-04-29 11:37:21 +03:00
|
|
|
wasContextSwitch: payload.context_switch,
|
2019-11-12 14:43:18 +03:00
|
|
|
});
|
2019-11-12 16:29:01 +03:00
|
|
|
try {
|
|
|
|
const result = await MatrixClientPeg.get().getRoomIdForAlias(payload.room_alias);
|
|
|
|
storeRoomAliasInCache(payload.room_alias, result.room_id);
|
|
|
|
roomId = result.room_id;
|
|
|
|
} catch (err) {
|
2021-10-15 17:30:53 +03:00
|
|
|
logger.error("RVS failed to get room id for alias: ", err);
|
2022-02-22 13:04:27 +03:00
|
|
|
dis.dispatch<ViewRoomErrorPayload>({
|
|
|
|
action: Action.ViewRoomError,
|
2019-11-12 16:29:01 +03:00
|
|
|
room_id: null,
|
|
|
|
room_alias: payload.room_alias,
|
|
|
|
err,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2019-11-12 14:43:18 +03:00
|
|
|
}
|
2019-11-12 16:29:01 +03:00
|
|
|
|
2022-01-12 23:12:28 +03:00
|
|
|
// Re-fire the payload with the newly found room_id
|
2019-11-12 16:29:01 +03:00
|
|
|
dis.dispatch({
|
2022-01-12 23:12:28 +03:00
|
|
|
...payload,
|
2019-11-12 16:29:01 +03:00
|
|
|
room_id: roomId,
|
2017-05-24 18:56:13 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 13:04:27 +03:00
|
|
|
private viewRoomError(payload: ViewRoomErrorPayload) {
|
2020-07-21 12:14:12 +03:00
|
|
|
this.setState({
|
2017-06-01 20:01:30 +03:00
|
|
|
roomId: payload.room_id,
|
|
|
|
roomAlias: payload.room_alias,
|
|
|
|
roomLoading: false,
|
|
|
|
roomLoadError: payload.err,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-17 21:03:27 +03:00
|
|
|
private async joinRoom(payload: JoinRoomPayload) {
|
2020-07-21 12:14:12 +03:00
|
|
|
this.setState({
|
2017-05-24 18:56:13 +03:00
|
|
|
joining: true,
|
|
|
|
});
|
2020-09-14 17:16:29 +03:00
|
|
|
|
|
|
|
const cli = MatrixClientPeg.get();
|
2022-01-07 12:23:54 +03:00
|
|
|
// take a copy of roomAlias & roomId as they may change by the time the join is complete
|
|
|
|
const { roomAlias, roomId } = this.state;
|
|
|
|
const address = roomAlias || roomId;
|
2021-04-28 11:07:02 +03:00
|
|
|
const viaServers = this.state.viaServers || [];
|
2020-09-14 17:16:29 +03:00
|
|
|
try {
|
2022-02-17 21:03:27 +03:00
|
|
|
await retry<Room, MatrixError>(() => cli.joinRoom(address, {
|
2021-04-28 11:04:02 +03:00
|
|
|
viaServers,
|
2022-02-17 21:03:27 +03:00
|
|
|
...(payload.opts || {}),
|
2021-03-24 19:45:53 +03:00
|
|
|
}), NUM_JOIN_RETRY, (err) => {
|
2020-09-14 17:16:29 +03:00
|
|
|
// if we received a Gateway timeout then retry
|
|
|
|
return err.httpStatus === 504;
|
|
|
|
});
|
2022-02-10 17:29:55 +03:00
|
|
|
|
2020-03-31 12:37:56 +03:00
|
|
|
// 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.
|
2022-02-17 21:03:27 +03:00
|
|
|
dis.dispatch<JoinRoomReadyPayload>({
|
2021-05-24 16:34:06 +03:00
|
|
|
action: Action.JoinRoomReady,
|
2022-01-07 12:23:54 +03:00
|
|
|
roomId,
|
2022-02-17 21:03:27 +03:00
|
|
|
metricsTrigger: payload.metricsTrigger,
|
2021-05-24 16:34:06 +03:00
|
|
|
});
|
2020-09-14 17:16:29 +03:00
|
|
|
} catch (err) {
|
2019-01-17 12:29:37 +03:00
|
|
|
dis.dispatch({
|
2021-05-24 16:34:06 +03:00
|
|
|
action: Action.JoinRoomError,
|
2022-01-07 12:23:54 +03:00
|
|
|
roomId,
|
2017-06-02 13:53:10 +03:00
|
|
|
err: err,
|
|
|
|
});
|
2020-09-14 17:16:29 +03:00
|
|
|
}
|
2017-06-02 13:53:10 +03:00
|
|
|
}
|
|
|
|
|
2021-10-27 17:24:31 +03:00
|
|
|
private static getInvitingUserId(roomId: string): string {
|
2020-07-29 17:51:37 +03:00
|
|
|
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 18:34:54 +03:00
|
|
|
public showJoinRoomError(err: MatrixError, roomId: string) {
|
|
|
|
let msg: ReactNode = err.message ? err.message : JSON.stringify(err);
|
2021-09-21 18:48:09 +03:00
|
|
|
logger.log("Failed to join room:", msg);
|
2021-05-24 16:34:06 +03:00
|
|
|
|
|
|
|
if (err.name === "ConnectionError") {
|
|
|
|
msg = _t("There was an error joining the room");
|
|
|
|
} else if (err.errcode === 'M_INCOMPATIBLE_ROOM_VERSION') {
|
|
|
|
msg = <div>
|
2021-07-20 00:43:11 +03:00
|
|
|
{ _t("Sorry, your homeserver is too old to participate in this room.") }<br />
|
|
|
|
{ _t("Please contact your homeserver administrator.") }
|
2021-05-24 16:34:06 +03:00
|
|
|
</div>;
|
|
|
|
} else if (err.httpStatus === 404) {
|
2021-10-27 17:24:31 +03:00
|
|
|
const invitingUserId = RoomViewStore.getInvitingUserId(roomId);
|
2021-05-24 16:34:06 +03: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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Modal.createTrackedDialog('Failed to join room', '', ErrorDialog, {
|
|
|
|
title: _t("Failed to join room"),
|
|
|
|
description: msg,
|
|
|
|
});
|
2017-05-24 18:56:13 +03:00
|
|
|
}
|
|
|
|
|
2022-02-22 13:04:27 +03:00
|
|
|
private joinRoomError(payload: JoinRoomErrorPayload) {
|
2021-10-27 17:24:31 +03:00
|
|
|
this.setState({
|
|
|
|
joining: false,
|
|
|
|
joinError: payload.err,
|
|
|
|
});
|
2022-01-07 12:23:54 +03:00
|
|
|
this.showJoinRoomError(payload.err, payload.roomId);
|
2021-10-27 17:24:31 +03:00
|
|
|
}
|
|
|
|
|
2020-07-21 12:14:12 +03:00
|
|
|
public reset() {
|
|
|
|
this.state = Object.assign({}, INITIAL_STATE);
|
2017-05-24 18:56:13 +03:00
|
|
|
}
|
|
|
|
|
2017-06-08 18:00:12 +03:00
|
|
|
// The room ID of the room currently being viewed
|
2020-07-21 12:14:12 +03:00
|
|
|
public getRoomId() {
|
|
|
|
return this.state.roomId;
|
2017-05-24 18:56:13 +03:00
|
|
|
}
|
|
|
|
|
2017-06-08 19:28:56 +03:00
|
|
|
// The event to scroll to when the room is first viewed
|
2020-07-21 12:14:12 +03:00
|
|
|
public getInitialEventId() {
|
|
|
|
return this.state.initialEventId;
|
2017-06-08 16:17:49 +03:00
|
|
|
}
|
|
|
|
|
2017-06-08 18:00:12 +03:00
|
|
|
// Whether to highlight the initial event
|
2020-07-21 12:14:12 +03:00
|
|
|
public isInitialEventHighlighted() {
|
|
|
|
return this.state.isInitialEventHighlighted;
|
2017-06-08 16:17:49 +03:00
|
|
|
}
|
|
|
|
|
2017-06-08 18:00:12 +03:00
|
|
|
// The room alias of the room (or null if not originally specified in view_room)
|
2020-07-21 12:14:12 +03:00
|
|
|
public getRoomAlias() {
|
|
|
|
return this.state.roomAlias;
|
2017-05-24 18:56:13 +03:00
|
|
|
}
|
|
|
|
|
2017-06-08 18:00:12 +03:00
|
|
|
// Whether the current room is loading (true whilst resolving an alias)
|
2020-07-21 12:14:12 +03:00
|
|
|
public isRoomLoading() {
|
|
|
|
return this.state.roomLoading;
|
2017-05-24 18:56:13 +03:00
|
|
|
}
|
|
|
|
|
2017-06-08 18:00:12 +03:00
|
|
|
// Any error that has occurred during loading
|
2020-07-21 12:14:12 +03:00
|
|
|
public getRoomLoadError() {
|
|
|
|
return this.state.roomLoadError;
|
2017-06-01 20:01:30 +03:00
|
|
|
}
|
|
|
|
|
2017-09-15 17:07:09 +03: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 12:21:20 +03:00
|
|
|
// so you should always observe the joined state from the member event
|
|
|
|
// if a room object is present.
|
2017-09-15 17:07:09 +03:00
|
|
|
// ie. The correct logic is:
|
2017-09-19 12:21:20 +03:00
|
|
|
// if (room) {
|
|
|
|
// if (myMember.membership == 'joined') {
|
|
|
|
// // user is joined to the room
|
|
|
|
// } else {
|
|
|
|
// // Not joined
|
|
|
|
// }
|
2017-09-15 17:07:09 +03:00
|
|
|
// } else {
|
|
|
|
// if (RoomViewStore.isJoining()) {
|
|
|
|
// // show spinner
|
|
|
|
// } else {
|
|
|
|
// // show join prompt
|
|
|
|
// }
|
|
|
|
// }
|
2020-07-21 12:14:12 +03:00
|
|
|
public isJoining() {
|
|
|
|
return this.state.joining;
|
2017-05-24 18:56:13 +03:00
|
|
|
}
|
|
|
|
|
2017-06-08 18:00:12 +03:00
|
|
|
// Any error that has occurred during joining
|
2020-07-21 12:14:12 +03:00
|
|
|
public getJoinError() {
|
|
|
|
return this.state.joinError;
|
2017-05-24 18:56:13 +03:00
|
|
|
}
|
2017-06-16 18:12:52 +03:00
|
|
|
|
2017-12-10 15:50:41 +03:00
|
|
|
// The mxEvent if one is currently being replied to/quoted
|
2020-07-21 12:14:12 +03:00
|
|
|
public getQuotingEvent() {
|
|
|
|
return this.state.replyingToEvent;
|
2017-12-10 15:50:41 +03:00
|
|
|
}
|
|
|
|
|
2020-07-21 12:14:12 +03:00
|
|
|
public shouldPeek() {
|
|
|
|
return this.state.shouldPeek;
|
2017-06-16 20:24:07 +03:00
|
|
|
}
|
2021-04-29 11:37:21 +03:00
|
|
|
|
|
|
|
public getWasContextSwitch() {
|
|
|
|
return this.state.wasContextSwitch;
|
|
|
|
}
|
2017-05-24 18:56:13 +03:00
|
|
|
}
|
|
|
|
|
2021-07-17 16:20:46 +03:00
|
|
|
let singletonRoomViewStore: RoomViewStore = null;
|
2019-01-17 12:29:37 +03:00
|
|
|
if (!singletonRoomViewStore) {
|
|
|
|
singletonRoomViewStore = new RoomViewStore();
|
|
|
|
}
|
2019-12-20 03:45:24 +03:00
|
|
|
export default singletonRoomViewStore;
|