Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 3e4e7ef

Browse files
authored
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
1 parent 76ca036 commit 3e4e7ef

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/SlashCommands.tsx

+21
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import RoomViewStore from "./stores/RoomViewStore";
6565
import { XOR } from "./@types/common";
6666
import { PosthogAnalytics } from "./PosthogAnalytics";
6767
import { ViewRoomPayload } from "./dispatcher/payloads/ViewRoomPayload";
68+
import VoipUserMapper from './VoipUserMapper';
6869

6970
// XXX: workaround for https://github.com/microsoft/TypeScript/issues/31816
7071
interface HTMLInputEvent extends Event {
@@ -1129,6 +1130,26 @@ export const Commands = [
11291130
},
11301131
category: CommandCategories.advanced,
11311132
}),
1133+
new Command({
1134+
command: "tovirtual",
1135+
description: _td("Switches to this room's virtual room, if it has one"),
1136+
category: CommandCategories.advanced,
1137+
isEnabled(): boolean {
1138+
return CallHandler.instance.getSupportsVirtualRooms();
1139+
},
1140+
runFn: (roomId) => {
1141+
return success((async () => {
1142+
const room = await VoipUserMapper.sharedInstance().getVirtualRoomForRoom(roomId);
1143+
if (!room) throw newTranslatableError("No virtual room for this room");
1144+
dis.dispatch<ViewRoomPayload>({
1145+
action: Action.ViewRoom,
1146+
room_id: room.roomId,
1147+
metricsTrigger: "SlashCommand",
1148+
metricsViaKeyboard: true,
1149+
});
1150+
})());
1151+
},
1152+
}),
11321153
new Command({
11331154
command: "query",
11341155
description: _td("Opens chat with the given user"),

src/VoipUserMapper.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,20 @@ export default class VoipUserMapper {
4242
return results[0].userid;
4343
}
4444

45-
public async getOrCreateVirtualRoomForRoom(roomId: string): Promise<string> {
45+
private async getVirtualUserForRoom(roomId: string): Promise<string | null> {
4646
const userId = DMRoomMap.shared().getUserIdForRoomId(roomId);
4747
if (!userId) return null;
4848

4949
const virtualUser = await this.userToVirtualUser(userId);
5050
if (!virtualUser) return null;
5151

52+
return virtualUser;
53+
}
54+
55+
public async getOrCreateVirtualRoomForRoom(roomId: string): Promise<string | null> {
56+
const virtualUser = await this.getVirtualUserForRoom(roomId);
57+
if (!virtualUser) return null;
58+
5259
const virtualRoomId = await ensureVirtualRoomExists(MatrixClientPeg.get(), virtualUser, roomId);
5360
MatrixClientPeg.get().setRoomAccountData(virtualRoomId, VIRTUAL_ROOM_EVENT_TYPE, {
5461
native_room: roomId,
@@ -59,6 +66,17 @@ export default class VoipUserMapper {
5966
return virtualRoomId;
6067
}
6168

69+
/**
70+
* Gets the ID of the virtual room for a room, or null if the room has no
71+
* virtual room
72+
*/
73+
public async getVirtualRoomForRoom(roomId: string): Promise<Room | null> {
74+
const virtualUser = await this.getVirtualUserForRoom(roomId);
75+
if (!virtualUser) return null;
76+
77+
return findDMForUser(MatrixClientPeg.get(), virtualUser);
78+
}
79+
6280
public nativeRoomForVirtualRoom(roomId: string): string {
6381
const cachedNativeRoomId = this.virtualToNativeRoomIdCache.get(roomId);
6482
if (cachedNativeRoomId) {

src/i18n/strings/en_EN.json

+2
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@
486486
"Displays list of commands with usages and descriptions": "Displays list of commands with usages and descriptions",
487487
"Displays information about a user": "Displays information about a user",
488488
"Send a bug report with logs": "Send a bug report with logs",
489+
"Switches to this room's virtual room, if it has one": "Switches to this room's virtual room, if it has one",
490+
"No virtual room for this room": "No virtual room for this room",
489491
"Opens chat with the given user": "Opens chat with the given user",
490492
"Unable to find Matrix ID for phone number": "Unable to find Matrix ID for phone number",
491493
"Sends a message to the given user": "Sends a message to the given user",

0 commit comments

Comments
 (0)