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

Commit 938aefc

Browse files
authored
Pass around MatrixClients instead of using MatrixClientPeg (#11000)
1 parent aa5a2e1 commit 938aefc

28 files changed

+176
-141
lines changed

src/AddThreepid.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ See the License for the specific language governing permissions and
1616
limitations under the License.
1717
*/
1818

19-
import { IAuthData, IRequestMsisdnTokenResponse, IRequestTokenResponse } from "matrix-js-sdk/src/matrix";
19+
import { IAuthData, IRequestMsisdnTokenResponse, IRequestTokenResponse, MatrixClient } from "matrix-js-sdk/src/matrix";
2020
import { MatrixError, HTTPError } from "matrix-js-sdk/src/matrix";
2121

22-
import { MatrixClientPeg } from "./MatrixClientPeg";
2322
import Modal from "./Modal";
2423
import { _t, UserFriendlyError } from "./languageHandler";
2524
import IdentityAuthClient from "./IdentityAuthClient";
2625
import { SSOAuthEntry } from "./components/views/auth/InteractiveAuthEntryComponents";
2726
import InteractiveAuthDialog from "./components/views/dialogs/InteractiveAuthDialog";
2827

29-
function getIdServerDomain(): string {
30-
const idBaseUrl = MatrixClientPeg.get().getIdentityServerUrl(true);
28+
function getIdServerDomain(matrixClient: MatrixClient): string {
29+
const idBaseUrl = matrixClient.getIdentityServerUrl(true);
3130
if (!idBaseUrl) {
3231
throw new UserFriendlyError("Identity server not set");
3332
}
@@ -55,11 +54,11 @@ export type Binding = {
5554
export default class AddThreepid {
5655
private sessionId: string;
5756
private submitUrl?: string;
58-
private clientSecret: string;
5957
private bind = false;
58+
private readonly clientSecret: string;
6059

61-
public constructor() {
62-
this.clientSecret = MatrixClientPeg.get().generateClientSecret();
60+
public constructor(private readonly matrixClient: MatrixClient) {
61+
this.clientSecret = matrixClient.generateClientSecret();
6362
}
6463

6564
/**
@@ -70,7 +69,7 @@ export default class AddThreepid {
7069
*/
7170
public async addEmailAddress(emailAddress: string): Promise<IRequestTokenResponse> {
7271
try {
73-
const res = await MatrixClientPeg.get().requestAdd3pidEmailToken(emailAddress, this.clientSecret, 1);
72+
const res = await this.matrixClient.requestAdd3pidEmailToken(emailAddress, this.clientSecret, 1);
7473
this.sessionId = res.sid;
7574
return res;
7675
} catch (err) {
@@ -90,12 +89,12 @@ export default class AddThreepid {
9089
*/
9190
public async bindEmailAddress(emailAddress: string): Promise<IRequestTokenResponse> {
9291
this.bind = true;
93-
if (await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind()) {
92+
if (await this.matrixClient.doesServerSupportSeparateAddAndBind()) {
9493
// For separate bind, request a token directly from the IS.
9594
const authClient = new IdentityAuthClient();
9695
const identityAccessToken = (await authClient.getAccessToken()) ?? undefined;
9796
try {
98-
const res = await MatrixClientPeg.get().requestEmailToken(
97+
const res = await this.matrixClient.requestEmailToken(
9998
emailAddress,
10099
this.clientSecret,
101100
1,
@@ -126,7 +125,7 @@ export default class AddThreepid {
126125
*/
127126
public async addMsisdn(phoneCountry: string, phoneNumber: string): Promise<IRequestMsisdnTokenResponse> {
128127
try {
129-
const res = await MatrixClientPeg.get().requestAdd3pidMsisdnToken(
128+
const res = await this.matrixClient.requestAdd3pidMsisdnToken(
130129
phoneCountry,
131130
phoneNumber,
132131
this.clientSecret,
@@ -153,12 +152,12 @@ export default class AddThreepid {
153152
*/
154153
public async bindMsisdn(phoneCountry: string, phoneNumber: string): Promise<IRequestMsisdnTokenResponse> {
155154
this.bind = true;
156-
if (await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind()) {
155+
if (await this.matrixClient.doesServerSupportSeparateAddAndBind()) {
157156
// For separate bind, request a token directly from the IS.
158157
const authClient = new IdentityAuthClient();
159158
const identityAccessToken = (await authClient.getAccessToken()) ?? undefined;
160159
try {
161-
const res = await MatrixClientPeg.get().requestMsisdnToken(
160+
const res = await this.matrixClient.requestMsisdnToken(
162161
phoneCountry,
163162
phoneNumber,
164163
this.clientSecret,
@@ -189,17 +188,17 @@ export default class AddThreepid {
189188
*/
190189
public async checkEmailLinkClicked(): Promise<[success?: boolean, result?: IAuthData | Error | null]> {
191190
try {
192-
if (await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind()) {
191+
if (await this.matrixClient.doesServerSupportSeparateAddAndBind()) {
193192
if (this.bind) {
194193
const authClient = new IdentityAuthClient();
195194
const identityAccessToken = await authClient.getAccessToken();
196195
if (!identityAccessToken) {
197196
throw new UserFriendlyError("No identity access token found");
198197
}
199-
await MatrixClientPeg.get().bindThreePid({
198+
await this.matrixClient.bindThreePid({
200199
sid: this.sessionId,
201200
client_secret: this.clientSecret,
202-
id_server: getIdServerDomain(),
201+
id_server: getIdServerDomain(this.matrixClient),
203202
id_access_token: identityAccessToken,
204203
});
205204
} else {
@@ -233,7 +232,7 @@ export default class AddThreepid {
233232
};
234233
const { finished } = Modal.createDialog(InteractiveAuthDialog, {
235234
title: _t("Add Email Address"),
236-
matrixClient: MatrixClientPeg.get(),
235+
matrixClient: this.matrixClient,
237236
authData: err.data,
238237
makeRequest: this.makeAddThreepidOnlyRequest,
239238
aestheticsForStagePhases: {
@@ -245,11 +244,11 @@ export default class AddThreepid {
245244
}
246245
}
247246
} else {
248-
await MatrixClientPeg.get().addThreePid(
247+
await this.matrixClient.addThreePid(
249248
{
250249
sid: this.sessionId,
251250
client_secret: this.clientSecret,
252-
id_server: getIdServerDomain(),
251+
id_server: getIdServerDomain(this.matrixClient),
253252
},
254253
this.bind,
255254
);
@@ -272,7 +271,7 @@ export default class AddThreepid {
272271
* @return {Promise<Object>} Response from /3pid/add call (in current spec, an empty object)
273272
*/
274273
private makeAddThreepidOnlyRequest = (auth?: { type: string; session?: string }): Promise<{}> => {
275-
return MatrixClientPeg.get().addThreePidOnly({
274+
return this.matrixClient.addThreePidOnly({
276275
sid: this.sessionId,
277276
client_secret: this.clientSecret,
278277
auth,
@@ -291,18 +290,18 @@ export default class AddThreepid {
291290
msisdnToken: string,
292291
): Promise<[success?: boolean, result?: IAuthData | Error | null] | undefined> {
293292
const authClient = new IdentityAuthClient();
294-
const supportsSeparateAddAndBind = await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind();
293+
const supportsSeparateAddAndBind = await this.matrixClient.doesServerSupportSeparateAddAndBind();
295294

296295
let result: { success: boolean } | MatrixError;
297296
if (this.submitUrl) {
298-
result = await MatrixClientPeg.get().submitMsisdnTokenOtherUrl(
297+
result = await this.matrixClient.submitMsisdnTokenOtherUrl(
299298
this.submitUrl,
300299
this.sessionId,
301300
this.clientSecret,
302301
msisdnToken,
303302
);
304303
} else if (this.bind || !supportsSeparateAddAndBind) {
305-
result = await MatrixClientPeg.get().submitMsisdnToken(
304+
result = await this.matrixClient.submitMsisdnToken(
306305
this.sessionId,
307306
this.clientSecret,
308307
msisdnToken,
@@ -317,10 +316,10 @@ export default class AddThreepid {
317316

318317
if (supportsSeparateAddAndBind) {
319318
if (this.bind) {
320-
await MatrixClientPeg.get().bindThreePid({
319+
await this.matrixClient.bindThreePid({
321320
sid: this.sessionId,
322321
client_secret: this.clientSecret,
323-
id_server: getIdServerDomain(),
322+
id_server: getIdServerDomain(this.matrixClient),
324323
id_access_token: await authClient.getAccessToken(),
325324
});
326325
} else {
@@ -354,7 +353,7 @@ export default class AddThreepid {
354353
};
355354
const { finished } = Modal.createDialog(InteractiveAuthDialog, {
356355
title: _t("Add Phone Number"),
357-
matrixClient: MatrixClientPeg.get(),
356+
matrixClient: this.matrixClient,
358357
authData: err.data,
359358
makeRequest: this.makeAddThreepidOnlyRequest,
360359
aestheticsForStagePhases: {
@@ -366,11 +365,11 @@ export default class AddThreepid {
366365
}
367366
}
368367
} else {
369-
await MatrixClientPeg.get().addThreePid(
368+
await this.matrixClient.addThreePid(
370369
{
371370
sid: this.sessionId,
372371
client_secret: this.clientSecret,
373-
id_server: getIdServerDomain(),
372+
id_server: getIdServerDomain(this.matrixClient),
374373
},
375374
this.bind,
376375
);

src/Resend.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ limitations under the License.
1717
import { MatrixEvent, EventStatus } from "matrix-js-sdk/src/models/event";
1818
import { Room } from "matrix-js-sdk/src/models/room";
1919
import { logger } from "matrix-js-sdk/src/logger";
20+
import { MatrixClient } from "matrix-js-sdk/src/matrix";
2021

21-
import { MatrixClientPeg } from "./MatrixClientPeg";
2222
import dis from "./dispatcher/dispatcher";
2323

2424
export default class Resend {
@@ -30,7 +30,7 @@ export default class Resend {
3030
return ev.status === EventStatus.NOT_SENT;
3131
})
3232
.map(function (event: MatrixEvent) {
33-
return Resend.resend(event);
33+
return Resend.resend(room.client, event);
3434
}),
3535
);
3636
}
@@ -41,30 +41,28 @@ export default class Resend {
4141
return ev.status === EventStatus.NOT_SENT;
4242
})
4343
.forEach(function (event: MatrixEvent) {
44-
Resend.removeFromQueue(event);
44+
Resend.removeFromQueue(room.client, event);
4545
});
4646
}
4747

48-
public static resend(event: MatrixEvent): Promise<void> {
49-
const room = MatrixClientPeg.get().getRoom(event.getRoomId())!;
50-
return MatrixClientPeg.get()
51-
.resendEvent(event, room)
52-
.then(
53-
function (res) {
54-
dis.dispatch({
55-
action: "message_sent",
56-
event: event,
57-
});
58-
},
59-
function (err: Error) {
60-
// XXX: temporary logging to try to diagnose
61-
// https://github.com/vector-im/element-web/issues/3148
62-
logger.log("Resend got send failure: " + err.name + "(" + err + ")");
63-
},
64-
);
48+
public static resend(client: MatrixClient, event: MatrixEvent): Promise<void> {
49+
const room = client.getRoom(event.getRoomId())!;
50+
return client.resendEvent(event, room).then(
51+
function (res) {
52+
dis.dispatch({
53+
action: "message_sent",
54+
event: event,
55+
});
56+
},
57+
function (err: Error) {
58+
// XXX: temporary logging to try to diagnose
59+
// https://github.com/vector-im/element-web/issues/3148
60+
logger.log("Resend got send failure: " + err.name + "(" + err + ")");
61+
},
62+
);
6563
}
6664

67-
public static removeFromQueue(event: MatrixEvent): void {
68-
MatrixClientPeg.get().cancelPendingEvent(event);
65+
public static removeFromQueue(client: MatrixClient, event: MatrixEvent): void {
66+
client.cancelPendingEvent(event);
6967
}
7068
}

src/RoomInvite.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { logger } from "matrix-js-sdk/src/logger";
2222
import { EventType } from "matrix-js-sdk/src/@types/event";
2323
import { MatrixClient } from "matrix-js-sdk/src/matrix";
2424

25-
import { MatrixClientPeg } from "./MatrixClientPeg";
2625
import MultiInviter, { CompletionStates } from "./utils/MultiInviter";
2726
import Modal from "./Modal";
2827
import { _t } from "./languageHandler";
@@ -115,7 +114,7 @@ export function inviteUsersToRoom(
115114
): Promise<void> {
116115
return inviteMultipleToRoom(client, roomId, userIds, sendSharedHistoryKeys, progressCallback)
117116
.then((result) => {
118-
const room = MatrixClientPeg.get().getRoom(roomId)!;
117+
const room = client.getRoom(roomId)!;
119118
showAnyInviteErrors(result.states, room, result.inviter);
120119
})
121120
.catch((err) => {
@@ -153,7 +152,7 @@ export function showAnyInviteErrors(
153152
}
154153
}
155154

156-
const cli = MatrixClientPeg.get();
155+
const cli = room.client;
157156
if (errorList.length > 0) {
158157
// React 16 doesn't let us use `errorList.join(<br />)` anymore, so this is our solution
159158
const description = (

src/Rooms.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function getDisplayAliasForAliasSet(canonicalAlias: string | null, altAli
4646
export function guessAndSetDMRoom(room: Room, isDirect: boolean): Promise<void> {
4747
let newTarget;
4848
if (isDirect) {
49-
const guessedUserId = guessDMRoomTargetId(room, MatrixClientPeg.get().getUserId()!);
49+
const guessedUserId = guessDMRoomTargetId(room, room.client.getSafeUserId());
5050
newTarget = guessedUserId;
5151
} else {
5252
newTarget = null;

0 commit comments

Comments
 (0)