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

Commit 88c12cd

Browse files
Johennest3chguy
andauthored
Use display name instead of user ID when rendering power events (PSC-82) (#9295)
Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
1 parent 516b4f0 commit 88c12cd

File tree

2 files changed

+30
-42
lines changed

2 files changed

+30
-42
lines changed

src/TextForEvent.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import { MatrixClientPeg } from "./MatrixClientPeg";
4343
import { ROOM_SECURITY_TAB } from "./components/views/dialogs/RoomSettingsDialog";
4444
import AccessibleButton from './components/views/elements/AccessibleButton';
4545
import RightPanelStore from './stores/right-panel/RightPanelStore';
46-
import UserIdentifierCustomisations from './customisations/UserIdentifier';
4746
import { ViewRoomPayload } from "./dispatcher/payloads/ViewRoomPayload";
4847
import { isLocationEvent } from './utils/EventUtils';
4948

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

6160
// These functions are frequently used just to check whether an event has
@@ -467,7 +466,7 @@ function textForPowerEvent(event: MatrixEvent): () => string | null {
467466
}
468467
if (from === previousUserDefault && to === currentUserDefault) { return; }
469468
if (to !== from) {
470-
const name = UserIdentifierCustomisations.getDisplayUserIdentifier(userId, { roomId: event.getRoomId() });
469+
const name = getRoomMemberDisplayname(event, userId);
471470
diffs.push({ userId, name, from, to });
472471
}
473472
});

test/TextForEvent-test.ts

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

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

@@ -174,14 +174,17 @@ describe('TextForEvent', () => {
174174
const userA = {
175175
id: '@a',
176176
name: 'Alice',
177+
rawDisplayName: 'Alice',
177178
};
178179
const userB = {
179180
id: '@b',
180-
name: 'Bob',
181+
name: 'Bob (@b)',
182+
rawDisplayName: 'Bob',
181183
};
182184
const userC = {
183185
id: '@c',
184-
name: 'Carl',
186+
name: 'Bob (@c)',
187+
rawDisplayName: 'Bob',
185188
};
186189
interface PowerEventProps {
187190
usersDefault?: number;
@@ -191,19 +194,23 @@ describe('TextForEvent', () => {
191194
}
192195
const mockPowerEvent = ({
193196
usersDefault, prevDefault, users, prevUsers,
194-
}: PowerEventProps): MatrixEvent => new MatrixEvent({
195-
type: EventType.RoomPowerLevels,
196-
sender: userA.id,
197-
state_key: "",
198-
content: {
199-
users_default: usersDefault,
200-
users,
201-
},
202-
prev_content: {
203-
users: prevUsers,
204-
users_default: prevDefault,
205-
},
206-
});
197+
}: PowerEventProps): MatrixEvent => {
198+
const mxEvent = new MatrixEvent({
199+
type: EventType.RoomPowerLevels,
200+
sender: userA.id,
201+
state_key: "",
202+
content: {
203+
users_default: usersDefault,
204+
users,
205+
},
206+
prev_content: {
207+
users: prevUsers,
208+
users_default: prevDefault,
209+
},
210+
});
211+
mxEvent.sender = { name: userA.name } as RoomMember;
212+
return mxEvent;
213+
};
207214

208215
beforeAll(() => {
209216
mockClient = createTestClient();
@@ -256,7 +263,7 @@ describe('TextForEvent', () => {
256263
[userB.id]: 50,
257264
},
258265
});
259-
const expectedText = "@a changed the power level of @b from Moderator to Admin.";
266+
const expectedText = "Alice changed the power level of Bob (@b) from Moderator to Admin.";
260267
expect(textForEvent(event)).toEqual(expectedText);
261268
});
262269

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

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

@@ -299,27 +306,9 @@ describe('TextForEvent', () => {
299306
[userC.id]: 101,
300307
},
301308
});
302-
const expectedText =
303-
"@a changed the power level of @b from Moderator to Admin, @c from Custom (101) to Moderator.";
304-
expect(textForEvent(event)).toEqual(expectedText);
305-
});
306-
307-
it("uses userIdentifier customisation", () => {
308-
(UserIdentifierCustomisations.getDisplayUserIdentifier as jest.Mock)
309-
.mockImplementation(userId => 'customised ' + userId);
310-
const event = mockPowerEvent({
311-
users: {
312-
[userB.id]: 100,
313-
},
314-
prevUsers: {
315-
[userB.id]: 50,
316-
},
317-
});
318-
// uses customised user id
319-
const expectedText = "@a changed the power level of customised @b from Moderator to Admin.";
309+
const expectedText = "Alice changed the power level of Bob (@b) from Moderator to Admin,"
310+
+ " Bob (@c) from Custom (101) to Moderator.";
320311
expect(textForEvent(event)).toEqual(expectedText);
321-
expect(UserIdentifierCustomisations.getDisplayUserIdentifier)
322-
.toHaveBeenCalledWith(userB.id, { roomId: event.getRoomId() });
323312
});
324313
});
325314

0 commit comments

Comments
 (0)