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

Commit 417a72b

Browse files
committed
Update the remainder of the module API interface
1 parent 7c33b16 commit 417a72b

File tree

3 files changed

+71
-20
lines changed

3 files changed

+71
-20
lines changed

src/modules/ProxiedModuleApi.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,24 @@ limitations under the License.
1717
import { ModuleApi } from "@matrix-org/react-sdk-module-api/lib/ModuleApi";
1818
import { TranslationStringsObject } from "@matrix-org/react-sdk-module-api/lib/types/translations";
1919
import { Optional } from "matrix-events-sdk";
20-
import { _t } from "../languageHandler";
2120
import { DialogProps } from "@matrix-org/react-sdk-module-api/lib/components/DialogContent";
22-
import Modal from "../Modal";
23-
import { ModuleUiDialog } from "../components/views/dialogs/ModuleUiDialog";
2421
import React from "react";
25-
import { AccountInformation } from "@matrix-org/react-sdk-module-api/lib/types/credentials";
22+
import { AccountAuthInfo } from "@matrix-org/react-sdk-module-api/lib/types/AccountAuthInfo";
23+
import { PlainSubstitution } from "@matrix-org/react-sdk-module-api/src/types/translations";
2624
import * as Matrix from "matrix-js-sdk/src/matrix";
25+
26+
import Modal from "../Modal";
27+
import { _t } from "../languageHandler";
28+
import { ModuleUiDialog } from "../components/views/dialogs/ModuleUiDialog";
2729
import SdkConfig from "../SdkConfig";
2830
import PlatformPeg from "../PlatformPeg";
2931
import { doSetLoggedIn } from "../Lifecycle";
3032
import dispatcher from "../dispatcher/dispatcher";
31-
import { PlainSubstitution } from "@matrix-org/react-sdk-module-api/src/types/translations";
33+
import { navigateToPermalink } from "../utils/permalinks/navigator";
34+
import { parsePermalink } from "../utils/permalinks/Permalinks";
35+
import { MatrixClientPeg } from "../MatrixClientPeg";
36+
import { getCachedRoomIDForAlias } from "../RoomAliasCache";
37+
import { Action } from "../dispatcher/actions";
3238

3339
export class ProxiedModuleApi implements ModuleApi {
3440
private cachedTranslations: Optional<TranslationStringsObject>;
@@ -59,7 +65,7 @@ export class ProxiedModuleApi implements ModuleApi {
5965
});
6066
}
6167

62-
public async registerAccount(username: string, password: string, displayName?: string): Promise<AccountInformation> {
68+
public async registerSimpleAccount(username: string, password: string, displayName?: string): Promise<AccountAuthInfo> {
6369
const hsUrl = SdkConfig.get("validated_server_config").hsUrl;
6470
const client = Matrix.createClient({ baseUrl: hsUrl });
6571
const req = {
@@ -95,23 +101,41 @@ export class ProxiedModuleApi implements ModuleApi {
95101
};
96102
}
97103

98-
public async useAccount(accountInfo: AccountInformation): Promise<void> {
104+
public async overwriteAccountAuth(accountInfo: AccountAuthInfo): Promise<void> {
99105
await doSetLoggedIn({
100106
...accountInfo,
101107
guest: false,
102108
}, true);
103109
}
104110

105-
public async switchToRoom(roomId: string, andJoin?: boolean): Promise<void> {
106-
dispatcher.dispatch({
107-
action: "view_room",
108-
room_id: roomId,
109-
});
110-
111-
if (andJoin) {
111+
public async navigatePermalink(uri: string, andJoin?: boolean): Promise<void> {
112+
navigateToPermalink(uri);
113+
114+
const parts = parsePermalink(uri);
115+
if (parts.roomIdOrAlias)
116+
if (parts.roomIdOrAlias && andJoin) {
117+
let roomId = parts.roomIdOrAlias;
118+
let servers = parts.viaServers;
119+
if (roomId.startsWith("#")) {
120+
roomId = getCachedRoomIDForAlias(parts.roomIdOrAlias);
121+
if (!roomId) {
122+
// alias resolution failed
123+
const result = await MatrixClientPeg.get().getRoomIdForAlias(parts.roomIdOrAlias);
124+
roomId = result.room_id;
125+
if (!servers) servers = result.servers; // use provided servers first, if available
126+
}
127+
}
112128
dispatcher.dispatch({
113-
action: "join_room",
129+
action: Action.ViewRoom,
130+
room_id: roomId,
131+
via_servers: servers,
114132
});
133+
134+
if (andJoin) {
135+
dispatcher.dispatch({
136+
action: Action.JoinRoom,
137+
});
138+
}
115139
}
116140
}
117141

src/stores/widgets/StopGapWidgetDriver.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import { tryTransformPermalinkToLocalHref } from "../../utils/permalinks/Permali
5050
import SettingsStore from "../../settings/SettingsStore";
5151
import { RoomViewStore } from "../RoomViewStore";
5252
import { ElementWidgetCapabilities } from "./ElementWidgetCapabilities";
53+
import { navigateToPermalink } from "../../utils/permalinks/navigator";
5354

5455
// TODO: Purge this from the universe
5556

@@ -280,10 +281,6 @@ export class StopGapWidgetDriver extends WidgetDriver {
280281
}
281282

282283
public async navigate(uri: string): Promise<void> {
283-
const localUri = tryTransformPermalinkToLocalHref(uri);
284-
if (!localUri || localUri === uri) { // parse failure can lead to an unmodified URL
285-
throw new Error("Failed to transform URI");
286-
}
287-
window.location.hash = localUri; // it'll just be a fragment
284+
navigateToPermalink(uri);
288285
}
289286
}

src/utils/permalinks/navigator.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { tryTransformPermalinkToLocalHref } from "./Permalinks";
18+
19+
/**
20+
* Converts a permalink to a local HREF and navigates accordingly. Throws if the permalink
21+
* cannot be transformed.
22+
* @param uri The permalink to navigate to.
23+
*/
24+
export function navigateToPermalink(uri: string): void {
25+
const localUri = tryTransformPermalinkToLocalHref(uri);
26+
if (!localUri || localUri === uri) { // parse failure can lead to an unmodified URL
27+
throw new Error("Failed to transform URI");
28+
}
29+
window.location.hash = localUri; // it'll just be a fragment
30+
}

0 commit comments

Comments
 (0)