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

Use display name instead of user ID when rendering power events (PSC-82) #9295

Merged
merged 6 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/TextForEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import { MatrixClientPeg } from "./MatrixClientPeg";
import { ROOM_SECURITY_TAB } from "./components/views/dialogs/RoomSettingsDialog";
import AccessibleButton from './components/views/elements/AccessibleButton';
import RightPanelStore from './stores/right-panel/RightPanelStore';
import UserIdentifierCustomisations from './customisations/UserIdentifier';
import { ViewRoomPayload } from "./dispatcher/payloads/ViewRoomPayload";
import { isLocationEvent } from './utils/EventUtils';

Expand All @@ -55,7 +54,7 @@ function getRoomMemberDisplayname(event: MatrixEvent, userId = event.getSender()
const client = MatrixClientPeg.get();
const roomId = event.getRoomId();
const member = client.getRoom(roomId)?.getMember(userId);
return member?.rawDisplayName || userId || _t("Someone");
return member?.name || member?.rawDisplayName || userId || _t("Someone");
}

// These functions are frequently used just to check whether an event has
Expand Down Expand Up @@ -467,7 +466,7 @@ function textForPowerEvent(event: MatrixEvent): () => string | null {
}
if (from === previousUserDefault && to === currentUserDefault) { return; }
if (to !== from) {
const name = UserIdentifierCustomisations.getDisplayUserIdentifier(userId, { roomId: event.getRoomId() });
const name = getRoomMemberDisplayname(event, userId);
Johennes marked this conversation as resolved.
Show resolved Hide resolved
diffs.push({ userId, name, from, to });
}
});
Expand Down
67 changes: 28 additions & 39 deletions test/TextForEvent-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { EventType, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { EventType, MatrixEvent, RoomMember } from "matrix-js-sdk/src/matrix";
import TestRenderer from 'react-test-renderer';
import { ReactElement } from "react";

Expand Down Expand Up @@ -174,14 +174,17 @@ describe('TextForEvent', () => {
const userA = {
id: '@a',
name: 'Alice',
rawDisplayName: 'Alice',
};
const userB = {
id: '@b',
name: 'Bob',
name: 'Bob (@b)',
rawDisplayName: 'Bob',
};
const userC = {
id: '@c',
name: 'Carl',
name: 'Bob (@c)',
rawDisplayName: 'Bob',
};
interface PowerEventProps {
usersDefault?: number;
Expand All @@ -191,19 +194,23 @@ describe('TextForEvent', () => {
}
const mockPowerEvent = ({
usersDefault, prevDefault, users, prevUsers,
}: PowerEventProps): MatrixEvent => new MatrixEvent({
type: EventType.RoomPowerLevels,
sender: userA.id,
state_key: "",
content: {
users_default: usersDefault,
users,
},
prev_content: {
users: prevUsers,
users_default: prevDefault,
},
});
}: PowerEventProps): MatrixEvent => {
const mxEvent = new MatrixEvent({
type: EventType.RoomPowerLevels,
sender: userA.id,
state_key: "",
content: {
users_default: usersDefault,
users,
},
prev_content: {
users: prevUsers,
users_default: prevDefault,
},
});
mxEvent.sender = { name: userA.name } as RoomMember;
return mxEvent;
};

beforeAll(() => {
mockClient = createTestClient();
Expand Down Expand Up @@ -256,7 +263,7 @@ describe('TextForEvent', () => {
[userB.id]: 50,
},
});
const expectedText = "@a changed the power level of @b from Moderator to Admin.";
const expectedText = "Alice changed the power level of Bob (@b) from Moderator to Admin.";
expect(textForEvent(event)).toEqual(expectedText);
});

Expand All @@ -271,7 +278,7 @@ describe('TextForEvent', () => {
[userB.id]: 50,
},
});
const expectedText = "@a changed the power level of @b from Moderator to Default.";
const expectedText = "Alice changed the power level of Bob (@b) from Moderator to Default.";
expect(textForEvent(event)).toEqual(expectedText);
});

Expand All @@ -284,7 +291,7 @@ describe('TextForEvent', () => {
[userB.id]: 50,
},
});
const expectedText = "@a changed the power level of @b from Moderator to Custom (-1).";
const expectedText = "Alice changed the power level of Bob (@b) from Moderator to Custom (-1).";
expect(textForEvent(event)).toEqual(expectedText);
});

Expand All @@ -299,27 +306,9 @@ describe('TextForEvent', () => {
[userC.id]: 101,
},
});
const expectedText =
"@a changed the power level of @b from Moderator to Admin, @c from Custom (101) to Moderator.";
expect(textForEvent(event)).toEqual(expectedText);
});

it("uses userIdentifier customisation", () => {
(UserIdentifierCustomisations.getDisplayUserIdentifier as jest.Mock)
.mockImplementation(userId => 'customised ' + userId);
const event = mockPowerEvent({
users: {
[userB.id]: 100,
},
prevUsers: {
[userB.id]: 50,
},
});
// uses customised user id
const expectedText = "@a changed the power level of customised @b from Moderator to Admin.";
const expectedText = "Alice changed the power level of Bob (@b) from Moderator to Admin,"
+ " Bob (@c) from Custom (101) to Moderator.";
expect(textForEvent(event)).toEqual(expectedText);
expect(UserIdentifierCustomisations.getDisplayUserIdentifier)
.toHaveBeenCalledWith(userB.id, { roomId: event.getRoomId() });
});
});

Expand Down