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

Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
pull/28217/head
Johannes Marbach 2022-09-22 09:42:07 +02:00 committed by GitHub
parent 516b4f0ff8
commit 88c12cdaa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 42 deletions

View File

@ -43,7 +43,6 @@ import { MatrixClientPeg } from "./MatrixClientPeg";
import { ROOM_SECURITY_TAB } from "./components/views/dialogs/RoomSettingsDialog"; import { ROOM_SECURITY_TAB } from "./components/views/dialogs/RoomSettingsDialog";
import AccessibleButton from './components/views/elements/AccessibleButton'; import AccessibleButton from './components/views/elements/AccessibleButton';
import RightPanelStore from './stores/right-panel/RightPanelStore'; import RightPanelStore from './stores/right-panel/RightPanelStore';
import UserIdentifierCustomisations from './customisations/UserIdentifier';
import { ViewRoomPayload } from "./dispatcher/payloads/ViewRoomPayload"; import { ViewRoomPayload } from "./dispatcher/payloads/ViewRoomPayload";
import { isLocationEvent } from './utils/EventUtils'; import { isLocationEvent } from './utils/EventUtils';
@ -55,7 +54,7 @@ function getRoomMemberDisplayname(event: MatrixEvent, userId = event.getSender()
const client = MatrixClientPeg.get(); const client = MatrixClientPeg.get();
const roomId = event.getRoomId(); const roomId = event.getRoomId();
const member = client.getRoom(roomId)?.getMember(userId); 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 // These functions are frequently used just to check whether an event has
@ -467,7 +466,7 @@ function textForPowerEvent(event: MatrixEvent): () => string | null {
} }
if (from === previousUserDefault && to === currentUserDefault) { return; } if (from === previousUserDefault && to === currentUserDefault) { return; }
if (to !== from) { if (to !== from) {
const name = UserIdentifierCustomisations.getDisplayUserIdentifier(userId, { roomId: event.getRoomId() }); const name = getRoomMemberDisplayname(event, userId);
diffs.push({ userId, name, from, to }); diffs.push({ userId, name, from, to });
} }
}); });

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. 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 TestRenderer from 'react-test-renderer';
import { ReactElement } from "react"; import { ReactElement } from "react";
@ -174,14 +174,17 @@ describe('TextForEvent', () => {
const userA = { const userA = {
id: '@a', id: '@a',
name: 'Alice', name: 'Alice',
rawDisplayName: 'Alice',
}; };
const userB = { const userB = {
id: '@b', id: '@b',
name: 'Bob', name: 'Bob (@b)',
rawDisplayName: 'Bob',
}; };
const userC = { const userC = {
id: '@c', id: '@c',
name: 'Carl', name: 'Bob (@c)',
rawDisplayName: 'Bob',
}; };
interface PowerEventProps { interface PowerEventProps {
usersDefault?: number; usersDefault?: number;
@ -191,19 +194,23 @@ describe('TextForEvent', () => {
} }
const mockPowerEvent = ({ const mockPowerEvent = ({
usersDefault, prevDefault, users, prevUsers, usersDefault, prevDefault, users, prevUsers,
}: PowerEventProps): MatrixEvent => new MatrixEvent({ }: PowerEventProps): MatrixEvent => {
type: EventType.RoomPowerLevels, const mxEvent = new MatrixEvent({
sender: userA.id, type: EventType.RoomPowerLevels,
state_key: "", sender: userA.id,
content: { state_key: "",
users_default: usersDefault, content: {
users, users_default: usersDefault,
}, users,
prev_content: { },
users: prevUsers, prev_content: {
users_default: prevDefault, users: prevUsers,
}, users_default: prevDefault,
}); },
});
mxEvent.sender = { name: userA.name } as RoomMember;
return mxEvent;
};
beforeAll(() => { beforeAll(() => {
mockClient = createTestClient(); mockClient = createTestClient();
@ -256,7 +263,7 @@ describe('TextForEvent', () => {
[userB.id]: 50, [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); expect(textForEvent(event)).toEqual(expectedText);
}); });
@ -271,7 +278,7 @@ describe('TextForEvent', () => {
[userB.id]: 50, [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); expect(textForEvent(event)).toEqual(expectedText);
}); });
@ -284,7 +291,7 @@ describe('TextForEvent', () => {
[userB.id]: 50, [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); expect(textForEvent(event)).toEqual(expectedText);
}); });
@ -299,28 +306,10 @@ describe('TextForEvent', () => {
[userC.id]: 101, [userC.id]: 101,
}, },
}); });
const expectedText = const expectedText = "Alice changed the power level of Bob (@b) from Moderator to Admin,"
"@a changed the power level of @b from Moderator to Admin, @c from Custom (101) to Moderator."; + " Bob (@c) from Custom (101) to Moderator.";
expect(textForEvent(event)).toEqual(expectedText); 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.";
expect(textForEvent(event)).toEqual(expectedText);
expect(UserIdentifierCustomisations.getDisplayUserIdentifier)
.toHaveBeenCalledWith(userB.id, { roomId: event.getRoomId() });
});
}); });
describe("textForCanonicalAliasEvent()", () => { describe("textForCanonicalAliasEvent()", () => {