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

Commit

Permalink
Wire up UserProfilesStores in permalinks
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 committed Mar 22, 2023
1 parent 79046b2 commit 7dbaea9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 26 deletions.
9 changes: 9 additions & 0 deletions src/hooks/usePermalinkMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ const determineUserId = (
return null;
};

/**
* Tries to determine a RoomMember.
*
* @param userId - User Id to get the member for
* @param targetRoom - permalink target room
* @returns RoomMember of the target room if it exists.
* If sharing at least one room with the user, then the result will be the profile fetched via API.
* null in all other cases.
*/
const determineMember = (userId: string, targetRoom: Room): RoomMember | null => {
const targetRoomMember = targetRoom.getMember(userId);

Expand Down
22 changes: 20 additions & 2 deletions test/components/views/elements/Pill-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { Action } from "../../../../src/dispatcher/actions";
import { ButtonEvent } from "../../../../src/components/views/elements/AccessibleButton";
import { SdkContextClass } from "../../../../src/contexts/SDKContext";

describe("<Pill>", () => {
let client: Mocked<MatrixClient>;
Expand All @@ -47,6 +48,7 @@ describe("<Pill>", () => {
let space1: Room;
const user1Id = "@user1:example.com";
const user2Id = "@user2:example.com";
const user3Id = "@user3:example.com";
let renderResult: RenderResult;
let pillParentClickHandler: (e: ButtonEvent) => void;

Expand All @@ -72,6 +74,7 @@ describe("<Pill>", () => {

beforeEach(() => {
client = mocked(stubClient());
SdkContextClass.instance.client = client;
DMRoomMap.makeShared();
room1 = new Room(room1Id, client, user1Id);
room1.name = "Room 1";
Expand All @@ -93,12 +96,13 @@ describe("<Pill>", () => {
room1.addLiveEvents([room1Message]);

room2 = new Room(room2Id, client, user1Id);
room2.currentState.setStateEvents([mkRoomMemberJoinEvent(user2Id, room2Id)]);
room2.name = "Room 2";

space1 = new Room(space1Id, client, client.getSafeUserId());
space1.name = "Space 1";

client.getRooms.mockReturnValue([room1, space1]);
client.getRooms.mockReturnValue([room1, room2, space1]);
client.getRoom.mockImplementation((roomId: string) => {
if (roomId === room1.roomId) return room1;
if (roomId === room2.roomId) return room2;
Expand Down Expand Up @@ -204,7 +208,7 @@ describe("<Pill>", () => {
});
});

it("should render the expected pill for a user not in the room", async () => {
it("should render the expected pill for a known user not in the room", async () => {
renderPill({
room: room1,
url: permalinkPrefix + user2Id,
Expand All @@ -218,6 +222,20 @@ describe("<Pill>", () => {
expect(renderResult.asFragment()).toMatchSnapshot();
});

it("should render the expected pill for an uknown user not in the room", async () => {
renderPill({
room: room1,
url: permalinkPrefix + user3Id,
});

// wait for profile query via API
await act(async () => {
await flushPromises();
});

expect(renderResult.asFragment()).toMatchSnapshot();
});

it("should not render anything if the type cannot be detected", () => {
renderPill({
url: permalinkPrefix,
Expand Down
70 changes: 46 additions & 24 deletions test/components/views/elements/__snapshots__/Pill-test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ exports[`<Pill> should render the expected pill for @room 1`] = `
</DocumentFragment>
`;
exports[`<Pill> should render the expected pill for a message in another room 1`] = `
exports[`<Pill> should render the expected pill for a known user not in the room 1`] = `
<DocumentFragment>
<div>
<bdi>
<a
class="mx_Pill mx_EventPill"
href="https://matrix.to/#/!room1:example.com/$123-456"
class="mx_Pill mx_UserPill"
href="https://matrix.to/#/@user2:example.com"
>
Message in
<span
aria-hidden="true"
class="mx_BaseAvatar"
Expand All @@ -82,7 +81,7 @@ exports[`<Pill> should render the expected pill for a message in another room 1`
class="mx_BaseAvatar_initial"
style="font-size: 10.4px; width: 16px; line-height: 16px;"
>
R
U
</span>
<img
alt=""
Expand All @@ -96,23 +95,23 @@ exports[`<Pill> should render the expected pill for a message in another room 1`
<span
class="mx_Pill_text"
>
Room 1
User 2
</span>
</a>
</bdi>
</div>
</DocumentFragment>
`;
exports[`<Pill> should render the expected pill for a message in the same room 1`] = `
exports[`<Pill> should render the expected pill for a message in another room 1`] = `
<DocumentFragment>
<div>
<bdi>
<a
class="mx_Pill mx_EventPill"
href="https://matrix.to/#/!room1:example.com/$123-456"
>
Message from
Message in
<span
aria-hidden="true"
class="mx_BaseAvatar"
Expand All @@ -123,7 +122,7 @@ exports[`<Pill> should render the expected pill for a message in the same room 1
class="mx_BaseAvatar_initial"
style="font-size: 10.4px; width: 16px; line-height: 16px;"
>
U
R
</span>
<img
alt=""
Expand All @@ -137,22 +136,23 @@ exports[`<Pill> should render the expected pill for a message in the same room 1
<span
class="mx_Pill_text"
>
User 1
Room 1
</span>
</a>
</bdi>
</div>
</DocumentFragment>
`;
exports[`<Pill> should render the expected pill for a room alias 1`] = `
exports[`<Pill> should render the expected pill for a message in the same room 1`] = `
<DocumentFragment>
<div>
<bdi>
<a
class="mx_Pill mx_RoomPill"
href="https://matrix.to/#/#room1:example.com"
class="mx_Pill mx_EventPill"
href="https://matrix.to/#/!room1:example.com/$123-456"
>
Message from
<span
aria-hidden="true"
class="mx_BaseAvatar"
Expand All @@ -163,7 +163,7 @@ exports[`<Pill> should render the expected pill for a room alias 1`] = `
class="mx_BaseAvatar_initial"
style="font-size: 10.4px; width: 16px; line-height: 16px;"
>
R
U
</span>
<img
alt=""
Expand All @@ -177,21 +177,21 @@ exports[`<Pill> should render the expected pill for a room alias 1`] = `
<span
class="mx_Pill_text"
>
Room 1
User 1
</span>
</a>
</bdi>
</div>
</DocumentFragment>
`;
exports[`<Pill> should render the expected pill for a space 1`] = `
exports[`<Pill> should render the expected pill for a room alias 1`] = `
<DocumentFragment>
<div>
<bdi>
<a
class="mx_Pill mx_RoomPill"
href="https://matrix.to/#/!space1:example.com"
href="https://matrix.to/#/#room1:example.com"
>
<span
aria-hidden="true"
Expand All @@ -203,7 +203,7 @@ exports[`<Pill> should render the expected pill for a space 1`] = `
class="mx_BaseAvatar_initial"
style="font-size: 10.4px; width: 16px; line-height: 16px;"
>
S
R
</span>
<img
alt=""
Expand All @@ -217,21 +217,21 @@ exports[`<Pill> should render the expected pill for a space 1`] = `
<span
class="mx_Pill_text"
>
Space 1
Room 1
</span>
</a>
</bdi>
</div>
</DocumentFragment>
`;
exports[`<Pill> should render the expected pill for a user not in the room 1`] = `
exports[`<Pill> should render the expected pill for a space 1`] = `
<DocumentFragment>
<div>
<bdi>
<a
class="mx_Pill mx_UserPill"
href="https://matrix.to/#/@user2:example.com"
class="mx_Pill mx_RoomPill"
href="https://matrix.to/#/!space1:example.com"
>
<span
aria-hidden="true"
Expand All @@ -243,7 +243,7 @@ exports[`<Pill> should render the expected pill for a user not in the room 1`] =
class="mx_BaseAvatar_initial"
style="font-size: 10.4px; width: 16px; line-height: 16px;"
>
U
S
</span>
<img
alt=""
Expand All @@ -257,7 +257,29 @@ exports[`<Pill> should render the expected pill for a user not in the room 1`] =
<span
class="mx_Pill_text"
>
User 2
Space 1
</span>
</a>
</bdi>
</div>
</DocumentFragment>
`;
exports[`<Pill> should render the expected pill for an uknown user not in the room 1`] = `
<DocumentFragment>
<div>
<bdi>
<a
class="mx_Pill mx_UserPill"
href="https://matrix.to/#/@user3:example.com"
>
<div
class="mx_Pill_UserIcon mx_BaseAvatar mx_BaseAvatar_image"
/>
<span
class="mx_Pill_text"
>
@user3:example.com
</span>
</a>
</bdi>
Expand Down

0 comments on commit 7dbaea9

Please sign in to comment.