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

Commit

Permalink
Add grouper for hidden events (#7649)
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored Jan 26, 2022
1 parent 00e868f commit 2f209c4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
94 changes: 90 additions & 4 deletions src/components/structures/MessagePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -462,17 +462,17 @@ export default class MessagePanel extends React.Component<IProps, IState> {
return !this.isMounted;
};

private get showHiddenEvents(): boolean {
public get showHiddenEvents(): boolean {
return this.context?.showHiddenEventsInTimeline ?? this.showHiddenEventsInTimeline;
}

// TODO: Implement granular (per-room) hide options
public shouldShowEvent(mxEv: MatrixEvent): boolean {
public shouldShowEvent(mxEv: MatrixEvent, forceHideEvents = false): boolean {
if (MatrixClientPeg.get().isUserIgnored(mxEv.getSender())) {
return false; // ignored = no show (only happens if the ignore happens after an event was received)
}

if (this.showHiddenEvents) {
if (this.showHiddenEvents && !forceHideEvents) {
return true;
}

Expand Down Expand Up @@ -1403,5 +1403,91 @@ class MemberGrouper extends BaseGrouper {
}
}

// Wrap consecutive hidden events in a ListSummary, ignore if redacted
class HiddenEventGrouper extends BaseGrouper {
static canStartGroup = function(panel: MessagePanel, ev: MatrixEvent): boolean {
return !panel.shouldShowEvent(ev, true) && panel.showHiddenEvents;
};

constructor(
public readonly panel: MessagePanel,
public readonly event: MatrixEvent,
public readonly prevEvent: MatrixEvent,
public readonly lastShownEvent: MatrixEvent,
protected readonly layout: Layout,
) {
super(panel, event, prevEvent, lastShownEvent, layout);
this.events = [event];
}

public shouldGroup(ev: MatrixEvent): boolean {
if (this.panel.wantsDateSeparator(this.events[0], ev.getDate())) {
return false;
}
return !this.panel.shouldShowEvent(ev, true);
}

public add(ev: MatrixEvent, showHiddenEvents?: boolean): void {
this.readMarker = this.readMarker || this.panel.readMarkerForEvent(ev.getId(), ev === this.lastShownEvent);
this.events.push(ev);
}

public getTiles(): ReactNode[] {
if (!this.events || !this.events.length) return [];

const isGrouped = true;
const panel = this.panel;
const ret = [];
const lastShownEvent = this.lastShownEvent;

if (panel.wantsDateSeparator(this.prevEvent, this.events[0].getDate())) {
const ts = this.events[0].getTs();
ret.push(
<li key={ts+'~'}><DateSeparator key={ts+'~'} ts={ts} /></li>,
);
}

const key = "hiddeneventlistsummary-" + (
this.prevEvent ? this.events[0].getId() : "initial"
);

const senders = new Set<RoomMember>();
let eventTiles = this.events.map((e, i) => {
senders.add(e.sender);
const prevEvent = i === 0 ? this.prevEvent : this.events[i - 1];
return panel.getTilesForEvent(
prevEvent, e, e === lastShownEvent, isGrouped, this.nextEvent, this.nextEventTile);
}).reduce((a, b) => a.concat(b), []);

if (eventTiles.length === 0) {
eventTiles = null;
}

ret.push(
<EventListSummary
key={key}
threshold={2}
events={this.events}
onToggle={panel.onHeightChanged} // Update scroll state
summaryMembers={Array.from(senders)}
summaryText={_t("%(count)s hidden messages.", { count: eventTiles.length })}
layout={this.layout}
>
{ eventTiles }
</EventListSummary>,
);

if (this.readMarker) {
ret.push(this.readMarker);
}

return ret;
}

public getNewPrevEvent(): MatrixEvent {
return this.events[this.events.length - 1];
}
}

// all the grouper classes that we use
const groupers = [CreationGrouper, MemberGrouper, RedactionGrouper];
const groupers = [CreationGrouper, MemberGrouper, RedactionGrouper, HiddenEventGrouper];
2 changes: 2 additions & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -3047,6 +3047,8 @@
"%(creator)s created and configured the room.": "%(creator)s created and configured the room.",
"%(count)s messages deleted.|other": "%(count)s messages deleted.",
"%(count)s messages deleted.|one": "%(count)s message deleted.",
"%(count)s hidden messages.|other": "%(count)s hidden messages.",
"%(count)s hidden messages.|one": "%(count)s hidden message.",
"Your Communities": "Your Communities",
"Did you know: you can use communities to filter your %(brand)s experience!": "Did you know: you can use communities to filter your %(brand)s experience!",
"You can click on an avatar in the filter panel at any time to see only the rooms and people associated with that community.": "You can click on an avatar in the filter panel at any time to see only the rooms and people associated with that community.",
Expand Down

0 comments on commit 2f209c4

Please sign in to comment.