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

Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
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 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';
@ -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
@ -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);
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.
*/
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";
@ -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;
@ -191,7 +194,8 @@ describe('TextForEvent', () => {
}
const mockPowerEvent = ({
usersDefault, prevDefault, users, prevUsers,
}: PowerEventProps): MatrixEvent => new MatrixEvent({
}: PowerEventProps): MatrixEvent => {
const mxEvent = new MatrixEvent({
type: EventType.RoomPowerLevels,
sender: userA.id,
state_key: "",
@ -204,6 +208,9 @@ describe('TextForEvent', () => {
users_default: prevDefault,
},
});
mxEvent.sender = { name: userA.name } as RoomMember;
return mxEvent;
};
beforeAll(() => {
mockClient = createTestClient();
@ -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);
});
@ -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);
});
@ -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);
});
@ -299,28 +306,10 @@ describe('TextForEvent', () => {
[userC.id]: 101,
},
});
const expectedText =
"@a changed the power level of @b from Moderator to Admin, @c from Custom (101) to Moderator.";
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);
});
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()", () => {