Skip to content

Commit

Permalink
Allow hiding invite and kick messages from timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
RavuAlHemio committed Oct 26, 2024
1 parent 7de5c84 commit 590ef74
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/components/structures/RoomView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export interface IRoomState {
showReadReceipts: boolean;
showRedactions: boolean;
showJoinLeaves: boolean;
showInviteKicks: boolean;
showAvatarChanges: boolean;
showDisplaynameChanges: boolean;
matrixClientIsReady: boolean;
Expand Down Expand Up @@ -406,6 +407,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
showReadReceipts: true,
showRedactions: true,
showJoinLeaves: true,
showInviteKicks: true,
showAvatarChanges: true,
showDisplaynameChanges: true,
matrixClientIsReady: context.client?.isInitialSyncComplete(),
Expand Down Expand Up @@ -608,6 +610,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
showReadReceipts: SettingsStore.getValue("showReadReceipts", roomId),
showRedactions: SettingsStore.getValue("showRedactions", roomId),
showJoinLeaves: SettingsStore.getValue("showJoinLeaves", roomId),
showInviteKicks: SettingsStore.getValue("showInviteKicks", roomId),
showAvatarChanges: SettingsStore.getValue("showAvatarChanges", roomId),
showDisplaynameChanges: SettingsStore.getValue("showDisplaynameChanges", roomId),
wasContextSwitch: this.context.roomViewStore.getWasContextSwitch(),
Expand Down Expand Up @@ -679,6 +682,9 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
SettingsStore.watchSetting("showJoinLeaves", roomId, (...[, , , value]) =>
this.setState({ showJoinLeaves: value as boolean }),
),
SettingsStore.watchSetting("showInviteKicks", roomId, (...[, , , value]) =>
this.setState({ showInviteKicks: value as boolean }),
),
SettingsStore.watchSetting("showAvatarChanges", roomId, (...[, , , value]) =>
this.setState({ showAvatarChanges: value as boolean }),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export default class PreferencesUserSettingsTab extends React.Component<IProps,
"showRedactions",
"showReadReceipts",
"showJoinLeaves",
"showInviteKicks",
"showDisplaynameChanges",
"showChatEffects",
"showAvatarChanges",
Expand Down
1 change: 1 addition & 0 deletions src/contexts/RoomContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const RoomContext = createContext<
showReadReceipts: true,
showRedactions: true,
showJoinLeaves: true,
showInviteKicks: true,
showAvatarChanges: true,
showDisplaynameChanges: true,
matrixClientIsReady: false,
Expand Down
1 change: 1 addition & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -2900,6 +2900,7 @@
"show_breadcrumbs": "Show shortcuts to recently viewed rooms above the room list",
"show_chat_effects": "Show chat effects (animations when receiving e.g. confetti)",
"show_displayname_changes": "Show display name changes",
"show_invite_kick": "Show invite/remove messages",
"show_join_leave": "Show join/leave messages (invites/removes/bans unaffected)",
"show_nsfw_content": "Show NSFW content",
"show_read_receipts": "Show read receipts sent by other users",
Expand Down
6 changes: 6 additions & 0 deletions src/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,12 @@ export const SETTINGS: { [setting: string]: ISetting } = {
default: true,
invertedSettingName: "hideJoinLeaves",
},
"showInviteKicks": {
supportedLevels: LEVELS_ROOM_SETTINGS_WITH_ROOM,
displayName: _td("settings|show_invite_kick"),
default: true,
invertedSettingName: "hideInviteKicks",
},
"showAvatarChanges": {
supportedLevels: LEVELS_ROOM_SETTINGS_WITH_ROOM,
displayName: _td("settings|show_avatar_changes"),
Expand Down
6 changes: 6 additions & 0 deletions src/shouldHideEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface IDiff {
isPart?: boolean;
isDisplaynameChange?: boolean;
isAvatarChange?: boolean;
isInvite?: boolean;
isKick?: boolean;
}

function memberEventDiff(ev: MatrixEvent): IDiff {
Expand All @@ -35,6 +37,9 @@ function memberEventDiff(ev: MatrixEvent): IDiff {
diff.isJoin = isMembershipChanged && content.membership === KnownMembership.Join;
diff.isPart =
isMembershipChanged && content.membership === KnownMembership.Leave && ev.getStateKey() === ev.getSender();
diff.isInvite = isMembershipChanged && content.membership === KnownMembership.Invite;
diff.isKick =
isMembershipChanged && content.membership === KnownMembership.Leave && ev.getStateKey() !== ev.getSender();

const isJoinToJoin = !isMembershipChanged && content.membership === KnownMembership.Join;
diff.isDisplaynameChange = isJoinToJoin && content.displayname !== prevContent.displayname;
Expand Down Expand Up @@ -67,6 +72,7 @@ export default function shouldHideEvent(ev: MatrixEvent, ctx?: IRoomState): bool

if (eventDiff.isMemberEvent) {
if ((eventDiff.isJoin || eventDiff.isPart) && !isEnabled("showJoinLeaves")) return true;
if ((eventDiff.isInvite || eventDiff.isKick) && !isEnabled("showInviteKicks")) return true;
if (eventDiff.isAvatarChange && !isEnabled("showAvatarChanges")) return true;
if (eventDiff.isDisplaynameChange && !isEnabled("showDisplaynameChanges")) return true;
}
Expand Down
1 change: 1 addition & 0 deletions test/test-utils/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function getRoomContext(room: Room, override: Partial<IRoomState>): IRoom
showReadReceipts: true,
showRedactions: true,
showJoinLeaves: true,
showInviteKicks: true,
showAvatarChanges: true,
showDisplaynameChanges: true,
matrixClientIsReady: false,
Expand Down
29 changes: 29 additions & 0 deletions test/unit-tests/components/structures/MessagePanel-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe("MessagePanel", function () {
showReadReceipts: true,
showRedactions: false,
showJoinLeaves: false,
showInviteKicks: true,
showAvatarChanges: false,
showDisplaynameChanges: true,
showHiddenEvents: false,
Expand Down Expand Up @@ -463,6 +464,34 @@ describe("MessagePanel", function () {
expect(summaryEventTiles.length).toEqual(tiles.length - 3);
});

it("shows invite events if the user wishes to see them", function () {
const events = mkCreationEvents();
const inviteEvent = events.find(
(event) =>
event.getType() === "m.room.member"
&& event.getContent()?.membership === "invite"
)!;
const { container } = render(getComponent({ events }, { showInviteKicks: true }));

// there must be an invite event
const inviteEventTiles = container.querySelectorAll(`.mx_EventTile[data-event-id=\"${inviteEvent.getId()}\"]`);
expect(inviteEventTiles.length).toBeGreaterThan(0);
});

it("hides invite events if the user does not wish to see them", function () {
const events = mkCreationEvents();
const inviteEvent = events.find(
(event) =>
event.getType() === "m.room.member"
&& event.getContent()?.membership === "invite"
)!;
const { container } = render(getComponent({ events }, { showInviteKicks: false }));

// our invite event must not exist
const inviteEventTiles = container.querySelectorAll(`.mx_EventTile[data-event-id=\"${inviteEvent.getId()}\"]`);
expect(inviteEventTiles.length).toBe(0);
});

it("should not collapse beacons as part of creation events", function () {
const events = mkCreationEvents();
const creationEvent = events.find((event) => event.getType() === "m.room.create")!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe("<SendMessageComposer/>", () => {
showReadReceipts: true,
showRedactions: true,
showJoinLeaves: true,
showInviteKicks: true,
showAvatarChanges: true,
showDisplaynameChanges: true,
matrixClientIsReady: false,
Expand Down

0 comments on commit 590ef74

Please sign in to comment.