Add slash command to switch to a room's virtual room (#7839)

* Add slash command to switch to a room's virtual room

* Update to new interface

* Return null if no virtual user

* Throw newTranslateableError

* Types

* Disable tovirtual if virtual rooms not supported
pull/21833/head
David Baker 2022-02-25 15:58:13 +00:00 committed by GitHub
parent 76ca0362e1
commit 3e4e7efd89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 1 deletions

View File

@ -65,6 +65,7 @@ import RoomViewStore from "./stores/RoomViewStore";
import { XOR } from "./@types/common"; import { XOR } from "./@types/common";
import { PosthogAnalytics } from "./PosthogAnalytics"; import { PosthogAnalytics } from "./PosthogAnalytics";
import { ViewRoomPayload } from "./dispatcher/payloads/ViewRoomPayload"; import { ViewRoomPayload } from "./dispatcher/payloads/ViewRoomPayload";
import VoipUserMapper from './VoipUserMapper';
// XXX: workaround for https://github.com/microsoft/TypeScript/issues/31816 // XXX: workaround for https://github.com/microsoft/TypeScript/issues/31816
interface HTMLInputEvent extends Event { interface HTMLInputEvent extends Event {
@ -1129,6 +1130,26 @@ export const Commands = [
}, },
category: CommandCategories.advanced, category: CommandCategories.advanced,
}), }),
new Command({
command: "tovirtual",
description: _td("Switches to this room's virtual room, if it has one"),
category: CommandCategories.advanced,
isEnabled(): boolean {
return CallHandler.instance.getSupportsVirtualRooms();
},
runFn: (roomId) => {
return success((async () => {
const room = await VoipUserMapper.sharedInstance().getVirtualRoomForRoom(roomId);
if (!room) throw newTranslatableError("No virtual room for this room");
dis.dispatch<ViewRoomPayload>({
action: Action.ViewRoom,
room_id: room.roomId,
metricsTrigger: "SlashCommand",
metricsViaKeyboard: true,
});
})());
},
}),
new Command({ new Command({
command: "query", command: "query",
description: _td("Opens chat with the given user"), description: _td("Opens chat with the given user"),

View File

@ -42,13 +42,20 @@ export default class VoipUserMapper {
return results[0].userid; return results[0].userid;
} }
public async getOrCreateVirtualRoomForRoom(roomId: string): Promise<string> { private async getVirtualUserForRoom(roomId: string): Promise<string | null> {
const userId = DMRoomMap.shared().getUserIdForRoomId(roomId); const userId = DMRoomMap.shared().getUserIdForRoomId(roomId);
if (!userId) return null; if (!userId) return null;
const virtualUser = await this.userToVirtualUser(userId); const virtualUser = await this.userToVirtualUser(userId);
if (!virtualUser) return null; if (!virtualUser) return null;
return virtualUser;
}
public async getOrCreateVirtualRoomForRoom(roomId: string): Promise<string | null> {
const virtualUser = await this.getVirtualUserForRoom(roomId);
if (!virtualUser) return null;
const virtualRoomId = await ensureVirtualRoomExists(MatrixClientPeg.get(), virtualUser, roomId); const virtualRoomId = await ensureVirtualRoomExists(MatrixClientPeg.get(), virtualUser, roomId);
MatrixClientPeg.get().setRoomAccountData(virtualRoomId, VIRTUAL_ROOM_EVENT_TYPE, { MatrixClientPeg.get().setRoomAccountData(virtualRoomId, VIRTUAL_ROOM_EVENT_TYPE, {
native_room: roomId, native_room: roomId,
@ -59,6 +66,17 @@ export default class VoipUserMapper {
return virtualRoomId; return virtualRoomId;
} }
/**
* Gets the ID of the virtual room for a room, or null if the room has no
* virtual room
*/
public async getVirtualRoomForRoom(roomId: string): Promise<Room | null> {
const virtualUser = await this.getVirtualUserForRoom(roomId);
if (!virtualUser) return null;
return findDMForUser(MatrixClientPeg.get(), virtualUser);
}
public nativeRoomForVirtualRoom(roomId: string): string { public nativeRoomForVirtualRoom(roomId: string): string {
const cachedNativeRoomId = this.virtualToNativeRoomIdCache.get(roomId); const cachedNativeRoomId = this.virtualToNativeRoomIdCache.get(roomId);
if (cachedNativeRoomId) { if (cachedNativeRoomId) {

View File

@ -486,6 +486,8 @@
"Displays list of commands with usages and descriptions": "Displays list of commands with usages and descriptions", "Displays list of commands with usages and descriptions": "Displays list of commands with usages and descriptions",
"Displays information about a user": "Displays information about a user", "Displays information about a user": "Displays information about a user",
"Send a bug report with logs": "Send a bug report with logs", "Send a bug report with logs": "Send a bug report with logs",
"Switches to this room's virtual room, if it has one": "Switches to this room's virtual room, if it has one",
"No virtual room for this room": "No virtual room for this room",
"Opens chat with the given user": "Opens chat with the given user", "Opens chat with the given user": "Opens chat with the given user",
"Unable to find Matrix ID for phone number": "Unable to find Matrix ID for phone number", "Unable to find Matrix ID for phone number": "Unable to find Matrix ID for phone number",
"Sends a message to the given user": "Sends a message to the given user", "Sends a message to the given user": "Sends a message to the given user",