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

Commit

Permalink
Fix start DM via right panel
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 committed Mar 3, 2023
1 parent 368dea7 commit bc93548
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
26 changes: 18 additions & 8 deletions src/components/views/right_panel/UserInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ export const getE2EStatus = (cli: MatrixClient, userId: string, devices: IDevice
return anyDeviceUnverified ? E2EStatus.Warning : E2EStatus.Verified;
};

async function openDmForUser(matrixClient: MatrixClient, user: RoomMember): Promise<void> {
/**
* Converts the member to a DirectoryMember and starts a DM with them.
*/
async function openDmForUser(matrixClient: MatrixClient, user: Member): Promise<void> {
const avatarUrl = user instanceof User ? user.avatarUrl : user.getMxcAvatarUrl();
const startDmUser = new DirectoryMember({
user_id: user.userId,
display_name: user.rawDisplayName,
avatar_url: user.getMxcAvatarUrl(),
avatar_url: avatarUrl,
});
startDmOnFirstMessage(matrixClient, [startDmUser]);
}
Expand Down Expand Up @@ -310,7 +314,7 @@ function DevicesSection({
);
}

const MessageButton = ({ member }: { member: RoomMember }): JSX.Element => {
const MessageButton = ({ member }: { member: Member }): JSX.Element => {
const cli = useContext(MatrixClientContext);
const [busy, setBusy] = useState(false);

Expand All @@ -332,7 +336,7 @@ const MessageButton = ({ member }: { member: RoomMember }): JSX.Element => {
};

export const UserOptionsSection: React.FC<{
member: RoomMember;
member: Member;
isIgnored: boolean;
canInvite: boolean;
isSpace?: boolean;
Expand Down Expand Up @@ -360,8 +364,9 @@ export const UserOptionsSection: React.FC<{
}, [cli, member]);

const ignore = useCallback(async () => {
const name = (member instanceof User ? member.displayName : member.name) || member.userId;
const { finished } = Modal.createDialog(QuestionDialog, {
title: _t("Ignore %(user)s", { user: member.name }),
title: _t("Ignore %(user)s", { user: name }),
description: (
<div>
{_t(
Expand Down Expand Up @@ -394,7 +399,7 @@ export const UserOptionsSection: React.FC<{
</AccessibleButton>
);

if (member.roomId && !isSpace) {
if (member instanceof RoomMember && member.roomId && !isSpace) {
const onReadReceiptButton = function (): void {
const room = cli.getRoom(member.roomId);
dis.dispatch<ViewRoomPayload>({
Expand All @@ -415,7 +420,7 @@ export const UserOptionsSection: React.FC<{
});
};

const room = cli.getRoom(member.roomId);
const room = member instanceof RoomMember ? cli.getRoom(member.roomId) : undefined;
if (room?.getEventReadUpTo(member.userId)) {
readReceiptButton = (
<AccessibleButton kind="link" onClick={onReadReceiptButton} className="mx_UserInfo_field">
Expand All @@ -431,7 +436,12 @@ export const UserOptionsSection: React.FC<{
);
}

if (canInvite && (member?.membership ?? "leave") === "leave" && shouldShowComponent(UIComponent.InviteUsers)) {
if (
member instanceof RoomMember &&
canInvite &&
(member?.membership ?? "leave") === "leave" &&
shouldShowComponent(UIComponent.InviteUsers)
) {
const roomId = member && member.roomId ? member.roomId : SdkContextClass.instance.roomViewStore.getRoomId();
const onInviteUserButton = async (ev: ButtonEvent): Promise<void> => {
try {
Expand Down
28 changes: 26 additions & 2 deletions test/components/views/right_panel/UserInfo-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ import MultiInviter from "../../../../src/utils/MultiInviter";
import * as mockVerification from "../../../../src/verification";
import Modal from "../../../../src/Modal";
import { E2EStatus } from "../../../../src/utils/ShieldUtils";
import { DirectoryMember, startDmOnFirstMessage } from "../../../../src/utils/direct-messages";

jest.mock("../../../../src/utils/direct-messages", () => ({
...jest.requireActual("../../../../src/utils/direct-messages"),
startDmOnFirstMessage: jest.fn(),
}));

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

Expand Down Expand Up @@ -121,7 +127,7 @@ const mockClient = mocked({
setPowerLevel: jest.fn(),
} as unknown as MatrixClient);

const defaultUserId = "@test:test";
const defaultUserId = "@user:example.com";
const defaultUser = new User(defaultUserId);

beforeEach(() => {
Expand Down Expand Up @@ -554,7 +560,7 @@ describe("<UserOptionsSection />", () => {
inviteSpy.mockRejectedValue({ this: "could be anything" });

// render the component and click the button
renderComponent({ canInvite: true, member: { ...member, roomId: null } });
renderComponent({ canInvite: true, member: new RoomMember(null, defaultUserId) });
const inviteButton = screen.getByRole("button", { name: /invite/i });
expect(inviteButton).toBeInTheDocument();
await userEvent.click(inviteButton);
Expand Down Expand Up @@ -612,6 +618,24 @@ describe("<UserOptionsSection />", () => {
await userEvent.click(screen.getByRole("button", { name: "Unignore" }));
expect(mockClient.setIgnoredUsers).toHaveBeenCalledWith([]);
});

it.each([
["for a RoomMember", member, member.getMxcAvatarUrl()],
["for a User", defaultUser, defaultUser.avatarUrl],
])(
"clicking »message« %s should start a DM",
async (test: string, member: RoomMember | User, expectedAvatarUrl: string) => {
renderComponent({ member });
await userEvent.click(screen.getByText("Message"));
expect(startDmOnFirstMessage).toHaveBeenCalledWith(mockClient, [
new DirectoryMember({
user_id: member.userId,
display_name: member.rawDisplayName,
avatar_url: expectedAvatarUrl,
}),
]);
},
);
});

describe("<PowerLevelEditor />", () => {
Expand Down

0 comments on commit bc93548

Please sign in to comment.