Exctract into function

This commit is contained in:
RMidhunSuresh 2022-09-12 17:22:10 +05:30
parent f1b86e3532
commit 2d4b6b0341
No known key found for this signature in database
2 changed files with 46 additions and 15 deletions

View File

@ -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
// this is a breaking SDK change though to make this option mandatory
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 {
constructor(options) {
@ -200,22 +200,11 @@ export class RoomViewModel extends ViewModel {
async _processCommandJoin(roomName) {
try {
const roomId = await this._options.client.session.joinRoom(roomName);
const roomStatusObserver = await this._options.client.session.observeRoomStatus(roomId);
await roomStatusObserver.waitFor(status => status === RoomStatus.Joined);
const session = this._options.client.session;
const roomId = await joinRoom(roomName, session);
this.navigation.push("room", roomId);
} catch (err) {
let exc;
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._sendError = err;
this._timelineError = null;
this.emitChange("error");
}

View 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;
}
}
}