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

Commit

Permalink
Pass around MatrixClients instead of using MatrixClientPeg (#11000)
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored May 30, 2023
1 parent aa5a2e1 commit 938aefc
Show file tree
Hide file tree
Showing 28 changed files with 176 additions and 141 deletions.
55 changes: 27 additions & 28 deletions src/AddThreepid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

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

import { MatrixClientPeg } from "./MatrixClientPeg";
import Modal from "./Modal";
import { _t, UserFriendlyError } from "./languageHandler";
import IdentityAuthClient from "./IdentityAuthClient";
import { SSOAuthEntry } from "./components/views/auth/InteractiveAuthEntryComponents";
import InteractiveAuthDialog from "./components/views/dialogs/InteractiveAuthDialog";

function getIdServerDomain(): string {
const idBaseUrl = MatrixClientPeg.get().getIdentityServerUrl(true);
function getIdServerDomain(matrixClient: MatrixClient): string {
const idBaseUrl = matrixClient.getIdentityServerUrl(true);
if (!idBaseUrl) {
throw new UserFriendlyError("Identity server not set");
}
Expand Down Expand Up @@ -55,11 +54,11 @@ export type Binding = {
export default class AddThreepid {
private sessionId: string;
private submitUrl?: string;
private clientSecret: string;
private bind = false;
private readonly clientSecret: string;

public constructor() {
this.clientSecret = MatrixClientPeg.get().generateClientSecret();
public constructor(private readonly matrixClient: MatrixClient) {
this.clientSecret = matrixClient.generateClientSecret();
}

/**
Expand All @@ -70,7 +69,7 @@ export default class AddThreepid {
*/
public async addEmailAddress(emailAddress: string): Promise<IRequestTokenResponse> {
try {
const res = await MatrixClientPeg.get().requestAdd3pidEmailToken(emailAddress, this.clientSecret, 1);
const res = await this.matrixClient.requestAdd3pidEmailToken(emailAddress, this.clientSecret, 1);
this.sessionId = res.sid;
return res;
} catch (err) {
Expand All @@ -90,12 +89,12 @@ export default class AddThreepid {
*/
public async bindEmailAddress(emailAddress: string): Promise<IRequestTokenResponse> {
this.bind = true;
if (await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind()) {
if (await this.matrixClient.doesServerSupportSeparateAddAndBind()) {
// For separate bind, request a token directly from the IS.
const authClient = new IdentityAuthClient();
const identityAccessToken = (await authClient.getAccessToken()) ?? undefined;
try {
const res = await MatrixClientPeg.get().requestEmailToken(
const res = await this.matrixClient.requestEmailToken(
emailAddress,
this.clientSecret,
1,
Expand Down Expand Up @@ -126,7 +125,7 @@ export default class AddThreepid {
*/
public async addMsisdn(phoneCountry: string, phoneNumber: string): Promise<IRequestMsisdnTokenResponse> {
try {
const res = await MatrixClientPeg.get().requestAdd3pidMsisdnToken(
const res = await this.matrixClient.requestAdd3pidMsisdnToken(
phoneCountry,
phoneNumber,
this.clientSecret,
Expand All @@ -153,12 +152,12 @@ export default class AddThreepid {
*/
public async bindMsisdn(phoneCountry: string, phoneNumber: string): Promise<IRequestMsisdnTokenResponse> {
this.bind = true;
if (await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind()) {
if (await this.matrixClient.doesServerSupportSeparateAddAndBind()) {
// For separate bind, request a token directly from the IS.
const authClient = new IdentityAuthClient();
const identityAccessToken = (await authClient.getAccessToken()) ?? undefined;
try {
const res = await MatrixClientPeg.get().requestMsisdnToken(
const res = await this.matrixClient.requestMsisdnToken(
phoneCountry,
phoneNumber,
this.clientSecret,
Expand Down Expand Up @@ -189,17 +188,17 @@ export default class AddThreepid {
*/
public async checkEmailLinkClicked(): Promise<[success?: boolean, result?: IAuthData | Error | null]> {
try {
if (await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind()) {
if (await this.matrixClient.doesServerSupportSeparateAddAndBind()) {
if (this.bind) {
const authClient = new IdentityAuthClient();
const identityAccessToken = await authClient.getAccessToken();
if (!identityAccessToken) {
throw new UserFriendlyError("No identity access token found");
}
await MatrixClientPeg.get().bindThreePid({
await this.matrixClient.bindThreePid({
sid: this.sessionId,
client_secret: this.clientSecret,
id_server: getIdServerDomain(),
id_server: getIdServerDomain(this.matrixClient),
id_access_token: identityAccessToken,
});
} else {
Expand Down Expand Up @@ -233,7 +232,7 @@ export default class AddThreepid {
};
const { finished } = Modal.createDialog(InteractiveAuthDialog, {
title: _t("Add Email Address"),
matrixClient: MatrixClientPeg.get(),
matrixClient: this.matrixClient,
authData: err.data,
makeRequest: this.makeAddThreepidOnlyRequest,
aestheticsForStagePhases: {
Expand All @@ -245,11 +244,11 @@ export default class AddThreepid {
}
}
} else {
await MatrixClientPeg.get().addThreePid(
await this.matrixClient.addThreePid(
{
sid: this.sessionId,
client_secret: this.clientSecret,
id_server: getIdServerDomain(),
id_server: getIdServerDomain(this.matrixClient),
},
this.bind,
);
Expand All @@ -272,7 +271,7 @@ export default class AddThreepid {
* @return {Promise<Object>} Response from /3pid/add call (in current spec, an empty object)
*/
private makeAddThreepidOnlyRequest = (auth?: { type: string; session?: string }): Promise<{}> => {
return MatrixClientPeg.get().addThreePidOnly({
return this.matrixClient.addThreePidOnly({
sid: this.sessionId,
client_secret: this.clientSecret,
auth,
Expand All @@ -291,18 +290,18 @@ export default class AddThreepid {
msisdnToken: string,
): Promise<[success?: boolean, result?: IAuthData | Error | null] | undefined> {
const authClient = new IdentityAuthClient();
const supportsSeparateAddAndBind = await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind();
const supportsSeparateAddAndBind = await this.matrixClient.doesServerSupportSeparateAddAndBind();

let result: { success: boolean } | MatrixError;
if (this.submitUrl) {
result = await MatrixClientPeg.get().submitMsisdnTokenOtherUrl(
result = await this.matrixClient.submitMsisdnTokenOtherUrl(
this.submitUrl,
this.sessionId,
this.clientSecret,
msisdnToken,
);
} else if (this.bind || !supportsSeparateAddAndBind) {
result = await MatrixClientPeg.get().submitMsisdnToken(
result = await this.matrixClient.submitMsisdnToken(
this.sessionId,
this.clientSecret,
msisdnToken,
Expand All @@ -317,10 +316,10 @@ export default class AddThreepid {

if (supportsSeparateAddAndBind) {
if (this.bind) {
await MatrixClientPeg.get().bindThreePid({
await this.matrixClient.bindThreePid({
sid: this.sessionId,
client_secret: this.clientSecret,
id_server: getIdServerDomain(),
id_server: getIdServerDomain(this.matrixClient),
id_access_token: await authClient.getAccessToken(),
});
} else {
Expand Down Expand Up @@ -354,7 +353,7 @@ export default class AddThreepid {
};
const { finished } = Modal.createDialog(InteractiveAuthDialog, {
title: _t("Add Phone Number"),
matrixClient: MatrixClientPeg.get(),
matrixClient: this.matrixClient,
authData: err.data,
makeRequest: this.makeAddThreepidOnlyRequest,
aestheticsForStagePhases: {
Expand All @@ -366,11 +365,11 @@ export default class AddThreepid {
}
}
} else {
await MatrixClientPeg.get().addThreePid(
await this.matrixClient.addThreePid(
{
sid: this.sessionId,
client_secret: this.clientSecret,
id_server: getIdServerDomain(),
id_server: getIdServerDomain(this.matrixClient),
},
this.bind,
);
Expand Down
42 changes: 20 additions & 22 deletions src/Resend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
import { MatrixEvent, EventStatus } from "matrix-js-sdk/src/models/event";
import { Room } from "matrix-js-sdk/src/models/room";
import { logger } from "matrix-js-sdk/src/logger";
import { MatrixClient } from "matrix-js-sdk/src/matrix";

import { MatrixClientPeg } from "./MatrixClientPeg";
import dis from "./dispatcher/dispatcher";

export default class Resend {
Expand All @@ -30,7 +30,7 @@ export default class Resend {
return ev.status === EventStatus.NOT_SENT;
})
.map(function (event: MatrixEvent) {
return Resend.resend(event);
return Resend.resend(room.client, event);
}),
);
}
Expand All @@ -41,30 +41,28 @@ export default class Resend {
return ev.status === EventStatus.NOT_SENT;
})
.forEach(function (event: MatrixEvent) {
Resend.removeFromQueue(event);
Resend.removeFromQueue(room.client, event);
});
}

public static resend(event: MatrixEvent): Promise<void> {
const room = MatrixClientPeg.get().getRoom(event.getRoomId())!;
return MatrixClientPeg.get()
.resendEvent(event, room)
.then(
function (res) {
dis.dispatch({
action: "message_sent",
event: event,
});
},
function (err: Error) {
// XXX: temporary logging to try to diagnose
// https://github.com/vector-im/element-web/issues/3148
logger.log("Resend got send failure: " + err.name + "(" + err + ")");
},
);
public static resend(client: MatrixClient, event: MatrixEvent): Promise<void> {
const room = client.getRoom(event.getRoomId())!;
return client.resendEvent(event, room).then(
function (res) {
dis.dispatch({
action: "message_sent",
event: event,
});
},
function (err: Error) {
// XXX: temporary logging to try to diagnose
// https://github.com/vector-im/element-web/issues/3148
logger.log("Resend got send failure: " + err.name + "(" + err + ")");
},
);
}

public static removeFromQueue(event: MatrixEvent): void {
MatrixClientPeg.get().cancelPendingEvent(event);
public static removeFromQueue(client: MatrixClient, event: MatrixEvent): void {
client.cancelPendingEvent(event);
}
}
5 changes: 2 additions & 3 deletions src/RoomInvite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { logger } from "matrix-js-sdk/src/logger";
import { EventType } from "matrix-js-sdk/src/@types/event";
import { MatrixClient } from "matrix-js-sdk/src/matrix";

import { MatrixClientPeg } from "./MatrixClientPeg";
import MultiInviter, { CompletionStates } from "./utils/MultiInviter";
import Modal from "./Modal";
import { _t } from "./languageHandler";
Expand Down Expand Up @@ -115,7 +114,7 @@ export function inviteUsersToRoom(
): Promise<void> {
return inviteMultipleToRoom(client, roomId, userIds, sendSharedHistoryKeys, progressCallback)
.then((result) => {
const room = MatrixClientPeg.get().getRoom(roomId)!;
const room = client.getRoom(roomId)!;
showAnyInviteErrors(result.states, room, result.inviter);
})
.catch((err) => {
Expand Down Expand Up @@ -153,7 +152,7 @@ export function showAnyInviteErrors(
}
}

const cli = MatrixClientPeg.get();
const cli = room.client;
if (errorList.length > 0) {
// React 16 doesn't let us use `errorList.join(<br />)` anymore, so this is our solution
const description = (
Expand Down
2 changes: 1 addition & 1 deletion src/Rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function getDisplayAliasForAliasSet(canonicalAlias: string | null, altAli
export function guessAndSetDMRoom(room: Room, isDirect: boolean): Promise<void> {
let newTarget;
if (isDirect) {
const guessedUserId = guessDMRoomTargetId(room, MatrixClientPeg.get().getUserId()!);
const guessedUserId = guessDMRoomTargetId(room, room.client.getSafeUserId());
newTarget = guessedUserId;
} else {
newTarget = null;
Expand Down
Loading

0 comments on commit 938aefc

Please sign in to comment.