mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 03:25:12 +01:00
Exctract into function
This commit is contained in:
parent
f1b86e3532
commit
2d4b6b0341
@ -23,7 +23,7 @@ import {imageToInfo} from "../common.js";
|
|||||||
// TODO: remove fallback so default isn't included in bundle for SDK users that have their custom tileClassForEntry
|
// TODO: remove fallback so default isn't included in bundle for SDK users that have their custom tileClassForEntry
|
||||||
// this is a breaking SDK change though to make this option mandatory
|
// this is a breaking SDK change though to make this option mandatory
|
||||||
import {tileClassForEntry as defaultTileClassForEntry} from "./timeline/tiles/index";
|
import {tileClassForEntry as defaultTileClassForEntry} from "./timeline/tiles/index";
|
||||||
import {RoomStatus} from "../../../matrix/room/common";
|
import {joinRoom} from "../../../matrix/room/joinRoom";
|
||||||
|
|
||||||
export class RoomViewModel extends ViewModel {
|
export class RoomViewModel extends ViewModel {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
@ -200,22 +200,11 @@ export class RoomViewModel extends ViewModel {
|
|||||||
|
|
||||||
async _processCommandJoin(roomName) {
|
async _processCommandJoin(roomName) {
|
||||||
try {
|
try {
|
||||||
const roomId = await this._options.client.session.joinRoom(roomName);
|
const session = this._options.client.session;
|
||||||
const roomStatusObserver = await this._options.client.session.observeRoomStatus(roomId);
|
const roomId = await joinRoom(roomName, session);
|
||||||
await roomStatusObserver.waitFor(status => status === RoomStatus.Joined);
|
|
||||||
this.navigation.push("room", roomId);
|
this.navigation.push("room", roomId);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
let exc;
|
this._sendError = err;
|
||||||
if ((err.statusCode ?? err.status) === 400) {
|
|
||||||
exc = new Error(`/join : '${roomName}' was not legal room ID or room alias`);
|
|
||||||
} else if ((err.statusCode ?? err.status) === 404 || (err.statusCode ?? err.status) === 502 || err.message == "Internal Server Error") {
|
|
||||||
exc = new Error(`/join : room '${roomName}' not found`);
|
|
||||||
} else if ((err.statusCode ?? err.status) === 403) {
|
|
||||||
exc = new Error(`/join : you're not invited to join '${roomName}'`);
|
|
||||||
} else {
|
|
||||||
exc = err;
|
|
||||||
}
|
|
||||||
this._sendError = exc;
|
|
||||||
this._timelineError = null;
|
this._timelineError = null;
|
||||||
this.emitChange("error");
|
this.emitChange("error");
|
||||||
}
|
}
|
||||||
|
42
src/matrix/room/joinRoom.ts
Normal file
42
src/matrix/room/joinRoom.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2022 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.
|
||||||
|
*/
|
||||||
|
import type {Session} from "../Session.js";
|
||||||
|
import {RoomStatus} from "./common";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Join a room and wait for it to arrive in the next sync
|
||||||
|
* @param roomId The id of the room to join
|
||||||
|
* @param session A session instance
|
||||||
|
*/
|
||||||
|
export async function joinRoom(roomId: string, session: Session) {
|
||||||
|
try {
|
||||||
|
const internalRoomId = await session.joinRoom(roomId);
|
||||||
|
const roomStatusObserver = await session.observeRoomStatus(internalRoomId);
|
||||||
|
await roomStatusObserver.waitFor((status: RoomStatus) => status === RoomStatus.Joined);
|
||||||
|
return internalRoomId;
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
if ((e.statusCode ?? e.status) === 400) {
|
||||||
|
throw new Error(`'${roomId}' is not a legal room ID or alias`);
|
||||||
|
} else if ((e.statusCode ?? e.status) === 404 || (e.statusCode ?? e.status) === 502 || e.message == "Internal Server eor") {
|
||||||
|
throw new Error(`Room '${roomId}' could not be found`);
|
||||||
|
} else if ((e.statusCode ?? e.status) === 403) {
|
||||||
|
throw new Error(`You are not invited to join '${roomId}'`);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user