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

Commit a1f4b64

Browse files
committed
Fix start DM via right panel
1 parent 4870867 commit a1f4b64

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/components/views/right_panel/UserInfo.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import PosthogTrackers from "../../../PosthogTrackers";
7979
import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
8080
import { DirectoryMember, startDmOnFirstMessage } from "../../../utils/direct-messages";
8181
import { SdkContextClass } from "../../../contexts/SDKContext";
82+
import { extractAvatarUrl } from "../../../utils/user";
8283

8384
export interface IDevice extends DeviceInfo {
8485
ambiguous?: boolean;
@@ -121,11 +122,14 @@ export const getE2EStatus = (cli: MatrixClient, userId: string, devices: IDevice
121122
return anyDeviceUnverified ? E2EStatus.Warning : E2EStatus.Verified;
122123
};
123124

124-
async function openDmForUser(matrixClient: MatrixClient, user: RoomMember): Promise<void> {
125+
/**
126+
* Converts the member to a DirectoryMember and starts a DM with them.
127+
*/
128+
async function openDmForUser(matrixClient: MatrixClient, user: Member): Promise<void> {
125129
const startDmUser = new DirectoryMember({
126130
user_id: user.userId,
127131
display_name: user.rawDisplayName,
128-
avatar_url: user.getMxcAvatarUrl(),
132+
avatar_url: extractAvatarUrl(user),
129133
});
130134
startDmOnFirstMessage(matrixClient, [startDmUser]);
131135
}

src/utils/user.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright 2023 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 { RoomMember, User } from "matrix-js-sdk/src/matrix";
18+
19+
export const extractAvatarUrl = (entity: User | RoomMember): string | undefined => {
20+
return entity instanceof User ? entity.avatarUrl : entity.getMxcAvatarUrl();
21+
};

test/components/views/right_panel/UserInfo-test.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ import MultiInviter from "../../../../src/utils/MultiInviter";
4343
import * as mockVerification from "../../../../src/verification";
4444
import Modal from "../../../../src/Modal";
4545
import { E2EStatus } from "../../../../src/utils/ShieldUtils";
46+
import { DirectoryMember, startDmOnFirstMessage } from "../../../../src/utils/direct-messages";
47+
48+
jest.mock("../../../../src/utils/direct-messages", () => ({
49+
...jest.requireActual("../../../../src/utils/direct-messages"),
50+
startDmOnFirstMessage: jest.fn(),
51+
}));
4652

4753
jest.mock("../../../../src/dispatcher/dispatcher");
4854

@@ -612,6 +618,24 @@ describe("<UserOptionsSection />", () => {
612618
await userEvent.click(screen.getByRole("button", { name: "Unignore" }));
613619
expect(mockClient.setIgnoredUsers).toHaveBeenCalledWith([]);
614620
});
621+
622+
it.each([
623+
["for a RoomMember", member, member.getMxcAvatarUrl()],
624+
["for a User", defaultUser, defaultUser.avatarUrl],
625+
])(
626+
"clicking »message« %s should start a DM",
627+
async (test: string, member: RoomMember | User, expectedAvatarUrl: string) => {
628+
renderComponent({ member });
629+
await userEvent.click(screen.getByText("Message"));
630+
expect(startDmOnFirstMessage).toHaveBeenCalledWith(mockClient, [
631+
new DirectoryMember({
632+
user_id: member.userId,
633+
display_name: member.rawDisplayName,
634+
avatar_url: expectedAvatarUrl,
635+
}),
636+
]);
637+
},
638+
);
615639
});
616640

617641
describe("<PowerLevelEditor />", () => {

0 commit comments

Comments
 (0)